ise.models.density_estimators

ise.models.density_estimators.normalizing_flow

class ise.models.density_estimators.normalizing_flow.NormalizingFlow(input_size=43, output_size=1, output_sequence_length=86, num_flow_transforms=5)[source]

Bases: Module

A normalizing flow model for probabilistic modeling using invertible transformations.

This model utilizes a sequence of invertible transformations to model complex probability distributions. It is built with a base distribution and a series of transformations, leveraging autoregressive neural networks.

num_flow_transforms

Number of flow transformations in the model.

Type:

int

num_input_features

Number of input features.

Type:

int

num_predicted_sle

Number of predicted sea-level equivalent values.

Type:

int

flow_hidden_features

Number of hidden features in the flow model.

Type:

int

output_sequence_length

Length of the output sequence.

Type:

int

device

Device on which the model is run (“cuda” or “cpu”).

Type:

str

base_distribution

The base normal distribution conditioned on input features.

Type:

distributions.normal.ConditionalDiagonalNormal

t

Composite transformation for the normalizing flow.

Type:

transforms.base.CompositeTransform

flow

The normalizing flow model.

Type:

flows.base.Flow

optimizer

Optimizer for training the model.

Type:

torch.optim.Adam

criterion

Log probability function used as the loss criterion.

Type:

callable

trained

Flag indicating if the model has been trained.

Type:

bool

aleatoric(features, num_samples, batch_size=128)[source]

Estimates aleatoric uncertainty by computing the standard deviation of multiple samples drawn from the normalizing flow model.

Parameters:
  • features (array-like or torch.Tensor) - Input features for sampling.

  • num_samples (int) - Number of samples per input feature set.

  • batch_size (int, optional) - Batch size for processing features. Defaults to 128.

Returns:

Aleatoric uncertainty estimates of shape (num_samples,).

Return type:

np.ndarray

fit(X, y, epochs=100, batch_size=64, save_checkpoints=True, checkpoint_path='checkpoint.pt', early_stopping=True, patience=10, verbose=True)[source]

Trains the normalizing flow model using maximum likelihood estimation.

Parameters:
  • X (array-like) - Input features of shape (num_samples, num_features).

  • y (array-like) - Target values of shape (num_samples, output_size).

  • epochs (int, optional) - Number of training epochs. Defaults to 100.

  • batch_size (int, optional) - Batch size for training. Defaults to 64.

  • save_checkpoints (bool, optional) - Whether to save model checkpoints. Defaults to True.

  • checkpoint_path (str, optional) - Path to save model checkpoints. Defaults to ‘checkpoint.pt’.

  • early_stopping (bool, optional) - Whether to use early stopping. Defaults to True.

  • patience (int, optional) - Number of epochs to wait before early stopping. Defaults to 10.

  • verbose (bool, optional) - Whether to print training progress. Defaults to True.

Raises:

ValueError - If checkpoint loading fails.

get_latent(x, latent_constant=0.0)[source]

Computes the latent space representation of the given input.

Parameters:
  • x (array-like or torch.Tensor) - Input data of shape (num_samples, num_features).

  • latent_constant (float, optional) - Constant value used for latent variable sampling. Defaults to 0.0.

Returns:

Latent space representation of the input data.

Return type:

torch.Tensor

static load(path)[source]

Loads a trained normalizing flow model from a saved checkpoint.

Parameters:

path (str) - Path to the saved model checkpoint.

Returns:

A restored instance of the NormalizingFlow model.

Return type:

NormalizingFlow

sample(features, num_samples, return_type='numpy')[source]

Generates samples from the trained normalizing flow model.

Parameters:
  • features (array-like or torch.Tensor) - Input features to condition the samples on.

  • num_samples (int) - Number of samples to generate per input feature set.

  • return_type (str, optional) - Return type, either “numpy” or “tensor”. Defaults to “numpy”.

Returns:

Generated samples of shape (num_samples, output_size).

Return type:

np.ndarray or torch.Tensor

save(path)[source]

Saves the trained model and its metadata.

Parameters:

path (str) - Path to save the model checkpoint.

Raises:

ValueError - If the model has not been trained before saving.