sega_learn.neural_networks.neuralNetworkBase

 
Modules
       
cupy
numpy
numba.core.types

 
Classes
       
builtins.object
NeuralNetworkBase

 
class NeuralNetworkBase(builtins.object)
    NeuralNetworkBase(layers, dropout_rate=0.0, reg_lambda=0.0, activations=None, loss_function=None, regressor=False)
 
NeuralNetworkBase is an abstract base class for building neural networks.
 
It provides a framework for initializing layers, performing forward and backward propagation,
training, evaluating, and predicting with a neural network. Subclasses should implement
the abstract methods to define specific behavior.
 
Attributes:
    layer_sizes (list): Sizes of the layers in the network.
    dropout_rate (float): Dropout rate for regularization.
    reg_lambda (float): Regularization strength for L2 regularization.
    activations (list): Activation functions for each layer.
    layers (list): List of layer objects or configurations.
    weights (list): List of weight matrices for the layers.
    biases (list): List of bias vectors for the layers.
    layer_outputs (ndarray): Outputs of each layer during forward propagation.
    is_binary (bool): Whether the network is for binary classification.
 
Methods:
    __init__(layers, dropout_rate=0.0, reg_lambda=0.0, activations=None, loss_function=None, regressor=False):
        Initializes the neural network with the given layers and parameters.
    initialize_layers():
        Abstract method to initialize the weights and biases of the layers.
    forward(X, training=True):
        Abstract method to perform forward propagation through the network.
    backward(y):
        Abstract method to perform backward propagation through the network.
    train(X_train, y_train, X_val=None, y_val=None, optimizer=None, epochs=100,
            batch_size=32, early_stopping_threshold=10, lr_scheduler=None, p=True,
            use_tqdm=True, n_jobs=1, track_metrics=False, track_adv_metrics=False):
        Abstract method to train the neural network using the provided training data.
    evaluate(X, y):
        Abstract method to evaluate the neural network on the provided data.
    predict(X):
        Abstract method to make predictions using the trained neural network.
    calculate_loss(X, y):
        Abstract method to calculate the loss of the neural network.
    apply_dropout(X):
        Applies dropout to the activation values for regularization.
    compute_l2_reg(weights):
        Computes the L2 regularization term for the given weights.
    calculate_precision_recall_f1(X, y):
        Calculates precision, recall, and F1 score for the predictions.
    create_scheduler(scheduler_type, optimizer, **kwargs):
        Creates a learning rate scheduler based on the specified type.
    plot_metrics(save_dir=None):
        Plots the training and validation metrics, including loss, accuracy,
        learning rate, and optionally precision, recall, and F1 score.
 
  Methods defined here:
__init__(self, layers, dropout_rate=0.0, reg_lambda=0.0, activations=None, loss_function=None, regressor=False)
Initializes the neural network with the specified layers, dropout rate, regularization, and activations.
 
Args:
    layers: (list) - A list of integers representing the sizes of each layer or a list of Layer objects.
    dropout_rate: (float), optional - The dropout rate for regularization (default is 0.0).
    reg_lambda: (float), optional - The regularization strength (default is 0.0).
    activations: (list of str), optional - A list of activation functions for each layer (default is None, which sets "relu" for hidden layers and "softmax" for the output layer).
    loss_function: (callable), optional - Custom loss function to use (default is None, which uses the default calculate_loss implementation).
    regressor: (bool), optional - If True, the network is treated as a regressor (default is False).
 
Raises:
    ValueError: If `layers` is not a list of integers or a list of Layer objects.
apply_dropout(self, X)
Applies dropout to the activation X.
 
Args:
    X: (ndarray) - Activation values.
 
Returns:
    ndarray: Activation values after applying dropout.
backward(self, y)
Performs backward propagation through the network.
calculate_loss(self, X, y)
Calculates the loss of the neural network.
calculate_precision_recall_f1(self, X, y)
Calculates precision, recall, and F1 score.
 
Args:
    X: (ndarray) - Input data
    y: (ndarray) - Target labels
Returns:
    precision: (float) - Precision score
    recall: (float) - Recall score
    f1: (float) - F1 score
compute_l2_reg(self, weights)
Computes the L2 regularization term.
 
Args:
    weights: (list) - List of weight matrices.
 
Returns:
    float: L2 regularization term.
create_scheduler(self, scheduler_type, optimizer, **kwargs)
Creates a learning rate scheduler.
evaluate(self, X, y)
Evaluates the neural network on the provided data.
forward(self, X, training=True)
Performs forward propagation through the network.
initialize_layers(self)
Initializes the weights and biases of the layers.
plot_metrics(self, save_dir=None)
Plots the training and validation metrics.
predict(self, X)
Makes predictions using the trained neural network.
train(self, X_train, y_train, X_val=None, y_val=None, optimizer=None, epochs=100, batch_size=32, early_stopping_threshold=10, lr_scheduler=None, p=True, use_tqdm=True, n_jobs=1, track_metrics=False, track_adv_metrics=False)
Trains the neural network using the provided training data.

Data descriptors defined here:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
Data
        CACHE = False
conv_spec = [('in_channels', int32), ('out_channels', int32), ('kernel_size', int32), ('stride', int32), ('padding', int32), ('weights', Array(float64, 4, 'A', False, aligned=True)), ('biases', Array(float64, 2, 'A', False, aligned=True)), ('activation', unicode_type), ('weight_gradients', Array(float64, 4, 'A', False, aligned=True)), ('bias_gradients', Array(float64, 2, 'A', False, aligned=True)), ('input_cache', Array(float64, 4, 'A', False, aligned=True)), ('X_cols', Array(float64, 3, 'A', False, aligned=True)), ('X_padded', Array(float64, 4, 'A', False, aligned=True)), ('h_out', int32), ('w_out', int32), ('input_size', int32), ('output_size', int32)]
flatten_spec = [('input_shape', UniTuple(int32, 3)), ('output_size', int32), ('input_cache', Array(float64, 4, 'A', False, aligned=True)), ('input_size', int32)]
float64 = float64
int32 = int32
spec = [('weights', Array(float64, 2, 'C', False, aligned=True)), ('biases', Array(float64, 2, 'C', False, aligned=True)), ('activation', unicode_type), ('weight_gradients', Array(float64, 2, 'C', False, aligned=True)), ('bias_gradients', Array(float64, 2, 'C', False, aligned=True)), ('input_cache', Array(float64, 2, 'C', False, aligned=True)), ('output_cache', Array(float64, 2, 'C', False, aligned=True)), ('input_size', int32), ('output_size', int32)]