Models (reservoirpy.Model)#
Note
See the following guides to:
Learn more about how to work with ReservoirPy Nodes: Node functional API
Learn more about how to combine nodes within a Model: From Nodes to Models
Models are an extension of the Node API. They allow to combine nodes into complex computational graphs, to create complicated Reservoir Computing architecture like Deep Echo State Networks.
See From Nodes to Models to learn more about how to create and manipulate
a Model.
- class reservoirpy.model.Model( )[source]#
Model base class.
- Parameters:
Methods
initialize(x[, y])Initializes a
Modelinstance at runtime, using samples of data to infer allNodedimensions and instantiate the feedback buffers.step([x])Call the Model function on a single step of data and update its state.
run([x, iters, workers])Run the Model on a sequence of data.
predict([x, iters, workers])Alias for
run().fit(x[, y, warmup, workers])Offline fitting method of a Model.
partial_fit(x[, y])Fit the Model in an online fashion.
Attributes
List of Nodes contained in the model, in insertion order.
List of
(NodeA, d, NodeB)edges, representing a connection fromNodeAtoNodeBwith a delay ofd.List of Nodes that expects an input
List of Nodes expected to output their values
Dictionary that associates a name to a Node with that name in the model.
List of nodes that can be trained
List of Nodes contained in the model, in the order they should be run.
Dictionary that associates a list of all Nodes connected to the key Node.
Dictionary that associates a list of all Nodes to which the key node is connected.
If True, the model can be trained (with
fit()).If True, the model has multiple Node inputs
If True, the model has multiple outputs and returns a dictionary that associates a node name to a node output.
If True, the model can be trained in parallel (offline).
If True, the model and its Nodes has been initialized.
- children: dict[Node, list[Node]]#
Dictionary that associates a list of all Nodes to which the key node is connected.
- edges: list[tuple[Node, int, Node]]#
List of
(NodeA, d, NodeB)edges, representing a connection fromNodeAtoNodeBwith a delay ofd.
- feedback_buffers: dict[tuple[Node, int, Node], ndarray[tuple[int, int], dtype[floating]]]#
Dictionary of edges -> np.ndarray where arrays contain the values to be sent to the receiving node.
- fit(
- x: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | dict[str, array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)]],
- y: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | dict[str, array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)]] | None = None,
- warmup: int = 0,
- workers: int = 1,
Offline fitting method of a Model.
- Parameters:
x (list or array-like of shape ([series, ] timesteps, input_dim) or a mapping of input, optional) – Input sequences dataset.
y (list or array-like of shape ([series], timesteps, output_dim), or a mapping of input optional) – Teacher signals dataset. If None, the method will try to fit the Node in an unsupervised way, if possible.
warmup (int, default to 0) – Number of timesteps to consider as warmup and discard at the beginning of each timeseries before training.
workers (int) –
- Returns:
Node trained offline.
- Return type:
- initialize(
- x: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | dict[str, array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)]] | array(d) | dict[str, array(d)],
- y: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | dict[str, array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)]] | array(d) | dict[str, array(d)] | None = None,
Initializes a
Modelinstance at runtime, using samples of data to infer allNodedimensions and instantiate the feedback buffers.- Parameters:
x (numpy.ndarray or dict of numpy.ndarray) – A vector of shape (1, ndim) corresponding to a timestep of data, or a dictionary mapping node names to vector of shapes (1, ndim of corresponding node).
y (numpy.ndarray or dict of numpy.ndarray, optional) – A vector of shape (1, ndim) corresponding to a timestep of target data, or a dictionary mapping node names to vector of shapes (1, ndim of corresponding node).
- is_multi_output: bool#
If True, the model has multiple outputs and returns a dictionary that associates a node name to a node output.
- is_online: bool#
If True, the model can be trained online (with
partial_fit()).
- named_nodes: dict[str, Node]#
Dictionary that associates a name to a Node with that name in the model.
- parents: dict[Node, list[Node]]#
Dictionary that associates a list of all Nodes connected to the key Node.
- partial_fit(
- x: array(t, d) | dict[str, array(t, d)],
- y: array(t, d) | dict[str, array(t, d)] | None = None,
Fit the Model in an online fashion.
This method both trains the Model parameters and produce predictions on the run. Calling
partial_fit()updates the Model without resetting the parameters, unlikefit().- Parameters:
x (array-like of shape (timesteps, input_dim)) – Input sequence of data.
y (array-like of shape (timesteps, output_dim), optional.) – Target sequence of data. If None, the Node will train in an unsupervised way, if possible.
- Returns:
All outputs computed during the training.
- Return type:
array of shape (timesteps, output_dim) or mapping of arrays
- predict(
- x: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | dict[str, array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)]] | None = None,
- iters: int | None = None,
- workers: int = 1,
Alias for
run().- Parameters:
x (array ([s,] t, d), list of arrays (t, d) or a mapping of them, optional) – A timeseries, multiple timeseries, or a mapping of timeseries or multiple timeseries. Input of the Model.
iters (int, optional) – If
xisNone, a dimensionless timeseries of lengthitersis used instead.workers (int, default to 1) – Number of workers used for parallelization. If set to -1, all available workers (threads or processes) are used.
- Returns:
A sequence of output vectors. If the model has multiple outputs, a mapping is returned instead.
- Return type:
array of shape ([n_inputs,] timesteps, output_dim) or list of arrays, or dict
- reset() tuple[dict[Node, dict[str, ndarray]], dict[tuple[Node, int, Node], ndarray[tuple[int, int], dtype[floating]]]][source]#
Reset all Node states and buffers in the Model.
- Returns:
dict[str, np.array], dict[Edge, array]
- Return type:
previous states of the Nodes and previous feedback buffer values.
- run(
- x: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | dict[str, array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)]] | None = None,
- iters: int | None = None,
- workers: int = 1,
Run the Model on a sequence of data.
- Parameters:
x (array ([s,] t, d), list of arrays (t, d) or a mapping of them, optional) – A timeseries, multiple timeseries, or a mapping of timeseries or multiple timeseries. Input of the Model.
iters (int, optional) – If
xisNone, a dimensionless timeseries of lengthitersis used instead.workers (int, default to 1) – Number of workers used for parallelization. If set to -1, all available workers (threads or processes) are used.
- Returns:
A sequence of output vectors. If the model has multiple outputs, a mapping is returned instead.
- Return type:
array of shape ([n_inputs,] timesteps, output_dim) or list of arrays, or dict
- step(
- x: array(d) | dict[str, array(d)] | None = None,
Call the Model function on a single step of data and update its state.
- Parameters:
x (array of shape (input_dim,), optional) – One single step of input data. If None, an empty array is used instead and the Node is assumed to have an input_dim of 0
- Returns:
An output vector. If the model has multiple outputs, a dictionary is returned instead.
- Return type:
array of shape (output_dim,) or dict of 1D arrays.