from_torch#

This module provides support for importing models into the sinabs from pytorch.

sinabs.from_torch.from_model(model: ~torch.nn.modules.module.Module, input_shape: ~typing.Optional[~typing.Tuple[int, int, int]] = None, spike_threshold: ~torch.Tensor = tensor(1.), 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: float = -1.0, bias_rescaling: float = 1.0, batch_size: ~typing.Optional[int] = None, num_timesteps: ~typing.Optional[int] = None, synops: bool = False, add_spiking_output: bool = False, spike_layer_class: ~typing.Type = <class 'sinabs.layers.iaf.IAFSqueeze'>, backend=None, kwargs_backend: ~typing.Optional[dict] = None)[source]#

Converts a Torch model and returns a Sinabs network object. The modules in the model are analyzed, and a copy is returned, with all ReLUs and NeuromorphicReLUs turned into SpikingLayers.

Parameters:
  • model (Module) – Torch model

  • input_shape (Optional[Tuple[int, int, int]]) – If provided, the layer dimensions are computed. Otherwise they will be computed at the first forward pass.

  • spike_threshold (Tensor) – The membrane potential threshold for spiking (same for all layers).

  • spike_fn (Callable) – The spike dynamics to determine the number of spikes out

  • reset_fn (Callable) – The reset mechanism of the neuron (like reset to zero, or subtract)

  • surrogate_grad_fn (Callable) – The surrogate gradient method for the spiking dynamics

  • min_v_mem (float) – The lower bound of the potential in (same for all layers).

  • bias_rescaling (float) – Biases are divided by this value.

  • batch_size (Optional[int]) – Must be provided if num_timesteps is None and is ignored otherwise.

  • num_timesteps (Optional[int]) – Number of timesteps per sample. If None, batch_size must be provided to seperate batch and time dimensions.

  • synops (bool) – If True (default: False), register hooks for counting synaptic operations during forward passes.

  • add_spiking_output (bool) – If True (default: False), add a spiking layer to the end of a sequential model if not present.

  • spike_layer_class (Type) – Can be for example sinabs.layers.IAFSqueeze (default) or EXODUS equivalent.

  • backend – String defining the simulation backend (currently sinabs or exodus)

  • kwargs_backend (Optional[dict]) – Dict with additional kwargs for the simulation backend