utils package

Submodules

utils.device module

utils.device.get_device(tensor)[source]

Get the device of a tensor

Parameters:
tensor: Tensor

tensor from which to extract the device

Returns:
computation device (0-n or cpu)
utils.device.to_same_device(*tensors, device=None)[source]

Move all tensors to the same device (by default the first tensor’s one)

Parameters:
tensors: Tensor

tensors which we want to move to a common device

Returns:
a tuple of tensors in the same order as the arguments moved to a common device

utils.helpers module

utils.helpers.batchify(func, batch, samples=[], **args)[source]
utils.helpers.get_token_first_indices(x, token)[source]

utils.moving_average module

class utils.moving_average.MovingAverage(init_value=0)[source]

Bases: object

Keeps a moving average of some quantity

add(new_value, new_weight=1)[source]

Includes a single value into a moving average

Parameters:
new_value: float

pointwise estimate of the quantity to estimate

new_weight: float

a weight by which to ponder the new value

add_array(new_values)[source]

Includes an array of values into the moving average

Parameters:
new_values: torch/np array of floats

pointwise estimates of the quantity to estimate

reset()[source]

Resets the moving average to its initial conditions

utils.moving_average.average(moving_averages)[source]

utils.observable module

class utils.observable.Observable[source]

Bases: object

Textbook observer pattern

dispatch(*args, **kwargs)[source]
enroll(function)[source]
utils.observable.forward(observable1, observable2)[source]

Forwards messages from an observable to another with the same signature

Module contents