tempor.models.utils module

Utilities for the models package directory.

tempor.models.utils.enable_reproducibility(random_seed: int = 0, torch_use_deterministic_algorithms: bool = False, torch_set_cudnn_deterministic: bool = False, torch_disable_cudnn_benchmark: bool = False, warn_cuda_env_vars: bool = True) None[source]

Attempt to enable reproducibility of results by removing sources of non-determinism (randomness) wherever possible. This function does not guarantee reproducible results, as there could be many other sources of randomness, e.g. data splitting, third party libraries etc.

The implementation is based on the information in PyTorch documentation here: https://pytorch.org/docs/stable/notes/randomness.html

Parameters:
random_seed : int, optional

The random seed to set. Defaults to 0.

torch_use_deterministic_algorithms : bool, optional

Whether to set torch.use_deterministic_algorithms(True). Defaults to False.

torch_set_cudnn_deterministic : bool, optional

Whether to set torch.backends.cudnn.deterministic = True. Defaults to False.

torch_disable_cudnn_benchmark : bool, optional

Whether to set torch.backends.cudnn.benchmark = False. Defaults to False.

warn_cuda_env_vars : bool, optional

Whether to raise a RuntimeWarning in case torch deterministic algorithms are enabled but the "CUDA_LAUNCH_BLOCKING"/"CUBLAS_WORKSPACE_CONFIG" environment variable has not been set. More details at https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html#torch.nn.LSTM. Defaults to True.

class tempor.models.utils.GumbelSoftmax(tau: float = 0.2, hard: bool = False, dim: int = -1)[source]

Bases: Module

Gumbel-Softmax activation function implementation.

forward(logits: Tensor) Tensor[source]

Forward pass.

training : bool
tempor.models.utils.NONLIN_MAP : dict[str, Module] = {'elu': ELU(alpha=1.0), 'gumbel_softmax': GumbelSoftmax(), 'leaky_relu': LeakyReLU(negative_slope=0.01), 'none': Identity(), 'relu': ReLU(), 'selu': SELU(), 'sigmoid': Sigmoid(), 'softmax': Softmax(dim=-1), 'tanh': Tanh()}

A map of names (str, keys) to nonlinearity modules (nn.Module, values).

tempor.models.utils.get_nonlin(name: Literal[none] | Literal[elu] | Literal[relu] | Literal[leaky_relu] | Literal[selu] | Literal[tanh] | Literal[sigmoid] | Literal[softmax] | Literal[gumbel_softmax]) Module[source]

Get a nonlinearity nn.Module (nonlinearity / activation function) by name.

Parameters:
name : Nonlin

Nonlinearity name.

Raises:

ValueError – If unknown nonlinearity name.

Returns:

Nonlinearity module.

Return type:

nn.Module

tempor.models.utils.SAMPLER_MAP : dict[str, type[Sampler]] = {'BatchSampler': <class 'torch.utils.data.sampler.BatchSampler'>, 'RandomSampler': <class 'torch.utils.data.sampler.RandomSampler'>, 'Sampler': <class 'torch.utils.data.sampler.Sampler'>, 'SequentialSampler': <class 'torch.utils.data.sampler.SequentialSampler'>, 'SubsetRandomSampler': <class 'torch.utils.data.sampler.SubsetRandomSampler'>, 'WeightedRandomSampler': <class 'torch.utils.data.sampler.WeightedRandomSampler'>}

A map of names (str, keys) to sampler classes (Type[torch.utils.data.sampler.Sampler], values).

tempor.models.utils.get_sampler(name: Literal[BatchSampler] | Literal[RandomSampler] | Literal[Sampler] | Literal[SequentialSampler] | Literal[SubsetRandomSampler] | Literal[WeightedRandomSampler] | None, **kwargs: Any) Sampler | None[source]

Get a sampler by name.

Parameters:
name : Union[Samp, None]

Sampler name.

**kwargs : Any

Sampler initializer kwargs.

Raises:

ValueError – If unknown sampler name.

Returns:

Sampler instance.

Return type:

Union[torch.utils.data.sampler.Sampler, None]

tempor.models.utils.get_device(device: None | int | str | device) device[source]

Get a torch device by name.

Parameters:
device : Union[None, int, str, torch.device]

Arguments to pass to torch.device, or None to use the default device.

Returns:

Device instance.

Return type:

torch.device