You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PositionEmbedding.py 1.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # __author__="Danqing Wang"
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # ==============================================================================
  17. import torch
  18. import numpy as np
  19. def get_sinusoid_encoding_table(n_position, d_hid, padding_idx=None):
  20. ''' Sinusoid position encoding table '''
  21. def cal_angle(position, hid_idx):
  22. return position / np.power(10000, 2 * (hid_idx // 2) / d_hid)
  23. def get_posi_angle_vec(position):
  24. return [cal_angle(position, hid_j) for hid_j in range(d_hid)]
  25. sinusoid_table = np.array([get_posi_angle_vec(pos_i) for pos_i in range(n_position)])
  26. sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2]) # dim 2i
  27. sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2]) # dim 2i+1
  28. if padding_idx is not None:
  29. # zero vector for padding dimension
  30. sinusoid_table[padding_idx] = 0.
  31. return torch.FloatTensor(sinusoid_table)