gluonts.torch.util module#

gluonts.torch.util.copy_parameters(net_source: torch.nn.modules.module.Module, net_dest: torch.nn.modules.module.Module, strict: bool = True) None[source]#

Copies parameters from one network to another.

Parameters
  • net_source – Input network.

  • net_dest – Output network.

  • strict – whether to strictly enforce that the keys in state_dict match the keys returned by this module’s state_dict() function. Default: True

gluonts.torch.util.get_forward_input_names(module: Type[torch.nn.modules.module.Module])[source]#
gluonts.torch.util.lagged_sequence_values(indices: List[int], prior_sequence: torch.Tensor, sequence: torch.Tensor, dim: int) torch.Tensor[source]#

Constructs an array of lagged values from a given sequence.

Parameters
  • indices – Indices of the lagged observations. For example, [0] indicates that, at any time t, the will have only the observation from time t itself; instead, [0, 24] indicates that the output will have observations from times t and t-24.

  • prior_sequence – Tensor containing the input sequence prior to the time range for which the output is required.

  • sequence – Tensor containing the input sequence in the time range where the output is required.

  • dim – Time dimension.

Returns

A tensor of shape (*sequence.shape, len(indices)).

Return type

Tensor

gluonts.torch.util.repeat_along_dim(a: torch.Tensor, dim: int, repeats: int) torch.Tensor[source]#

Repeat a tensor along a given dimension, using torch.repeat internally.

Parameters
  • a – Original tensor to repeat.

  • dim – Dimension to repeat data over.

  • repeats – How many time to repeat the input tensor.

Returns

A tensor with the same size as the input one, except dimension dim which is multiplied by repeats.

Return type

torch.Tensor

gluonts.torch.util.resolve_device(device: Union[str, torch.device]) Union[str, torch.device][source]#

Resolves a torch device to the most appropriate one.

The "auto" device is resolved to "cuda" if CUDA is available, and to "cpu" otherwise. Otherwise the device is unchanged.

gluonts.torch.util.slice_along_dim(a: torch.Tensor, dim: int, slice_: slice) torch.Tensor[source]#

Slice a tensor along a given dimension.

Parameters
  • a – Original tensor to slice.

  • dim – Dimension to slice over.

  • slice – Slice to take.

Returns

A tensor with the same size as the input one, except dimension dim which has length equal to the slice length.

Return type

torch.Tensor

gluonts.torch.util.take_last(a: torch.Tensor, dim: int, num: int) torch.Tensor[source]#

Take last elements from a given tensor along a given dimension.

Parameters
  • a – Original tensor to slice.

  • dim – Dimension to slice over.

  • num – Number of trailing elements to retain (non-negative).

Returns

A tensor with the same size as the input one, except dimension dim which has length equal to num.

Return type

torch.Tensor

gluonts.torch.util.unsqueeze_expand(a: torch.Tensor, dim: int, size: int) torch.Tensor[source]#

Unsqueeze a dimension and expand over it in one go.

Parameters
  • a – Original tensor to unsqueeze.

  • dim – Dimension to unsqueeze.

  • size – Size for the new dimension.

Returns

A tensor with an added dimension dim of size size.

Return type

torch.Tensor

gluonts.torch.util.weighted_average(x: torch.Tensor, weights: Optional[torch.Tensor] = None, dim=None) torch.Tensor[source]#

Computes the weighted average of a given tensor across a given dim, masking values associated with weight zero,

meaning instead of nan * 0 = nan you will get 0 * 0 = 0.

Parameters
  • x – Input tensor, of which the average must be computed.

  • weights – Weights tensor, of the same shape as x.

  • dim – The dim along which to average x

Returns

The tensor with values averaged along the specified dim.

Return type

Tensor