brainspy.processors.modules package#
Module contents#
Provides with additional modules, which are associated to a Processor class by an aggregation relationship, that can already replicate the scaling of DNPUs in ways that are known to work well. See https://raw.githubusercontent.com/BraiNEdarwin/brains-py/master/docs/figures/processor.jpg for more information.
Submodules#
brainspy.processors.modules.bn module#
File for applying a batch normalisation layer after a DNPU class.
- class brainspy.processors.modules.bn.DNPUBatchNorm(processor: ~brainspy.processors.processor.Processor, data_input_indices: list, forward_pass_type: str = 'vec', affine=False, track_running_stats=True, momentum=0.1, eps=1e-05, custom_bn=<class 'torch.nn.modules.batchnorm.BatchNorm1d'>)[source]#
Bases:
DNPUA child of brainspy.processors.dnpu.DNPU class that adds a batch normalisation layer after the output. for adding a batch normalisation layer after the output of a DNPU.
More information about batch normalisation can be found in: https://pytorch.org/docs/stable/generated/torch.nn.BatchNorm1d.html
- forward(x)[source]#
Run a forward pass through the processor, including any time-multiplexing modules that are declared to be measured in the same layer. After getting the output from the processor the output is passed through the batch normalisation layer.
- Parameters:
x (torch.Tensor) – Input data.
- Returns:
Output data.
- Return type:
torch.Tensor
- get_logged_variables()[source]#
Get the otuput results from each layer from the last forward pass.
- Returns:
Dictionary containing the output from the last forward pass as a dictionary. 1. c_dnpu_output: Output of the dnpu / dnpu layer 2. d_batch_norm_output: Output of the batch norm layer
- Return type:
dict
- training: bool#
brainspy.processors.modules.conv module#
File for applying a convolution layer using DNPU based kernels.
- class brainspy.processors.modules.conv.DNPUConv2d(processor, data_input_indices: list, in_channels: int, out_channels: int, kernel_size: int, stride: int = 1, padding: int = 0, forward_pass_type: str = 'vec')[source]#
Bases:
DNPUA child of brainspy.processors.dnpu.DNPU class that performs a convolution operation with DNPUs.
More information about the convolution operation can be found in: https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html?highlight=conv#torch.nn.Conv2d
- add_input_transform(input_range, strict=True)[source]#
Adds a linear transformation required to convert the input into the input electrode voltage ranges. It automatically calculates the input electrode voltage ranges for a particular DNPU according to the voltage ranges it was trained with. It is used typically to perform a current to voltage transformation, but it can also be applied for transforming the raw values from a dataset into voltages. The application of the input transformation occurs when the data has been reshaped into [Batch_size, window_no, in_chanels, node_no, input_electrode_no]. This function has to be called from outside the module, after its initialisation.
- Parameters:
input_range (list) – The range that the original raw input data is going to have. It can be specified with two values [min, max], representing the minimum and maximum values that the input data is expected to have. In this case, the linear transformation will be adapted to the length of the input dimension automatically. E.g. input_range = [0,1]. It can also be specified for different minimum and maximum value ranges per electrode. In this case, the list has to be specified with the same shape as the input_range variable of the class. This can be obtained by calling the get_input_ranges method.
- forward(x)[source]#
Forward pass of the convolution module.
- Parameters:
x (torch.Tensor) – Input to the convolution, with a shape of (batch_size, channel_no, input_img_height, input_image_width)
- Returns:
result – Image out of the convolution. With a shape of (batch_size, channel_no, output_feature_height, output_feature_width)
- Return type:
torch.Tensor
- init_params()[source]#
Initialises the control electrode indices and the data input electrode indices according to the size of the convolution. After that, reinitialises the control voltages (bias) that were initialised on the super call, but this time with the new dimensions for the control and data input indices.
- merge_electrode_data(x)[source]#
Merge the input data to be fed to the input data electrodes with the data to be fed to the control voltage electrodes.
- Parameters:
x (torch.tensor) – Input data that will be fed into the input data electrodes.
- Returns:
data (torch.Tensor) – A tensor with the input data and control voltage data to be fed through the activation electrodes, ordered according to the configurations of the indices for the data input and control voltage inputs to the DNPU convolution architecture. The data is given with a shape of: (batch_size,electrode_no).
original_data_dim (torch.Size) – The original data dimensions of the data before being converted into a shape of (batch_size,electrode_no). This information is used to reconstruct the output tensor after is passed through the processor.
- postprocess(result, data_dim, output_dim)[source]#
The shape of the output of the convolution after passing through the processor is of (batch_size,electrode_no). This method does the final operations of the convolution, and to make the output have the same data shape as it would from outside a covolution.
The postprocessing sums the values from the input kernel dimensions, and then applies either a sum or a linear operation to combine the outputs of the DNPU Convolution module.
- Parameters:
result (torch.Tensor) – A tensor with the input data and control voltage data to be fed through the activation electrodes, ordered according to the configurations of the indices for the data input and control voltage inputs to the DNPU convolution architecture. The data is given with a shape of: (batch_size,electrode_no).
data_dim (torch.Shape) – The original data dimensions of the data before being converted into a shape of (batch_size,electrode_no). This information is used to reconstruct the output tensor after is passed through the processor.
output_dim (int) – Dimension of the output after the convolution. It can be calculated with the method get_output_dim of this module.
- Returns:
result – Image out of the convolution. With a shape of (batch_size, channel_no, output_feature_height, output_feature_width)
- Return type:
torch.Tensor
- preprocess(x)[source]#
It extracts sliding local blocks from a batched input tensor. Then, it reshapes the input in a vectorised way, so that the input has the following a shape of (batch_size, dnpu_electrode_no). It applies batch norm and/or a linear transformation if these are added by calling add_input_transform after the initialisation of this module. These call only needs to happen once.
- Parameters:
x (torch.Tensor) – The raw input data to the convolution.
- Return type:
torch.Tensor
- training: bool#