-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathsanity_check.py
316 lines (267 loc) · 12.9 KB
/
sanity_check.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
CS224N 2018-19: Homework 5
sanity_check.py: sanity checks for assignment 5
Usage:
sanity_check.py 1e
sanity_check.py 1f
sanity_check.py 1g
sanity_check.py 1h
sanity_check.py 1i
sanity_check.py 1j
sanity_check.py 2a
sanity_check.py 2b
sanity_check.py 2c
sanity_check.py 2d
"""
import json
import math
import pickle
import sys
import time
import numpy as np
from docopt import docopt
from typing import List, Tuple, Dict, Set, Union
from tqdm import tqdm
from utils import pad_sents_char, read_corpus, batch_iter
from vocab import Vocab, VocabEntry
from char_decoder import CharDecoder
from nmt_model import NMT
import torch
import torch.nn as nn
import torch.nn.utils
#----------
# CONSTANTS
#----------
BATCH_SIZE = 5
EMBED_SIZE = 3
HIDDEN_SIZE = 3
DROPOUT_RATE = 0.0
class DummyVocab():
def __init__(self):
self.char2id = json.load(open('./sanity_check_en_es_data/char_vocab_sanity_check.json', 'r'))
self.id2char = {id: char for char, id in self.char2id.items()}
self.char_unk = self.char2id['<unk>']
self.start_of_word = self.char2id["{"]
self.end_of_word = self.char2id["}"]
def question_1e_sanity_check():
""" Sanity check for words2charindices function.
"""
print ("-"*80)
print("Running Sanity Check for Question 1e: words2charindices()")
print ("-"*80)
vocab = VocabEntry()
print('Running test on small list of sentences')
sentences = [["a", "b", "c?"], ["~d~", "c", "b", "a"]]
small_ind = vocab.words2charindices(sentences)
small_ind_gold = [[[1, 30, 2], [1, 31, 2], [1, 32, 70, 2]], [[1, 85, 33, 85, 2], [1, 32, 2], [1, 31, 2], [1, 30, 2]]]
assert(small_ind == small_ind_gold), \
"small test resulted in indices list {:}, expected {:}".format(small_ind, small_ind_gold)
print('Running test on large list of sentences')
tgt_sents = [['<s>', "Let's", 'start', 'by', 'thinking', 'about', 'the', 'member', 'countries', 'of', 'the', 'OECD,', 'or', 'the', 'Organization', 'of', 'Economic', 'Cooperation', 'and', 'Development.', '</s>'], ['<s>', 'In', 'the', 'case', 'of', 'gun', 'control,', 'we', 'really', 'underestimated', 'our', 'opponents.', '</s>'], ['<s>', 'Let', 'me', 'share', 'with', 'those', 'of', 'you', 'here', 'in', 'the', 'first', 'row.', '</s>'], ['<s>', 'It', 'suggests', 'that', 'we', 'care', 'about', 'the', 'fight,', 'about', 'the', 'challenge.', '</s>'], ['<s>', 'A', 'lot', 'of', 'numbers', 'there.', 'A', 'lot', 'of', 'numbers.', '</s>']]
tgt_ind = vocab.words2charindices(tgt_sents)
tgt_ind_gold = pickle.load(open('./sanity_check_en_es_data/1e_tgt.pkl', 'rb'))
assert(tgt_ind == tgt_ind_gold), "target vocab test resulted in indices list {:}, expected {:}".format(tgt_ind, tgt_ind_gold)
print("All Sanity Checks Passed for Question 1e: words2charindices()!")
print ("-"*80)
def question_1f_sanity_check():
""" Sanity check for pad_sents_char() function.
"""
print ("-"*80)
print("Running Sanity Check for Question 1f: Padding")
print ("-"*80)
vocab = VocabEntry()
print("Running test on a list of sentences")
sentences = [['Human:', 'What', 'do', 'we', 'want?'], ['Computer:', 'Natural', 'language', 'processing!'], ['Human:', 'When', 'do', 'we', 'want', 'it?'], ['Computer:', 'When', 'do', 'we', 'want', 'what?']]
word_ids = vocab.words2charindices(sentences)
padded_sentences = pad_sents_char(word_ids, 0)
gold_padded_sentences = torch.load('./sanity_check_en_es_data/gold_padded_sentences.pkl')
assert padded_sentences == gold_padded_sentences, "Sentence padding is incorrect: it should be:\n {} but is:\n{}".format(gold_padded_sentences, padded_sentences)
print("Sanity Check Passed for Question 1f: Padding!")
print("-"*80)
def question_1g_test():
""" Custom simple test for to_input_tensor_char() function.
"""
print ("-"*80)
print("Running Sanity Check for Question 1g: Padding")
print ("-"*80)
vocab = VocabEntry()
print("Running test on a list of sentences")
sentences = [['Human:', 'What', 'do', 'we', 'want?'],
['Computer:', 'Natural', 'language', 'processing!'],
['Human:', 'When', 'do', 'we', 'want', 'it?'],
['Computer:', 'When', 'do', 'we', 'want', 'what?']]
gold_shape = torch.Size([6, 4, 21]) # (max sentence length, batch size, max word length)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
input_tensor = vocab.to_input_tensor_char(sentences, device)
# print("We get torch tensor:\n", input_tensor)
assert input_tensor.shape == gold_shape, "Ouput tensor shape is incorrect: it should be:\n {} but is:\n{}".format(gold_shape, input_tensor.shape)
print("Sanity Check Passed for Question 1g: Padding!")
print("-"*80)
def question_1h_test():
""" Custom simple test for Highway module.
(TODO: More detail sanity checks rather than just check the shape)
"""
from highway import Highway
print ("-"*80)
print("Running Sanity Check for Question 1h: Highway")
print ("-"*80)
print("Running test on a random tensor")
sentence_length = 6
max_word_length = 21
tensor_shape = [sentence_length, BATCH_SIZE, max_word_length, EMBED_SIZE] # (max sentence length, batch size, max word length, embedding dimension)
gold_shape = torch.Size(tensor_shape)
highway = Highway(EMBED_SIZE, dropout_rate=0)
test_tensor = torch.randn(tensor_shape)
output_tensor = highway(test_tensor)
assert output_tensor.shape == gold_shape, "Ouput tensor shape is incorrect: it should be:\n {} but is:\n{}".format(gold_shape, output_tensor.shape)
print("Sanity Check Passed for Question 1h: Highway!")
print("-"*80)
def question_1i_test():
""" Custom simple test for CNN module.
(TODO: More detail sanity checks rather than just check the shape)
"""
from cnn import CNN
print ("-"*80)
print("Running Sanity Check for Question 1i: CNN")
print ("-"*80)
print("Running test on a random tensor")
max_word_length = 21
kernel_size = 5
char_embed = EMBED_SIZE
word_embed = EMBED_SIZE + 1
tensor_shape = [BATCH_SIZE, char_embed, max_word_length] # (batch size, character embedding dimension, max word length)
output_shape = [BATCH_SIZE, word_embed]
gold_shape = torch.Size(output_shape)
cnn = CNN(char_embed, word_embed, max_word_length, kernel_size)
test_tensor = torch.randn(tensor_shape)
output_tensor = cnn(test_tensor)
assert output_tensor.shape == gold_shape, "Ouput tensor shape is incorrect: it should be:\n {} but is:\n{}".format(gold_shape, output_tensor.shape)
print("Sanity Check Passed for Question 1i: CNN!")
print("-"*80)
def question_1j_sanity_check(model):
""" Sanity check for model_embeddings.py
basic shape check
"""
print ("-"*80)
print("Running Sanity Check for Question 1j: Model Embedding")
print ("-"*80)
sentence_length = 10
max_word_length = 21
inpt = torch.zeros(sentence_length, BATCH_SIZE, max_word_length, dtype=torch.long)
ME_source = model.model_embeddings_source
output = ME_source.forward(inpt)
output_expected_size = [sentence_length, BATCH_SIZE, EMBED_SIZE]
assert(list(output.size()) == output_expected_size), "output shape is incorrect: it should be:\n {} but is:\n{}".format(output_expected_size, list(output.size()))
print("Sanity Check Passed for Question 1j: Model Embedding!")
print("-"*80)
def question_2a_sanity_check(decoder, char_vocab):
""" Sanity check for CharDecoder.__init__()
basic shape check
"""
print ("-"*80)
print("Running Sanity Check for Question 2a: CharDecoder.__init__()")
print ("-"*80)
assert(decoder.charDecoder.input_size == EMBED_SIZE), "Input dimension is incorrect:\n it should be {} but is: {}".format(EMBED_SIZE, decoder.charDecoder.input_size)
assert(decoder.charDecoder.hidden_size == HIDDEN_SIZE), "Hidden dimension is incorrect:\n it should be {} but is: {}".format(HIDDEN_SIZE, decoder.charDecoder.hidden_size)
assert(decoder.char_output_projection.in_features == HIDDEN_SIZE), "Input dimension is incorrect:\n it should be {} but is: {}".format(HIDDEN_SIZE, decoder.char_output_projection.in_features)
assert(decoder.char_output_projection.out_features == len(char_vocab.char2id)), "Output dimension is incorrect:\n it should be {} but is: {}".format(len(char_vocab.char2id), decoder.char_output_projection.out_features)
assert(decoder.decoderCharEmb.num_embeddings == len(char_vocab.char2id)), "Number of embeddings is incorrect:\n it should be {} but is: {}".format(len(char_vocab.char2id), decoder.decoderCharEmb.num_embeddings)
assert(decoder.decoderCharEmb.embedding_dim == EMBED_SIZE), "Embedding dimension is incorrect:\n it should be {} but is: {}".format(EMBED_SIZE, decoder.decoderCharEmb.embedding_dim)
print("Sanity Check Passed for Question 2a: CharDecoder.__init__()!")
print("-"*80)
def question_2b_sanity_check(decoder, char_vocab):
""" Sanity check for CharDecoder.forward()
basic shape check
"""
print ("-"*80)
print("Running Sanity Check for Question 2b: CharDecoder.forward()")
print ("-"*80)
sequence_length = 4
inpt = torch.zeros(sequence_length, BATCH_SIZE, dtype=torch.long)
logits, (dec_hidden1, dec_hidden2) = decoder.forward(inpt)
logits_expected_size = [sequence_length, BATCH_SIZE, len(char_vocab.char2id)]
dec_hidden_expected_size = [1, BATCH_SIZE, HIDDEN_SIZE]
assert(list(logits.size()) == logits_expected_size), "Logits shape is incorrect:\n it should be {} but is:\n{}".format(logits_expected_size, list(logits.size()))
assert(list(dec_hidden1.size()) == dec_hidden_expected_size), "Decoder hidden state shape is incorrect:\n it should be {} but is: {}".format(dec_hidden_expected_size, list(dec_hidden1.size()))
assert(list(dec_hidden2.size()) == dec_hidden_expected_size), "Decoder hidden state shape is incorrect:\n it should be {} but is: {}".format(dec_hidden_expected_size, list(dec_hidden2.size()))
print("Sanity Check Passed for Question 2b: CharDecoder.forward()!")
print("-"*80)
def question_2c_sanity_check(decoder):
""" Sanity check for CharDecoder.train_forward()
basic shape check
"""
print ("-"*80)
print("Running Sanity Check for Question 2c: CharDecoder.train_forward()")
print ("-"*80)
sequence_length = 4
inpt = torch.zeros(sequence_length, BATCH_SIZE, dtype=torch.long)
loss = decoder.train_forward(inpt)
assert(list(loss.size()) == []), "Loss should be a scalar but its shape is: {}".format(list(loss.size()))
print("Sanity Check Passed for Question 2c: CharDecoder.train_forward()!")
print("-"*80)
def question_2d_sanity_check(decoder):
""" Sanity check for CharDecoder.decode_greedy()
basic shape check
"""
print ("-"*80)
print("Running Sanity Check for Question 2d: CharDecoder.decode_greedy()")
print ("-"*80)
sequence_length = 4
inpt = torch.zeros(1, BATCH_SIZE, HIDDEN_SIZE, dtype=torch.float)
initialStates = (inpt, inpt)
device = decoder.char_output_projection.weight.device
decodedWords = decoder.decode_greedy(initialStates, device)
assert(len(decodedWords) == BATCH_SIZE), "Length of decodedWords should be {} but is: {}".format(BATCH_SIZE, len(decodedWords))
print("Sanity Check Passed for Question 2d: CharDecoder.decode_greedy()!")
print("-"*80)
def main():
""" Main func.
"""
args = docopt(__doc__)
# Check Python & PyTorch Versions
assert (sys.version_info >= (3, 5)), "Please update your installation of Python to version >= 3.5"
assert(torch.__version__ >= "1.0.0"), "Please update your installation of PyTorch. You have {} and you should have version 1.0.0".format(torch.__version__)
# Seed the Random Number Generators
seed = 1234
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed * 13 // 7)
vocab = Vocab.load('./sanity_check_en_es_data/vocab_sanity_check.json')
# Create NMT Model
model = NMT(
embed_size=EMBED_SIZE,
hidden_size=HIDDEN_SIZE,
dropout_rate=DROPOUT_RATE,
vocab=vocab)
char_vocab = DummyVocab()
# Initialize CharDecoder
decoder = CharDecoder(
hidden_size=HIDDEN_SIZE,
char_embedding_size=EMBED_SIZE,
target_vocab=char_vocab)
if args['1e']:
question_1e_sanity_check()
elif args['1f']:
question_1f_sanity_check()
elif args['1g']:
question_1g_test()
elif args['1h']:
question_1h_test()
elif args['1i']:
question_1i_test()
elif args['1j']:
question_1j_sanity_check(model)
elif args['2a']:
question_2a_sanity_check(decoder, char_vocab)
elif args['2b']:
question_2b_sanity_check(decoder, char_vocab)
elif args['2c']:
question_2c_sanity_check(decoder)
elif args['2d']:
question_2d_sanity_check(decoder)
else:
raise RuntimeError('invalid run mode')
if __name__ == '__main__':
main()