Echo State Network reservoirpy.ESN#
- class reservoirpy.ESN(
- reservoir: Reservoir | None = None,
- readout: Ridge | None = None,
- feedback: bool = False,
- input_to_readout: bool = False,
- return_reservoir_activity: bool = False,
- **kwargs,
Simple Echo State Network.
This class is provided as a wrapper for a simple reservoir connected to a readout.
- Parameters:
units (int, optional) – Number of reservoir units. If None, the number of units will be inferred from the
Wmatrix shape.reservoir (Node, optional) – A Node instance to use as a reservoir, such as a
Reservoirnode.readout (Node, optional) – A Node instance to use as a readout, such as a
Ridgenode (only this one is supported).feedback (bool, defaults to False) – If True, the readout is connected to the reservoir through a feedback connection.
input_to_readout (bool, defaults to False) – If True, the input is directly fed to the readout. See Input-to-readout connections.
return_reservoir_activity (bool, defaults to False) – If True, the model outputs a dict with the reservoir activity in addition to the readout output.
**kwargs – Arguments passed to the reservoir and readout.
See also
Reservoir,RidgeExample
>>> from reservoirpy import ESN >>> >>> model = ESN(units=100, sr=0.9, ridge=1e-6) # reservoir and readout parameters at once >>> model.fit(x_train, y_train) >>> >>> model = ESN(reservoir=Reservoir(100, sr=0.9), readout=Ridge(1e-5)) # passing nodes as parameters >>> >>> model = ESN(units=100, input_to_readout=True, feedback=True) # more complex model
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
A
Ridgeinstance.Is readout connected to reservoir through feedback (False by default).
Is the readout directly receiving the input (False by default).
Input node, if
input_to_readoutis set to TrueList 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: FeedbackBuffers#
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.
- output_node: Output | None = None#
Output node for the reservoir, if
return_reservoir_activityis set to True
- 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
- readout: TrainableNode#
A
Ridgeinstance.
- 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.
- return_reservoir_activity: bool#
Are the reservoir states returned by the model along the readout output (False by default).
- 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.