drytorch.utils.average

Module containing classes and functions to average samples.

Functions

get_moving_average([decay, mass_coverage])

Return a moving average by specifying the decay.

get_trailing_mean(window_size)

Return a trailing average by specifying window size.

Classes

AbstractAverager(**kwargs)

Average tensor values from dict-like objects.

Averager(**kwargs)

Subclass of Aggregator with an implementation for float values.

TorchAverager(**kwargs)

Subclass of Aggregator with an implementation for torch.Tensor.

class TorchAverager(**kwargs: _T)[source]

Bases: AbstractAverager[Tensor]

Subclass of Aggregator with an implementation for torch.Tensor.

Initialize.

Parameters:

kwargs (_T) – named values to average.

all_reduce() dict[str, Tensor][source]

Synchronize the values across processes.

Return type:

dict[str, Tensor]

get_moving_average(decay: float = 0.9, mass_coverage: float = 0.99) Callable[[Sequence[float]], float][source]

Return a moving average by specifying the decay.

Parameters:
  • decay (float) – the closer to 0, the more the last elements have weight.

  • mass_coverage (float) – cumulative weight proportion before tail dropping.

Returns:

The moving average function.

Raises:
  • ValueError – if the decay is not between 0 and 1.

  • ValueError – if the mass_coverage is not between 1 - decay and 1.

Return type:

Callable[[Sequence[float]], float]

get_trailing_mean(window_size: int) Callable[[Sequence[float]], float][source]

Return a trailing average by specifying window size.

Parameters:

window_size (int) – number of items to aggregate.

Returns:

The windowed average function.

Raises:

ValueError if the window size is negative.

Return type:

Callable[[Sequence[float]], float]