Utils

class torchelie.utils.DetachedModule(m)

Wrap a module to eval model, can’t be turned back to training mode, and make it invisible to recursive calls on `nn.Module`s

Parameters:m (nn.Module) – a module
class torchelie.utils.FrozenModule(m)

Wrap a module to eval model, can’t be turned back to training mode

Parameters:m (nn.Module) – a module
torchelie.utils.as_multiclass_shape(preds, as_probs=False)

Manipulate an array of logit predictions so that binary prediction is not a special case anymore. Outputs preds as (Batch size, Num classes (>=2)).

if preds has one dim, another one is added. If this is binary classification, a second column is added as 1 - preds.

Parameters:
  • preds (tensor) – predictions
  • as_probs (bool) – whether to return the preds as logits or probs
Returns:

the predictions reshaped

torchelie.utils.bgram(m)

Return the batched Gram matrix of m

Parameters:m (tensor) – a matrix of dim 3, first one is the batch
Returns:The batch of Gram matrix
torchelie.utils.dict_by_key(d, k)

Recursively index a dict by a hierarchical key

` >>> dict_by_key({'a': [{'b': 42}]}, 'a.0.b') 42 `

Parameters:
  • d (dict, list, and any level of nesting) – the data to index
  • k (str) – the key
Returns:

The value in d indexed by k

torchelie.utils.entropy(out, dim=1, reduce='mean')

Compute the entropy of the categorical distribution specified by the logits out along dimension dim.

Parameters:
  • out (tensor) – logits categorial distribution
  • dim (int) – the dimension along which the distributions are specified
  • reduce (str) – “mean”, “none” or “sum”
torchelie.utils.forever(iterable)

Cycle through iterable forever

Parameters:iterable (iterable) – the iterable
torchelie.utils.freeze(net)

Freeze all parameters of net

torchelie.utils.gram(m)

Return the Gram matrix of m

Parameters:m (tensor) – a matrix of dim 2
Returns:The Gram matrix
torchelie.utils.ilerp(a, b, t)

Inverse or lerp. For t between a and b, returns the fraction or a and b in t.

\(\frac{t - a}{b - a}\)

Parameters:
  • a (number or tensor) – a
  • b (number or tensor) – b
  • t (number or tensor) – t between a and b
Returns:

result

torchelie.utils.kaiming(m, a=0, nonlinearity='relu')

Initialize a module with kaiming normal init

Parameters:
  • m (nn.Module) – the module to init
  • a (float) – the slope of the nonlinearity
  • nonlinearity (str) – type of the nonlinearity
Returns:

the initialized module

torchelie.utils.layer_by_name(net, name)

Get a submodule at any depth of a net by its name

Parameters:
  • net (nn.Module) – the base module containing other modules
  • name (str) – a name of a submodule of net, like “layer3.0.conv1”.
Returns:

The found layer or None

torchelie.utils.lerp(a, b, t)

Linearly interpolate between a and b according to t.

\((1 - t)a + tb\)

Parameters:
  • a (number or tensor) – a
  • b (number or tensor) – b
  • t (number or tensor) – t between 0 and 1
Returns:

result between a and b

torchelie.utils.load_recursive_state_dict(x, obj)

Reload a state dict saved with recursive_state_dict()

Parameters:
  • x – the recursive state dict
  • obj – the object that has been recursive_state_dict()ed
torchelie.utils.n002(m)

Initialize a module with gaussian weights of standard deviation 0.02

Parameters:m (nn.Module) – the module to init
Returns:the initialized module
torchelie.utils.nb_parameters(net)

Counts the number of parameters of net

Parameters:net (nn.Module) – the net
Returns:the number of params
torchelie.utils.recursive_state_dict(x)

Recursively call state_dict() on all elements contained in a list / tuple / dict so that it can be saved safely via torch.save().

Parameters:x – any nesting of list / tuple / dict containing state_dict()able objects
Returns:the same structure with state dicts
torchelie.utils.send_to_device(x, device, non_blocking=False)

Send all tensors contained in x to device, when x is an arbitrary nested datastructure of dicts and lists containing tensors

Parameters:
  • x – the tensors
  • device – a torch device
  • non_blocking (bool) – non blocking
Returns:

x with device changed

torchelie.utils.unfreeze(net)

Unfreeze all parameters of net

torchelie.utils.xavier(m)

Initialize a module with xavier normal init

Parameters:m (nn.Module) – the module to init
Returns:the initialized module