sega_learn.neural_networks.neuralNetworkNumbaBackend

 
Modules
       
numpy
numba.core.types
warnings

 
Classes
       
sega_learn.neural_networks.neuralNetworkBase.NeuralNetworkBase(builtins.object)
NumbaBackendNeuralNetwork

 
class NumbaBackendNeuralNetwork(sega_learn.neural_networks.neuralNetworkBase.NeuralNetworkBase)
    NumbaBackendNeuralNetwork(layers, dropout_rate=0.2, reg_lambda=0.01, activations=None, loss_function=None, regressor=False, compile_numba=True, progress_bar=True)
 
A neural network implementation using Numba for Just-In-Time (JIT) compilation to optimize performance.
 
This class supports forward and backward propagation, training, evaluation, and hyperparameter tuning
with various optimizers and activation functions.
 
Attributes:
    compiled (bool): Indicates whether Numba functions are compiled.
    trainable_layers (list): Layers with trainable parameters (weights and biases).
    progress_bar (bool): Whether to display a progress bar during training.
 
Methods:
    __init__(layers, dropout_rate, reg_lambda, activations, compile_numba, progress_bar):
        Initializes the neural network with the specified parameters.
    store_init_layers():
        Stores the initial layers and their parameters for restoration after initialization.
    restore_layers():
        Restores the layers and their parameters after initialization.
    initialize_new_layers():
        Initializes the layers of the neural network with specified sizes and activation functions.
    forward(X, training):
        Performs forward propagation through the neural network.
    backward(y):
        Performs backward propagation to calculate gradients.
    is_not_instance_of_classes(obj, classes):
        Checks if an object is not an instance of any class in a list of classes.
    train(X_train, y_train, X_val, y_val, optimizer, epochs, batch_size, early_stopping_threshold,
          lr_scheduler, p, use_tqdm, n_jobs, track_metrics, track_adv_metrics, save_animation,
          save_path, fps, dpi, frame_every):
        Trains the neural network model with the specified parameters.
    evaluate(X, y):
        Evaluates the neural network on the given data and returns accuracy and predictions.
    predict(X):
        Predicts the output for the given input data.
    calculate_loss(X, y):
        Calculates the loss with L2 regularization.
    _create_optimizer(optimizer_type, learning_rate, JIT):
        Helper method to create optimizer instances.
    tune_hyperparameters(X_train, y_train, X_val, y_val, param_grid, layer_configs, optimizer_types,
                         lr_range, epochs, batch_size):
        Performs hyperparameter tuning using grid search.
    compile_numba_functions(progress_bar):
        Compiles all Numba JIT functions to improve performance.
 
 
Method resolution order:
NumbaBackendNeuralNetwork
sega_learn.neural_networks.neuralNetworkBase.NeuralNetworkBase
builtins.object

Methods defined here:
__init__(self, layers, dropout_rate=0.2, reg_lambda=0.01, activations=None, loss_function=None, regressor=False, compile_numba=True, progress_bar=True)
Initializes the Numba backend neural network.
 
Args:
    layers: (list) - List of layer sizes or Layer objects.
    dropout_rate: (float) - Dropout rate for regularization.
    reg_lambda: (float) - L2 regularization parameter.
    activations: (list) - List of activation functions for each layer.
    loss_function: (callable) optional - Custom loss function (default: selects based on task).
    regressor: (bool) - Whether the model is a regressor (default is False).
    compile_numba: (bool) - Whether to compile Numba functions.
    progress_bar: (bool) - Whether to display a progress bar.
backward(self, y)
Performs backward propagation to calculate the gradients.
 
Args:
    y (ndarray): Target labels of shape (m, output_size).
calculate_loss(self, X, y)
Calculates the loss with L2 regularization.
 
Args:
    X (ndarray): Input data.
    y (ndarray): Target labels.
 
Returns:
    float: The calculated loss value.
compile_numba_functions(self, progress_bar=True)
Compiles all Numba JIT functions to improve performance.
 
Args:
    progress_bar (bool): Whether to display a progress bar.
evaluate(self, X, y)
Evaluates the neural network on the given data.
 
Args:
    X (ndarray): Input data.
    y (ndarray): Target labels.
 
Returns:
    tuple: Accuracy and predicted labels.
forward(self, X, training=True)
Performs forward propagation through the neural network.
 
Args:
    X (ndarray): Input data of shape (batch_size, input_size).
    training (bool): Whether the network is in training mode (applies dropout).
 
Returns:
    ndarray: Output predictions of shape (batch_size, output_size).
initialize_new_layers(self)
Initializes the layers of the neural network.
 
Each layer is created with the specified number of neurons and activation function.
predict(self, X)
Predicts the output for the given input data.
 
