layers.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import torch
  2. from torch import nn
  3. class LayerNorm(torch.nn.LayerNorm):
  4. """Layer normalization module.
  5. :param int nout: output dim size
  6. :param int dim: dimension to be normalized
  7. """
  8. def __init__(self, nout, dim=-1, eps=1e-5):
  9. """Construct an LayerNorm object."""
  10. super(LayerNorm, self).__init__(nout, eps=eps)
  11. self.dim = dim
  12. def forward(self, x):
  13. """Apply layer normalization.
  14. :param torch.Tensor x: input tensor
  15. :return: layer normalized tensor
  16. :rtype torch.Tensor
  17. """
  18. if self.dim == -1:
  19. return super(LayerNorm, self).forward(x)
  20. return super(LayerNorm, self).forward(x.transpose(1, -1)).transpose(1, -1)
  21. class Reshape(nn.Module):
  22. def __init__(self, *args):
  23. super(Reshape, self).__init__()
  24. self.shape = args
  25. def forward(self, x):
  26. return x.view(self.shape)
  27. class Permute(nn.Module):
  28. def __init__(self, *args):
  29. super(Permute, self).__init__()
  30. self.args = args
  31. def forward(self, x):
  32. return x.permute(self.args)
  33. def Embedding(num_embeddings, embedding_dim, padding_idx=None):
  34. m = nn.Embedding(num_embeddings, embedding_dim, padding_idx=padding_idx)
  35. nn.init.normal_(m.weight, mean=0, std=embedding_dim ** -0.5)
  36. if padding_idx is not None:
  37. nn.init.constant_(m.weight[padding_idx], 0)
  38. return m