Skip to content

Commit 3c6557b

Browse files
hereismaricaisq
authored andcommitted
Using tf.keras + default Initializers in Python files. (#204)
Involves the "sentiment" and "translation" examples.
1 parent aad103f commit 3c6557b

File tree

2 files changed

+32
-42
lines changed

2 files changed

+32
-42
lines changed

sentiment/python/imdb.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import json
3333
import os
3434

35-
import keras
35+
import tensorflow as tf
3636
import tensorflowjs as tfjs
3737

3838

@@ -50,7 +50,7 @@ def get_word_index(reverse=False):
5050
Returns:
5151
The word index as a `dict`.
5252
"""
53-
word_index = keras.datasets.imdb.get_word_index()
53+
word_index = tf.keras.datasets.imdb.get_word_index()
5454
if reverse:
5555
word_index = dict((word_index[key], key) for key in word_index)
5656
return word_index
@@ -85,10 +85,10 @@ def get_imdb_data(vocabulary_size, max_len):
8585
y_test: Same as `y_train`, but for test.
8686
"""
8787
print("Getting IMDB data with vocabulary_size %d" % vocabulary_size)
88-
(x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data(
88+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.imdb.load_data(
8989
num_words=vocabulary_size)
90-
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_len)
91-
x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_len)
90+
x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_len)
91+
x_test = tf.keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_len)
9292
return x_train, y_train, x_test, y_test
9393

9494

@@ -122,38 +122,30 @@ def train_model(model_type,
122122
ValueError: on invalid model type.
123123
"""
124124

125-
model = keras.Sequential()
126-
model.add(keras.layers.Embedding(vocabulary_size, embedding_size))
125+
model = tf.keras.Sequential()
126+
model.add(tf.keras.layers.Embedding(vocabulary_size, embedding_size))
127127
if model_type == 'bidirectional_lstm':
128128
# TODO(cais): Uncomment the following once bug b/74429960 is fixed.
129-
# model.add(keras.layers.Embedding(
129+
# model.add(tf.keras.layers.Embedding(
130130
# vocabulary_size, 128, input_length=maxlen))
131-
# model.add(keras.layers.Bidirectional(
132-
# keras.layers.LSTM(64,
133-
# kernel_initializer='glorot_normal',
134-
# recurrent_initializer ='glorot_normal')))
135-
# model.add(keras.layers.Dropout(0.5))
131+
# model.add(tf.keras.layers.Bidirectional(
132+
# tf.keras.layers.LSTM(64))
133+
# model.add(tf.keras.layers.Dropout(0.5))
136134
raise NotImplementedError()
137135
elif model_type == 'cnn':
138-
model.add(keras.layers.Dropout(0.2))
139-
model.add(keras.layers.Conv1D(250,
136+
model.add(tf.keras.layers.Dropout(0.2))
137+
model.add(tf.keras.layers.Conv1D(250,
140138
3,
141139
padding='valid',
142140
activation='relu',
143141
strides=1))
144-
model.add(keras.layers.GlobalMaxPooling1D())
145-
model.add(keras.layers.Dense(250, activation='relu'))
142+
model.add(tf.keras.layers.GlobalMaxPooling1D())
143+
model.add(tf.keras.layers.Dense(250, activation='relu'))
146144
elif model_type == 'lstm':
147-
model.add(
148-
keras.layers.LSTM(
149-
128,
150-
kernel_initializer='glorot_normal',
151-
recurrent_initializer='glorot_normal'))
152-
# TODO(cais): Remove glorot_normal and use the default orthogonal once
153-
# SVD is available.
145+
model.add(tf.keras.layers.LSTM(128))
154146
else:
155147
raise ValueError("Invalid model type: '%s'" % model_type)
156-
model.add(keras.layers.Dense(1, activation='sigmoid'))
148+
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
157149

158150
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
159151
model.fit(x_train, y_train,
@@ -210,7 +202,7 @@ def main():
210202
tfjs.converters.save_keras_model(model, FLAGS.artifacts_dir)
211203
print('\nSaved model artifacts in directory: %s' % FLAGS.artifacts_dir)
212204

213-
205+
214206
if __name__ == '__main__':
215207
parser = argparse.ArgumentParser('IMDB sentiment classification model')
216208
parser.add_argument(

translation/python/translation.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import json
3232
import os
3333

34-
from keras.models import Model
35-
from keras.layers import Input, LSTM, Dense
34+
from tensorflow.keras.models import Model
35+
from tensorflow.keras.layers import Input, LSTM, Dense
3636
import numpy as np
3737
import tensorflowjs as tfjs
3838

@@ -143,8 +143,7 @@ def seq2seq_model(num_encoder_tokens, num_decoder_tokens, latent_dim):
143143
# Define an input sequence and process it.
144144
encoder_inputs = Input(shape=(None, num_encoder_tokens))
145145
encoder = LSTM(latent_dim,
146-
return_state=True,
147-
recurrent_initializer=FLAGS.recurrent_initializer)
146+
return_state=True)
148147
_, state_h, state_c = encoder(encoder_inputs)
149148
# We discard `encoder_outputs` and only keep the states.
150149
encoder_states = [state_h, state_c]
@@ -156,8 +155,7 @@ def seq2seq_model(num_encoder_tokens, num_decoder_tokens, latent_dim):
156155
# return states in the training model, but we will use them in inference.
157156
decoder_lstm = LSTM(FLAGS.latent_dim,
158157
return_sequences=True,
159-
return_state=True,
160-
recurrent_initializer=FLAGS.recurrent_initializer)
158+
return_state=True)
161159
decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
162160
initial_state=encoder_states)
163161
decoder_dense = Dense(num_decoder_tokens, activation='softmax')
@@ -285,11 +283,20 @@ def main():
285283
# Take one sequence (part of the training set)
286284
# for trying out decoding.
287285
input_seq = encoder_input_data[seq_index: seq_index + 1]
286+
# Get expected output
287+
target_seq = decoder_target_data[seq_index]
288+
# One-hot to index
289+
target_seq = [
290+
reverse_target_char_index[np.argmax(c)] for c in target_seq
291+
]
292+
# Array to string
293+
target_seq = ''.join(target_seq).replace('\n', '')
288294
decoded_sentence = decode_sequence(
289295
input_seq, encoder_model, decoder_model, num_decoder_tokens,
290296
target_begin_index, reverse_target_char_index, max_decoder_seq_length)
291297
print('-')
292298
print('Input sentence:', input_texts[seq_index])
299+
print('Target sentence:', target_seq)
293300
print('Decoded sentence:', decoded_sentence)
294301

295302

@@ -308,7 +315,7 @@ def main():
308315
parser.add_argument(
309316
'--epochs',
310317
type=int,
311-
default=100,
318+
default=20,
312319
help='Number of training epochs.')
313320
parser.add_argument(
314321
'--latent_dim',
@@ -325,15 +332,6 @@ def main():
325332
type=int,
326333
default=100,
327334
help='Number of example sentences to test at the end of the training.')
328-
# TODO(cais): This is a workaround for the limitation in TF.js Layers that the
329-
# default recurrent initializer "Orthogonal" is currently not supported.
330-
# Remove this once "Orthogonal" becomes available.
331-
parser.add_argument(
332-
'--recurrent_initializer',
333-
type=str,
334-
default='orthogonal',
335-
help='Custom initializer for recurrent kernels of LSTMs (e.g., '
336-
'glorot_uniform)')
337335
parser.add_argument(
338336
'--artifacts_dir',
339337
type=str,

0 commit comments

Comments
 (0)