utils

listify

morgana.utils.listify(object_or_list)[source]

Converts input to an iterable if it is not already one.

format_float_tensor

morgana.utils.format_float_tensor(tensor)[source]

Formats the a single value or a 1-dimensional vector as a string.

map_nested

morgana.utils.map_nested(func, data)[source]

Recursively applies a function on a nested data structure. Base cases: np.ndarray torch.Tensor.

infer_device

morgana.utils.infer_device(tensor)[source]

Gets the device from a torch.Tensor instance.

detach_batched_seqs

morgana.utils.detach_batched_seqs(*sequence_features, seq_len)[source]

Converts torch.Tensor to np.ndarray. Moves data to CPU, detaches gradients, and removes padding.

Parameters
  • sequence_features (list[torch.Tensor] or torch.Tensor, shape (batch_size, max_seq_len, feat_dim)) – List of batched sequence features to be detached.

  • seq_len (np.ndarray or torch.Tensor, shape (batch_size,)) – Sequence length used to remove padding from each batch item.

Returns

Sequence features as np.ndarray without paddingWhat.

Return type

list[list[np.ndarray]] or list[np.ndarray], shape (batch_size, (seq_len, feat_dim))

get_epoch_from_checkpoint_path

morgana.utils.get_epoch_from_checkpoint_path(checkpoint_path)[source]

Extracts the epoch number from a checkpoint path of the form .*checkpoints/epoch_(NUM)_.*.pt

sequence_mask

morgana.utils.sequence_mask(seq_len, max_len=None, dtype=<class 'torch.ByteTensor'>, device=None)[source]

Creates a sequence mask with a given type.

Parameters
  • seq_len (torch.Tensor, shape (batch_size,)) – Sequence lengths.

  • max_len (int, optional) – Maximum sequence length. If None, max(seq_len) will be used to infer the max_len.

  • dtype (type) – Type for the mask that will be returned.

  • device (str or torch.device) – Name of the device to place the mask on.

Returns

mask – Sequence mask.

Return type

torch.Tensor, shape (batch_size, max_len, 1)

both_voiced_mask

morgana.utils.both_voiced_mask(*sequence_features, dtype=<class 'torch.ByteTensor'>)[source]

Calculates whether the sequence features are non-zero at the same time.

upsample_to_repetitions

morgana.utils.upsample_to_repetitions(sequence_feature, repeats)[source]

Copies sequence items according to some number of repetitions. Functionality is the same as np.repeat.

This is useful for upsampling phone-level linguistic features to frame-level, where repeats would be durations.

Parameters
  • sequence_feature (torch.Tensor, shape (batch_size, max_seq_len, feat_dim)) – Sequence feature at some lower frame-rate, this will be upsampled.

  • repeats (torch.Tensor, shape (batch_size, max_seq_len, 1)) – Number of repetitions of each sequence item.

Returns

upsampled_sequence_feature – Sequence feature upsampled using repetitions of individual sequence items.

Return type

torch.Tensor, shape (batch_size, max_repeated_len, feat_dim)

RecurrentCuDNNWrapper

class morgana.utils.RecurrentCuDNNWrapper(layer)[source]

Bases: torch.nn.modules.module.Module

Wraps a torch layer with sequence packing. This requires the sequence lengths to sort, pack and unpack.

Parameters

layer (torch.nn.RNNBase) – PyTorch recurrent layer to be wrapped.

SequentialWithRecurrent

class morgana.utils.SequentialWithRecurrent(*args)[source]

Bases: torch.nn.modules.container.Sequential

Wraps torch.nn.Sequential to take custom forward arguments used by RecurrentCuDNNWrapper.

ExponentialMovingAverage

class morgana.utils.ExponentialMovingAverage(model, decay)[source]

Bases: object

Exponential moving average helper to apply gradient updates to an EMA model.

Parameters
  • model (torch.nn.Module) –

  • decay (float) – Decay rate of previous parameter values. Parameter updates are also scaled by 1 - decay.

_update_param(self, name, x)[source]

Performs update on one parameter. shadow = decay * shadow + (1 - decay) * x.

update_params(self, other_model)[source]

Updates all parameters of self.model using a separate model’s updated parameters.