nn.layers.Layer

class nn.layers.Layer(weights: ndarray, dt: float = 1.0, noise_std: float = 0.0, name: str = 'unnamed', *args, **kwargs)[source]

Bases: ABC

Base class for Layers in rockpool

This abstract class acts as a base class from which to derive subclasses that represent layers of neurons. As an abstract class, Layer cannot be instantiated.

Attributes overview

class_name

(str) Class name of self

dt

(float) Simulation time step of this layer

input_type

(Type[TimeSeries]) Input TimeSeries subclass accepted by this layer.

noise_std

(float) Noise injected into the state of this layer during evolution

output_type

(Type[TimeSeries]) Output TimeSeries subclass emitted by this layer.

size

(int) Number of units in this layer (N)

size_in

(int) Number of input channels accepted by this layer (M)

size_out

(int) Number of output channels produced by this layer (O)

start_print

(str) Return a string containing the layer subclass name and the layer name attribute

state

(ndarray) Internal state of this layer (N)

t

(float) The current evolution time of this layer

weights

(ndarray) Weights encapsulated by this layer (MxN)

Methods overview

__init__(weights[,Β dt,Β noise_std,Β name])

Implement an abstract layer of neurons (no implementation, must be subclassed)

evolve([ts_input,Β duration,Β num_timesteps])

Abstract method to evolve the state of this layer

load_from_dict(config,Β **kwargs)

Generate instance of a Layer subclass with parameters loaded from a dictionary

load_from_file(filename,Β **kwargs)

Generate an instance of a Layer subclass, with parameters loaded from a file

randomize_state()

Randomize the internal state of this layer

reset_all()

Reset both the internal clock and the internal state of the layer

reset_state()

Reset the internal state of this layer

reset_time()

Reset the internal clock of this layer to 0

save(config,Β filename)

Save a set of parameters to a json file

save_layer(filename)

Obtain layer paramters from to_dict and save in a json file

to_dict()

Convert parameters of this layer to a dict if they are relevant for reconstructing an identical layer

__init__(weights: ndarray, dt: float = 1.0, noise_std: float = 0.0, name: str = 'unnamed', *args, **kwargs)[source]

Implement an abstract layer of neurons (no implementation, must be subclassed)

Parameters:
  • weights (ArrayLike[float]) – Weight matrix for this layer. Indexed as [pre, post]

  • dt (float) – Time-step used for evolving this layer. Default: 1

  • noise_std (float) – Std. Dev. of state noise when evolving this layer. Default: 0. Defined as the expected std. dev. after 1s of integration time

  • name (str) – Name of this layer. Default: β€˜unnamed’

_abc_impl = <_abc._abc_data object>
_check_input_dims(inp: ndarray) ndarray[source]

Verify if dimensions of an input matches this layer instance

If input dimension == 1, scale it up to self._size_in by repeating signal.

Parameters:

inp (ndarray) – ArrayLike containing input data

Return ndarray:

inp, possibly with dimensions repeated

_determine_timesteps(ts_input: TimeSeries | None = None, duration: float | None = None, num_timesteps: int | None = None) int[source]

Determine how many time steps to evolve with the given input

Parameters:
  • ts_input (Optional[TimeSeries]) – TxM or Tx1 time series of input signals for this layer

  • duration (Optional[float]) – Duration of the desired evolution, in seconds. If not provided, num_timesteps or the duration of ts_input will be used to determine evolution time

  • num_timesteps (Optional[int]) – Number of evolution time steps, in units of dt. If not provided, duration or the duration of ts_input will be used to determine evolution time

Return int:

num_timesteps: Number of evolution time steps

_expand_to_net_size(inp, var_name: str = 'input', allow_none: bool = True) ndarray[source]

Replicate out a scalar to the size of the layer

Parameters:
  • inp (Any) – scalar or array-like

  • var_name (Optional[str]) – Name of the variable to include in error messages. Default: β€œinput”

  • allow_none (Optionbal[bool]) – If True, allow None as a value for inp. Otherwise an error will be raised. Default: True, allow None

Return ndarray:

Values of inp, replicated out to the size of the current layer

Raises:
  • AssertionError – If inp is incompatibly sized to replicate out to the layer size

  • AssertionError – If inp is None, and allow_none is False

_expand_to_shape(inp, shape: tuple, var_name: str = 'input', allow_none: bool = True) ndarray[source]

Replicate out a scalar to an array of shape shape

