Input Layer

class hy2dl.modelzoo.inputlayer.InputLayer(cfg: Config, embedding_type: str = 'hindcast')

Bases: Module

Input layer to preprocess static and dynamic inputs.

This layer prepares the data before passing it to the main models. This can include running the dynamic and static attributes through embedding networks, preprocessing and assembling data at different temporal frequencies (e.g. daily, hourly), doing probabilistic masking and handling missing data.

In the simplest case, the layer takes the dictionary containing the sample information and assembles the tensor to be sent to the main model.

Parameters:
  • cfg (Config) – Configuration file.

  • embedding_type (str) – Type of embedding to use (hindcast or forecast).

static build_embedding(input_dim: int, embedding: dict[str, str | float | list[int]] | None)

Build embedding

Parameters:
  • input_dim (int) – Input dimension of the first layer.

  • embedding (dict[str, str | float | list[int]]) – Dictionary with the embedding characteristics

Returns:

Embedding network or nn.Identity

Return type:

nn.Sequential | nn.Identity

static build_ffnn(input_dim: int, spec: list[int], activation: str = 'relu', dropout: float = 0.0) Sequential

Builds a feedforward neural network based on the given specification.

Parameters:
  • input_dim (int) – Input dimension of the first layer.

  • spec (List[int]) – Dimension of the different hidden layers.

  • activation (str) – Activation function to use between layers (relu, linear, tanh, sigmoid). Default is ‘relu’.

  • dropout (float) – Dropout rate to apply after each layer (except the last one). Default is 0.0 (no dropout).

Returns:

A sequential model containing the feedforward neural network layers.

Return type:

nn.Sequential

forward(sample: dict[str, Any], assemble: bool = True) Tensor | dict[str, Tensor]

Forward pass of embedding networks.

Parameters:
  • sample (dict[str, Any]) – Dictionary with the different variables that will be used in the forward pass, see hy2dl.datasetzoo.basedataset.Basedataset.__getitems__() for details.

  • assemble (bool) – Whether to assemble the different tensors into a single tensor or return a dictionary with the different

Returns:

Either the processed tensor or a dictionary with the different tensors that then have the be assembled manually

Return type:

torch.Tensor | dict[str, torch.Tensor]