NeuroTorch

neurotorch.transforms package

Submodules

neurotorch.transforms.base module

class neurotorch.transforms.base.ConstantValuesTransform(n_steps: int, batch_wise: bool = True)

Bases: Module

__init__(n_steps: int, batch_wise: bool = True)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x: Any)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class neurotorch.transforms.base.IdentityTransform

Bases: Module

__init__()

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x: Any)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class neurotorch.transforms.base.LinearRateToSpikes(n_steps: int, *, data_min: float = 0.0, data_max: float = 1.0, epsilon: float = 1e-06)

Bases: Module

__init__(n_steps: int, *, data_min: float = 0.0, data_max: float = 1.0, epsilon: float = 1e-06)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

firing_periods_to_spikes(firing_periods: ndarray) ndarray
forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class neurotorch.transforms.base.MaybeSoftmax(dim: int = -1)

Bases: Module

__init__(dim: int = -1)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class neurotorch.transforms.base.ReduceFuncTanh(reduce_func: Callable[[Dict[str, Tensor] | Tensor], Dict[str, Tensor] | Tensor])

Bases: Module

Applies a reduction function to the output of a recurrent layer and then applies a tanh activation.

__init__(reduce_func: Callable[[Dict[str, Tensor] | Tensor], Dict[str, Tensor] | Tensor])

Constructor of the ReduceFuncTanh class.

Parameters:

reduce_func – The reduction function to apply to the output of the recurrent layer. Must take a torch.Tensor or a dictionary of shape(s) (batch_size, seq_len, hidden_size) as input and return a torch.Tensor or a dictionary of shape(s) (batch_size, hidden_size) as output.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class neurotorch.transforms.base.ReduceMax(dim: int = 1)

Bases: Module

__init__(dim: int = 1)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

extra_repr() str

Set the extra representation of the module

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class neurotorch.transforms.base.ReduceMean(dim: int = 1)

Bases: Module

__init__(dim: int = 1)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

extra_repr() str

Set the extra representation of the module

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class neurotorch.transforms.base.ReduceSum(dim: int = 1)

Bases: Module

__init__(dim: int = 1)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

extra_repr() str

Set the extra representation of the module

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class neurotorch.transforms.base.ToDevice(device: device, non_blocking: bool = True)

Bases: Module

__init__(device: device, non_blocking: bool = True)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

property device
forward(x: Tensor)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

to(device: int | device | None, non_blocking: bool | None = None, *args, **kwargs)

Moves and/or casts the parameters and buffers.

This can be called as

to(device=None, dtype=None, non_blocking=False)
to(dtype, non_blocking=False)
to(tensor, non_blocking=False)
to(memory_format=torch.channels_last)

Its signature is similar to torch.Tensor.to(), but only accepts floating point or complex dtypes. In addition, this method will only cast the floating point or complex parameters and buffers to dtype (if given). The integral parameters and buffers will be moved device, if that is given, but with dtypes unchanged. When non_blocking is set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.

See below for examples.

Note

This method modifies the module in-place.

Parameters:
  • device (torch.device) – the desired device of the parameters and buffers in this module

  • dtype (torch.dtype) – the desired floating point or complex dtype of the parameters and buffers in this module

  • tensor (torch.Tensor) – Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module

  • memory_format (torch.memory_format) – the desired memory format for 4D parameters and buffers in this module (keyword only argument)

Returns:

self

Return type:

Module

Examples:

>>> # xdoctest: +IGNORE_WANT("non-deterministic")
>>> linear = nn.Linear(2, 2)
>>> linear.weight
Parameter containing:
tensor([[ 0.1913, -0.3420],
        [-0.5113, -0.2325]])
>>> linear.to(torch.double)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1913, -0.3420],
        [-0.5113, -0.2325]], dtype=torch.float64)
>>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1)
>>> gpu1 = torch.device("cuda:1")
>>> linear.to(gpu1, dtype=torch.half, non_blocking=True)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1914, -0.3420],
        [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1')
>>> cpu = torch.device("cpu")
>>> linear.to(cpu)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1914, -0.3420],
        [-0.5112, -0.2324]], dtype=torch.float16)

