Table Of Contents
Table Of Contents

gluonts.support.util module

class gluonts.support.util.HybridContext(net: mxnet.gluon.block.HybridBlock, hybridize: bool, data_batch: Optional[List[mxnet.ndarray.ndarray.NDArray]] = None, **kwargs)[source]

Bases: object

A context manager that ensures that an MXNet network is operating in a hybridized / not hybridized mode.

Parameters:
  • net – The network whose hybrid mode has to be modified within the enclosing context.
  • hybridize – A boolean flag inidicating whether the hybrid mode should be set or not.
  • kwargs – A dictionary of optional arguments to pass to the hybridize() call of the enclosed HybridBlock network.
class gluonts.support.util.SignalHandler(handlers_map: Dict[int, Optional[Callable[[int, Any], None]]])[source]

Bases: object

A context manager that attaches a set of signal handlers within its scope.

Parameters:handlers_map – A dictionary mapping signal numbers to associated signal handlers to be attached within the scope of the enclosing SignalHandler instance.
Callback = typing.Union[typing.Callable[[int, typing.Any], NoneType], NoneType]
class gluonts.support.util.Timer[source]

Bases: object

Context manager for measuring the time of enclosed code fragments.

gluonts.support.util.copy_parameters(net_source: mxnet.gluon.block.Block, net_dest: mxnet.gluon.block.Block, ignore_extra: bool = False, allow_missing: bool = False) → None[source]

Copies parameters from one network to another.

Parameters:
  • net_source – Input network.
  • net_dest – Output network.
  • ignore_extra – Whether to ignore parameters from the source that are not present in the target.
  • allow_missing – Whether to allow additional parameters in the target not present in the source.
gluonts.support.util.cumsum(F, x: Union[mxnet.ndarray.ndarray.NDArray, mxnet.symbol.symbol.Symbol], exclusive: bool = False, reverse: bool = False) → Union[mxnet.ndarray.ndarray.NDArray, mxnet.symbol.symbol.Symbol][source]

Find cumulative sum on the last axis by multiplying with lower triangular ones-matrix:

\[\begin{split}\operatorname{cumsum}(x) = \begin{cases} \operatorname{ltr\_ones} \times x & \text{for cumulative sum}\\ x \times \operatorname{ltr\_ones} & \text{for cumulative sum in the reverse order} \end{cases}\end{split}\]

Also supports exclusive flag to start the cumsum with zero. For example, if \(x = [a, b, c]\), we have

\[\begin{split}\operatorname{cumsum}(x) = \begin{cases} [a, a + b, a + b + c] & \text{if }\mathit{reverse = False, exclusive = False}\\ [0, a, a + b] & \text{if }\mathit{reverse = False, exclusive = True}\\ [a + b + c, b + c, c] & \text{if }\mathit{reverse = True, exclusive = False}\\ [b + c, c, 0] & \text{if }\mathit{reverse = True, exclusive = True}\\ \end{cases}\end{split}\]
Parameters:
  • F – The function space to use.
  • x – A tensor with shape \((..., n)\).
  • exclusive – If True, the cumulative sum starts with zero.
  • reverse – If True, the cumulative sum is performed in the opposite direction.
Returns:

A modified tensor with identical shape and cumulative sums in the last axis.

Return type:

Tensor

gluonts.support.util.erf(F, x: Union[mxnet.ndarray.ndarray.NDArray, mxnet.symbol.symbol.Symbol])[source]
gluonts.support.util.export_repr_block(rb: mxnet.gluon.block.HybridBlock, model_dir: pathlib.Path, model_name: str, epoch: int = 0) → None[source]

Serializes a representable Gluon block.

Parameters:
  • rb – The block to export.
  • model_dir – The path where the model will be saved.
  • model_name – The name identifying the model.
  • epoch – The epoch number, which together with the model_name identifies the model parameters.
gluonts.support.util.export_symb_block(hb: mxnet.gluon.block.HybridBlock, model_dir: pathlib.Path, model_name: str, epoch: int = 0) → None[source]