Args:
    X (ndarray): Input data.
 
Returns:
    ndarray: Predicted outputs.
restore_layers(self)
Restores the layers after initialization.
store_init_layers(self)
Stores the layers to restore after initialization.
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, save_animation=False, save_path='training_animation.mp4', fps=1, dpi=100, frame_every=1)
Trains the neural network model.
 
Args:
    X_train: (ndarray) - Training data features.
    y_train: (ndarray) - Training data labels.
    X_val: (ndarray) - Validation data features, optional.
    y_val: (ndarray) - Validation data labels, optional.
    optimizer: (Optimizer) - Optimizer for updating parameters (default: JITAdam, lr=0.0001).
    epochs: (int) - Number of training epochs (default: 100).
    batch_size: (int) - Batch size for mini-batch gradient descent (default: 32).
    early_stopping_threshold: (int) - Patience for early stopping (default: 10).
    lr_scheduler: (Scheduler) - Learning rate scheduler (default: None).
    p: (bool) - Whether to print training progress (default: True).
    use_tqdm: (bool) - Whether to use tqdm for progress bar (default: True).
    n_jobs: (int) - Number of jobs for parallel processing (default: 1).
    track_metrics: (bool) - Whether to track training metrics (default: False).
    track_adv_metrics: (bool) - Whether to track advanced metrics (default: False).
    save_animation: (bool) - Whether to save the animation of metrics (default: False).
    save_path: (str) - Path to save the animation file. File extension must be .mp4 or .gif (default: 'training_animation.mp4').
    fps: (int) - Frames per second for the saved animation (default: 1).
    dpi: (int) - DPI for the saved animation (default: 100).
    frame_every: (int) - Capture frame every N epochs (to reduce file size) (default: 1).
tune_hyperparameters(self, X_train, y_train, X_val, y_val, param_grid, layer_configs=None, optimizer_types=None, lr_range=(0.0001, 0.01, 5), epochs=30, batch_size=32)
Performs hyperparameter tuning using grid search.
 
Args:
    X_train: (np.ndarray) - Training feature data.
    y_train: (np.ndarray) - Training target data.
    X_val: (np.ndarray) - Validation feature data.
    y_val: (np.ndarray) - Validation target data.
    param_grid: (dict) - Dictionary of parameters to try.
    layer_configs: (list), optional - List of layer configurations (default is None).
    optimizer_types: (list), optional - List of optimizer types (default is None).
    lr_range: (tuple), optional - (min_lr, max_lr, num_steps) for learning rates (default is (0.0001, 0.01, 5)).
    epochs: (int), optional - Max epochs for each trial (default is 30).
    batch_size: (int), optional - Batch size for training (default is 32).
 
Returns:
    best_params: (dict) - Best hyperparameters found.
    best_accuracy: (float) - Best validation accuracy.

Static methods defined here:
is_not_instance_of_classes(obj, classes)
Checks if an object is not an instance of any class in a list of classes.
 
Args:
    obj: The object to check.
    classes: A list of classes.
 
Returns:
    bool: True if the object is not an instance of any class in the list of classes, False otherwise.

Methods inherited from sega_learn.neural_networks.neuralNetworkBase.NeuralNetworkBase:
apply_dropout(self, X)
Applies dropout to the activation X.
 
Args:
    X: (ndarray) - Activation values.
 
Returns:
    ndarray: Activation values after applying dropout.
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.
initialize_layers(self)
Initializes the weights and biases of the layers.
plot_metrics(self, save_dir=None)
Plots the training and validation metrics.

Data descriptors inherited from sega_learn.neural_networks.neuralNetworkBase.NeuralNetworkBase:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
Data
        CACHE = False
NUMBA_AVAILABLE = True
TQDM_AVAILABLE = True
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)]
spec_adadelta = [('learning_rate', float64), ('rho', float64), ('epsilon', float64), ('reg_lambda', float64), ('E_g2', Array(float64, 3, 'C', False, aligned=True)), ('E_delta_x2', Array(float64, 3, 'C', False, aligned=True))]
spec_adam = [('learning_rate', float64), ('beta1', float64), ('beta2', float64), ('epsilon', float64), ('reg_lambda', float64), ('m', Array(float64, 3, 'C', False, aligned=True)), ('v', Array(float64, 3, 'C', False, aligned=True)), ('t', int32), ('dW', Array(float64, 2, 'A', False, aligned=True)), ('db', Array(float64, 2, 'A', False, aligned=True)), ('index', int32)]
spec_sgd = [('learning_rate', float64), ('momentum', float64), ('reg_lambda', float64), ('velocity', Array(float64, 3, 'C', False, aligned=True))]