-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_tf.py
130 lines (99 loc) · 4.81 KB
/
test_tf.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import unittest
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import tensorflow as tf
import tensorlayer as tl
from tqdm import tqdm
from sklearn.utils import shuffle
from v2.transformer import Transformer
from v2.models_params import TINY_PARAMS as params
from tests.utils import CustomTestCase
from tensorlayer.cost import cross_entropy_seq
from utils import metrics
from models import optimizer
import time
class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
def __init__(self, d_model, warmup_steps=5):
super(CustomSchedule, self).__init__()
self.d_model = d_model
self.d_model = tf.cast(self.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)
class Model_SEQ2SEQ_Test(CustomTestCase):
@classmethod
def setUpClass(cls):
cls.batch_size = 16
cls.embedding_size = 32
cls.trainX = np.random.randint(low=2, high=50, size=(50, 10))
cls.trainY = np.random.randint(low=2, high=50, size=(50, 10))
cls.trainX[:,-1] = 1
cls.trainY[:,-1] = 1
# Parameters
cls.src_len = len(cls.trainX)
cls.tgt_len = len(cls.trainY)
assert cls.src_len == cls.tgt_len
cls.num_epochs = 1000
cls.n_step = cls.src_len // cls.batch_size
@classmethod
def tearDownClass(cls):
pass
def test_basic_simpleSeq2Seq(self):
model_ = Transformer(params)
self.vocab_size = params["vocab_size"]
# optimizer_ = optimizer.LazyAdam(
# params["learning_rate"],
# params["optimizer_adam_beta1"],
# params["optimizer_adam_beta2"],
# epsilon=params["optimizer_adam_epsilon"])
learning_rate = CustomSchedule(params["hidden_size"])
optimizer_ = tf.optimizers.Adam(learning_rate=0.01)
# optimizer_ = tf.keras.optimizers.Adam(learning_rate, beta_1=0.9, beta_2=0.98,
# epsilon=1e-9)
# optimizer_ = optimizer.LazyAdam(learning_rate, beta_1=0.9, beta_2=0.98,
# epsilon=1e-9)
for epoch in range(self.num_epochs):
trainX, trainY = shuffle(self.trainX, self.trainY)
total_loss, n_iter = 0, 0
for X, Y in tqdm(tl.iterate.minibatches(inputs=trainX, targets=trainY, batch_size=self.batch_size,
shuffle=False), total=self.n_step,
desc='Epoch[{}/{}]'.format(epoch + 1, self.num_epochs), leave=False):
with tf.GradientTape() as tape:
targets = Y
output = model_(inputs = [X, Y], training=True)
# print(len(model_.trainable_weights))
# print(model_.trainable_weights)
# exit()
# print(logits.shape, Y.shape)
logits = metrics.MetricLayer(self.vocab_size)([output, targets])
logits, loss = metrics.LossLayer(self.vocab_size, 0.1)([logits, targets])
# logits = tf.keras.layers.Lambda(lambda x: x, name="logits")(logits)
# print(time.time()-start)
# output = tf.reshape(output, [-1, output.shape[-1]])
# print(", ".join([t.name for t in model_.trainable_weights]))
# layer_normalization_print = [x for x in [t.name for t in model_.trainable_weights] if "feed_forward_network" in x ]
# print(", ".join(x for x in [t.name for t in model_.trainable_weights] if "feed_forward_network" in x ))
# print("number of layers : ", len(model_.trainable_weights))
# exit()
# loss = cross_entropy_seq(logits=output, target_seqs=Y)
grad = tape.gradient(loss, model_.trainable_weights)
# print(grad)
# exit()
optimizer_.apply_gradients(zip(grad, model_.trainable_weights))
# print(time.time()-start)
total_loss += loss
n_iter += 1
test_sample = trainX[0:2, :]
top_n = 1
for i in range(top_n):
prediction = model_(inputs = [test_sample], training=False)
print("Prediction: >>>>> ", prediction["outputs"], "\n Target: >>>>> ", trainY[0:2, :], "\n\n")
# printing average loss after every epoch
print('Epoch [{}/{}]: loss {:.4f}'.format(epoch + 1, self.num_epochs, total_loss / n_iter))
if __name__ == '__main__':
unittest.main()