tempor.models.clairvoyance2.components.torch.rnn module

class tempor.models.clairvoyance2.components.torch.rnn.RecurrentNet(rnn_type: str, input_size: int, hidden_size: int, nonlinearity: str | None, num_layers: int, bias: bool, dropout: float, bidirectional: bool, proj_size: int | None)[source]

Bases: Module

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x: Tensor, h: Tensor | tuple[Tensor, Tensor] | None) tuple[Tensor, Tensor | tuple[Tensor, Tensor]][source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_output_and_h_dim() tuple[int, tuple[int, int, int]][source]

A convenience method that computes the size of the output of forward() for each time-step. Useful for defining the input size of a downstream module to be applied at each timestep.

For logic behind this calculation see: * https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html * https://pytorch.org/docs/stable/generated/torch.nn.GRU.html * https://pytorch.org/docs/stable/generated/torch.nn.RNN.html

Note

This class always has batch_first=True, so timesteps are in the second dimension of the output.

Returns:

(output feature dimension, (D * num_layers, H_out, H_cell)

Return type:

Tuple[int, Tuple[int, int, int]]

training : bool
class tempor.models.clairvoyance2.components.torch.rnn.AutoregressiveMixin(feed_first_n: int | None = None)[source]

Bases: ABC

autoregress(x: Tensor, **kwargs) Tensor[source]
class tempor.models.clairvoyance2.components.torch.rnn.RecurrentFFNet(rnn_type: str, input_size: int, hidden_size: int, nonlinearity: str | None, num_layers: int, bias: bool, dropout: float, bidirectional: bool, proj_size: int | None, ff_out_size: int, ff_in_size_adjust: int = 0, ff_hidden_dims: Sequence[int] = (), ff_out_activation: str | None = 'ReLU', ff_hidden_activations: str | None = 'ReLU')[source]

Bases: AutoregressiveMixin, Module

rnn_out_postprocess(rnn_out: Tensor, **kwargs) Tensor[source]
forward(x: Tensor, h: Tensor | tuple[Tensor, Tensor] | None, padding_indicator: float | None = None, **kwargs_rnn_out_callback) tuple[Tensor, Tensor, Tensor | tuple[Tensor, Tensor]][source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training : bool
class tempor.models.clairvoyance2.components.torch.rnn.PackedContainer(packed: torch.nn.utils.rnn.PackedSequence, unpacked: torch.Tensor | NoneType = None, unpacked_lens: torch.Tensor | NoneType = None)[source]

Bases: object

packed : PackedSequence
unpacked : Tensor | None = None
unpacked_lens : Tensor | None = None
tempor.models.clairvoyance2.components.torch.rnn.packed(x: Tensor, padding_indicator: float, batch_first: bool = True, enforce_sorted: bool = False)[source]
tempor.models.clairvoyance2.components.torch.rnn.apply_to_each_timestep(module: Module, input_tensor: Tensor, output_size: int, expected_module_input_size: int, padding_indicator: float | None, concat_tensors: Iterable[Tensor] = ()) Tensor[source]

Applies module forward to each timestep of input_tensor. Timestep dimension is expected to be dimension 1.

Parameters:
module: Module

Module to apply at each timestep.

input_tensor: Tensor

Tensor to apply module to. Shape: [n_samples, n_timesteps, n_features].

output_size: int

The size of the feature (last) dimension of the module output.

expected_module_input_size: int

Will check that module input dimension is this value.

padding_indicator: float | None

If None, assume no padding in input_tensor timestep dimension. If a float value (or nan), those tensor elements are treated as padding and not passed through module.

concat_tensors: Iterable[Tensor] = ()

Optionally provide a sequence of tensors to concatenate to input at each timestep, before passing to module. Defaults to tuple().

Raises:

RuntimeError – If expected_module_input_size input size check fails.

Returns:

Output tensor.

Return type:

torch.Tensor

tempor.models.clairvoyance2.components.torch.rnn.mask_and_reshape(mask_selector: BoolTensor, tensor: Tensor) Tensor[source]