>>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble)
>>> linear.weight
Parameter containing:
tensor([[ 0.3741+0.j,  0.2382+0.j],
        [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128)
>>> linear(torch.ones(3, 2, dtype=torch.cdouble))
tensor([[0.6122+0.j, 0.1150+0.j],
        [0.6122+0.j, 0.1150+0.j],
        [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
training: bool
class neurotorch.transforms.base.ToTensor(dtype=torch.float32, device: device | None = None)

Bases: Module

__init__(dtype=torch.float32, device: device | None = None)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x: Any) Tensor

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
neurotorch.transforms.base.to_numpy(x: ~typing.Any, dtype=<class 'numpy.float32'>)
neurotorch.transforms.base.to_tensor(x: Any, dtype=torch.float32)

neurotorch.transforms.spikes_auto_encoder module

class neurotorch.transforms.spikes_auto_encoder.SpikesAutoEncoder(n_units: int, n_encoder_steps: int, encoder_type: Type[LIFLayer | SpyLIFLayer | ALIFLayer] | None = None, spikes_encoder: Module | None = None, spikes_decoder: Module | None = None, **kwargs)

Bases: BaseModel

__init__(n_units: int, n_encoder_steps: int, encoder_type: Type[LIFLayer | SpyLIFLayer | ALIFLayer] | None = None, spikes_encoder: Module | None = None, spikes_decoder: Module | None = None, **kwargs)

Constructor of the BaseModel class. This class is the base class of all models.

Parameters:
  • input_sizes (Union[Dict[str, DimensionLike], SizeTypes]) – The input sizes of the model.

  • output_size (Union[Dict[str, DimensionLike], SizeTypes]) – The output size of the model.

  • name (str) – The name of the model.

  • checkpoint_folder (str) – The folder where the checkpoints are saved.

  • device (torch.device) – The device of the model. If None, the default device is used.

  • input_transform (Union[Dict[str, Callable], List[Callable]]) – The transforms to apply to the inputs. The input_transform must work batch-wise.

  • output_transform (Union[Dict[str, Callable], List[Callable]]) – The transforms to apply to the outputs. The output_transform must work batch-wise.

Keyword Arguments:

kwargs – Additional arguments.

decode(x: Tensor)
property device: device

The device of the model. :rtype: torch.device

Type:

return

encode(x: Tensor) Tensor
forward(x: Tensor, **kwargs)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_and_reset_regularization_loss() Tensor

Get the regularization loss as a sum of all the regularization losses of the layers. Then reset the regularization losses.

Returns:

the regularization loss.

get_prediction_trace(inputs: Dict[str, Any] | Tensor, **kwargs) Dict[str, Tensor] | Tensor

Get the prediction trace of the network.

Parameters:
  • inputs (Union[Dict[str, Any], torch.Tensor]) – The inputs of the network.

  • kwargs – Additional arguments.

Returns:

The prediction trace.

Return type:

Union[Dict[str, torch.Tensor], torch.Tensor]

get_raw_prediction(inputs: Tensor, *args, **kwargs) Tuple[Any, Any, Any] | Tuple[Any, Any] | Any

The raw prediction of the network.

Parameters:
  • inputs (torch.Tensor) – The inputs of the network.

  • re_outputs_trace (bool) – If True, the outputs trace will be returned.

  • re_hidden_states (bool) – If True, the hidden states will be returned.

Returns:

The raw prediction.

Return type:

Union[Tuple[Any, Any, Any], Tuple[Any, Any], Any]

input_transform: torch.nn.ModuleDict
output_transform: torch.nn.ModuleDict
training: bool

neurotorch.transforms.spikes_decoders module

class neurotorch.transforms.spikes_decoders.MeanConv(kernel_size: int, alpha: float | Tensor | ndarray = 1.0, learn_alpha: bool = True, learn_kernel: bool = True, activation: Module = Identity(), pad_value: float | None = 0.0, pad_mode: str = 'left')

Bases: Module

Apply a weighted mean filter to the input tensor.

__init__(kernel_size: int, alpha: float | Tensor | ndarray = 1.0, learn_alpha: bool = True, learn_kernel: bool = True, activation: Module = Identity(), pad_value: float | None = 0.0, pad_mode: str = 'left')

Constructor for MeanConv class.

Parameters:
  • kernel_size – Size of the convolution kernel.

  • alpha – Multiplicative factor for the convolution kernel.

  • learn_alpha – Whether to learn the alpha parameter.

  • learn_kernel – Whether to learn the convolution kernel.

  • activation – Activation function to apply to the output.

  • pad_value – Value to use for padding the input tensor. This is used to ensure that the input tensor has a valid shape for the convolution operation. If None, the input tensor is not padded.

  • pad_mode – Padding mode. If “left”, the padding is applied to the left of the input tensor. If “right”, the padding is applied to the right of the input tensor. If “both”, the padding is applied to both sides of the input tensor.

extra_repr()

Set the extra representation of the module

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

forward(inputs: Tensor)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool

neurotorch.transforms.spikes_encoders module

class neurotorch.transforms.spikes_encoders.ALIFEncoder(n_steps: int, n_units: int, dt: float = 0.001, device: device | None = None, spikes_layer_kwargs: dict | None = None)

Bases: SpikesEncoder

__init__(n_steps: int, n_units: int, dt: float = 0.001, device: device | None = None, spikes_layer_kwargs: dict | None = None)

Constructor for the SpikesEncoder class.

Parameters:
  • n_steps – Number of steps to encode.

  • n_units – Number of units in the encoder.

  • spikes_layer_type – Dynamic of the spikes layer.

  • dt – Time step of the simulation.

  • device – Device to use for the encoder.

  • kwargs – Keyword arguments for the spikes layer.

Keyword Arguments:

kwargs

  • <forward_weights>: Forward weights of the spikes layer. Default: eye(n_units).

  • <use_recurrent_connection>: Whether to use a recurrent connection. Default: False.

  • <name>: Name of the spikes layer. Default: “encoder”.

training: bool
class neurotorch.transforms.spikes_encoders.LIFEncoder(n_steps: int, n_units: int, dt: float = 0.001, device: device | None = None, spikes_layer_kwargs: dict | None = None)

Bases: SpikesEncoder

__init__(n_steps: int, n_units: int, dt: float = 0.001, device: device | None = None, spikes_layer_kwargs: dict | None = None)

Constructor for the SpikesEncoder class.

Parameters:
  • n_steps – Number of steps to encode.

  • n_units – Number of units in the encoder.

  • spikes_layer_type – Dynamic of the spikes layer.

  • dt – Time step of the simulation.

  • device – Device to use for the encoder.

  • kwargs – Keyword arguments for the spikes layer.

Keyword Arguments:

kwargs

  • <forward_weights>: Forward weights of the spikes layer. Default: eye(n_units).

  • <use_recurrent_connection>: Whether to use a recurrent connection. Default: False.

  • <name>: Name of the spikes layer. Default: “encoder”.

training: bool
class neurotorch.transforms.spikes_encoders.SpikesEncoder(n_steps: int, n_units: int, spikes_layer_type: Type[LIFLayer | SpyLIFLayer | ALIFLayer], dt: float = 0.001, device: device | None = None, **kwargs)

Bases: Module

__init__(n_steps: int, n_units: int, spikes_layer_type: Type[LIFLayer | SpyLIFLayer | ALIFLayer], dt: float = 0.001, device: device | None = None, **kwargs)

Constructor for the SpikesEncoder class.

Parameters:
  • n_steps – Number of steps to encode.

  • n_units – Number of units in the encoder.

  • spikes_layer_type – Dynamic of the spikes layer.

  • dt – Time step of the simulation.

  • device – Device to use for the encoder.

  • kwargs – Keyword arguments for the spikes layer.

Keyword Arguments:

kwargs

  • <forward_weights>: Forward weights of the spikes layer. Default: eye(n_units).

  • <use_recurrent_connection>: Whether to use a recurrent connection. Default: False.

  • <name>: Name of the spikes layer. Default: “encoder”.

forward(x: Any)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_and_reset_regularization_loss() Tensor

Get the regularization loss as a sum of all the regularization losses of the layers. Then reset the regularization losses.

Returns:

the regularization loss.

to(device)

Moves and/or casts the parameters and buffers.

This can be called as

to(device=None, dtype=None, non_blocking=False)
to(dtype, non_blocking=False)
to(tensor, non_blocking=False)
to(memory_format=torch.channels_last)

Its signature is similar to torch.Tensor.to(), but only accepts floating point or complex dtypes. In addition, this method will only cast the floating point or complex parameters and buffers to dtype (if given). The integral parameters and buffers will be moved device, if that is given, but with dtypes unchanged. When non_blocking is set, it tries to convert/move asynchronously with respect to the host if possible, e.g., moving CPU Tensors with pinned memory to CUDA devices.

See below for examples.

Note

This method modifies the module in-place.

Parameters:
  • device (torch.device) – the desired device of the parameters and buffers in this module

  • dtype (torch.dtype) – the desired floating point or complex dtype of the parameters and buffers in this module

  • tensor (torch.Tensor) – Tensor whose dtype and device are the desired dtype and device for all parameters and buffers in this module

  • memory_format (torch.memory_format) – the desired memory format for 4D parameters and buffers in this module (keyword only argument)

Returns:

self

Return type:

Module

Examples:

>>> # xdoctest: +IGNORE_WANT("non-deterministic")
>>> linear = nn.Linear(2, 2)
>>> linear.weight
Parameter containing:
tensor([[ 0.1913, -0.3420],
        [-0.5113, -0.2325]])
>>> linear.to(torch.double)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1913, -0.3420],
        [-0.5113, -0.2325]], dtype=torch.float64)
>>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA1)
>>> gpu1 = torch.device("cuda:1")
>>> linear.to(gpu1, dtype=torch.half, non_blocking=True)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1914, -0.3420],
        [-0.5112, -0.2324]], dtype=torch.float16, device='cuda:1')
