sega_learn.neural_networks.layers_jit

 
Modules
       
numpy
numba.core.types

 
Classes
       
builtins.object
JITRNNLayer
JITConvLayer(builtins.object)
JITConvLayer
JITDenseLayer(builtins.object)
JITDenseLayer
JITFlattenLayer(builtins.object)
JITFlattenLayer

 
class JITConvLayer(JITConvLayer)
    JITConvLayer(*args, **kwargs)
 
A convolutional layer implementation for neural networks using Numba JIT compilation.
 
 
Method resolution order:
JITConvLayer
JITConvLayer
builtins.object

Data and other attributes defined here:
class_type = jitclass.JITConvLayer#20e9bd1bc90<in_channels:in...2,w_out:int32,input_size:int32,output_size:int32>

Methods inherited from JITConvLayer:
__init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, activation='relu')
Initializes the convolutional layer with weights, biases, and activation function.
 
Args:
    in_channels: (int) - Number of input channels.
    out_channels: (int) - Number of output channels.
    kernel_size: (int) - Size of the convolutional kernel (assumes square kernels).
    stride: (int), optional - Stride of the convolution (default is 1).
    padding: (int), optional - Padding added to the input (default is 0).
    activation: (str), optional - Activation function to use (default is "relu").
 
Attributes:
    weights: (np.ndarray) - Convolutional weight matrix initialized using He initialization.
    biases: (np.ndarray) - Bias vector initialized to zeros.
    activation: (str) - Activation function for the layer.
    weight_gradients: (np.ndarray) - Gradients of the weights, initialized to zeros.
    bias_gradients: (np.ndarray) - Gradients of the biases, initialized to zeros.
    input_cache: (np.ndarray) - Cached input values for backpropagation, initialized to zeros.
    X_cols: (np.ndarray) - Cached column-transformed input for backpropagation, initialized to zeros.
    X_padded: (np.ndarray) - Cached padded input for backpropagation, initialized to zeros.
    h_out: (int) - Height of the output feature map, initialized to 0.
    w_out: (int) - Width of the output feature map, initialized to 0.
    input_size: (int) - Number of input channels.
    output_size: (int) - Number of output channels.
activate(self, Z)
Apply activation function.
activation_derivative(self, Z)
Apply activation derivative.
backward(self, d_out, reg_lambda=0)
Backward pass for convolutional layer.
 
Args:
    d_out (np.ndarray): Gradient of the loss with respect to the layer output
    reg_lambda (float, optional): Regularization parameter
 
Returns:
    dX: Gradient with respect to the input X
forward(self, X)
Forward pass for convolutional layer.
 
Args:
    X: numpy array with shape (batch_size, in_channels, height, width)
 
Returns:
    Output feature maps after convolution and activation.
zero_grad(self)
Reset the gradients of the weights and biases to zero.

Data descriptors inherited from JITConvLayer:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
class JITDenseLayer(JITDenseLayer)
    JITDenseLayer(*args, **kwargs)
 
Initializes a fully connected layer object, where each neuron is connected to all neurons in the previous layer.
 
Each layer consists of weights, biases, and an activation function.
 
Args:
    input_size (int): The size of the input to the layer.
    output_size (int): The size of the output from the layer.
    activation (str): The activation function to be used in the layer.
 
Attributes:
    weights (np.ndarray): Weights of the layer.
    biases (np.ndarray): Biases of the layer.
    activation (str): Activation function name.
    weight_gradients (np.ndarray): Gradients of the weights.
    bias_gradients (np.ndarray): Gradients of the biases.
    input_cache (np.ndarray): Cached input for backpropagation.
    output_cache (np.ndarray): Cached output for backpropagation.
 
Methods:
    zero_grad(): Resets the gradients of the weights and biases to zero.
    forward(X): Performs the forward pass of the layer.
    backward(dA, reg_lambda): Performs the backward pass of the layer.
    activate(Z): Applies the activation function.
    activation_derivative(Z): Applies the derivative of the activation function.
 
 
Method resolution order:
JITDenseLayer
JITDenseLayer
builtins.object

Data and other attributes defined here:
class_type = jitclass.JITDenseLayer#20e9bd03090<weights:array...oat64, 2d, C),input_size:int32,output_size:int32>

Methods inherited from JITDenseLayer:
__init__(self, input_size, output_size, activation='relu')
Initializes the layer with weights, biases, and activation function.
 
Args:
    input_size: (int) - The number of input features to the layer.
    output_size: (int) - The number of output features from the layer.
    activation: (str), optional - The activation function to use (default is "relu").
 
Attributes:
    weights: (np.ndarray) - The weight matrix initialized using He initialization for ReLU or Leaky ReLU,
                or Xavier initialization for other activations.
    biases: (np.ndarray) - The bias vector initialized to zeros.
    activation: (str) - The activation function for the layer.
    weight_gradients: (np.ndarray) - Gradients of the weights, initialized to zeros.
    bias_gradients: (np.ndarray) - Gradients of the biases, initialized to zeros.
    input_cache: (np.ndarray) - Cached input values for backpropagation, initialized to zeros.
    output_cache: (np.ndarray) - Cached output values for backpropagation, initialized to zeros.
    input_size: (int) - The number of input features to the layer.
    output_size: (int) - The number of output features from the layer.
activate(self, Z)
Apply activation function.
activation_derivative(self, Z)
Apply activation derivative.
backward(self, dA, reg_lambda)
Perform the backward pass of the layer.
forward(self, X)
Perform the forward pass of the layer.
zero_grad(self)
Reset the gradients of the weights and biases to zero.

Data descriptors inherited from JITDenseLayer:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
class JITFlattenLayer(JITFlattenLayer)
    JITFlattenLayer(*args, **kwargs)
 
A layer that flattens multi-dimensional input into a 2D array (batch_size, flattened_size).
 
Useful for transitioning from convolutional layers to dense layers.
 
 
Method resolution order:
JITFlattenLayer
JITFlattenLayer
builtins.object

Data and other attributes defined here:
class_type = jitclass.JITFlattenLayer#20e9bd19090<input_shape...put_cache:array(float64, 4d, A),input_size:int32>

Methods inherited from JITFlattenLayer:
__init__(self)
Initializes the layer with placeholder values for input and output dimensions.
 
Attributes:
    input_shape: (tuple) - Shape of the input data, initialized to (0, 0, 0).
               This will be set during the forward pass.
    output_size: (int) - Size of the output, initialized to 0.
             This will be set during the forward pass.
    input_size: (int) - Size of the input, initialized to 0.
            This will be set during the forward pass.
    input_cache: (any) - Cache for input data, to be set during the forward pass.
backward(self, dA, reg_lambda=0)
Reshapes the gradient back to the original input shape.
 
Args:
    dA (np.ndarray): Gradient of the loss with respect to the layer's output,
                   shape (batch_size, flattened_size)
    reg_lambda (float): Regularization parameter (unused in FlattenLayer).
 
Returns:
    np.ndarray: Gradient with respect to the input, reshaped to original input shape.
forward(self, X)
Flattens the input tensor.
 
Args:
    X (np.ndarray): Input data of shape (batch_size, channels, height, width)
 
Returns:
    np.ndarray: Flattened output of shape (batch_size, flattened_size)

Data descriptors inherited from JITFlattenLayer:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
class JITRNNLayer(builtins.object)
    JITRNNLayer(input_size, hidden_size, activation='tanh')
 
A recurrent layer implementation for neural networks using Numba JIT compilation.
 
  Methods defined here:
__init__(self, input_size, hidden_size, activation='tanh')
Will be implemented later.

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