Skip to content
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

fix bugs about TransformerDecoder's arguments #168

Merged
merged 2 commits into from
May 31, 2019
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ simple-examples.tgz

/examples/vae_text/simple-examples/
/examples/vae_text/data/
/examples/vae_text/models/
/examples/vae_text/yahoo.zip
/examples/vae_text/simple-examples.tgz

/examples/transformer/data/
/examples/transformer/temp/
Expand Down
7 changes: 3 additions & 4 deletions examples/transformer/transformer_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ def main():
# (text sequence length excluding padding)
encoder_input_length = tf.reduce_sum(
1 - tf.to_int32(tf.equal(encoder_input, 0)), axis=1)
decoder_input_length = tf.reduce_sum(
1 - tf.to_int32(tf.equal(decoder_input, 0)), axis=1)

labels = tf.placeholder(tf.int64, shape=(None, None))
is_target = tf.to_float(tf.not_equal(labels, 0))
Expand Down Expand Up @@ -152,8 +150,9 @@ def main():
start_tokens = tf.fill([batch_size], bos_token_id)

def _embedding_fn(x, y):
return tgt_embedder(x) * config_model.hidden_dim ** 0.5 + pos_embedder(
y)
x_w_embed = tgt_embedder(x)
y_p_embed = pos_embedder(y)
return x_w_embed * config_model.hidden_dim ** 0.5 + y_p_embed

predictions = decoder(
memory=encoder_output,
Expand Down
4 changes: 1 addition & 3 deletions examples/vae_text/config_lstm_ptb.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
}


decoder_hparams = {
"type": "lstm"
}
decoder_type = 'lstm'

enc_cell_hparams = {
"type": "LSTMBlockCell",
Expand Down
4 changes: 1 addition & 3 deletions examples/vae_text/config_lstm_yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
residual_dropout = 0.2
num_blocks = 3

decoder_hparams = {
"type": "lstm"
}
decoder_type = 'lstm'

enc_cell_hparams = {
"type": "LSTMBlockCell",
Expand Down
97 changes: 48 additions & 49 deletions examples/vae_text/config_trans_ptb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""VAE config.
"""Config file of VAE with Trasnformer decoder, on PTB data.
"""

# pylint: disable=invalid-name, too-few-public-methods, missing-docstring

dataset = "ptb"
dataset = 'ptb'
num_epochs = 100
hidden_size = 256
dec_dropout_in = 0.
Expand All @@ -29,10 +28,10 @@
latent_dims = 32

lr_decay_hparams = {
"init_lr": 0.001,
"threshold": 2,
"decay_factor": 0.5,
"max_decay": 5
'init_lr': 0.001,
'threshold': 2,
'decay_factor': 0.5,
'max_decay': 5
}


Expand All @@ -42,24 +41,22 @@
residual_dropout = 0.2
num_blocks = 3

decoder_hparams = {
"type": "transformer"
}
decoder_type = 'transformer'

enc_cell_hparams = {
"type": "LSTMBlockCell",
"kwargs": {
"num_units": hidden_size,
"forget_bias": 0.
'type': 'LSTMBlockCell',
'kwargs': {
'num_units': hidden_size,
'forget_bias': 0.
},
"dropout": {"output_keep_prob": 1. - enc_dropout_out},
"num_layers": 1
'dropout': {'output_keep_prob': 1. - enc_dropout_out},
'num_layers': 1
}

enc_emb_hparams = {
'name': 'lookup_table',
"dim": embed_dim,
"dropout_rate": enc_dropout_in,
'dim': embed_dim,
'dropout_rate': enc_dropout_in,
'initializer' : {
'type': 'random_normal_initializer',
'kwargs': {
Expand All @@ -71,8 +68,8 @@

dec_emb_hparams = {
'name': 'lookup_table',
"dim": embed_dim,
"dropout_rate": dec_dropout_in,
'dim': embed_dim,
'dropout_rate': dec_dropout_in,
'initializer' : {
'type': 'random_normal_initializer',
'kwargs': {
Expand All @@ -82,16 +79,18 @@
}
}

max_pos = 200 # max sequence length in training data
dec_pos_emb_hparams = {
'dim': hidden_size,
}

# due to the residual connection, the embed_dim should be equal to hidden_size
trans_hparams = {
'output_layer_bias': False,
'embedding_dropout': embedding_dropout,
'residual_dropout': residual_dropout,
'num_blocks': num_blocks,
'dim': hidden_size,
'position_embedder_hparams': {
'dim': hidden_size,
},
'initializer': {
'type': 'variance_scaling_initializer',
'kwargs': {
Expand Down Expand Up @@ -138,48 +137,48 @@

# KL annealing
kl_anneal_hparams = {
"warm_up": 10,
"start": 0.1
'warm_up': 10,
'start': 0.1
}

train_data_hparams = {
"num_epochs": 1,
"batch_size": batch_size,
"seed": 123,
"dataset": {
"files": './simple-examples/data/ptb.train.txt',
"vocab_file": './simple-examples/data/vocab.txt'
'num_epochs': 1,
'batch_size': batch_size,
'seed': 123,
'dataset': {
'files': './simple-examples/data/ptb.train.txt',
'vocab_file': './simple-examples/data/vocab.txt'
}
}

val_data_hparams = {
"num_epochs": 1,
"batch_size": batch_size,
"seed": 123,
"dataset": {
"files": './simple-examples/data/ptb.valid.txt',
"vocab_file": './simple-examples/data/vocab.txt'
'num_epochs': 1,
'batch_size': batch_size,
'seed': 123,
'dataset': {
'files': './simple-examples/data/ptb.valid.txt',
'vocab_file': './simple-examples/data/vocab.txt'
}
}

test_data_hparams = {
"num_epochs": 1,
"batch_size": batch_size,
"dataset": {
"files": './simple-examples/data/ptb.test.txt',
"vocab_file": './simple-examples/data/vocab.txt'
'num_epochs': 1,
'batch_size': batch_size,
'dataset': {
'files': './simple-examples/data/ptb.test.txt',
'vocab_file': './simple-examples/data/vocab.txt'
}
}

opt_hparams = {
"optimizer": {
"type": "AdamOptimizer",
"kwargs": {
"learning_rate": 0.001
'optimizer': {
'type': 'AdamOptimizer',
'kwargs': {
'learning_rate': 0.001
}
},
"gradient_clip": {
"type": "clip_by_global_norm",
"kwargs": {"clip_norm": 5.}
'gradient_clip': {
'type': 'clip_by_global_norm',
'kwargs': {'clip_norm': 5.}
}
}
12 changes: 8 additions & 4 deletions examples/vae_text/config_trans_yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
residual_dropout = 0.2
num_blocks = 3

decoder_hparams = {
"type": "transformer"
}
decoder_type = 'transformer'

enc_cell_hparams = {
"type": "LSTMBlockCell",
Expand Down Expand Up @@ -82,6 +80,12 @@
}
}


max_pos = 200 # max sequence length in training data
dec_pos_emb_hparams = {
'dim': hidden_size,
}

# due to the residual connection, the embed_dim should be equal to hidden_size
trans_hparams = {
'output_layer_bias': False,
Expand Down Expand Up @@ -134,7 +138,7 @@
}

# KL annealing
kl_anneal_hparams={
kl_anneal_hparams = {
"warm_up": 10,
"start": 0.1
}
Expand Down
Loading