Fork of https://github.com/alokprasad/fastspeech_squeezewave to also fix denoising in squeezewave
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.2 KiB

  1. import torch.nn as nn
  2. import torch.nn.functional as F
  3. import numpy as np
  4. from transformer.Modules import ScaledDotProductAttention
  5. import hparams as hp
  6. class MultiHeadAttention(nn.Module):
  7. ''' Multi-Head Attention module '''
  8. def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
  9. super().__init__()
  10. self.n_head = n_head
  11. self.d_k = d_k
  12. self.d_v = d_v
  13. self.w_qs = nn.Linear(d_model, n_head * d_k)
  14. self.w_ks = nn.Linear(d_model, n_head * d_k)
  15. self.w_vs = nn.Linear(d_model, n_head * d_v)
  16. nn.init.normal_(self.w_qs.weight, mean=0,
  17. std=np.sqrt(2.0 / (d_model + d_k)))
  18. nn.init.normal_(self.w_ks.weight, mean=0,
  19. std=np.sqrt(2.0 / (d_model + d_k)))
  20. nn.init.normal_(self.w_vs.weight, mean=0,
  21. std=np.sqrt(2.0 / (d_model + d_v)))
  22. self.attention = ScaledDotProductAttention(
  23. temperature=np.power(d_k, 0.5))
  24. self.layer_norm = nn.LayerNorm(d_model)
  25. self.fc = nn.Linear(n_head * d_v, d_model)
  26. nn.init.xavier_normal_(self.fc.weight)
  27. self.dropout = nn.Dropout(dropout)
  28. def forward(self, q, k, v, mask=None):
  29. d_k, d_v, n_head = self.d_k, self.d_v, self.n_head
  30. sz_b, len_q, _ = q.size()
  31. sz_b, len_k, _ = k.size()
  32. sz_b, len_v, _ = v.size()
  33. residual = q
  34. q = self.w_qs(q).view(sz_b, len_q, n_head, d_k)
  35. k = self.w_ks(k).view(sz_b, len_k, n_head, d_k)
  36. v = self.w_vs(v).view(sz_b, len_v, n_head, d_v)
  37. q = q.permute(2, 0, 1, 3).contiguous().view(-1,
  38. len_q, d_k) # (n*b) x lq x dk
  39. k = k.permute(2, 0, 1, 3).contiguous().view(-1,
  40. len_k, d_k) # (n*b) x lk x dk
  41. v = v.permute(2, 0, 1, 3).contiguous().view(-1,
  42. len_v, d_v) # (n*b) x lv x dv
  43. mask = mask.repeat(n_head, 1, 1) # (n*b) x .. x ..
  44. output, attn = self.attention(q, k, v, mask=mask)
  45. output = output.view(n_head, sz_b, len_q, d_v)
  46. output = output.permute(1, 2, 0, 3).contiguous().view(
  47. sz_b, len_q, -1) # b x lq x (n*dv)
  48. output = self.dropout(self.fc(output))
  49. output = self.layer_norm(output + residual)
  50. return output, attn
  51. class PositionwiseFeedForward(nn.Module):
  52. ''' A two-feed-forward-layer module '''
  53. def __init__(self, d_in, d_hid, dropout=0.1):
  54. super().__init__()
  55. # Use Conv1D
  56. # position-wise
  57. self.w_1 = nn.Conv1d(
  58. d_in, d_hid, kernel_size=hp.fft_conv1d_kernel, padding=hp.fft_conv1d_padding)
  59. # position-wise
  60. self.w_2 = nn.Conv1d(
  61. d_hid, d_in, kernel_size=hp.fft_conv1d_kernel, padding=hp.fft_conv1d_padding)
  62. self.layer_norm = nn.LayerNorm(d_in)
  63. self.dropout = nn.Dropout(dropout)
  64. def forward(self, x):
  65. residual = x
  66. output = x.transpose(1, 2)
  67. output = self.w_2(F.relu(self.w_1(output)))
  68. output = output.transpose(1, 2)
  69. output = self.dropout(output)
  70. output = self.layer_norm(output + residual)
  71. return output