>>> cpu = torch.device("cpu")
>>> linear.to(cpu)
Linear(in_features=2, out_features=2, bias=True)
>>> linear.weight
Parameter containing:
tensor([[ 0.1914, -0.3420],
        [-0.5112, -0.2324]], dtype=torch.float16)

>>> linear = nn.Linear(2, 2, bias=None).to(torch.cdouble)
>>> linear.weight
Parameter containing:
tensor([[ 0.3741+0.j,  0.2382+0.j],
        [ 0.5593+0.j, -0.4443+0.j]], dtype=torch.complex128)
>>> linear(torch.ones(3, 2, dtype=torch.cdouble))
tensor([[0.6122+0.j, 0.1150+0.j],
        [0.6122+0.j, 0.1150+0.j],
        [0.6122+0.j, 0.1150+0.j]], dtype=torch.complex128)
training: bool
class neurotorch.transforms.spikes_encoders.SpyLIFEncoder(n_steps: int, n_units: int, dt: float = 0.001, device: device | None = None, spikes_layer_kwargs: dict | None = None)

Bases: SpikesEncoder

__init__(n_steps: int, n_units: int, dt: float = 0.001, device: device | None = None, spikes_layer_kwargs: dict | None = None)

Constructor for the SpikesEncoder class.

Parameters:
  • n_steps – Number of steps to encode.

  • n_units – Number of units in the encoder.

  • spikes_layer_type – Dynamic of the spikes layer.

  • dt – Time step of the simulation.

  • device – Device to use for the encoder.

  • kwargs – Keyword arguments for the spikes layer.

