Skip to content

Instantly share code, notes, and snippets.

@ChunML
Last active October 30, 2019 06:16
Show Gist options
  • Save ChunML/3ac180bb985db20a97d760772e32e881 to your computer and use it in GitHub Desktop.
Save ChunML/3ac180bb985db20a97d760772e32e881 to your computer and use it in GitHub Desktop.
def positional_embedding(pos, model_size):
PE = np.zeros((1, model_size))
for i in range(model_size):
if i % 2 == 0:
PE[:, i] = np.sin(pos / 10000 ** (i / model_size))
else:
PE[:, i] = np.cos(pos / 10000 ** ((i - 1) / model_size))
return PE
max_length = max(len(data_en[0]), len(data_fr_in[0]))
MODEL_SIZE = 128
pes = []
for i in range(max_length):
pes.append(positional_embedding(i, MODEL_SIZE))
pes = np.concatenate(pes, axis=0)
pes = tf.constant(pes, dtype=tf.float32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment