Created
August 3, 2019 13:47
-
-
Save NMZivkovic/1001d71dc34232be5771f171e283ea48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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