utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import torch
  2. from torch.nn import functional as F
  3. import numpy as np
  4. def sequence_mask(lengths, maxlen=None, dtype=torch.float32, device=None):
  5. if maxlen is None:
  6. maxlen = lengths.max()
  7. row_vector = torch.arange(0, maxlen, 1).to(lengths.device)
  8. matrix = torch.unsqueeze(lengths, dim=-1)
  9. mask = row_vector < matrix
  10. mask = mask.detach()
  11. return mask.type(dtype).to(device) if device is not None else mask.type(dtype)
  12. def apply_cmvn(inputs, mvn):
  13. device = inputs.device
  14. dtype = inputs.dtype
  15. frame, dim = inputs.shape
  16. meams = np.tile(mvn[0:1, :dim], (frame, 1))
  17. vars = np.tile(mvn[1:2, :dim], (frame, 1))
  18. inputs -= torch.from_numpy(meams).type(dtype).to(device)
  19. inputs *= torch.from_numpy(vars).type(dtype).to(device)
  20. return inputs.type(torch.float32)
  21. def drop_and_add(inputs: torch.Tensor,
  22. outputs: torch.Tensor,
  23. training: bool,
  24. dropout_rate: float = 0.1,
  25. stoch_layer_coeff: float = 1.0):
  26. outputs = F.dropout(outputs, p=dropout_rate, training=training, inplace=True)
  27. outputs *= stoch_layer_coeff
  28. input_dim = inputs.size(-1)
  29. output_dim = outputs.size(-1)
  30. if input_dim == output_dim:
  31. outputs += inputs
  32. return outputs