Keyword Arguments:

kwargs

  • <forward_weights>: Forward weights of the spikes layer. Default: eye(n_units).

  • <use_recurrent_connection>: Whether to use a recurrent connection. Default: False.

  • <name>: Name of the spikes layer. Default: “encoder”.

training: bool

neurotorch.transforms.vision module

class neurotorch.transforms.vision.ImgToSpikes(n_steps: int, t_max: float | None = None, tau=0.02, thr=0.2, use_periods=False, epsilon=1e-07)

Bases: object

__call__(x) Tensor

Call self as a function.

__init__(n_steps: int, t_max: float | None = None, tau=0.02, thr=0.2, use_periods=False, epsilon=1e-07)
Parameters:
  • n_steps – The number of time step

  • t_max

  • tau – The membrane time constant of the LIF neuron to be charged

  • thr – The firing threshold value

  • epsilon – A generic (small) epsilon > 0

firing_periods_to_spikes(firing_periods: ndarray) ndarray
firing_periods_to_spikes_clip(firing_periods: ndarray) ndarray
firing_periods_to_spikes_loop(firing_periods: ndarray) ndarray
firing_times_to_spikes(firing_times: ndarray) ndarray
pixels_to_firing_periods(x: ndarray) ndarray

Computes first firing time latency for a current input x assuming the charge time of a current based LIF neuron. :param x: the normalized between 0.0 and 1.0 input pixels :return: Time between spikes for each pixel of x

neurotorch.transforms.wrappers module

class neurotorch.transforms.wrappers.CallableToModuleWrapper(callable_object: Callable)

Bases: Module

__init__(callable_object: Callable)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(*args, **kwargs)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool

Module contents