HORNSeqNetwork#
- class brainmass.HORNSeqNetwork(n_input, n_hidden, n_output, alpha=0.04, omega=0.2243994752564138, gamma=0.01, v=0.0, x_init=ZeroInit(unit=1), y_init=ZeroInit(unit=1), delay_init=ZeroInit(unit=1), delay=None, rec_w_init=KaimingNormal(mode=fan_in, nonlinearity=relu, unit=1), rec_b_init=ZeroInit(unit=1), inp_w_init=KaimingNormal(mode=fan_in, nonlinearity=relu, unit=1), inp_b_init=ZeroInit(unit=1))[source]#
Multi-layer HORN network for sequential processing tasks.
This network stacks multiple
HORNSeqLayerinstances to create a deep recurrent architecture based on harmonic oscillator dynamics. It is designed for sequence-to-sequence or sequence-to-label tasks such as time series classification, sequence generation, or temporal pattern recognition.The network processes input sequences through L layers of HORN dynamics:
\[\begin{split}\begin{aligned} \mathbf{h}^{(0)}_t &= \mathbf{s}_t, \\ \mathbf{h}^{(\ell)}_t &= \text{HORNSeqLayer}^{(\ell)}(\mathbf{h}^{(\ell-1)}), \quad \ell = 1, \ldots, L, \\ \mathbf{o}_T &= \mathbf{W}^{ho} \mathbf{x}^{(L)}_T + \mathbf{b}^{ho}, \end{aligned}\end{split}\]where \(\mathbf{s}\) is the input sequence, \(\mathbf{h}^{(\ell)}\) denotes the hidden state sequence at layer \(\ell\), \(\mathbf{x}^{(L)}_T\) is the final position state from the last layer at the final time step, and \(\mathbf{o}_T\) is the output.
- Parameters:
n_input (
int) – Dimensionality of input features at each time step.n_hidden (
int|Sequence[int]) – Hidden state dimensionality for each layer. If an integer, creates a single hidden layer with that size. If a sequence (list or tuple), creates multiple layers where each element specifies the hidden size for that layer.n_output (
int) – Dimensionality of the final output.alpha (
Callable|Array|ndarray|bool|number|bool|int|float|complex|Quantity|Param) – Excitability factor for HORN dynamics (dimensionless), shared across all layers. Default is0.04.omega (
Callable|Array|ndarray|bool|number|bool|int|float|complex|Quantity|Param) – Natural frequency for HORN dynamics (radians per time step), shared across all layers. Default is2π/28 ≈ 0.224.gamma (
Callable|Array|ndarray|bool|number|bool|int|float|complex|Quantity|Param) – Damping coefficient for HORN dynamics (dimensionless), shared across all layers. Default is0.01.v (
Callable|Array|ndarray|bool|number|bool|int|float|complex|Quantity|Param) – Amplitude feedback coefficient (dimensionless), shared across all layers. Default is0.0.x_init (
Callable|Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Initializer for HORN position state \(\mathbf{x}\) in all layers. Default isbraintools.init.ZeroInit().y_init (
Callable|Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Initializer for HORN velocity state \(\mathbf{y}\) in all layers. Default isbraintools.init.ZeroInit().delay (
Array|ndarray|bool|number|bool|int|float|complex|Quantity|Callable|None) – Synaptic delay configuration for recurrent connections in all layers. IfNone, no delays are used. Default isNone.rec_w_init (
Callable|Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Initializer for recurrent weight matrices in all layers. Default isbraintools.init.KaimingNormal().rec_b_init (
Array|ndarray|bool|number|bool|int|float|complex|Quantity|Callable|None) – Initializer for recurrent bias vectors in all layers. Default isbraintools.init.ZeroInit().inp_w_init (
Callable|Array|ndarray|bool|number|bool|int|float|complex|Quantity) – Initializer for input weight matrices (layer-to-layer and final output). Default isbraintools.init.KaimingNormal().inp_b_init (
Array|ndarray|bool|number|bool|int|float|complex|Quantity|Callable|None) – Initializer for input bias vectors (layer-to-layer and final output). Default isbraintools.init.ZeroInit().
- Return type:
Any
- layers#
List of HORN sequential layers. The number of layers equals the length of
n_hiddenif it is a sequence, or 1 ifn_hiddenis an integer.- Type:
list of HORNSeqLayer
- h2o#
Hidden-to-output linear transformation mapping the final layer’s position state to the output space. Shape is
(n_hidden[-1], n_output).- Type:
brainstate.nn.Linear
Notes
Layer stacking: The output of layer \(\ell\) (the position state sequence \(\mathbf{x}^{(\ell)}\)) becomes the input to layer \(\ell+1\). Each layer maintains its own position and velocity states.
Final output: The
updatemethod returns the output of the hidden-to-output transformation applied to the position state \(\mathbf{x}^{(L)}_T\) from the last HORN layer at the final time step. This is suitable for sequence classification tasks.Prediction mode: The
predictmethod returns the position state sequences from all layers, which can be useful for analyzing the hierarchical representations learned by the network.Parameter sharing: All HORN dynamics parameters (
alpha,omega,gamma,v,h) are shared across all layers and all oscillators within each layer. This reduces the number of hyperparameters but can be relaxed by passing arrays or per-layer configurations.Flexible depth: The network depth can be easily configured by adjusting the
n_hiddenparameter. For example,n_hidden=[64, 128, 64]creates a 3-layer network with varying hidden sizes.
See also
HORNStepSingle-step HORN dynamics
HORNSeqLayerSingle-layer HORN sequential processor
Examples
Create a 2-layer HORN network for sequence classification:
>>> import brainstate >>> net = HORNSeqNetwork( ... n_input=10, ... n_hidden=[64, 128], ... n_output=5, ... alpha=0.04, ... omega=2*3.14159/28 ... ) >>> inputs = brainstate.random.randn(20, 10) # (time_steps, input_dim) >>> output = net(inputs) # (output_dim,)
- update(inputs)[source]#
Process input sequence through all layers and return final output.
Sequentially processes the input through each HORN layer, then applies a linear transformation to the final layer’s position state to produce the output. Suitable for sequence classification or regression tasks.
- Parameters:
inputs (array-like) – Input sequence with shape
(T, batch?, n_input)whereTis the number of time steps,batch?represents optional batch dimensions, andn_inputis the input feature dimension.- Returns:
Final output with shape
(n_output,)or(batch?, n_output). Computed by applying the hidden-to-output transformationh2oto the position state of the last layer.- Return type:
array-like
Notes
This method performs the following steps:
Process input through layer 1 to get hidden sequence 1
Process hidden sequence 1 through layer 2 to get hidden sequence 2
Continue through all remaining layers
Apply linear transformation to final layer’s position state
The output uses only the final position state
xfrom the last layer, not the full sequence.
Process input sequence and return outputs from all layers.
Similar to
update, but returns the position state sequences from all intermediate layers. Useful for analyzing hierarchical representations or multi-scale temporal features learned by the network.- Parameters:
inputs (array-like) – Input sequence with shape
(T, batch?, n_input)whereTis the number of time steps,batch?represents optional batch dimensions, andn_inputis the input feature dimension.- Returns:
List of position state sequences, one per layer. Each element has shape
(T, batch?, n_hidden[i])wheren_hidden[i]is the hidden size of layeri. The list length equals the number of layers.- Return type:
list of array-like
Notes
Unlike
update, this method does not apply theh2otransformation to produce a final output. Instead, it returns the raw position state sequences from each layer, which can be useful for:Visualizing layer-wise dynamics
Extracting multi-scale temporal features
Analyzing information flow through the network
Debugging layer activations
See also
updateStandard forward pass for task output