-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadngo.py
637 lines (544 loc) · 23.8 KB
/
loadngo.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# -*- coding: utf-8 -*-
"""LoadnGo.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1qD_f-Jta35f7Ix1SGLUccbKD4sJbWbFF
"""
# Commented out IPython magic to ensure Python compatibility.
import math
import os
import gc
import time
import re
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import backend as K
import tensorflow_datasets as tfds
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
# Parameters for our model
INPUT_COLUMN = 'input'
TARGET_COLUMN = 'target'
MAX_VOCAB_SIZE = 2**14
BATCH_SIZE = 16 # Batch size for training.
EPOCHS = 0 # Number of epochs to train for.
MAX_LENGTH = 50
# Global parameters
root_folder=''
checkpoint_folder = ""
# Variable for data directory
checkpoint_path = os.path.abspath(os.path.join(root_folder, checkpoint_folder))
# Both train and test set are in the root data directory
import nltk
from nltk.corpus import indian
from nltk.tag import tnt
import string
nltk.download('punkt')
nltk.download('indian')
import random
def subword_tokenize(corpus, vocab_size, max_length):
# Create the vocabulary using Subword tokenization
tokenizer_corpus = tfds.deprecated.text.SubwordTextEncoder.build_from_corpus(
corpus, target_vocab_size=vocab_size)
# Get the final vocab size, adding the eos and sos tokens
num_words = tokenizer_corpus.vocab_size + 2
# Set eos and sos token
sos_token = [num_words-2]
eos_token = [num_words-1]
# Tokenize the corpus
sentences = [sos_token + tokenizer_corpus.encode(sentence) + eos_token
for sentence in corpus]
# Identify the index of the sentences longer than max length
idx_to_remove = [count for count, sent in enumerate(sentences)
if len(sent) > max_length]
#Pad the sentences
sentences = tf.keras.preprocessing.sequence.pad_sequences(sentences,
value=0,
padding='post',
maxlen=max_length)
return sentences, tokenizer_corpus, num_words, sos_token, eos_token, idx_to_remove
import pickle
with open('./data/encoder_inputs.pickle','rb') as f:
encoder_inputs=pickle.load(f)
with open('./data/tokenizer_inputs.pickle','rb') as f:
tokenizer_inputs=pickle.load(f)
with open('./data/num_words_inputs.pickle','rb') as f:
num_words_inputs=pickle.load(f)
with open('./data/sos_token_input.pickle','rb') as f:
sos_token_input=pickle.load(f)
with open('./data/eos_token_input.pickle','rb') as f:
eos_token_input=pickle.load(f)
with open('./data/del_idx_inputs.pickle','rb') as f:
del_idx_inputs=pickle.load(f)
with open('./data/decoder_outputs.pickle','rb') as f:
decoder_outputs=pickle.load(f)
with open('./data/tokenizer_outputs.pickle','rb') as f:
tokenizer_outputs=pickle.load(f)
with open('./data/num_words_output.pickle','rb') as f:
num_words_output=pickle.load(f)
with open('./data/sos_token_output.pickle','rb') as f:
sos_token_output=pickle.load(f)
with open('./data/eos_token_output.pickle','rb') as f:
eos_token_output=pickle.load(f)
with open('./data/del_idx_outputs.pickle','rb') as f:
del_idx_outputs=pickle.load(f)
input_data=[]
target_data=[]
with open('./data/correct1.txt') as f:
for line in f:
target_data.append(line)
with open('./data/wrong1.txt') as f:
for line in f:
input_data.append(line)
NUM_SAMPLES=len(input_data)
# Define a dataset
dataset = tf.data.Dataset.from_tensor_slices(
(encoder_inputs, decoder_outputs))
dataset = dataset.shuffle(len(input_data), reshuffle_each_iteration=True).batch(
BATCH_SIZE, drop_remainder=True)
dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)
def scaled_dot_product_attention(queries, keys, values, mask):
# Calculate the dot product, QK_transpose
product = tf.matmul(queries, keys, transpose_b=True)
# Get the scale factor
keys_dim = tf.cast(tf.shape(keys)[-1], tf.float32)
# Apply the scale factor to the dot product
scaled_product = product / tf.math.sqrt(keys_dim)
# Apply masking when it is requiered
if mask is not None:
scaled_product += (mask * -1e9)
# dot product with Values
attention = tf.matmul(tf.nn.softmax(scaled_product, axis=-1), values)
return attention
class MultiHeadAttention(layers.Layer):
def __init__(self, n_heads):
super(MultiHeadAttention, self).__init__()
self.n_heads = n_heads
def build(self, input_shape):
self.d_model = input_shape[-1]
assert self.d_model % self.n_heads == 0
# Calculate the dimension of every head or projection
self.d_head = self.d_model // self.n_heads
# Set the weight matrices for Q, K and V
self.query_lin = layers.Dense(units=self.d_model)
self.key_lin = layers.Dense(units=self.d_model)
self.value_lin = layers.Dense(units=self.d_model)
# Set the weight matrix for the output of the multi-head attention W0
self.final_lin = layers.Dense(units=self.d_model)
def split_proj(self, inputs, batch_size): # inputs: (batch_size, seq_length, d_model)
# Set the dimension of the projections
shape = (batch_size,
-1,
self.n_heads,
self.d_head)
# Split the input vectors
splited_inputs = tf.reshape(inputs, shape=shape) # (batch_size, seq_length, nb_proj, d_proj)
return tf.transpose(splited_inputs, perm=[0, 2, 1, 3]) # (batch_size, nb_proj, seq_length, d_proj)
def call(self, queries, keys, values, mask):
# Get the batch size
batch_size = tf.shape(queries)[0]
# Set the Query, Key and Value matrices
queries = self.query_lin(queries)
keys = self.key_lin(keys)
values = self.value_lin(values)
# Split Q, K y V between the heads or projections
queries = self.split_proj(queries, batch_size)
keys = self.split_proj(keys, batch_size)
values = self.split_proj(values, batch_size)
# Apply the scaled dot product
attention = scaled_dot_product_attention(queries, keys, values, mask)
# Get the attention scores
attention = tf.transpose(attention, perm=[0, 2, 1, 3])
# Concat the h heads or projections
concat_attention = tf.reshape(attention,
shape=(batch_size, -1, self.d_model))
# Apply W0 to get the output of the multi-head attention
outputs = self.final_lin(concat_attention)
return outputs
class PositionalEncoding(layers.Layer):
def __init__(self):
super(PositionalEncoding, self).__init__()
def get_angles(self, pos, i, d_model): # pos: (seq_length, 1) i: (1, d_model)
angles = 1 / np.power(10000., (2*(i//2)) / np.float32(d_model))
return pos * angles # (seq_length, d_model)
def call(self, inputs):
# input shape batch_size, seq_length, d_model
seq_length = inputs.shape.as_list()[-2]
d_model = inputs.shape.as_list()[-1]
# Calculate the angles given the input
angles = self.get_angles(np.arange(seq_length)[:, np.newaxis],
np.arange(d_model)[np.newaxis, :],
d_model)
# Calculate the positional encodings
angles[:, 0::2] = np.sin(angles[:, 0::2])
angles[:, 1::2] = np.cos(angles[:, 1::2])
# Expand the encodings with a new dimension
pos_encoding = angles[np.newaxis, ...]
return inputs + tf.cast(pos_encoding, tf.float32)
class EncoderLayer(layers.Layer):
def __init__(self, FFN_units, n_heads, dropout_rate):
super(EncoderLayer, self).__init__()
# Hidden units of the feed forward component
self.FFN_units = FFN_units
# Set the number of projectios or heads
self.n_heads = n_heads
# Dropout rate
self.dropout_rate = dropout_rate
def build(self, input_shape):
self.d_model = input_shape[-1]
# Build the multihead layer
self.multi_head_attention = MultiHeadAttention(self.n_heads)
self.dropout_1 = layers.Dropout(rate=self.dropout_rate)
# Layer Normalization
self.norm_1 = layers.LayerNormalization(epsilon=1e-6)
# Fully connected feed forward layer
self.ffn1_relu = layers.Dense(units=self.FFN_units, activation="relu")
self.ffn2 = layers.Dense(units=self.d_model)
self.dropout_2 = layers.Dropout(rate=self.dropout_rate)
# Layer normalization
self.norm_2 = layers.LayerNormalization(epsilon=1e-6)
def call(self, inputs, mask, training):
# Forward pass of the multi-head attention
attention = self.multi_head_attention(inputs,
inputs,
inputs,
mask)
attention = self.dropout_1(attention, training=training)
# Call to the residual connection and layer normalization
attention = self.norm_1(attention + inputs)
# Call to the FC layer
outputs = self.ffn1_relu(attention)
outputs = self.ffn2(outputs)
outputs = self.dropout_2(outputs, training=training)
# Call to residual connection and the layer normalization
outputs = self.norm_2(outputs + attention)
return outputs
class Encoder(layers.Layer):
def __init__(self,
n_layers,
FFN_units,
n_heads,
dropout_rate,
vocab_size,
d_model,
name="encoder"):
super(Encoder, self).__init__(name=name)
self.n_layers = n_layers
self.d_model = d_model
# The embedding layer
self.embedding = layers.Embedding(vocab_size, d_model)
# Positional encoding layer
self.pos_encoding = PositionalEncoding()
self.dropout = layers.Dropout(rate=dropout_rate)
# Stack of n layers of multi-head attention and FC
self.enc_layers = [EncoderLayer(FFN_units,
n_heads,
dropout_rate)
for _ in range(n_layers)]
def call(self, inputs, mask, training):
# Get the embedding vectors
outputs = self.embedding(inputs)
# Scale the embeddings by sqrt of d_model
outputs *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
# Positional encodding
outputs = self.pos_encoding(outputs)
outputs = self.dropout(outputs, training)
# Call the stacked layers
for i in range(self.n_layers):
outputs = self.enc_layers[i](outputs, mask, training)
return outputs
class DecoderLayer(layers.Layer):
def __init__(self, FFN_units, n_heads, dropout_rate):
super(DecoderLayer, self).__init__()
self.FFN_units = FFN_units
self.n_heads = n_heads
self.dropout_rate = dropout_rate
def build(self, input_shape):
self.d_model = input_shape[-1]
# Self multi head attention, causal attention
self.multi_head_causal_attention = MultiHeadAttention(self.n_heads)
self.dropout_1 = layers.Dropout(rate=self.dropout_rate)
self.norm_1 = layers.LayerNormalization(epsilon=1e-6)
# Multi head attention, encoder-decoder attention
self.multi_head_enc_dec_attention = MultiHeadAttention(self.n_heads)
self.dropout_2 = layers.Dropout(rate=self.dropout_rate)
self.norm_2 = layers.LayerNormalization(epsilon=1e-6)
# Feed foward
self.ffn1_relu = layers.Dense(units=self.FFN_units,
activation="relu")
self.ffn2 = layers.Dense(units=self.d_model)
self.dropout_3 = layers.Dropout(rate=self.dropout_rate)
self.norm_3 = layers.LayerNormalization(epsilon=1e-6)
def call(self, inputs, enc_outputs, mask_1, mask_2, training):
# Call the masked causal attention
attention = self.multi_head_causal_attention(inputs,
inputs,
inputs,
mask_1)
attention = self.dropout_1(attention, training)
# Residual connection and layer normalization
attention = self.norm_1(attention + inputs)
# Call the encoder-decoder attention
attention_2 = self.multi_head_enc_dec_attention(attention,
enc_outputs,
enc_outputs,
mask_2)
attention_2 = self.dropout_2(attention_2, training)
# Residual connection and layer normalization
attention_2 = self.norm_2(attention_2 + attention)
# Call the Feed forward
outputs = self.ffn1_relu(attention_2)
outputs = self.ffn2(outputs)
outputs = self.dropout_3(outputs, training)
# Residual connection and layer normalization
outputs = self.norm_3(outputs + attention_2)
return outputs
class Decoder(layers.Layer):
def __init__(self,
n_layers,
FFN_units,
n_heads,
dropout_rate,
vocab_size,
d_model,
name="decoder"):
super(Decoder, self).__init__(name=name)
self.d_model = d_model
self.n_layers = n_layers
# Embedding layer
self.embedding = layers.Embedding(vocab_size, d_model)
# Positional encoding layer
self.pos_encoding = PositionalEncoding()
self.dropout = layers.Dropout(rate=dropout_rate)
# Stacked layers of multi-head attention and feed forward
self.dec_layers = [DecoderLayer(FFN_units,
n_heads,
dropout_rate)
for _ in range(n_layers)]
def call(self, inputs, enc_outputs, mask_1, mask_2, training):
# Get the embedding vectors
outputs = self.embedding(inputs)
# Scale by sqrt of d_model
outputs *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
# Positional encodding
outputs = self.pos_encoding(outputs)
outputs = self.dropout(outputs, training)
# Call the stacked layers
for i in range(self.n_layers):
outputs = self.dec_layers[i](outputs,
enc_outputs,
mask_1,
mask_2,
training)
return outputs
class Transformer(tf.keras.Model):
def __init__(self,
vocab_size_enc,
vocab_size_dec,
d_model,
n_layers,
FFN_units,
n_heads,
dropout_rate,
name="transformer"):
super(Transformer, self).__init__(name=name)
# Build the encoder
self.encoder = Encoder(n_layers,
FFN_units,
n_heads,
dropout_rate,
vocab_size_enc,
d_model)
# Build the decoder
self.decoder = Decoder(n_layers,
FFN_units,
n_heads,
dropout_rate,
vocab_size_dec,
d_model)
# build the linear transformation and softmax function
self.last_linear = layers.Dense(units=vocab_size_dec, name="lin_ouput")
def create_padding_mask(self, seq): #seq: (batch_size, seq_length)
# Create the mask for padding
mask = tf.cast(tf.math.equal(seq, 0), tf.float32)
return mask[:, tf.newaxis, tf.newaxis, :]
def create_look_ahead_mask(self, seq):
# Create the mask for the causal attention
seq_len = tf.shape(seq)[1]
look_ahead_mask = 1 - tf.linalg.band_part(tf.ones((seq_len, seq_len)), -1, 0)
return look_ahead_mask
def call(self, enc_inputs, dec_inputs, training):
# Create the padding mask for the encoder
enc_mask = self.create_padding_mask(enc_inputs)
# Create the mask for the causal attention
dec_mask_1 = tf.maximum(
self.create_padding_mask(dec_inputs),
self.create_look_ahead_mask(dec_inputs)
)
# Create the mask for the encoder-decoder attention
dec_mask_2 = self.create_padding_mask(enc_inputs)
# Call the encoder
enc_outputs = self.encoder(enc_inputs, enc_mask, training)
# Call the decoder
dec_outputs = self.decoder(dec_inputs,
enc_outputs,
dec_mask_1,
dec_mask_2,
training)
# Call the Linear and Softmax functions
outputs = self.last_linear(dec_outputs)
return outputs
def loss_function(target, pred):
mask = tf.math.logical_not(tf.math.equal(target, 0))
loss_ = loss_object(target, pred)
mask = tf.cast(mask, dtype=loss_.dtype)
loss_ *= mask
return tf.reduce_mean(loss_)
class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
def __init__(self, d_model, warmup_steps=4000):
super(CustomSchedule, self).__init__()
self.d_model = tf.cast(d_model, tf.float32)
self.warmup_steps = warmup_steps
def __call__(self, step):
arg1 = tf.math.rsqrt(step)
arg2 = step * (self.warmup_steps**-1.5)
return tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2)
def main_train(dataset, transformer, n_epochs, print_every=50):
''' Train the transformer model for n_epochs using the data generator dataset'''
losses = []
accuracies = []
# In every epoch
for epoch in range(n_epochs):
print("Starting epoch {}".format(epoch+1))
start = time.time()
# Reset the losss and accuracy calculations
train_loss.reset_states()
train_accuracy.reset_states()
# Get a batch of inputs and targets
for (batch, (enc_inputs, targets)) in enumerate(dataset):
# Set the decoder inputs
dec_inputs = targets[:, :-1]
# Set the target outputs, right shifted
dec_outputs_real = targets[:, 1:]
with tf.GradientTape() as tape:
# Call the transformer and get the predicted output
predictions = transformer(enc_inputs, dec_inputs, True)
# Calculate the loss
loss = loss_function(dec_outputs_real, predictions)
# Update the weights and optimizer
gradients = tape.gradient(loss, transformer.trainable_variables)
optimizer.apply_gradients(zip(gradients, transformer.trainable_variables))
# Save and store the metrics
train_loss(loss)
train_accuracy(dec_outputs_real, predictions)
if batch % print_every == 0:
losses.append(train_loss.result())
accuracies.append(train_accuracy.result())
print("Epoch {} Batch {} Loss {:.4f} Accuracy {:.4f}".format(
epoch+1, batch, train_loss.result(), train_accuracy.result()))
# Checkpoint the model on every epoch
ckpt_save_path = ckpt_manager.save()
print("Saving checkpoint for epoch {} in {}".format(epoch+1,
ckpt_save_path))
print("Time for 1 epoch: {} secs\n".format(time.time() - start))
return losses, accuracies
def predictText(inp_sentence, tokenizer_in, tokenizer_out, target_max_len):
# Tokenize the input sequence using the tokenizer_in
inp_sentence = sos_token_input + tokenizer_in.encode(inp_sentence) + eos_token_input
enc_input = tf.expand_dims(inp_sentence, axis=0)
# Set the initial output sentence to sos
out_sentence = sos_token_output
# Reshape the output
output = tf.expand_dims(out_sentence, axis=0)
# For max target len tokens
for _ in range(target_max_len):
# Call the transformer and get the logits
predictions = transformer(enc_input, output, False) #(1, seq_length, VOCAB_SIZE_ES)
# Extract the logists of the next word
prediction = predictions[:, -1:, :]
# The highest probability is taken
predicted_id = tf.cast(tf.argmax(prediction, axis=-1), tf.int32)
# Check if it is the eos token
if predicted_id == eos_token_output:
return tf.squeeze(output, axis=0)
# Concat the predicted word to the output sequence
output = tf.concat([output, predicted_id], axis=-1)
return tf.squeeze(output, axis=0)
from strsimpy.cosine import Cosine
def get_cosine(s0,s1):
cosine = Cosine(2)
p0 = cosine.get_profile(s0)
p1 = cosine.get_profile(s1)
return cosine.similarity_profiles(p0, p1)
# Set hyperparamters for the model
D_MODEL = 512 # 512
N_LAYERS = 6 # 6
FFN_UNITS = 2048 # 2048
N_HEADS = 8 # 8
DROPOUT_RATE = 0.1 # 0.1
# Clean the session
tf.keras.backend.clear_session()
# Create the Transformer model
transformer = Transformer(vocab_size_enc=num_words_inputs,
vocab_size_dec=num_words_output,
d_model=D_MODEL,
n_layers=N_LAYERS,
FFN_units=FFN_UNITS,
n_heads=N_HEADS,
dropout_rate=DROPOUT_RATE)
# Define a categorical cross entropy loss
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True,
reduction="none")
# Define a metric to store the mean loss of every epoch
train_loss = tf.keras.metrics.Mean(name="train_loss")
# Define a matric to save the accuracy in every epoch
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name="train_accuracy")
# Create the scheduler for learning rate decay
leaning_rate = CustomSchedule(D_MODEL)
# Create the Adam optimizer
optimizer = tf.keras.optimizers.Adam(leaning_rate,
beta_1=0.9,
beta_2=0.98,
epsilon=1e-9)
#Create the Checkpoint
# ckpt = tf.train.Checkpoint(transformer=transformer,
# optimizer=optimizer)
def translate(sentence):
# Get the predicted sequence for the input sentence
output = predictText(sentence, tokenizer_inputs, tokenizer_outputs, MAX_LENGTH).numpy()
# Transform the sequence of tokens to a sentence
predicted_sentence = tokenizer_outputs.decode(
[i for i in output if i < sos_token_output]
)
cos = get_cosine(sentence, predicted_sentence)
if cos>0.5:
return predicted_sentence
else:
return sentence
def splitter(sentences):
result = []
temp=""
for i in range(len(sentences)):
if sentences[i]=='?' or sentences[i]=='.' or sentences[i]=='|' or sentences[i]=='!' or sentences[i]=='।':
if i+1<len(sentences) and (sentences[i+1]!="'" or sentences[i+1]!='"'):
temp+=sentences[i]
result.append(temp)
temp=""
else:
temp+=sentences[i]
result.append(temp)
return result
# ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=5)
# if ckpt_manager.latest_checkpoint:
# ckpt.restore(ckpt_manager.latest_checkpoint)
# print("Last checkpoint restored.")
transformer.load_weights("./weights/")
import random
for i in range(1):
temp = random.randint(0,300)
print("input:\t\t",input_data[temp])
pred=translate(input_data[temp])
print("prediction:\t\t",pred)
# print(len(pred))
print("target:\t\t",target_data[temp])