reservoirpy.nodes.ScikitLearnNode#

class reservoirpy.nodes.ScikitLearnNode(
model,
output_dim: int | None = None,
name: str | None = None,
**kwargs,
)[source]#

A node interfacing a scikit-learn linear model that can be used as an offline readout node.

The ScikitLearnNode takes a scikit-learn model as parameter and creates a node with the specified model.

We currently support classifiers (like sklearn.linear_model.LogisticRegression or sklearn.linear_model.RidgeClassifier) and regressors (like sklearn.linear_model.Lasso or sklearn.linear_model.ElasticNet).

For more information on the above-mentioned estimators, please visit scikit-learn linear model API reference

Parameters:
  • model (class, scikit-learn model) – scikit-learn class to be wrapped by the Node.

  • output_dim (int, optional) – Number of units in the readout, can be inferred at first call.

  • name (str, optional) – Node name.

  • **kwargs – Additional keyword arguments passed to the scikit-learn model.

Example

>>> from reservoirpy.nodes import Reservoir, ScikitLearnNode
>>> from sklearn.linear_model import Lasso
>>> reservoir = Reservoir(units=100)
>>> readout = ScikitLearnNode(model=Lasso, model_hypers={"alpha":1e-5})
>>> model = reservoir >> readout

Methods

__init__(model[, output_dim, name])

fit(x[, y, warmup])

Offline fitting method of a Node.

initialize(x[, y])

Define input and output dimensions, and instantiate variables.

predict([x, iters, workers])

Alias for run()

reset()

Reset all Node state

run([x, iters, workers])

Run the Node on a sequence of data.

step([x])

Call the Node function on a single step of data and update the state of the Node.

Attributes

initialized

True if the Node has been initialized

input_dim

Expected dimension of the Node input.

name

Optional name of the Node.

output_dim

Expected dimension of the Node input.

model

scikit-learn class to be wrapped by the Node.

model_kwargs

Additional keyword arguments passed to the scikit-learn model.

instances

Model instance or list of model instances if multiple output are expected and the scikit-learn model doesn't support it.

state

Current state of the Node.

fit(
x: 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)] | None = None,
warmup: int = 0,
)[source]#

Offline fitting method of a Node.

Parameters:
  • x (list or array-like of shape ([series, ] timesteps, input_dim), optional) – Input sequences dataset.

  • y (list or array-like of shape ([series], timesteps, output_dim), 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.

Returns:

Node trained offline.

Return type:

Node

initialize(
x: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | array(d),
y: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | array(d) | None = None,
)[source]#

Define input and output dimensions, and instantiate variables.

Only called once, before fitting or running the node.

Parameters:
  • x (array of shape (input_dim,) or (timestep, input_dim)) – Input data to the node.

  • y (None) – Training data to the node. As it is not a trainable node, y is expected to be None.

initialized: bool = False#

True if the Node has been initialized

input_dim: int = None#

Expected dimension of the Node input. Can be None before initialization

instances: sklearn.base.BaseEstimator | list[sklearn.base.BaseEstimator]#

Model instance or list of model instances if multiple output are expected and the scikit-learn model doesn’t support it.

model: type[sklearn.base.BaseEstimator]#

scikit-learn class to be wrapped by the Node.

model_kwargs: dict[str, Any]#

Additional keyword arguments passed to the scikit-learn model.

name: str | None = None#

Optional name of the Node.

output_dim: int = None#

Expected dimension of the Node input. Can be None before initialization

predict(
x: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | None = None,
iters: int | None = None,
workers=1,
) array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)][source]#

Alias for run()

Run the Node on a sequence of data. Can update the state of the Node several times.

Parameters:
  • x (array-like of shape ([n_inputs,] timesteps, input_dim) or list of) – arrays of shape (timesteps, input_dim), optional A sequence of data of shape (timesteps, features).

  • iters (int, optional) – If x is None, a dimensionless timeseries of length iters is 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.

Return type:

array of shape ([n_inputs,] timesteps, output_dim) or list of arrays

reset() dict[str, ndarray][source]#

Reset all Node state

Returns:

dict[str, np.array]

Return type:

previous state of the Node.

run(
x: array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)] | None = None,
iters: int | None = None,
workers=1,
) array(t, d) | array(s, t, d) | ~typing.Sequence[array(t, d)][source]#

Run the Node on a sequence of data. Can update the state of the Node several times.

Parameters:
  • x (array-like of shape ([n_inputs,] timesteps, input_dim) or list of arrays of shape (timesteps, input_dim), optional) – A timeseries, array of shape (timesteps, features), or a sequence of timeseries. Input of the Node.

  • iters (int, optional) – If x is None, a dimensionless timeseries of length iters is 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.

Return type:

array of shape ([n_inputs,] timesteps, output_dim) or list of arrays

state: State#

Current state of the Node. Must have “out” as one of the keys.

step(x: array(d) | None = None)[source]#

Call the Node function on a single step of data and update the state of the Node.

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.

Return type:

array of shape (output_dim,)