pyml.neural_network.nn.NN#
- class NN[source]#
Bases:
object
Neural network class for regression & classification
This class provides functionality for building, training, and evaluating neural networks.
- Variables:
layers (list[_Layer]) – A list of layers in the neural network.
trainable_layers (list[_TrainableTransformation]) – A list of trainable layers in the neural network.
loss (_Loss) – The loss function used for training the network.
optimizer (_Optimizer) – The optimizer used for updating network parameters during training.
accuracy (_Accuracy) – The accuracy metric used for evaluating the network’s performance.
finished_build (bool) – Indicates whether the neural network has been built.
Examples
>>> from pyml.nn import NN >>> network = NN() >>> network.add_layer(Dense(4, 512)) >>> network.add_layer(ReLU()) >>> network.add_layer(Dropout(0.2)) >>> network.add_layer(Dense(512, NUM_CLASSES)) >>> network.add_layer(Softmax())
>>> model.set_loss(CategoricalCrossentropy()) >>> model.set_optimizer(Adam(learning_rate=0.005, decay=5e-5)) >>> model.set_accuracy(MultiClassAccuracy())
>>> model.build()
Methods
__init__
Adds layer to the model
Perform a backward pass through the neural network.
Build the neural network architecture.
Check if essential hyperparameters are specified.
Evaluate the neural network on validation data.
Perform a forward pass through the entire neural network.
_summary_
Load a trained model from a file.
Load and set the model parameters from a file.
Generate predictions using the trained neural network.
Print a summary of training or evaluation results.
Save the entire trained model to a file.
Save the model parameters to a file.
Set the accuracy metric for the neural network.
Set the loss function for the neural network.
Set the parameters of the trainable layers in the model.
Set the optimizer for the neural network.
Train the neural network.
- _forward(layer, X, training=False)[source]#
Perform a forward pass through a layer.
If the layer is a dropout layer, the training context (true or false is also passed).
- add_layer(layer)[source]#
Adds layer to the model
Iteratively adds layers to the model. These layers can be e.g. conventional dense layers or activation functions. Be aware: the order of appending the layers is crucial and matters.
- Return type:
- Parameters:
layer (_Layer) – The layer to be added to the network.
- Raises:
InconsistentLayerSizes – Raised when adding a layer where input size does not match the output size of the previous layer
Examples
>>> from pyml.nn import NN >>> model = NN() >>> model.add(Dense(4, 16)) >>> model.add(ReLU()) >>> model.add(Dense(16, 32)) >>> model.add(ReLU()) >>> ...
- backward(output, y)[source]#
Perform a backward pass through the neural network.
- Return type:
- Parameters:
output (numpy.ndarray) – The output of the network.
y (numpy.ndarray) – The target data.
- build()[source]#
Build the neural network architecture.
This method sets up the connections between layers and prepares the network for training.
- Return type:
- check_hyperparameters()[source]#
Check if essential hyperparameters are specified.
- Return type:
- Raises:
HyperparametersNotSpecified – If any of the essential hyperparameters is not specified.
- evaluate(X_val, y_val, *, batch_size=None, verbose=0)[source]#
Evaluate the neural network on validation data.
- Return type:
- Parameters:
- forward(X, training=False)[source]#
Perform a forward pass through the entire neural network.
- Return type:
ndarray
- Parameters:
X (numpy.ndarray) – The input data.
training (bool, optional) – Indicates if the network is in training mode, by default False.
- Returns:
The output of the network.
- Return type:
numpy.ndarray
- static load(path)[source]#
Load a trained model from a file.
- Return type:
- Parameters:
path (str) – The path to the file from which the model will be loaded.
- Returns:
The loaded trained model.
- Return type:
Examples
>>> loaded_model = NN.load('trained_model.model')
- predict(X, *, batch_size=None)[source]#
Generate predictions using the trained neural network.
- Return type:
ndarray
- Parameters:
X (np.ndarray) – The input data for which predictions are to be generated.
batch_size (int, optional) – The batch size for prediction, by default None.
- Returns:
The predictions generated by the network.
- Return type:
numpy.ndarray
- static print_summary(context, accuracy, loss, data_loss=None, regularization_loss=None, learning_rate=None)[source]#
Print a summary of training or evaluation results.
- Return type:
- Parameters:
context (str) – Context for the summary (e.g., ‘training’, ‘validation’).
accuracy (float) – The accuracy achieved during training or evaluation.
loss (float) – The total loss achieved during training or evaluation.
data_loss (float, optional) – The data loss component of the total loss, by default None.
regularization_loss (float, optional) – The regularization loss component of the total loss, by default None.
learning_rate (float, optional) – The learning rate used during training, by default None.
- save_model(path)[source]#
Save the entire trained model to a file.
Examples
>>> network.save_model('trained_model.model')
- set_accuracy(accuracy)[source]#
Set the accuracy metric for the neural network.
- Return type:
- Parameters:
accuracy (_Accuracy) – The accuracy metric to be used for evaluation.
- set_loss(loss)[source]#
Set the loss function for the neural network.
- Return type:
- Parameters:
loss (_Loss) – The loss function to be used for training.
- set_optimizer(optimizer)[source]#
Set the optimizer for the neural network.
- Return type:
- Parameters:
optimizer (_Optimizer) – The optimizer to be used for updating parameters.
- train(X, y, *, epochs=1, batch_size=None, validation_data=None, verbose=0, print_summary_every=1, save_file_path=None)[source]#
Train the neural network.
- Return type:
Examples
>>> X_train, y_train, X_test, y_test = ... # Load training data >>> network.train(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=32)
- Parameters:
X (numpy.ndarray) – The input training data.
y (numpy.ndarray) – The target training data.
epochs (int, optional) – The number of training epochs, by default 1.
batch_size (int, optional) – The batch size for training, by default None.
validation_data (numpy.ndarray, optional) – Validation data for evaluation during training. Should include data (X) and their labels (y), by default None.
verbose (int, optional) – Verbosity level (0: no prints, 1: epoch summary, 2: detailed prints), by default 0.
print_summary_every (int, optional) – Print summary every ‘print_summary_every’ steps, by default 1.
save_file_path (str, optional) – Path to save the model parameters after training, by default None. TODO: NOT IMPLEMENTED YET