Classifier API Reference¶
The orc.classifier subpackage provides reservoir computing models for time series classification.
Base Classes¶
RCClassifierBase ¶
RCClassifierBase(driver: DriverBase, readout: ReadoutBase, embedding: EmbedBase, n_classes: int, state_repr: str = 'final', dtype: Float = float64, seed: int = 0)
Bases: Module, ABC
Base class for reservoir computer classifiers.
Defines the interface for the reservoir computer classifier which includes the driver, readout and embedding layers. The classifier forces input sequences through the reservoir, extracts a feature vector from the reservoir states, and applies a trained readout to produce class probabilities.
Attributes:
| Name | Type | Description |
|---|---|---|
driver |
DriverBase
|
Driver layer of the reservoir computer. |
readout |
ReadoutBase
|
Readout layer of the reservoir computer. Output dimension equals n_classes. |
embedding |
EmbedBase
|
Embedding layer of the reservoir computer. |
in_dim |
int
|
Dimension of the input data. |
out_dim |
int
|
Dimension of the output data (equals n_classes). |
res_dim |
int
|
Dimension of the reservoir. |
n_classes |
int
|
Number of classification classes. |
state_repr |
str
|
Reservoir state representation for classification. "final" uses the last reservoir state, "mean" averages states after spinup. |
dtype |
type
|
Data type of the reservoir computer (jnp.float64 is highly recommended). |
seed |
int
|
Random seed for generating the PRNG key for the reservoir computer. |
Methods:
| Name | Description |
|---|---|
force |
Teacher forces the reservoir with the input sequence. |
classify |
Classify an input sequence, returning class probabilities. |
set_readout |
Replaces the readout layer of the reservoir computer. |
set_embedding |
Replaces the embedding layer of the reservoir computer. |
Initialize RCClassifier Base.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
driver
|
DriverBase
|
Driver layer of the reservoir computer. |
required |
readout
|
ReadoutBase
|
Readout layer of the reservoir computer. |
required |
embedding
|
EmbedBase
|
Embedding layer of the reservoir computer. |
required |
n_classes
|
int
|
Number of classification classes. |
required |
state_repr
|
str
|
Reservoir state representation for classification. "final" uses the last reservoir state, "mean" averages states after spinup. |
'final'
|
dtype
|
type
|
Data type of the reservoir computer (jnp.float64 is highly recommended). |
float64
|
seed
|
int
|
Random seed for generating the PRNG key for the reservoir computer. |
0
|
Source code in src/orc/classifier/base.py
force ¶
Teacher forces the reservoir with the input sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_seq
|
Array
|
Input sequence to force the reservoir, (shape=(seq_len, in_dim)). |
required |
res_state
|
Array
|
Initial reservoir state, (shape=(res_dim,)). |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Forced reservoir sequence, (shape=(seq_len, res_dim)). |
Source code in src/orc/classifier/base.py
classify ¶
Classify an input sequence.
Forces the reservoir with the input sequence, extracts a feature vector from the reservoir states, and returns softmax class probabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_seq
|
Array
|
Input sequence to classify, (shape=(seq_len, in_dim)). |
required |
res_state
|
Array
|
Initial reservoir state, (shape=(res_dim,)). |
None
|
spinup
|
int
|
Number of initial reservoir states to discard before extracting features. Only used when state_repr="mean". |
0
|
Returns:
| Type | Description |
|---|---|
Array
|
Class probabilities, (shape=(n_classes,)). |
Source code in src/orc/classifier/base.py
__call__ ¶
Classify an input sequence, wrapper for classify method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_seq
|
Array
|
Input sequence to classify, (shape=(seq_len, in_dim)). |
required |
res_state
|
Array
|
Initial reservoir state, (shape=(res_dim,)). |
required |
spinup
|
int
|
Number of initial reservoir states to discard before extracting features. Only used when state_repr="mean". |
0
|
Returns:
| Type | Description |
|---|---|
Array
|
Class probabilities, (shape=(n_classes,)). |
Source code in src/orc/classifier/base.py
set_readout ¶
set_readout(readout: ReadoutBase) -> RCClassifierBase
Replace readout layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
readout
|
ReadoutBase
|
New readout layer. |
required |
Returns:
| Type | Description |
|---|---|
RCClassifierBase
|
Updated model with new readout layer. |
Source code in src/orc/classifier/base.py
set_embedding ¶
set_embedding(embedding: EmbedBase) -> RCClassifierBase
Replace embedding layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding
|
EmbedBase
|
New embedding layer. |
required |
Returns:
| Type | Description |
|---|---|
RCClassifierBase
|
Updated model with new embedding layer. |
Source code in src/orc/classifier/base.py
Models¶
ESNClassifier ¶
ESNClassifier(data_dim: int, n_classes: int, res_dim: int, leak_rate: float = 0.6, bias: float = 1.6, embedding_scaling: float = 0.08, Wr_density: float = 0.02, Wr_spectral_radius: float = 0.8, dtype: type = float64, seed: int = 0, quadratic: bool = False, use_sparse_eigs: bool = True, state_repr: str = 'final')
Bases: RCClassifierBase
Basic implementation of ESN for classification tasks.
Attributes:
| Name | Type | Description |
|---|---|---|
res_dim |
int
|
Reservoir dimension. |
data_dim |
int
|
Input data dimension. |
n_classes |
int
|
Number of classification classes. |
driver |
ESNDriver
|
Driver implementing the Echo State Network dynamics. |
readout |
ReadoutBase
|
Trainable linear readout layer with out_dim=n_classes. |
embedding |
LinearEmbedding
|
Untrainable linear embedding layer. |
Methods:
| Name | Description |
|---|---|
force |
Teacher forces the reservoir with the input sequence. |
classify |
Classify an input sequence, returning class probabilities. |
set_readout |
Replace readout layer. |
set_embedding |
Replace embedding layer. |
Initialize the ESN classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dim
|
int
|
Dimension of the input data. |
required |
n_classes
|
int
|
Number of classification classes. |
required |
res_dim
|
int
|
Dimension of the reservoir adjacency matrix Wr. |
required |
leak_rate
|
float
|
Integration leak rate of the reservoir dynamics. |
0.6
|
bias
|
float
|
Bias term for the reservoir dynamics. |
1.6
|
embedding_scaling
|
float
|
Scaling factor for the embedding layer. |
0.08
|
Wr_density
|
float
|
Density of the reservoir adjacency matrix Wr. |
0.02
|
Wr_spectral_radius
|
float
|
Largest eigenvalue of the reservoir adjacency matrix Wr. |
0.8
|
dtype
|
type
|
Data type of the model (jnp.float64 is highly recommended). |
float64
|
seed
|
int
|
Random seed for generating the PRNG key for the reservoir computer. |
0
|
quadratic
|
bool
|
Use quadratic nonlinearity in output, default False. |
False
|
use_sparse_eigs
|
bool
|
Whether to use sparse eigensolver for setting the spectral radius of wr. Default is True, which is recommended to save memory and compute time. If False, will use dense eigensolver which may be more accurate. |
True
|
state_repr
|
str
|
Reservoir state representation for classification. "final" uses the last reservoir state, "mean" averages states after spinup. |
'final'
|
Source code in src/orc/classifier/models.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
Training Functions¶
train_ESNClassifier ¶
train_ESNClassifier(model: ESNClassifier, train_seqs: Array, labels: Array, spinup: int = 0, beta: float = 8e-08) -> ESNClassifier
Training function for ESNClassifier.
Trains the classifier by forcing each input sequence through the reservoir, extracting a feature vector from the reservoir states, and solving ridge regression against one-hot encoded class labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
ESNClassifier
|
ESNClassifier model to train. |
required |
train_seqs
|
Array
|
Batch of training input sequences, (shape=(n_samples, seq_len, data_dim)). |
required |
labels
|
Array
|
Integer class labels for each sequence, (shape=(n_samples,)). Values should be in [0, n_classes). |
required |
spinup
|
int
|
Number of initial reservoir states to discard before extracting features. Only used when model.state_repr="mean". |
0
|
beta
|
float
|
Tikhonov regularization parameter. |
8e-08
|
Returns:
| Name | Type | Description |
|---|---|---|
model |
ESNClassifier
|
Trained ESN classifier model. |