-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer.py
346 lines (248 loc) · 10.8 KB
/
transformer.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
'''
This is a sequence to sequence approach to using the Transformer model.
'''
from __future__ import absolute_import, unicode_literals
# TensorFlow
import tensorflow as tf
# TensorFlow Datasets for encoding
import tensorflow_datasets as tfds
# The Transformer model
from model.Transformer import Transformer
from model.CustomSchedule import CustomSchedule
from model.network import create_masks, loss_function
# Utilities
import random
import json
import time
import os
import numpy as np
DIR_PATH = os.path.abspath(os.path.dirname(__file__))
CONFIG_FILE = os.path.join(DIR_PATH, "config.json")
with open(CONFIG_FILE, encoding='utf-8-sig') as fh:
data = json.load(fh)
settings = dict(data)
CKPT_MODEL = settings["checkpoint"]
ENCODE_METHOD = tfds.features.text.SubwordTextEncoder
# Data constraints
MAX_LENGTH = 40
# Hyperparameters
NUM_LAYERS = settings["layers"]
D_MODEL = settings["model-depth"]
DFF = settings["dff"]
NUM_HEADS = settings["attn-heads"]
DROPOUT = settings["dropout"]
# Training settings
EPOCHS = settings["epochs"]
BATCH_SIZE = settings["batch-size"]
# Adam optimizer params
BETA_1 = 0.95
BETA_2 = 0.99
EPSILON = 1e-9
CONTINUE_FROM_CKPT = False
MODEL_NAME = f"t_{NUM_LAYERS}_{NUM_HEADS}_{D_MODEL}_{DFF}_{int(time.time())}"
if not CKPT_MODEL == False:
# If a model name is given train from that model
CONTINUE_FROM_CKPT = True
MODEL_NAME = CKPT_MODEL
CHECKPOINT_PATH = os.path.join(DIR_PATH,
f"checkpoints/{CKPT_MODEL}/")
MODEL_PATH = os.path.join(DIR_PATH,
f"checkpoints/{MODEL_NAME}/")
# Random seed for repeatability
SEED = settings["seed"]
random.seed(SEED)
tf.random.set_seed(SEED)
def filter_max_length(x, y, max_length=MAX_LENGTH):
return tf.logical_and(tf.size(x) <= max_length,
tf.size(y) <= max_length)
def print_epoch(what, clear=False):
# Overwrite the line to see live updated results
print(f"{what}\r", end="")
if clear:
# Clear the line being overwritten by print_epoch
print("\n")
if __name__ == "__main__":
print("Starting Transformer training...")
train_X = []
train_y = []
# AND Boolean logic
examples = [([0, 0], 0),
([0, 1], 0),
([1, 0], 0),
([1, 1], 1)]
print(f"Shuffling data with seed: {SEED}\n")
random.shuffle(examples)
# Get training examples
for example in examples:
try:
Xs, ys = example
train_X.append(str(Xs))
train_y.append(str(ys))
except:
pass
assert len(train_X) == len(train_y)
print(f"Set to train with {len(train_X)} examples.\n")
print("Building vocabulary...\n")
# Convert arrays to TensorFlow constants
train_X_const = tf.constant(train_X)
train_y_const = tf.constant(train_y)
# Turn the constants into TensorFlow Datasets
training_dataset = tf.data.Dataset.from_tensor_slices((train_X_const,
train_y_const))
tokenizer_X = ENCODE_METHOD.build_from_corpus((X.numpy() for X, _ in training_dataset),
target_vocab_size=2**13)
tokenizer_y = ENCODE_METHOD.build_from_corpus((y.numpy() for _, y in training_dataset),
target_vocab_size=2**13)
print("\nEncoding inputs...")
def encode(lang1, lang2):
lang1 = [tokenizer_X.vocab_size] + tokenizer_X.encode(
lang1.numpy()) + [tokenizer_X.vocab_size + 1]
lang2 = [tokenizer_y.vocab_size] + tokenizer_y.encode(
lang2.numpy()) + [tokenizer_y.vocab_size + 1]
return lang1, lang2
def tf_encode(txt, eq):
return tf.py_function(encode, [txt, eq], [tf.int64, tf.int64])
training_dataset = training_dataset.map(tf_encode)
training_dataset = training_dataset.filter(filter_max_length)
# Cache the dataset to memory to get a speedup while reading from it.
training_dataset = training_dataset.cache()
# Batch the data
training_dataset = training_dataset.padded_batch(BATCH_SIZE,
padded_shapes=([-1], [-1]))
training_dataset = training_dataset.prefetch(tf.data.experimental.AUTOTUNE)
input_vocab_size = tokenizer_X.vocab_size + 2
target_vocab_size = tokenizer_y.vocab_size + 2
print("...done.")
print("\nDefining the Transformer model...")
# Using the Adam optimizer
optimizer = tf.keras.optimizers.Adam(CustomSchedule(D_MODEL),
beta_1=BETA_1,
beta_2=BETA_2,
epsilon=EPSILON)
train_loss = tf.keras.metrics.Mean(name="train_loss")
train_acc = tf.keras.metrics.SparseCategoricalAccuracy(
name="train_acc")
transformer = Transformer(NUM_LAYERS,
D_MODEL,
NUM_HEADS,
DFF,
input_vocab_size,
target_vocab_size,
DROPOUT)
print("...done.")
print("\nTraining...\n")
# Model saving
ckpt = tf.train.Checkpoint(transformer=transformer,
optimizer=optimizer)
if CONTINUE_FROM_CKPT:
# Load last checkpoint
ckpt_manager = tf.train.CheckpointManager(ckpt,
CHECKPOINT_PATH,
max_to_keep=999)
else:
if not os.path.isdir(f"checkpoints"):
os.mkdir(f"checkpoints")
if not os.path.isdir(f"checkpoints/{MODEL_NAME}"):
os.mkdir(f"checkpoints/{MODEL_NAME}")
ckpt_manager = tf.train.CheckpointManager(ckpt,
MODEL_PATH,
max_to_keep=999)
# If a checkpoint exists, restore the latest checkpoint.
if ckpt_manager.latest_checkpoint and CONTINUE_FROM_CKPT:
ckpt.restore(ckpt_manager.latest_checkpoint).expect_partial()
print(f"Restored from {CHECKPOINT_PATH} checkpoint!\n")
@tf.function
def train_step(inp, tar):
tar_inp = tar[:, :-1]
tar_real = tar[:, 1:]
enc_padding_mask, combined_mask, dec_padding_mask = create_masks(inp,
tar_inp)
with tf.GradientTape() as tape:
# predictions.shape == (batch_size, seq_len, vocab_size)
predictions, _ = transformer(inp, tar_inp,
True,
enc_padding_mask,
combined_mask,
dec_padding_mask)
loss = loss_function(tar_real, predictions)
gradients = tape.gradient(loss,
transformer.trainable_variables)
optimizer.apply_gradients(zip(gradients,
transformer.trainable_variables))
train_loss(loss)
train_acc(tar_real, predictions)
# Train
for epoch in range(EPOCHS):
start = time.time()
train_loss.reset_states()
train_acc.reset_states()
for (batch, (inp, tar)) in enumerate(training_dataset):
train_step(inp, tar)
if batch % 10 == 0:
print_epoch("Epoch {}/{} Batch {} Loss {:.4f} Accuracy {:.4f}".format(
epoch + 1,
EPOCHS,
batch,
train_loss.result(),
train_acc.result()))
print_epoch("Epoch {}/{} Batch {} Loss {:.4f} Accuracy {:.4f}".format(
epoch + 1,
EPOCHS,
batch,
train_loss.result(),
train_acc.result()), clear=True)
# Calculate the time the epoch took to complete
# The first epoch seems to take significantly longer than the others
print(f"Epoch took {int(time.time() - start)}s\n")
if epoch == (EPOCHS - 1):
# Save a checkpoint of model weights
ckpt_save_path = ckpt_manager.save()
print(f'Saved {MODEL_NAME} to {ckpt_save_path}\n')
# Delete old config
os.remove(CONFIG_FILE)
settings["checkpoint"] = MODEL_NAME
# Write the config to use the checkpoint on next run
with open(CONFIG_FILE, mode="w") as fh:
json.dump(settings, fh)
break
print("...done.")
def evaluate(inp_sentence):
start_token = [tokenizer_X.vocab_size]
end_token = [tokenizer_X.vocab_size + 1]
inp_sentence = start_token + \
tokenizer_X.encode(inp_sentence) + end_token
encoder_input = tf.expand_dims(inp_sentence, 0)
decoder_input = [tokenizer_y.vocab_size]
output = tf.expand_dims(decoder_input, 0)
for i in range(MAX_LENGTH):
enc_padding_mask, combined_mask, dec_padding_mask = create_masks(encoder_input,
output)
# predictions.shape == (batch_size, seq_len, vocab_size)
predictions, attention_weights = transformer(encoder_input,
output,
False,
enc_padding_mask,
combined_mask,
dec_padding_mask)
# Select the last word from the seq_len dimension
predictions = predictions[:, -1:, :] # (batch_size, 1, vocab_size)
predicted_id = tf.cast(tf.argmax(predictions, axis=-1), tf.int32)
# Return the result if the predicted_id is equal to the end token
if tf.equal(predicted_id, tokenizer_y.vocab_size + 1):
return tf.squeeze(output, axis=0), attention_weights
# Concatentate the predicted_id to the output which is given to the decoder
# as its input.
output = tf.concat([output, predicted_id], axis=-1)
return tf.squeeze(output, axis=0), attention_weights
def translate(sentence):
result, attention_weights = evaluate(sentence)
prediction = tokenizer_y.decode([i for i in result
if i < tokenizer_y.vocab_size])
return prediction
print(f"\nTesting AND logic...\n")
print(f"[0, 0] -> {translate('[0, 0]')}")
print(f"[0, 1] -> {translate('[0, 1]')}")
print(f"[1, 0] -> {translate('[1, 0]')}")
print(f"[1, 1] -> {translate('[1, 1]')}")
print(f"\n...done.")
print("\nExiting.")