Parameters:
  • inp (Any) – scalar or array-like of input data

  • shape (Tuple[int]) – tuple defining array shape that input should be expanded to

  • var_name (Optional[str]) – Name of the variable to include in error messages. Default: β€œinput”

  • allow_none (Optional[bool]) – If True, then None is permitted as argument for inp. Otherwise an error will be raised. Default: True, allow None

Return ndarray:

inp, replicated to the correct shape

Raises:
  • AssertionError – If inp is shaped incompatibly to be replicated to the desired shape

  • AssertionError – If inp is None and allow_none is False

_expand_to_size(inp, size: int, var_name: str = 'input', allow_none: bool = True) ndarray[source]

Replicate out a scalar to a desired size

Parameters:
  • inp (Any) – scalar or array-like

  • size (int) – Size that input should be expanded to

  • var_name (Optional[str]) – Name of the variable to include in error messages. Default: β€œinput”

  • allow_none (Optional[bool]) – If True, allow None as a value for inp. Otherwise and error will be raised. Default: True, allow None

Return ndarray:

Array of inp, possibly expanded to the desired size

Raises:
  • AssertionError – If inp is incompatibly shaped to expand to the desired size

  • AssertionError – If inp is None and allow_none is False

_expand_to_weight_size(inp, var_name: str = 'input', allow_none: bool = True) ndarray[source]

Replicate out a scalar to the size of the layer’s weights

Parameters:
  • inp (Any) – scalar or array-like

  • var_name (Optional[str]) – Name of the variable to include in error messages. Default: β€œinput”

  • allow_none (Optionbal[bool]) – If True, allow None as a value for inp. Otherwise an error will be raised. Default: True, allow None

Return ndarray:

Values of inp, replicated out to the size of the current layer

Raises:
  • AssertionError – If inp is incompatibly sized to replicate out to the layer size

  • AssertionError – If inp is None, and allow_none is False

_gen_time_trace(t_start: float, num_timesteps: int) ndarray[source]

Generate a time trace starting at t_start, of length num_timesteps + 1 with time step length _dt

Parameters:
  • t_start (float) – Start time, in seconds

  • num_timesteps (int) – Number of time steps to generate, in units of .dt

Return (ndarray):

Generated time trace

_prepare_input(ts_input: TimeSeries | None = None, duration: float | None = None, num_timesteps: int | None = None) Tuple[ndarray, ndarray, int][source]

Sample input, set up time base

This function checks an input signal, and prepares a discretised time base according to the time step of the current layer

Parameters:
  • ts_input (Optional[TimeSeries]) – TimeSeries of TxM or Tx1 Input signals for this layer

  • duration (Optional[float]) – Duration of the desired evolution, in seconds. If not provided, then either num_timesteps or the duration of ts_input will define the evolution time

  • num_timesteps (Optional[int]) – Integer number of evolution time steps, in units of .dt. If not provided, then duration or the duration of ts_input will define the evolution time

Return (ndarray, ndarray, int):

(time_base, input_steps, num_timesteps) time_base: T1 Discretised time base for evolution input_raster (T1xN) Discretised input signal for layer num_timesteps: Actual number of evolution time steps, in units of .dt

_prepare_input_continuous(ts_input: TSContinuous | None = None, duration: float | None = None, num_timesteps: int | None = None) Tuple[ndarray, ndarray, int][source]

Sample input, set up time base

This function checks an input signal, and prepares a discretised time base according to the time step of the current layer

Parameters:
  • ts_input (Optional[TSContinuous]) – TSContinuous of TxM or Tx1 Input signals for this layer

  • duration (Optional[float]) – Duration of the desired evolution, in seconds. If not provided, then either num_timesteps or the duration of ts_input will define the evolution time

  • num_timesteps (Optional[int]) – Integer number of evolution time steps, in units of .dt. If not provided, then duration or the duration of ts_input will define the evolution time

Return (ndarray, ndarray, int):

(time_base, input_steps, num_timesteps) time_base: T1 Discretised time base for evolution input_steps: (T1xN) Discretised input signal for layer num_timesteps: Actual number of evolution time steps, in units of .dt

_prepare_input_events(ts_input: TSEvent | None = None, duration: float | None = None, num_timesteps: int | None = None) Tuple[ndarray, ndarray, int][source]

Sample input from a TSEvent time series, set up evolution time base

This function checks an input signal, and prepares a discretised time base according to the time step of the current layer

