nn.many_to_many_rnn.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).