-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_reddit_jokes_sw_tf_ver2_gpt_upsampled.py
305 lines (250 loc) · 9.27 KB
/
train_reddit_jokes_sw_tf_ver2_gpt_upsampled.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
import time
import numpy as np
import pandas as pd
import pickle as pkl
import byte_pair_encoding as bpe
import tensorflow as tf
import tensorflow_addons as tfa
import tf_ver2_gpt_keras_ups as gpt
# Define the weight update step for multiple sub-batches. #
def sub_batch_train_step(
model, sub_batch_sz,
x_encode, x_output, optimizer,
learning_rate=1.0e-3, grad_clip=1.0):
optimizer.lr.assign(learning_rate)
batch_size = x_encode.shape[0]
if batch_size <= sub_batch_sz:
sub_batch = 1
elif batch_size % sub_batch_sz == 0:
sub_batch = int(batch_size / sub_batch_sz)
else:
sub_batch = int(batch_size / sub_batch_sz) + 1
model_params = model.trainable_variables
acc_gradients = [tf.zeros_like(var) for var in model_params]
tot_losses = 0.0
for n_sub in range(sub_batch):
id_st = n_sub*sub_batch_sz
if n_sub != (sub_batch-1):
id_en = (n_sub+1)*sub_batch_sz
else:
id_en = batch_size
tmp_encode = x_encode[id_st:id_en, :]
tmp_output = x_output[id_st:id_en, :]
with tf.GradientTape() as grad_tape:
output_logits = model(tmp_encode, training=True)
tmp_losses = tf.reduce_sum(tf.reduce_sum(
tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=tmp_output, logits=output_logits), axis=1))
# Accumulate the gradients. #
tot_losses += tmp_losses
tmp_gradients = grad_tape.gradient(
tmp_losses, model_params)
acc_gradients = [tf.add(
acc_grad, grad) for acc_grad, grad \
in zip(acc_gradients, tmp_gradients)]
# Update using the optimizer. #
avg_losses = tot_losses / batch_size
acc_gradients = [tf.math.divide_no_nan(
acc_grad, batch_size) for acc_grad in acc_gradients]
clipped_gradients, _ = tf.clip_by_global_norm(
acc_gradients, grad_clip)
optimizer.apply_gradients(
zip(clipped_gradients, model_params))
return avg_losses
# Model Parameters. #
p_keep = 0.9
p_drop = 1.0 - p_keep
batch_size = 256
sub_batch = 64
seq_length = 30
kernel_sz = 5
num_heads = 4
num_layers = 3
gradient_clip = 1.00
maximum_iter = 10000
restore_flag = True
save_step = 500
warmup_steps = 5000
display_step = 100
anneal_step = 2500
anneal_rate = 0.75
hidden_size = 256
ffwd_size = 4*hidden_size
warmup_flag = True
cooling_step = 1000
model_ckpt_dir = "TF_Models/gpt_ups_sw_reddit"
train_loss_file = "train_loss_gpt_ups_sw_reddit.csv"
# Load the data. #
tmp_pkl_file = "/home/Data/reddit_jokes/"
tmp_pkl_file += "reddit_jokes_subword_v1.pkl"
with open(tmp_pkl_file, "rb") as tmp_load_file:
full_data = pkl.load(tmp_load_file)
subword_vocab = pkl.load(tmp_load_file)
idx_2_subword = pkl.load(tmp_load_file)
subword_2_idx = pkl.load(tmp_load_file)
vocab_size = len(subword_vocab)
print("Vocabulary Size:", str(vocab_size) + ".")
# Set the number of threads to use. #
tf.config.threading.set_intra_op_parallelism_threads(1)
tf.config.threading.set_inter_op_parallelism_threads(1)
tmp_data = []
for tmp_row in full_data:
if len(tmp_row) > 1 and \
len(tmp_row) <= seq_length:
tmp_data.append(tmp_row)
num_data = len(tmp_data)
SOS_token = subword_2_idx["<SOS>"]
EOS_token = subword_2_idx["<EOS>"]
PAD_token = subword_2_idx["<PAD>"]
UNK_token = subword_2_idx["<UNK>"]
print("Total of", str(len(tmp_data)), "rows loaded.")
# Build the GPT. #
print("Building the GPT Upsampling Model.")
start_time = time.time()
gpt_model = gpt.GPTUpsample(
num_layers, num_heads, hidden_size,
ffwd_size, vocab_size, seq_length,
kernel_sz, rate1=0.0, rate2=p_drop)
gpt_optimizer = tfa.optimizers.AdamW(
weight_decay=1.0e-4)
elapsed_time = (time.time() - start_time) / 60
print("GPT Keras Model Built",
"(" + str(elapsed_time) + " mins).")
# Print the model summary. #
tmp_zero = np.zeros(
[sub_batch, seq_length], dtype=np.int32)
tmp_pred = gpt_model(tmp_zero, training=True)
print(gpt_model.summary())
print("-" * 50)
del tmp_zero, tmp_pred
# Create the model checkpoint. #
ckpt = tf.train.Checkpoint(
step=tf.Variable(0),
gpt_model=gpt_model,
gpt_optimizer=gpt_optimizer)
manager = tf.train.CheckpointManager(
ckpt, model_ckpt_dir, max_to_keep=1)
if restore_flag:
ckpt.restore(manager.latest_checkpoint)
if manager.latest_checkpoint:
print("Model restored from {}".format(
manager.latest_checkpoint))
else:
print("Error: No latest checkpoint found.")
train_loss_df = pd.read_csv(train_loss_file)
train_loss_list = [tuple(
train_loss_df.iloc[x].values) \
for x in range(len(train_loss_df))]
else:
print("Training a new model.")
train_loss_list = []
# Train the Transformer model. #
tmp_out_seq = np.zeros(
[batch_size, seq_length+1], dtype=np.int32)
# Warmup learning schedule. #
n_iter = ckpt.step.numpy().astype(np.int32)
if warmup_flag:
step_min = float(max(n_iter, warmup_steps))**(-0.5)
learning_rate = float(hidden_size)**(-0.5) * step_min
else:
initial_lr = 0.001
learning_rate = max(
anneal_rate**(n_iter // anneal_step)*initial_lr, 1.0e-5)
print("-" * 50)
print("Training the GPT Upsampling Network",
"(" + str(n_iter) + " iterations).")
print(str(num_data), "training samples.")
print("-" * 50)
# Update the neural network's weights. #
tot_loss = 0.0
start_tm = time.time()
while n_iter < maximum_iter:
if warmup_flag:
step_min = float(max(n_iter, warmup_steps))**(-0.5)
learning_rate = float(hidden_size)**(-0.5) * step_min
else:
if n_iter % anneal_step == 0:
anneal_factor = np.power(
anneal_rate, int(n_iter / anneal_step))
learning_rate = max(
anneal_factor*initial_lr, 1.0e-6)
# Select a sample from the data. #
batch_sample = np.random.choice(
num_data, size=batch_size, replace=False)
tmp_out_seq[:, :] = PAD_token
for n_index in range(batch_size):
tmp_index = batch_sample[n_index]
tmp_p_idx = tmp_data[tmp_index]
n_input = len(tmp_p_idx)
tmp_out_seq[n_index, :n_input] = tmp_p_idx
tmp_out_seq[n_index, n_input] = EOS_token
del tmp_p_idx
# Set the training data. #
tmp_input = tmp_out_seq[:, :-1]
tmp_output = tmp_out_seq[:, 1:]
tmp_loss = sub_batch_train_step(
gpt_model, sub_batch,
tmp_input, tmp_output, gpt_optimizer,
learning_rate=learning_rate, grad_clip=gradient_clip)
n_iter += 1
ckpt.step.assign_add(1)
tot_loss += tmp_loss.numpy()
if n_iter % display_step == 0:
end_tm = time.time()
avg_loss = tot_loss / display_step
tot_loss = 0.0
elapsed_tm = (end_tm - start_tm) / 60.0
sample_test = np.random.choice(num_data, size=1)
tmp_p_index = tmp_data[sample_test[0]]
in_phrase = bpe.bp_decode(
tmp_p_index, idx_2_subword)
in_phrase = " ".join(in_phrase).replace(
"<", "").replace(">", "")
n_tokens = len(tmp_p_index)
n_sample = min(np.random.randint(
1, high=n_tokens-1), int(n_tokens/2))
tmp_test = np.array(tmp_p_index).reshape(1, -1)
tmp_test = tmp_test.astype(np.int32)
tmp_infer = gpt_model.infer(
tmp_test[:, :n_sample])
del sample_test, n_tokens
gen_phrase = bpe.bp_decode(
tmp_infer[0].numpy(), idx_2_subword)
gen_phrase = " ".join(gen_phrase).replace(
"<", "").replace(">", "")
test_phrase = bpe.bp_decode(
tmp_p_index[:n_sample], idx_2_subword)
test_phrase = " ".join(test_phrase).replace(
"<", "").replace(">", "")
del tmp_p_index
print("Iteration", str(n_iter) + ".")
print("Elapsed Time:", str(elapsed_tm), "mins.")
print("Gradient Clip:", str(gradient_clip) + ".")
print("Learning Rate:", str(learning_rate) + ".")
print("Average Xent Loss:", str(avg_loss) + ".")
print("")
print("Input Phrase:")
print(test_phrase)
print("Generated Phrase:")
print(gen_phrase)
print("Actual Phrase:")
print(in_phrase)
del n_sample
train_loss_list.append((n_iter, avg_loss))
start_tm = time.time()
print("-" * 50)
# Save the model. #
if n_iter % save_step == 0:
# Save the model. #
save_path = manager.save()
print("Saved model to {}".format(save_path))
tmp_df_losses = pd.DataFrame(
train_loss_list, columns=["n_iter", "xent_loss"])
tmp_df_losses.to_csv(train_loss_file, index=False)
del tmp_df_losses
# Cool the GPU. #
if n_iter % cooling_step == 0:
print("Cooling GPU for 2 minutes.")
time.sleep(120)
print("Resume Training.")
print("-" * 50)