-
Notifications
You must be signed in to change notification settings - Fork 3
/
w4_unittest.py
253 lines (213 loc) · 7.91 KB
/
w4_unittest.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
import numpy as np
import trax
#from trax import layers as tl
#from trax.fastmath import numpy as fastnp
#from trax.supervised import training
# UNIT TEST for UNQ_C1
def test_get_conversation(target):
data = {'file1.json': {'log':[{'text': 'hi'},
{'text': 'hello'},
{'text': 'nice'}]},
'file2.json':{'log':[{'text': 'a b'},
{'text': ''},
{'text': 'good '},
{'text': 'no?'}]}}
res1 = target('file1.json', data)
res2 = target('file2.json', data)
expected1 = ' Person 1: hi Person 2: hello Person 1: nice'
expected2 = ' Person 1: a b Person 2: Person 1: good Person 2: no?'
success = 0
fails = 0
try:
assert res1 == expected1
success += 1
except ValueError:
print('Error in test 1 \nResult : ', res1, 'x \nExpected: ', expected1)
fails += 1
try:
assert res2 == expected2
success += 1
except:
print('Error in test 2 \nResult : ', res2, ' \nExpected: ', expected2)
fails += 1
if fails == 0:
print("\033[92m All tests passed")
else:
print('\033[92m', success," Tests passed")
print('\033[91m', fails, " Tests failed")
# UNIT TEST for UNQ_C2
def test_reversible_layer_forward(target):
f1 = lambda x: x + 2
g1 = lambda x: x * 3
f2 = lambda x: x + 1
g2 = lambda x: x * 2
input_vector1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
expected1 = np.array([8, 10, 12, 14, 29, 36, 43, 50])
input_vector2 = np.array([1] * 128)
expected2 = np.array([3] * 64 + [7] * 64)
success = 0
fails = 0
try:
res = target(input_vector1, f1, g1)
assert isinstance(res, np.ndarray)
success += 1
except:
print('Wrong type! Output is not of type np.ndarray')
fails += 1
try:
res = target(input_vector1, f1, g1)
assert np.allclose(res, expected1)
success += 1
except ValueError:
print('Error in test 1 \nResult : ', res, 'x \nExpected: ', expected1)
fails += 1
try:
res = target(input_vector2, f2, g2)
assert np.allclose(res, expected2)
success += 1
except:
print('Error in test 2 \nResult : ', res, ' \nExpected: ', expected2)
fails += 1
if fails == 0:
print("\033[92m All tests passed")
else:
print('\033[92m', success," Tests passed")
print('\033[91m', fails, " Tests failed")
# UNIT TEST for UNQ_C3
def test_reversible_layer_reverse(target):
f1 = lambda x: x + 2
g1 = lambda x: x * 3
f2 = lambda x: x + 1
g2 = lambda x: x * 2
input_vector1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
expected1 = np.array([-3, 0, 3, 6, 2, 0, -2, -4])
input_vector2 = np.array([1] * 128)
expected2 = np.array([1] * 64 + [-1] * 64)
success = 0
fails = 0
try:
res = target(input_vector1, f1, g1)
assert isinstance(res, np.ndarray)
success += 1
except:
print('Wrong type! Output is not of type np.ndarray')
fails += 1
try:
res = target(input_vector1, f1, g1)
assert np.allclose(res, expected1)
success += 1
except ValueError:
print('Error in test 1 \nResult : ', res, 'x \nExpected: ', expected1)
fails += 1
try:
res = target(input_vector2, f2, g2)
assert np.allclose(res, expected2)
success += 1
except:
print('Error in test 2 \nResult : ', res, ' \nExpected: ', expected2)
fails += 1
if fails == 0:
print("\033[92m All tests passed")
else:
print('\033[92m', success," Tests passed")
print('\033[91m', fails, " Tests failed")
# UNIT TEST for UNQ_C4
def test_ReformerLM(target):
test_cases = [
{
"name":"layer_len_check",
"expected":11,
"error":"We found {} layers in your model. It should be 11.\nCheck the LSTM stack before the dense layer"
},
{
"name":"simple_test_check",
"expected":"Serial[ShiftRight(1)Embedding_train_512DropoutPositionalEncodingDup_out2ReversibleSerial_in2_out2[ReversibleHalfResidualV2_in2_out2[Serial[LayerNorm]SelfAttention]ReversibleSwap_in2_out2ReversibleHalfResidualV2_in2_out2[Serial[LayerNormDense_2048DropoutFastGeluDense_512Dropout]]ReversibleSwap_in2_out2ReversibleHalfResidualV2_in2_out2[Serial[LayerNorm]SelfAttention]ReversibleSwap_in2_out2ReversibleHalfResidualV2_in2_out2[Serial[LayerNormDense_2048DropoutFastGeluDense_512Dropout]]ReversibleSwap_in2_out2]Concatenate_in2LayerNormDropoutDense_trainLogSoftmax]",
"error":"The ReformerLM is not defined properly."
}
]
temp_model = target('train')
success = 0
fails = 0
for test_case in test_cases:
try:
if test_case['name'] == "simple_test_check":
assert test_case["expected"] == str(temp_model).replace(' ', '').replace('\n','')
success += 1
if test_case['name'] == "layer_len_check":
if test_case["expected"] == len(temp_model.sublayers):
success += 1
else:
print(test_case["error"].format(len(temp_model.sublayers)))
fails += 1
except:
print(test_case['error'])
fails += 1
if fails == 0:
print("\033[92m All tests passed")
else:
print('\033[92m', success," Tests passed")
print('\033[91m', fails, " Tests failed")
# UNIT TEST for UNQ_C5
def test_tasks(train_task, eval_task):
target = train_task
success = 0
fails = 0
# Test the labeled data parameter for train_task
try:
strlabel = str(target._labeled_data)
assert ("generator" in strlabel) and ("add_loss_weights" in strlabel)
success += 1
except:
fails += 1
print("Wrong labeled data parameter in train_task")
# Test the cross entropy loss data parameter
try:
strlabel = str(target._loss_layer)
assert(strlabel == "CrossEntropyLoss_in3")
success += 1
except:
fails += 1
print("Wrong loss functions. CrossEntropyLoss_in3 was expected")
# Test the optimizer parameter
try:
assert(isinstance(target.optimizer, trax.optimizers.adam.Adam))
success += 1
except:
fails += 1
print("Wrong optimizer")
# Test the schedule parameter
try:
assert(isinstance(target._lr_schedule,trax.supervised.lr_schedules._BodyAndTail))
success += 1
except:
fails += 1
print("Wrong learning rate schedule type")
# Test the _n_steps_per_checkpoint parameter
try:
assert(target._n_steps_per_checkpoint==10)
success += 1
except:
fails += 1
print("Wrong checkpoint step frequency")
target = eval_task
# Test the labeled data parameter for eval_task
try:
strlabel = str(target._labeled_data)
assert ("generator" in strlabel) and ("add_loss_weights" in strlabel)
success += 1
except:
fails += 1
print("Wrong labeled data parameter in eval_task")
# Test the metrics in eval_task
try:
strlabel = str(target._metrics).replace(' ', '')
assert(strlabel == "[CrossEntropyLoss_in3,Accuracy_in3]")
success += 1
except:
fails += 1
print(f"Wrong metrics. found {strlabel} but expected [CrossEntropyLoss_in3,Accuracy_in3]")
if fails == 0:
print("\033[92m All tests passed")
else:
print('\033[92m', success," Tests passed")
print('\033[91m', fails, " Tests failed")