Skip to content

Fixed few bugs related to positional encodding #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hyperparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Hyperparams:
num_epochs = 20
num_heads = 8
dropout_rate = 0.1
sinusoid = False # If True, use sinusoid. If false, positional embedding.
sinusoid = True # If True, use sinusoid. If false, positional embedding.



Expand Down
24 changes: 17 additions & 7 deletions modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
June 2017 by kyubyong park.
[email protected].
https://www.github.com/kyubyong/transformer

'''

from __future__ import print_function
import tensorflow as tf

import numpy as np
def normalize(inputs,
epsilon = 1e-8,
scope="ln",
reuse=None):
'''Applies layer normalization.
'''

Applies layer normalization.

Args:
inputs: A tensor with 2 or more dimensions, where the first dimension has
Expand Down Expand Up @@ -45,7 +48,8 @@ def embedding(inputs,
scale=True,
scope="embedding",
reuse=None):
'''Embeds a given tensor.
'''
Embeds a given tensor.

Args:
inputs: A `Tensor` with type `int32` or `int64` containing the ids
Expand Down Expand Up @@ -153,15 +157,20 @@ def positional_encoding(inputs,

# Convert to a tensor
lookup_table = tf.convert_to_tensor(position_enc)

lookup_table= tf.cast(lookup_table, tf.float32)

if zero_pad:
lookup_table = tf.concat((tf.zeros(shape=[1, num_units]),
lookup_table[1:, :]), 0)


outputs = tf.nn.embedding_lookup(lookup_table, position_ind)



if scale:
outputs = outputs * num_units**0.5



return outputs


Expand Down Expand Up @@ -260,7 +269,8 @@ def feedforward(inputs,
num_units=[2048, 512],
scope="multihead_attention",
reuse=None):
'''Point-wise feed forward net.
'''
Point-wise feed forward net.

Args:
inputs: A 3d tensor with shape of [N, T, C].
Expand Down
1 change: 0 additions & 1 deletion train.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def __init__(self, is_training=True):
## Positional Encoding
if hp.sinusoid:
self.dec += positional_encoding(self.decoder_inputs,
vocab_size=hp.maxlen,
num_units=hp.hidden_units,
zero_pad=False,
scale=False,
Expand Down