Base Layer Class

class HeteroSymNN.Core.Nets.layers.Layer(num_inputs: int, layer_configuration: LayerConstructionConfig, batch_size: int = 1, Gpu_id: int = 0)[source]

Bases: object

Base layer class mainly used for dense networks

Parameters:
  • _num_inputs (int) – Number of inputs the layer is going to receive.

  • layer_configuration (LayerConstructionConfig) – Configuration of the layer including the node activation functions and their constants as well as the initial _weights, _biases and connection mask.

  • batch_size (int, optional) – Initial batch size for the layer, by default 1.

  • Gpu_id (int, optional) – Id of the GPU to use if available, by default 0.

Examples

Generaly one doesn’t need to instanciate this class directly but if some want to do it, here is an example of how to do it.

>>> from HeteroSymNN.Core.Nets.layers import Layer
>>> from HeteroSymNN.Core.initializers import HeNormal
>>> num_inputs = 3
>>> num_nodes = 5
>>> inicial_params = HeNormal().generate(3,5)
>>> layer_config = [("relu", {}),("relu", {}),("sigmoid", {}),("relu", {}),("relu", {})]
>>> constuctor = (layer_config,inicial_params)
>>> layer = Layer(num_inputs,constuctor)
property ACTIVATION_FUNCTION_CONSTANTS: BackendArray

Property to get the current activation function constants array.

To change any constant or list of constants use change_constant.

Returns:

Array of activation function constants.

Return type:

BackendArray

property COMPUTATIONAL_METHOD: Literal['GPU_CUDA', 'CPU_JIT', 'CPU_PYTHON']

Property to get the current computational method being used for calculations.

Returns:

Used computational method.

Return type:

Literal[“GPU_CUDA”,”CPU_JIT”,”CPU_PYTHON”]

property CURRENT_DEVICE: Literal['CPU', 'GPU']

Property to get the current device where the parameters like _weights, _biases and connection mask are located.

Returns:

Location of the layer parameters.

Return type:

Literal[“CPU”,”GPU”]

property GPU_ID: int

Property to get the current GPU ID being used for calculations.

If want to set a new GPU ID use set_gpu_id.

Returns:

Current GPU ID.

Return type:

int

property INICIAL_LAYER_NODE_CONFIGS: list[NodeConfig]

Property to get the initial layer node configurations used during layer construction.

Returns:

List of NodeConfig used during layer construction.

Return type:

list[NodeConfig]

property NUM_INPUTS: int

Property to get the number of inputs to the layer.

Returns:

Number of inputs to the layer.

Return type:

int

property NUM_NODES: int

Property to get the number of nodes in the layer.

Returns:

Number of nodes in the layer.

Return type:

int

backward(error_values: BackendArray) BackendArray[source]

Method to perform the backward pass of the layer.

Parameters:

error_values (BackendArray) – Error values from the next layer.

Returns:

Error values to be passed to the previous layer.

Return type:

BackendArray

batch_size_change(batch_size: int) None[source]

Method to change the batch size of the layer. This will reallocate the internal buffers if needed.

Parameters:

batch_size (int) – New batch size for the layer.

change_constant(new_values: list[ConstantToUpdate] | ConstantToUpdate) None[source]

Method to change the value of a activation function constant of a node or list of nodes.

Parameters:

new_values (Union[list[ConstantToUpdate], ConstantToUpdate]) – New value or list of new values to set. Each value is a tuple containing the node index, the constant name, and the new value.

forward(input_values: BackendArray) BackendArray[source]

Method to perform the forward pass of the layer.

Parameters:

input_values (BackendArray) – Input values to the layer.

Returns:

Output values of the layer after applying the activation functions.

Return type:

BackendArray

get_parameters() dict[str, numpy.ndarray][source]

Method to get the parameters of the layer.

Returns:

Dictionary of the parameters by string.

Return type:

dict[str, ndarray]

recunstruct_layer_config() list[NodeConfig][source]

Regenerates the list of NodeConfig with the updated activation funcion constants.

Returns:

List of NodeConfig with the current constants.

Return type:

list[NodeConfig]

set_connection_mask(connection_mask: numpy.ndarray) None[source]

Method to set the connection mask of the layer.

Parameters:

_connection_mask (np.ndarray) – 2D array of ones and zeros representing the connection mask. in the of shape (num_nodes, num_inputs).

set_gpu_id(new_id: int) None[source]

Method to change the GPU that is going to be used for the calculations.

If computer does not have valid GPUs the function will raise a ValueError.

Parameters:

new_id (int) – New GPU Id to use.

set_parameters(params: dict[str, numpy.ndarray]) None[source]

Method to set the parameters of the layer.

Parameters:

params (dict[str, np.ndarray]) – Dictionary containing the new parameters with keys ‘weights’ and ‘biases’.

to(device: Literal['CPU', 'GPU']) None[source]

Change the location of the waights, biases, connection mask and activation function constants from CPU to GPU or GPU to CPU.

Parameters:

device (Literal["CPU", "GPU"]) – To which device to change the data of the layer.