nn.many_to_many_rnn

nn.many_to_many_rnn

Many-to-many recurrent network for variable-length sequence regression.

Ported from spotpython.light.regression.ManyToManyRNNRegressor without the Lightning wrapper: the architecture (packed bidirectional nn.RNN followed by dropout, a fully connected layer, an activation, and a linear output head) and its hyperparameter names are identical, so tuning results remain comparable. Training is driven by an objective such as spotoptim.function.sequence_cv_objective.SequenceCVObjective instead of a Lightning trainer.

Requires the torch optional extra (pip install 'spotoptim[torch]').

Classes

Name Description
ManyToManyRNN Recurrent network mapping a padded sequence batch to per-step outputs.
ManyToManyRNNRegressor ManyToManyRNN with the spotPython hyperparameter interface.

ManyToManyRNN

nn.many_to_many_rnn.ManyToManyRNN(
    input_size,
    output_size=1,
    rnn_units=256,
    fc_units=256,
    activation_fct=None,
    dropout=0.0,
    bidirectional=True,
)

Recurrent network mapping a padded sequence batch to per-step outputs.

The input batch is packed with the true sequence lengths, passed through a single (optionally bidirectional) nn.RNN layer, unpacked, and fed through dropout, a fully connected layer, an activation, and a linear output head. Layer names and forward semantics match spotPython’s ManyToManyRNN.

Parameters

Name Type Description Default
input_size int Number of input features per time step. required
output_size int Number of outputs per time step. Defaults to 1. 1
rnn_units int Hidden size of the RNN layer. Defaults to 256. 256
fc_units int Width of the fully connected layer. Defaults to 256. 256
activation_fct Optional[nn.Module] Activation between the fully connected layer and the output head. Defaults to nn.ReLU(). None
dropout float Dropout probability applied to the RNN output. Defaults to 0.0. 0.0
bidirectional bool Whether the RNN is bidirectional. Defaults to True. True

Examples

import torch
from spotoptim.nn.many_to_many_rnn import ManyToManyRNN

torch.manual_seed(0)
model = ManyToManyRNN(input_size=1, rnn_units=8, fc_units=8)
x = torch.zeros(2, 5, 1)
lengths = torch.tensor([5, 3])
print(model(x, lengths).shape)
torch.Size([2, 5, 1])

Methods

Name Description
forward Compute per-step outputs for a padded batch.
forward
nn.many_to_many_rnn.ManyToManyRNN.forward(x, lengths)

Compute per-step outputs for a padded batch.

Parameters
Name Type Description Default
x torch.Tensor Padded input batch, shape (B, T_max, input_size). required
lengths torch.Tensor True sequence lengths, shape (B,). required
Returns
Name Type Description
torch.Tensor torch.Tensor: Padded outputs, shape (B, T_max, output_size).

ManyToManyRNNRegressor

nn.many_to_many_rnn.ManyToManyRNNRegressor(
    input_dim=1,
    output_dim=1,
    rnn_units=256,
    fc_units=256,
    act_fn='ReLU',
    dropout_prob=0.0,
    bidirectional=True,
    **kwargs,
)

ManyToManyRNN with the spotPython hyperparameter interface.

Accepts the hyperparameter names of the spotPython light_hyper_dict entry ManyToManyRNNRegressor (rnn_units, fc_units, act_fn, dropout_prob, bidirectional) plus the input_dim/output_dim convention used by spotoptim objectives. Training hyperparameters such as epochs, batch_size, patience, optimizer, and lr_mult are accepted via **kwargs and ignored here — they are consumed by the training objective.

Parameters

Name Type Description Default
input_dim int Number of input features per time step. Defaults to 1. 1
output_dim int Number of outputs per time step. Defaults to 1. 1
rnn_units int Hidden size of the RNN layer. Defaults to 256. 256
fc_units int Width of the fully connected layer. Defaults to 256. 256
act_fn Union[str, nn.Module] Activation between the fully connected layer and the output head, either a name accepted by get_activation or a module instance. Defaults to “ReLU”. 'ReLU'
dropout_prob float Dropout probability applied to the RNN output. Defaults to 0.0. 0.0
bidirectional bool Whether the RNN is bidirectional. Defaults to True. True
**kwargs Ignored. Accepts surplus tuning hyperparameters. {}

Attributes

Name Type Description
layers ManyToManyRNN The underlying recurrent network.

Examples

import torch
from spotoptim.nn.many_to_many_rnn import ManyToManyRNNRegressor

torch.manual_seed(0)
model = ManyToManyRNNRegressor(
    input_dim=1, output_dim=1, rnn_units=16, fc_units=16,
    act_fn="Tanh", dropout_prob=0.1, epochs=128, batch_size=2,
)
x = torch.zeros(3, 4, 1)
lengths = torch.tensor([4, 2, 3])
print(model(x, lengths).shape)
torch.Size([3, 4, 1])

Methods

Name Description
forward Compute per-step outputs for a padded batch.
forward
nn.many_to_many_rnn.ManyToManyRNNRegressor.forward(x, lengths)

Compute per-step outputs for a padded batch.

Parameters
Name Type Description Default
x torch.Tensor Padded input batch, shape (B, T_max, input_dim). required
lengths torch.Tensor True sequence lengths, shape (B,). required
Returns
Name Type Description
torch.Tensor torch.Tensor: Padded outputs, shape (B, T_max, output_dim).

Functions

Name Description
get_activation Return the activation module for a spotPython act_fn level name.

get_activation

nn.many_to_many_rnn.get_activation(name)

Return the activation module for a spotPython act_fn level name.

Parameters

Name Type Description Default
name str Activation name. Options: - “Sigmoid” - “Tanh” - “ReLU” - “LeakyReLU”: negative slope 0.1, matching spotPython. - “ELU” - “Swish”: implemented as nn.SiLU. required

Returns

Name Type Description
nn.Module nn.Module: A fresh activation module instance.

Raises

Name Type Description
ValueError If name is not a supported activation name.

Examples

from spotoptim.nn.many_to_many_rnn import get_activation

act = get_activation("LeakyReLU")
print(type(act).__name__, act.negative_slope)
LeakyReLU 0.1