-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgru_v2.py
205 lines (167 loc) · 5.55 KB
/
gru_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# %%
import argparse
import contextlib
import io
import os
import time
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
NAME = 'gru_v2'
## model
def build_model(units, timesteps, vocab_size):
inputs = layers.Input(shape=(timesteps, vocab_size))
x = layers.GRU(units, return_sequences=True)(inputs)
x = layers.Dropout(0.25)(x)
x = layers.GRU(units)(x)
x = layers.Dropout(0.25)(x)
x = layers.Dense(vocab_size)(x)
outputs = layers.Activation('softmax')(x)
return keras.Model(inputs, outputs, name=NAME)
## dataset
def build_dataset(data, vocab, timesteps, stride):
x = []
y = []
for i in range(0, len(data) - timesteps, stride):
data_slice = data[i : i + timesteps]
x.append([vocab[c] for c in data_slice])
y.append(vocab[data[i + timesteps]])
return tf.data.Dataset.from_tensor_slices((x, y))
## sampling
def sample(model, input, limit, vocab_size, t_x, seed=0):
output = list(input)
sentence = [0 for i in range(t_x)]
sentence[0 : len(input)] = input
for _ in range(limit):
x = sentence
x = tf.one_hot(x, vocab_size, axis=-1)
x = tf.reshape(x, [1, -1, vocab_size])
predictions = model.predict(x)
idx = np.random.choice(vocab_size, p=predictions[0])
output.append(idx)
sentence = sentence[1:] + [idx]
seed += 1
return output
class Sampler(keras.callbacks.Callback):
def __init__(self, input, vocab_size, char_to_ix, ix_to_char):
super(Sampler, self).__init__()
self.input = [char_to_ix[c] for c in input]
self.vocab_size = vocab_size
self.ix_to_char = ix_to_char
def on_epoch_end(self, epoch, logs=None):
output = sample(self.model, self.input, 3 * T_X, self.vocab_size, T_X)
sentence = ''.join([self.ix_to_char[idx] for idx in output])
print('\n', sentence, '\n')
## utils
def parse_args():
parser = argparse.ArgumentParser(description=NAME)
# storage paths
parser.add_argument(
'--data_dir', type=str, default='/tmp', help='The location of the input data.'
)
parser.add_argument(
'--model_dir',
type=str,
default='/tmp',
help='The location of the model checkpoint files.',
)
parser.add_argument(
'--download',
action='store_true',
default=False,
help='Whether to download data to `--data_dir`.',
)
# training
parser.add_argument(
'--train_epochs',
type=int,
default=1,
help='The number of epochs used to train.',
)
parser.add_argument(
'--epochs_between_evals',
type=int,
default=1,
help='The number of training epochs to run between evaluations.',
)
parser.add_argument(
'--batch_size',
type=int,
default=128,
help='Batch size for training and evaluation.',
)
# distribution
parser.add_argument(
'--distribution_strategy',
type=str,
default='mirrored',
help='The Distribution Strategy to use for training.',
)
parser.add_argument(
'--num_gpus', type=int, default=1, help='How many GPUs to use at each worker .'
)
parser.add_argument(
'--tpu', type=str, default=None, help='The Cloud TPU to use for training.'
)
return parser.parse_args()
## main
N_A = 128
T_X = 40
LEARNING_RATE = 0.001
SAMPLE_INPUT = 'two households, both alike in dignity, '
def main():
# args = parse_args()
args = {
'model_dir': 'models',
'train_epochs': 100,
'batch_size': 32,
'distribution_strategy': None,
}
keras.backend.clear_session()
export_path = os.path.join(args['model_dir'], 'saved_model')
# distribution
if args['distribution_strategy'] == 'tpu':
resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu=args.tpu)
if args['tpu'] not in ('', 'local'):
tf.config.experimental_connect_to_cluster(resolver)
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
strategy = tf.distribute.experimental.TPUStrategy(resolver)
strategy_scope = strategy.scope()
else:
strategy_scope = contextlib.nullcontext()
# dataset
data = io.open('./data/shakespeare.txt', encoding='utf-8').read().lower()
vocab = sorted(list(set(data)))
vocab_size = len(vocab)
char_to_ix = dict((c, i) for i, c in enumerate(vocab))
ix_to_char = dict((i, c) for i, c in enumerate(vocab))
ds = build_dataset(data, char_to_ix, T_X, stride=3)
ds = ds.map(lambda x, y: (tf.one_hot(x, vocab_size, axis=-1), y))
ds = ds.shuffle(buffer_size=50000).batch(args['batch_size'])
# model
with strategy_scope:
model = build_model(N_A, T_X, vocab_size)
model.compile(
optimizer=keras.optimizers.Adam(LEARNING_RATE),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'],
)
model.summary()
# training
callbacks = [
# keras.callbacks.ModelCheckpoint(ckpt_full_path, save_weights_only=True),
# keras.callbacks.TensorBoard(log_dir=args.model_dir),
Sampler(SAMPLE_INPUT, vocab_size, char_to_ix, ix_to_char),
]
_ = model.fit(
ds,
epochs=args['train_epochs'],
verbose=1,
callbacks=callbacks,
)
model.save(export_path, include_optimizer=False)
if __name__ == '__main__':
main()
# %%