Parameters:
  • ts_input (Optional[TSEvent]) – TimeSeries of TxM or Tx1 Input signals for this layer

  • duration (Optional[float]) – Duration of the desired evolution, in seconds. If not provided, then either num_timesteps or the duration of ts_input will determine evolution itme

  • num_timesteps (Optional[int]) – Number of evolution time steps, in units of .dt. If not provided, then either duration or the duration of ts_input will determine evolution time

Return (ndarray, ndarray, int):

time_base: T1X1 vector of time points – time base for the rasterisation spike_raster: Boolean or integer raster containing spike information. T1xM array num_timesteps: Actual number of evolution time steps, in units of .dt

property _t
property class_name: str

(str) Class name of self

property dt: float

(float) Simulation time step of this layer

abstract evolve(ts_input: TimeSeries | None = None, duration: float | None = None, num_timesteps: int | None = None) TimeSeries[source]

Abstract method to evolve the state of this layer

This method must be overridden to produce a concrete Layer subclass. The evolve method is the main interface for simulating a layer. It must accept an input time series which determines the signals injected into the layer as input, and return an output time series representing the output of the layer.

Parameters:
  • ts_input (Optional[TimeSeries]) – (TxM) External input trace to use when evolving the layer

  • duration (Optional[float]) – Duration in seconds to evolve the layer. If not provided, then num_timesteps or the duration of ts_input is used to determine evolution time

  • num_timesteps (Optional[int]) – Number of time steps to evolve the layer, in units of .dt. If not provided, then duration or the duration of ts_input is used to determine evolution time

Return TimeSeries:

(TxN) Output of this layer

property input_type

(Type[TimeSeries]) Input TimeSeries subclass accepted by this layer.

classmethod load_from_dict(config: Dict, **kwargs) cls[source]

Generate instance of a Layer subclass with parameters loaded from a dictionary

Parameters:
  • cls (Any) – A Layer subclass. This class will be used to reconstruct a layer based on the parameters stored in filename

  • config (Dict) – Dictionary containing parameters of a Layer subclass

  • kwargs – Any keyword arguments of the class __init__() method where the parameters from config should be overridden

Return LayerLayer:

Instance of Layer subclass with parameters from config

classmethod load_from_file(filename: str, **kwargs) cls[source]

Generate an instance of a Layer subclass, with parameters loaded from a file

Parameters:
  • cls (Any) – A Layer subclass. This class will be used to reconstruct a layer based on the parameters stored in filename

  • filename (str) – Path to the file where parameters are stored

  • kwargs – Any keyword arguments of the class __init__ method where the parameter stored in the file should be overridden

Return LayerLayer:

Instance of Layer subclass with parameters loaded from filename

property noise_std

(float) Noise injected into the state of this layer during evolution

This value represents the standard deviation of a white noise process. When subclassing Layer, this value should be corrected by the dt attribute

property output_type

(Type[TimeSeries]) Output TimeSeries subclass emitted by this layer.

randomize_state()[source]

Randomize the internal state of this layer

Unless overridden, this method randomizes the layer state based on the current state, using a Normal distribution with std. dev. of 20% of the current state values

reset_all()[source]

Reset both the internal clock and the internal state of the layer

reset_state()[source]

Reset the internal state of this layer

Sets state attribute to all zeros

reset_time()[source]

Reset the internal clock of this layer to 0

save(config: Dict, filename: str)[source]

Save a set of parameters to a json file

Parameters:
  • config (Dict) – Dictionary of attributes to be saved

  • filename (str) – Path of file where parameters are stored

save_layer(filename: str)[source]

Obtain layer paramters from to_dict and save in a json file

Parameters:

filename (str) – Path of file where parameters are to be stored

property size: int

(int) Number of units in this layer (N)

property size_in: int

(int) Number of input channels accepted by this layer (M)

property size_out: int

(int) Number of output channels produced by this layer (O)

property start_print

(str) Return a string containing the layer subclass name and the layer name attribute

property state

(ndarray) Internal state of this layer (N)

property t

(float) The current evolution time of this layer

abstract to_dict() Dict[source]

Convert parameters of this layer to a dict if they are relevant for reconstructing an identical layer

The base class Layer configures the dictionary, by storing attributes weights; dt; noise_std; name; and class_name. To enable correct saving / loading of your derived Layer subclass, you should first call self.super().to_dict() and then store all additional arguments to __init__() required by your class to instantiate an identical object.

Return Dict:

A dictionary that can be used to reconstruct the layer

property weights: ndarray

(ndarray) Weights encapsulated by this layer (MxN)