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:
ModuleInitializes 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
Moduleinstance 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.
-
class tempor.models.clairvoyance2.components.torch.rnn.AutoregressiveMixin(feed_first_n: int | None =
None)[source]¶ Bases:
ABC
-
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-
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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
forward(x: Tensor, h: Tensor | tuple[Tensor, Tensor] | None, padding_indicator: float | None =
-
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¶
-
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
moduleforward to each timestep ofinput_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
moduleoutput.- expected_module_input_size: int¶
Will check that module input dimension is this value.
- padding_indicator: float | None¶
If
None, assume no padding ininput_tensortimestep dimension. If a float value (ornan), those tensor elements are treated as padding and not passed throughmodule.- concat_tensors: Iterable[Tensor] =
()¶ Optionally provide a sequence of tensors to concatenate to input at each timestep, before passing to
module. Defaults totuple().
- Raises:¶
RuntimeError – If
expected_module_input_sizeinput size check fails.- Returns:¶
Output tensor.
- Return type:¶
torch.Tensor