timesead.utils.torch_utils

Attributes

activations

Classes

ConstantLR

Decays the learning rate of each parameter group by gamma every step_size epochs.

Functions

run_deterministic(→ None)

run_fast(→ None)

clear_gpu_memory(→ None)

set_threads(→ None)

collate_fn_variable_length_ts(...)

unpack_sequence(→ List[torch.Tensor])

generate_random_sequences_like(→ Union[torch.Tensor, ...)

generate_random_sequences(→ Union[torch.Tensor, ...)

dataset_to_numpy_array(→ List[numpy.array])

Accumulates elements from a torch dataset into a numpy array.

sequences_to_dataframe(→ List[pandas.DataFrame])

list2tensor(→ torch.Tensor)

tensor2scalar(→ Union[torch.Tensor, float, int])

batched_dot(→ torch.Tensor)

Computes a batched dot product.

get_device(→ torch.device)

nested_list2tensor(→ torch.Tensor)

exponential_moving_avg_(series, alpha[, avg_num, ...])

Computes the online adjusted exp weighted average of the errors.

unsqueeze_like(→ torch.Tensor)

transform_tensor(→ Union[Tuple[torch.Tensor], ...)

log1mexp(→ torch.Tensor)

Numerically accurate evaluation of log(1 - exp(x)) for x < 0.

Module Contents

timesead.utils.torch_utils.activations
timesead.utils.torch_utils.run_deterministic() None
Return type:

None

timesead.utils.torch_utils.run_fast() None
Return type:

None

timesead.utils.torch_utils.clear_gpu_memory() None
Return type:

None

timesead.utils.torch_utils.set_threads(n: int | None = None) None
Parameters:

n (Optional[int])

Return type:

None

timesead.utils.torch_utils.collate_fn_variable_length_ts(tensors: List[Tuple[torch.Tensor, torch.Tensor]]) Tuple[torch.nn.utils.rnn.PackedSequence, torch.nn.utils.rnn.PackedSequence]
Parameters:

tensors (List[Tuple[torch.Tensor, torch.Tensor]])

Return type:

Tuple[torch.nn.utils.rnn.PackedSequence, torch.nn.utils.rnn.PackedSequence]

timesead.utils.torch_utils.unpack_sequence(packed_sequence: torch.nn.utils.rnn.PackedSequence) List[torch.Tensor]
Parameters:

packed_sequence (torch.nn.utils.rnn.PackedSequence)

Return type:

List[torch.Tensor]

timesead.utils.torch_utils.generate_random_sequences_like(sequences: torch.Tensor | torch.nn.utils.rnn.PackedSequence) torch.Tensor | torch.nn.utils.rnn.PackedSequence
Parameters:

sequences (Union[torch.Tensor, torch.nn.utils.rnn.PackedSequence])

Return type:

Union[torch.Tensor, torch.nn.utils.rnn.PackedSequence]

timesead.utils.torch_utils.generate_random_sequences(shape: List[Tuple[int, int]] | Tuple[int, int], n: int = 1) torch.Tensor | torch.nn.utils.rnn.PackedSequence
Parameters:
Return type:

Union[torch.Tensor, torch.nn.utils.rnn.PackedSequence]

timesead.utils.torch_utils.dataset_to_numpy_array(dataset: torch.utils.data.Dataset | torch.utils.data.DataLoader, n: int = -1) List[numpy.array]

Accumulates elements from a torch dataset into a numpy array.

Parameters:
Returns:

Dataset as a list of numpy arrays.

Return type:

list(numpy.array)

timesead.utils.torch_utils.sequences_to_dataframe(sequences: torch.Tensor | torch.nn.utils.rnn.PackedSequence) List[pandas.DataFrame]
Parameters:

sequences (Union[torch.Tensor, torch.nn.utils.rnn.PackedSequence])

Return type:

List[pandas.DataFrame]

timesead.utils.torch_utils.list2tensor(input: List[torch.Tensor] | torch.Tensor) torch.Tensor
Parameters:

input (Union[List[torch.Tensor], torch.Tensor])

Return type:

torch.Tensor

timesead.utils.torch_utils.tensor2scalar(tensor: torch.Tensor) torch.Tensor | float | int
Parameters:

tensor (torch.Tensor)

Return type:

Union[torch.Tensor, float, int]

timesead.utils.torch_utils.batched_dot(vec1: torch.Tensor, vec2: torch.Tensor) torch.Tensor

Computes a batched dot product.

Parameters:
  • vec1 (torch.Tensor) – Tensor of shape (*, D).

  • vec2 (torch.Tensor) – Tensor of shape (*, D). The batch shapes of both inputs must be broadcastable.

Returns:

Tensor of shape (*)

Return type:

torch.Tensor

timesead.utils.torch_utils.get_device(module: torch.nn.Module) torch.device
Parameters:

module (torch.nn.Module)

Return type:

torch.device

timesead.utils.torch_utils.nested_list2tensor(inputs: List) torch.Tensor
Parameters:

inputs (List)

Return type:

torch.Tensor

timesead.utils.torch_utils.exponential_moving_avg_(series: torch.Tensor, alpha: float, avg_num: float = 0, avg_denom: float = 0)

Computes the online adjusted exp weighted average of the errors. See https://pandas.pydata.org/docs/user_guide/window.html#window-exponentially-weighted. This modifies the given TS tensor in-place.

Parameters:
timesead.utils.torch_utils.unsqueeze_like(to_squeeze: torch.Tensor, like: torch.Tensor, keep_dims: List | None = None) torch.Tensor
Parameters:
Return type:

torch.Tensor

timesead.utils.torch_utils.transform_tensor(tensor: Tuple[torch.Tensor] | torch.Tensor, current_shape: str, target_shape: str) Tuple[torch.Tensor] | torch.Tensor
Parameters:
Return type:

Union[Tuple[torch.Tensor], torch.Tensor]

timesead.utils.torch_utils.log1mexp(x: torch.Tensor) torch.Tensor

Numerically accurate evaluation of log(1 - exp(x)) for x < 0. See [Maechler2012accurate] for details.

[Maechler2012accurate]

Maechler, Martin (2012). Accurately Computing log(1-exp(-|a|)). Assessed from the Rmpfr package.

Parameters:

x (torch.Tensor)

Return type:

torch.Tensor

class timesead.utils.torch_utils.ConstantLR(optimizer: torch.optim.Optimizer)

Bases: torch.optim.lr_scheduler.StepLR

Decays the learning rate of each parameter group by gamma every step_size epochs.

Notice that such decay can happen simultaneously with other changes to the learning rate from outside this scheduler. When last_epoch=-1, sets initial lr as lr.

Parameters:
  • optimizer (Optimizer) – Wrapped optimizer.

  • step_size (int) – Period of learning rate decay.

  • gamma (float) – Multiplicative factor of learning rate decay. Default: 0.1.

  • last_epoch (int) – The index of last epoch. Default: -1.

Example

>>> # xdoctest: +SKIP
>>> # Assuming optimizer uses lr = 0.05 for all groups
>>> # lr = 0.05     if epoch < 30
>>> # lr = 0.005    if 30 <= epoch < 60
>>> # lr = 0.0005   if 60 <= epoch < 90
>>> # ...
>>> scheduler = StepLR(optimizer, step_size=30, gamma=0.1)
>>> for epoch in range(100):
>>>     train(...)
>>>     validate(...)
>>>     scheduler.step()