Serializes a hybridized Gluon HybridBlock.

Parameters:
  • hb – The block to export.
  • model_dir – The path where the model will be saved.
  • model_name – The name identifying the model.
  • epoch – The epoch number, which together with the model_name identifies the model parameters.
gluonts.support.util.get_download_path() → pathlib.Path[source]
Returns:default path to download datasets or models of gluon-ts. The path is either $MXNET_HOME if the environment variable is defined or /home/username/.mxnet/gluon-ts/
Return type:Path
gluonts.support.util.get_hybrid_forward_input_names(hb: mxnet.gluon.block.HybridBlock)[source]
gluonts.support.util.hybrid_block_to_symbol_block(hb: mxnet.gluon.block.HybridBlock, data_batch: List[mxnet.ndarray.ndarray.NDArray]) → mxnet.gluon.block.SymbolBlock[source]

Converts a Gluon HybridBlock to a SymbolBlock. Following the Gluon API, this is achieved by a hybridize() call on the passed HybridBlock, a single forward pass (using the provided data batch), and a combination of an export() and an import() calls of the input block.

Note that MXNet has problems with this method.

Parameters:
  • hb – The Gluon HybridBlock to convert.
  • data_batch – Data to use for the forward pass after the hybridize() call.
Returns:

The resulting Gluon block backed by an MXNet symbol graph.

Return type:

mx.gluon.SymbolBlock

gluonts.support.util.import_repr_block(model_dir: pathlib.Path, model_name: str, epoch: int = 0) → mxnet.gluon.block.HybridBlock[source]

Deserializes a representable Gluon block.

Parameters:
  • model_dir – The path where the model is saved.
  • model_name – The name identifying the model.
  • epoch – The epoch number, which together with the model_name identifies the model parameters.
Returns:

The deserialized block.

Return type:

mx.gluon.HybridBlock

gluonts.support.util.import_symb_block(num_inputs: int, model_dir: pathlib.Path, model_name: str, epoch: int = 0) → mxnet.gluon.block.SymbolBlock[source]

Deserializes a hybridized Gluon HybridBlock as a SymbolBlock.

Parameters:
  • num_inputs – The number of inputs of the serialized block.
  • model_dir – The path where the model is saved.
  • model_name – The name identifying the model.
  • epoch – The epoch number, which together with the model_name identifies the model parameters.
Returns:

The deserialized block.

Return type:

mx.gluon.SymbolBlock

gluonts.support.util.make_nd_diag(F, x: Union[mxnet.ndarray.ndarray.NDArray, mxnet.symbol.symbol.Symbol], d: int) → Union[mxnet.ndarray.ndarray.NDArray, mxnet.symbol.symbol.Symbol][source]

Make a diagonal tensor, given the diagonal

Parameters:
  • F – The function space to use.
  • x – Diagonal to use, shape \((..., d)\).
  • d – Last dimension of x.
Returns:

A tensor y of shape \((..., d, d)\) such that \(y[..., i, i] = x[..., i]\).

Return type:

Tensor

gluonts.support.util.map_dct_values(fn: Callable, dct: dict) → dict[source]

Maps fn over a dicts values.

gluonts.support.util.weighted_average(F, x: Union[mxnet.ndarray.ndarray.NDArray, mxnet.symbol.symbol.Symbol], weights: Union[mxnet.ndarray.ndarray.NDArray, mxnet.symbol.symbol.Symbol, None] = None, axis=None) → Union[mxnet.ndarray.ndarray.NDArray, mxnet.symbol.symbol.Symbol][source]

Computes the weighted average of a given tensor across a given axis.

Parameters:
  • F – The function space to use.
  • x – Input tensor, of which the average must be computed.
  • weights – Weights tensor, of the same shape as x.
  • axis – The axis along which to average x
Returns:

The tensor with values averaged along the specified axis.

Return type:

Tensor