reservoirpy.nodes.Ridge#

class reservoirpy.nodes.Ridge(
ridge: float = 0.0,
fit_bias: bool = True,
Wout: ndarray | sparray | Callable | None = None,
bias: ndarray | sparray | Callable | None = None,
input_dim: int | None = None,
output_dim: int | None = None,
name: str | None = None,
)[source]#

A single layer of neurons learning with Tikhonov linear regression.

Output weights of the layer are computed following:

\[\hat{\mathbf{W}}_{out} = \mathbf{YX}^\top ~ (\mathbf{XX}^\top + \lambda\mathbf{Id})^{-1}\]

Outputs \(\mathbf{y}\) of the node are the result of:

\[\mathbf{y} = \mathbf{W}_{out}^\top \mathbf{x} + \mathbf{b}\]
where:
  • \(\mathbf{X}\) is the accumulation of all inputs during training;

  • \(\mathbf{Y}\) is the accumulation of all targets during training;

  • \(\mathbf{b}\) is the first row of \(\hat{\mathbf{W}}_{out}\);

  • \(\mathbf{W}_{out}\) is the rest of \(\hat{\mathbf{W}}_{out}\).

If fit_bias is True, then \(\mathbf{b}\) is non-zero, and a constant term is added to \(\mathbf{X}\) to compute it.

Parameters:
  • ridge (float, default to 0.0) – L2 regularization parameter.

  • fit_bias (bool, default to True) – If True, then a bias parameter will be learned along with output weights.

  • Wout (callable or array-like of shape (input_dim, units), optional) – Output weights matrix or initializer. If a callable (like a function) is used, then this function should accept any keywords parameters and at least two parameters that will be used to define the shape of the returned weight matrix.

  • bias (callable or array-like of shape (units,), optional) – Bias weights vector or initializer. If a callable (like a function) is used, then this function should accept any keywords parameters and at least two parameters that will be used to define the shape of the returned weight matrix.

  • input_dim (int, optional) – Input dimension. Can be inferred at first call.

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

  • name (str, optional) – Node name.

Example

>>> x = np.random.normal(size=(100, 3))
>>> noise = np.random.normal(scale=0.1, size=(100, 1))
>>> y = x @ np.array([[10], [-0.2], [7.]]) + noise + 12.
>>>
>>> from reservoirpy.nodes import Ridge
>>> ridge_regressor = Ridge(ridge=0.001)
>>>
>>> ridge_regressor.fit(x, y)
>>> ridge_regressor.Wout, ridge_regressor.bias
array([[ 9.992, -0.205,  6.989]]).T, array([[12.011]])

Methods

__init__([ridge, fit_bias, Wout, bias, ...])

fit(x[, y, warmup, workers])

Offline fitting method of a Node.

initialize(x[, y])

Define input and output dimensions, and instantiate variables.

master(generator)

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.

worker(x, y)

Attributes

input_dim

Expected dimension of the Node input.

name

Optional name of the Node.

output_dim

Expected dimension of the Node input.

ridge

Regularization parameter (\(\\lambda\)) (0.0 by default).

fit_bias

If True, learn a bias term (True by default).

Wout

Learned output weights (\(\\mathbf{W}_{out}\)).

bias

Learned bias (\(\\mathbf{b}\)).

initialized

True if the Node has been initialized

state

Current state of the Node.

Wout: ndarray | sparray#

Learned output weights (\(\\mathbf{W}_{out}\)).

bias: ndarray | sparray#

Learned bias (\(\\mathbf{b}\)).

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,
workers: int = 1,
) ParallelNode[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.

  • workers (int) –

Returns:

Node trained offline.

Return type:

Node

fit_bias: bool#

If True, learn a bias term (True by default).

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#

True if the Node has been initialized

input_dim: int = None#

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

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.

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.

ridge: float#

Regularization parameter (\(\\lambda\)) (0.0 by default).

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: dict[str, ndarray]#

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,)