Skip to content

Instantly share code, notes, and snippets.

@NMZivkovic
Created August 3, 2019 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NMZivkovic/1001d71dc34232be5771f171e283ea48 to your computer and use it in GitHub Desktop.
Save NMZivkovic/1001d71dc34232be5771f171e283ea48 to your computer and use it in GitHub Desktop.
class PositionalEncoding(object):
def __init__(self, position, d):
angle_rads = self._get_angles(np.arange(position)[:, np.newaxis], np.arange(d)[np.newaxis, :], d)
sines = np.sin(angle_rads[:, 0::2])
cosines = np.cos(angle_rads[:, 1::2])
self._encoding = np.concatenate([sines, cosines], axis=-1)
self._encoding = self._encoding[np.newaxis, ...]
def _get_angles(self, position, i, d):
angle_rates = 1 / np.power(10000, (2 * (i//2)) / np.float32(d))
return position * angle_rates
def get_positional_encoding(self):
return tf.cast(self._encoding, dtype=tf.float32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment