LIF
Contents
LIF#
A Leaky Integrate Fire (LIF) layer and variations thereof.
- class sinabs.layers.LIF(tau_mem: typing.Union[float, torch.Tensor], tau_syn: typing.Optional[typing.Union[float, torch.Tensor]] = None, spike_threshold: float = 1.0, spike_fn: typing.Callable = <class 'sinabs.activation.spike_generation.MultiSpike'>, reset_fn: typing.Callable = MembraneSubtract(subtract_value=None), surrogate_grad_fn: typing.Callable = SingleExponential(grad_width=0.5, grad_scale=1.0), min_v_mem: typing.Optional[float] = None, train_alphas: bool = False, shape: typing.Optional[torch.Size] = None, norm_input: bool = True, record_states: bool = False)#
A Leaky Integrate and Fire neuron layer.
Neuron dynamics in discrete time:
\[ \begin{align}\begin{aligned}V_{mem}(t+1) = \alpha V_{mem}(t) + (1-\alpha)\sum z(t)\\\text{if } V_{mem}(t) >= V_{th} \text{, then } V_{mem} \rightarrow V_{reset}\end{aligned}\end{align} \]where \(\alpha = e^{-1/tau_{mem}}\) and \(\sum z(t)\) represents the sum of all input currents at time \(t\).
- Parameters
tau_mem (float) – Membrane potential time constant.
tau_syn (float) – Synaptic decay time constants. If None, no synaptic dynamics are used, which is the default.
spike_threshold (float) – Spikes are emitted if v_mem is above that threshold. By default set to 1.0.
spike_fn (torch.autograd.Function) – Choose a Sinabs or custom torch.autograd.Function that takes a dict of states, a spike threshold and a surrogate gradient function and returns spikes. Be aware that the class itself is passed here (because torch.autograd methods are static) rather than an object instance.
reset_fn (Callable) – A function that defines how the membrane potential is reset after a spike.
surrogate_grad_fn (Callable) – Choose how to define gradients for the spiking non-linearity during the backward pass. This is a function of membrane potential.
min_v_mem (float or None) – Lower bound for membrane potential v_mem, clipped at every time step.
train_alphas (bool) – When True, the discrete decay factor exp(-1/tau) is used for training rather than tau itself.
shape (torch.Size) – Optionally initialise the layer state with given shape. If None, will be inferred from input_size.
norm_input (bool) – When True, normalise input current by tau. This helps when training time constants.
record_states (bool) – When True, will record all internal states such as v_mem or i_syn in a dictionary attribute recordings. Default is False.
- forward(input_data: torch.Tensor)#
Forward pass with given data.
- Parameters
input_current – torch.Tensor Data to be processed. Expected shape: (batch, time, …)
- Returns
- torch.Tensor
Output data. Same shape as input_data.
LIFRecurrent#
- class sinabs.layers.LIFRecurrent(tau_mem: typing.Union[float, torch.Tensor], rec_connect: torch.nn.modules.module.Module, tau_syn: typing.Optional[typing.Union[float, torch.Tensor]] = None, spike_threshold: float = 1.0, spike_fn: typing.Callable = <class 'sinabs.activation.spike_generation.MultiSpike'>, reset_fn: typing.Callable = MembraneSubtract(subtract_value=None), surrogate_grad_fn: typing.Callable = SingleExponential(grad_width=0.5, grad_scale=1.0), min_v_mem: typing.Optional[float] = None, train_alphas: bool = False, shape: typing.Optional[torch.Size] = None, norm_input: bool = True, record_states: bool = False)#
A Leaky Integrate and Fire neuron layer with recurrent connections.
Neuron dynamics in discrete time:
\[ \begin{align}\begin{aligned}V_{mem}(t+1) = \alpha V_{mem}(t) + (1-\alpha)\sum z(t)\\\text{if } V_{mem}(t) >= V_{th} \text{, then } V_{mem} \rightarrow V_{reset}\end{aligned}\end{align} \]where \(\alpha = e^{-1/tau_{mem}}\) and \(\sum z(t)\) represents the sum of all input currents at time \(t\).
- Parameters
tau_mem (float) – Membrane potential time constant.
rec_connect (torch.nn.Module) – An nn.Module which defines the recurrent connectivity, e.g. nn.Linear
tau_syn (float) – Synaptic decay time constants. If None, no synaptic dynamics are used, which is the default.
spike_threshold (float) – Spikes are emitted if v_mem is above that threshold. By default set to 1.0.
activation_fn (Callable) – a torch.autograd.Function to provide forward and backward calls. Takes care of all the spiking behaviour.
min_v_mem (float or None) – Lower bound for membrane potential v_mem, clipped at every time step.
train_alphas (bool) – When True, the discrete decay factor exp(-1/tau) is used for training rather than tau itself.
shape (torch.Size) – Optionally initialise the layer state with given shape. If None, will be inferred from input_size.
norm_input (bool) – When True, normalise input current by tau. This helps when training time constants.
record_states (bool) – When True, will record all internal states such as v_mem or i_syn in a dictionary attribute recordings. Default is False.
- forward(input_data: torch.Tensor)#
Forward pass with given data.
- Parameters
input_current – torch.Tensor Data to be processed. Expected shape: (batch, time, …)
- Returns
- torch.Tensor
Output data. Same shape as input_data.
LIFSqueeze#
- class sinabs.layers.LIFSqueeze(batch_size=None, num_timesteps=None, **kwargs)#
Same as parent LIF class, only takes in squeezed 4D input (Batch*Time, Channel, Height, Width) instead of 5D input (Batch, Time, Channel, Height, Width) in order to be compatible with layers that can only take a 4D input, such as convolutional and pooling layers.
- forward(input_data: torch.Tensor) torch.Tensor #
Forward pass with given data.
- Parameters
input_current – torch.Tensor Data to be processed. Expected shape: (batch, time, …)
- Returns
- torch.Tensor
Output data. Same shape as input_data.