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
(str) Class name of
self
(float) Simulation time step of this layer
(Type[TimeSeries]) Input
TimeSeries
subclass accepted by this layer.(float) Noise injected into the state of this layer during evolution
(Type[TimeSeries]) Output
TimeSeries
subclass 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
name
attribute(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
Layer
subclass with parameters loaded from a dictionaryload_from_file
(filename,Β **kwargs)Generate an instance of a
Layer
subclass, 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
json
filesave_layer
(filename)Obtain layer paramters from
to_dict
and save in ajson
fileto_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 ofts_input
will be used to determine evolution timenum_timesteps (Optional[int]) β Number of evolution time steps, in units of
dt
. If not provided,duration
or the duration ofts_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
, allowNone
as 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
inp
is incompatibly sized to replicate out to the layer sizeAssertionError β If
inp
isNone
, andallow_none
isFalse
- _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
, thenNone
is permitted as argument forinp
. Otherwise an error will be raised. Default:True
, allowNone
- Return ndarray:
inp
, replicated to the correct shape- Raises:
AssertionError β If
inp
is shaped incompatibly to be replicated to the desired shapeAssertionError β If
inp
isNone
andallow_none
isFalse
- _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
inp
is incompatibly shaped to expand to the desired sizeAssertionError β If
inp
isNone
andallow_none
isFalse
- _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
, allowNone
as 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
inp
is incompatibly sized to replicate out to the layer sizeAssertionError β If
inp
isNone
, andallow_none
isFalse
- _gen_time_trace(t_start: float, num_timesteps: int) ndarray [source]ο
Generate a time trace starting at
t_start
, of lengthnum_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 layerduration (Optional[float]) β Duration of the desired evolution, in seconds. If not provided, then either
num_timesteps
or the duration ofts_input
will define the evolution timenum_timesteps (Optional[int]) β Integer number of evolution time steps, in units of
.dt
. If not provided, thenduration
or the duration ofts_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 layerduration (Optional[float]) β Duration of the desired evolution, in seconds. If not provided, then either
num_timesteps
or the duration ofts_input
will define the evolution timenum_timesteps (Optional[int]) β Integer number of evolution time steps, in units of
.dt
. If not provided, thenduration
or the duration ofts_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 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_timesteps
or the duration ofts_input
will determine evolution itmenum_timesteps (Optional[int]) β Number of evolution time steps, in units of
.dt
. If not provided, then eitherduration
or the duration ofts_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. Theevolve
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 ofts_input
is used to determine evolution timenum_timesteps (Optional[int]) β Number of time steps to evolve the layer, in units of
.dt
. If not provided, thenduration
or the duration ofts_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 infilename
config (Dict) β Dictionary containing parameters of a
Layer
subclasskwargs β Any keyword arguments of the class
__init__()
method where the parameters fromconfig
should be overridden
- Return Layer
Layer
: Instance of
Layer
subclass with parameters fromconfig
- 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 infilename
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 Layer
Layer
: Instance of
Layer
subclass 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 thedt
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
- 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 ajson
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 attributesweights
;dt
;noise_std
;name
; andclass_name
. To enable correct saving / loading of your derivedLayer
subclass, 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)