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:
ABCBase 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,
Layercannot be instantiated.Attributes overview
(str) Class name of
self(float) Simulation time step of this layer
(Type[TimeSeries]) Input
TimeSeriessubclass accepted by this layer.(float) Noise injected into the state of this layer during evolution
(Type[TimeSeries]) Output
TimeSeriessubclass emitted by this layer.(int) Number of units in this layer (N)
(int) Number of input channels accepted by this layer (M)
(int) Number of output channels produced by this layer (O)
(str) Return a string containing the layer subclass name and the layer
nameattribute(ndarray) Internal state of this layer (N)
(float) The current evolution time of this layer
(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
Layersubclass with parameters loaded from a dictionaryload_from_file(filename, **kwargs)Generate an instance of a
Layersubclass, with parameters loaded from a fileRandomize the internal state of this layer
Reset both the internal clock and the internal state of the layer
Reset the internal state of this layer
Reset the internal clock of this layer to 0
save(config, filename)Save a set of parameters to a
jsonfilesave_layer(filename)Obtain layer paramters from
to_dictand save in ajsonfileto_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_timestepsor the duration ofts_inputwill be used to determine evolution timenum_timesteps (Optional[int]) – Number of evolution time steps, in units of
dt. If not provided,durationor the duration ofts_inputwill 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, allowNoneas a value forinp. Otherwise an error will be raised. Default:True, allowNone
- Return ndarray:
Values of
inp, replicated out to the size of the current layer- Raises:
AssertionError – If
inpis incompatibly sized to replicate out to the layer sizeAssertionError – If
inpisNone, andallow_noneisFalse
- _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, thenNoneis permitted as argument forinp. Otherwise an error will be raised. Default:True, allowNone
- Return ndarray:
inp, replicated to the correct shape- Raises:
AssertionError – If
inpis shaped incompatibly to be replicated to the desired shapeAssertionError – If
inpisNoneandallow_noneisFalse
- _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 forinp. Otherwise and error will be raised. Default:True, allowNone
- Return ndarray:
Array of
inp, possibly expanded to the desired size- Raises:
AssertionError – If
inpis incompatibly shaped to expand to the desired sizeAssertionError – If
inpisNoneandallow_noneisFalse
- _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, allowNoneas a value forinp. Otherwise an error will be raised. Default:True, allowNone
- Return ndarray:
Values of
inp, replicated out to the size of the current layer- Raises:
AssertionError – If
inpis incompatibly sized to replicate out to the layer sizeAssertionError – If
inpisNone, andallow_noneisFalse
- _gen_time_trace(t_start: float, num_timesteps: int) ndarray[source]
Generate a time trace starting at
t_start, of lengthnum_timesteps + 1with 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]) –
TimeSeriesof TxM or Tx1 Input signals for this layerduration (Optional[float]) – Duration of the desired evolution, in seconds. If not provided, then either
num_timestepsor the duration ofts_inputwill define the evolution timenum_timesteps (Optional[int]) – Integer number of evolution time steps, in units of
.dt. If not provided, thendurationor the duration ofts_inputwill 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]) –
TSContinuousof TxM or Tx1 Input signals for this layerduration (Optional[float]) – Duration of the desired evolution, in seconds. If not provided, then either
num_timestepsor the duration ofts_inputwill define the evolution timenum_timesteps (Optional[int]) – Integer number of evolution time steps, in units of
.dt. If not provided, thendurationor the duration ofts_inputwill 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
TSEventtime series, set up evolution time baseThis 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_timestepsor the duration ofts_inputwill determine evolution itmenum_timesteps (Optional[int]) – Number of evolution time steps, in units of
.dt. If not provided, then eitherdurationor the duration ofts_inputwill 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
Layersubclass. Theevolvemethod 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_timestepsor the duration ofts_inputis used to determine evolution timenum_timesteps (Optional[int]) – Number of time steps to evolve the layer, in units of
.dt. If not provided, thendurationor the duration ofts_inputis used to determine evolution time
- Return TimeSeries:
(TxN) Output of this layer
- property input_type
(Type[TimeSeries]) Input
TimeSeriessubclass accepted by this layer.
- classmethod load_from_dict(config: Dict, **kwargs) cls[source]
Generate instance of a
Layersubclass with parameters loaded from a dictionary- Parameters:
cls (Any) – A
Layersubclass. This class will be used to reconstruct a layer based on the parameters stored infilenameconfig (Dict) – Dictionary containing parameters of a
Layersubclasskwargs – Any keyword arguments of the class
__init__()method where the parameters fromconfigshould be overridden
- Return Layer
Layer: Instance of
Layersubclass with parameters fromconfig
- classmethod load_from_file(filename: str, **kwargs) cls[source]
Generate an instance of a
Layersubclass, with parameters loaded from a file- Parameters:
cls (Any) – A
Layersubclass. This class will be used to reconstruct a layer based on the parameters stored infilenamefilename (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 Layer
Layer: Instance of
Layersubclass with parameters loaded fromfilename
- 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 thedtattribute
- property output_type
(Type[TimeSeries]) Output
TimeSeriessubclass 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
- save(config: Dict, filename: str)[source]
Save a set of parameters to a
jsonfile- 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_dictand save in ajsonfile- 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
nameattribute
- 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
Layerconfigures the dictionary, by storing attributesweights;dt;noise_std;name; andclass_name. To enable correct saving / loading of your derivedLayersubclass, you should first callself.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)