PyTorch Training¶
-
flood_forecast.pytorch_training.
handle_meta_data
(model: flood_forecast.time_model.PyTorchForecast)[source]¶ A function to init models with meta-data s :param model: A PyTorchForecast model with meta_data parameter block in config file. :type model: PyTorchForecast :return: Returns a tuple of the initial meta-representationf :rtype: tuple(PyTorchForecast, torch.Tensor, torch.nn)
-
flood_forecast.pytorch_training.
train_transformer_style
(model: flood_forecast.time_model.PyTorchForecast, training_params: Dict, takes_target=False, forward_params: Dict = {}, model_filepath: str = 'model_save') → None[source]¶ Function to train any PyTorchForecast model
- Parameters
model (PyTorchForecast) – A properly wrapped PyTorchForecast model
training_params (Dict) – A dictionary of the necessary parameters for training.
takes_target (bool, optional) – A parameter to determine whether a model requires the target, defaults to False
forward_params (Dict, optional) – [description], defaults to {}
model_filepath (str, optional) – The file path to load modeel weights from, defaults to “model_save”
- Raises
ValueError – Has an error
-
flood_forecast.pytorch_training.
get_meta_representation
(column_id: str, uuid: str, meta_model: flood_forecast.time_model.PyTorchForecast) → torch.Tensor[source]¶
-
flood_forecast.pytorch_training.
handle_scaling
(validation_dataset, src, output: torch.Tensor, labels, probabilistic, m, output_std)[source]¶
-
flood_forecast.pytorch_training.
compute_loss
(labels, output, src, criterion, validation_dataset, probabilistic=None, output_std=None, m=1)[source]¶ Function for computing the loss
- Parameters
labels (torch.Tensor) – The real values for the target. Shape can be variable but should follow (batch_size, time)
output (torch.Tensor) – The output of the model
src (torch.Tensor) – The source values (only really needed for the MASELoss function)
criterion ([type]) – [description]
validation_dataset (torch.utils.data.dataset) – Only passed when unscaling of data is needed.
probabilistic ([type], optional) – Whether the model is a probabalistic returns a distribution, defaults to None
output_std ([type], optional) – The standard distribution, defaults to None
m (int, optional) – The number of targets defaults to 1
- Returns
Returns the computed loss
- Return type
float
-
flood_forecast.pytorch_training.
torch_single_train
(model: flood_forecast.time_model.PyTorchForecast, opt: torch.optim.optimizer.Optimizer, criterion: Type[torch.nn.modules.loss._Loss], data_loader: torch.utils.data.dataloader.DataLoader, takes_target: bool, meta_data_model: flood_forecast.time_model.PyTorchForecast, meta_data_model_representation: torch.Tensor, meta_loss=None, multi_targets=1, forward_params: Dict = {}) → float[source]¶
-
flood_forecast.pytorch_training.
multi_step_forecasts_append
(self)[source]¶ Function to handle forecasts that span multiple time steps
-
flood_forecast.pytorch_training.
compute_validation
(validation_loader: torch.utils.data.dataloader.DataLoader, model, epoch: int, sequence_size: int, criterion: Type[torch.nn.modules.loss._Loss], device: torch.device, decoder_structure=False, meta_data_model=None, use_wandb: bool = False, meta_model=None, multi_targets=1, val_or_test='validation_loss', probabilistic=False) → float[source]¶ Function to compute the validation loss metrics
- Parameters
validation_loader (DataLoader) – The data-loader of either validation or test-data
model ([type]) – model
epoch (int) – The epoch where the validation/test loss is being computed.
sequence_size (int) – [description]
criterion (Type[torch.nn.modules.loss._Loss]) – [description]
device (torch.device) – The device
decoder_structure (bool, optional) – Whether the model should use sequential decoding, defaults to False
meta_data_model (PyTorchForecast, optional) – The model to handle the meta-data, defaults to None
use_wandb (bool, optional) – Whether Weights and Biases is in use, defaults to False
meta_model (bool, optional) – Whether the model leverages meta-data, defaults to None
multi_targets (int, optional) – Whether the model, defaults to 1
val_or_test (str, optional) – Whether validation or test loss is computed, defaults to “validation_loss”
probabilistic (bool, optional) – Whether the model is probablistic, defaults to False
- Returns
The loss of the first metric in the list.
- Return type
float