utils¶
listify¶
format_float_tensor¶
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
tonp.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¶
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 themax_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¶
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¶
SequentialWithRecurrent¶
-
class
morgana.utils.
SequentialWithRecurrent
(*args)[source]¶ Bases:
torch.nn.modules.container.Sequential
Wraps
torch.nn.Sequential
to take custom forward arguments used byRecurrentCuDNNWrapper
.
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
.