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__

add_layer

Adds layer to the model

backward

Perform a backward pass through the neural network.

build

Build the neural network architecture.

check_hyperparameters

Check if essential hyperparameters are specified.

evaluate

Evaluate the neural network on validation data.

forward

Perform a forward pass through the entire neural network.

get_model_parameters

_summary_

load

Load a trained model from a file.

load_model_parameters

Load and set the model parameters from a file.

predict

Generate predictions using the trained neural network.

print_summary

Print a summary of training or evaluation results.

save_model

Save the entire trained model to a file.

save_model_parameters

Save the model parameters to a file.

set_accuracy

Set the accuracy metric for the neural network.

set_loss

Set the loss function for the neural network.

set_model_parameters

Set the parameters of the trainable layers in the model.

set_optimizer

Set the optimizer for the neural network.

train

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).

Return type:

None

Parameters:
  • layer (_Layer) – The layer to perform the forward pass on.

  • X (numpy.ndarray) – The input data.

  • training (bool, optional) – Indicates if the network is in training mode, by default False.

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:

None

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:

None

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:

None

check_hyperparameters()[source]#

Check if essential hyperparameters are specified.

Return type:

None

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:

None

Parameters:
  • X_val (numpy.ndarray) – The input validation data.

  • y_val (numpy.ndarray) – The target validation data.

  • batch_size (int, optional) – The batch size for evaluation, by default None.

  • verbose (int, optional) – Verbosity level (0: no prints, 1: summary prints), by default 0.

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

get_model_parameters()[source]#

_summary_

Return type:

list[ndarray]

Returns:

A list of numpy arrays containing the parameters of each trainable layer.

Return type:

list[numpy.ndarray]

static load(path)[source]#

Load a trained model from a file.

Return type:

NN

Parameters:

path (str) – The path to the file from which the model will be loaded.

Returns:

The loaded trained model.

Return type:

NN

Examples

>>> loaded_model = NN.load('trained_model.model')
load_model_parameters(path)[source]#

Load and set the model parameters from a file.

Return type:

None

Parameters:

path (str) – The path to the file from which the parameters will be loaded.

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:

None

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.

Return type:

None

Parameters:

path (str) – The path to the file where the model will be saved.

Examples

>>> network.save_model('trained_model.model')
save_model_parameters(path)[source]#

Save the model parameters to a file.

Return type:

None

Parameters:

path (str) – The path to the file where the parameters will be saved.

set_accuracy(accuracy)[source]#

Set the accuracy metric for the neural network.

Return type:

None

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:

None

Parameters:

loss (_Loss) – The loss function to be used for training.

set_model_parameters(parameters)[source]#

Set the parameters of the trainable layers in the model.

Return type:

None

Parameters:

parameters (list[tuple[numpy.ndarray]]) – A list storing parameters for each trainable layer within a tuple.

set_optimizer(optimizer)[source]#

Set the optimizer for the neural network.

Return type:

None

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:

None

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