-
Notifications
You must be signed in to change notification settings - Fork 0
/
inter_infer.py
205 lines (170 loc) · 7.3 KB
/
inter_infer.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
from __future__ import print_function # Use a function definition from future version (say 3.x from 2.7 interpreter)
import os
import re
import time
import cntk as C
import numpy as np
# C.device.try_set_default_device(C.device.cpu())
regex = re.compile(r"^[^\d\W]\w*$", re.UNICODE)
keywords = ["async", "await", "break", "continue", "class", "extends", "constructor", "super", "extends", "const",
"let", "var", "debugger", "delete", "do", "while", "export", "import", "for", "each", "in", "of",
"function", "return", "get", "set", "if", "else", "instanceof", "typeof", "null", "undefined", "switch",
"case", "default", "this", "true", "false", "try", "catch", "finally", "void", "yield", "any", "boolean",
"null", "never", "number", "string", "symbol", "undefined", "void", "as", "is", "enum", "type", "interface",
"abstract", "implements", "static", "readonly", "private", "protected", "public", "declare", "module",
"namespace", "require", "from", "of", "package"]
files = {
'train': {'file': 'data/inter_train.ctf', 'location': 0},
'valid': {'file': 'data/inter_valid.ctf', 'location': 0},
'test': {'file': 'data/inter_test.ctf', 'location': 0},
'source': {'file': 'data/inter_source_wl', 'location': 1},
'target': {'file': 'data/inter_target_wl', 'location': 1}
}
# load dictionaries
source_wl = [line.rstrip('\n') for line in open(files['source']['file'])]
target_wl = [line.rstrip('\n') for line in open(files['target']['file'])]
source_dict = {source_wl[i]: i for i in range(len(source_wl))}
target_dict = {target_wl[i]: i for i in range(len(target_wl))}
# number of words in vocab, slot labels, and intent labels
vocab_size = len(source_dict)
num_labels = len(target_dict)
epoch_size = 16209589
minibatch_size = 3500
emb_dim = 300
hidden_dim = 650
num_epochs = 10
training_log_file = "training_logs/inter-project.txt"
# Create Training log directory
if not os.path.isdir("training_logs"):
os.mkdir("training_logs")
# Create the containers for input feature (x) and the label (y)
x = C.sequence.input_variable(vocab_size, name="x")
y = C.sequence.input_variable(num_labels, name="y")
t = C.sequence.input_variable(hidden_dim, name="t")
def BiRecurrence(fwd, bwd):
F = C.layers.Recurrence(fwd)
G = C.layers.Recurrence(bwd, go_backwards=True)
x = C.placeholder()
apply_x = C.splice(F(x), G(x)) # concatenate the tensors
return apply_x
def create_model():
embed = C.layers.Embedding(emb_dim, name='embed')
encoder = BiRecurrence(C.layers.GRU(hidden_dim // 2), C.layers.GRU(hidden_dim // 2))
recoder = BiRecurrence(C.layers.GRU(hidden_dim // 2), C.layers.GRU(hidden_dim // 2))
project = C.layers.Dense(num_labels, name='classify')
do = C.layers.Dropout(0.5)
def recode(x, t):
inp = embed(x)
inp = C.layers.LayerNormalization()(inp)
enc = encoder(inp)
rec = recoder(enc + t)
proj = project(do(rec))
dec = C.ops.softmax(proj)
return enc, dec
return recode
def criterion(model, labels):
ce = -C.reduce_sum(labels * C.ops.log(model))
errs = C.classification_error(model, labels)
return ce, errs
def enhance_data(data, enc):
guesses = enc.eval({x: data[x]})
inputs = C.ops.argmax(x).eval({x: data[x]})
tables = []
for i in range(len(inputs)):
ts = []
table = {}
counts = {}
for j in range(len(inputs[i])):
inp = int(inputs[i][j])
if inp not in table:
table[inp] = guesses[i][j]
counts[inp] = 1
else:
table[inp] += guesses[i][j]
counts[inp] += 1
for inp in table:
table[inp] /= counts[inp]
for j in range(len(inputs[i])):
inp = int(inputs[i][j])
ts.append(table[inp])
tables.append(np.array(np.float32(ts)))
s = C.io.MinibatchSourceFromData(dict(t=(tables, C.layers.typing.Sequence[C.layers.typing.tensor])))
mems = s.next_minibatch(minibatch_size)
data[t] = mems[s.streams['t']]
def create_trainer():
masked_dec = dec * C.ops.clip(C.ops.argmax(y), 0, 1)
loss, label_error = criterion(masked_dec, y)
loss *= C.ops.clip(C.ops.argmax(y), 0, 1)
lr_schedule = C.learning_parameter_schedule_per_sample([1e-3] * 2 + [5e-4] * 2 + [1e-4], epoch_size=int(epoch_size))
momentum_as_time_constant = C.momentum_as_time_constant_schedule(1000)
learner = C.adam(parameters=dec.parameters,
lr=lr_schedule,
momentum=momentum_as_time_constant,
gradient_clipping_threshold_per_sample=15,
gradient_clipping_with_truncation=True)
progress_printer = C.logging.ProgressPrinter(tag='Training', num_epochs=num_epochs)
trainer = C.Trainer(dec, (loss, label_error), learner, progress_printer)
C.logging.log_number_of_parameters(dec)
return trainer
def create_reader(path, is_training):
return C.io.MinibatchSource(C.io.CTFDeserializer(path, C.io.StreamDefs(
source=C.io.StreamDef(field='S0', shape=vocab_size, is_sparse=True),
slot_labels=C.io.StreamDef(field='S1', shape=num_labels, is_sparse=True)
)), randomize=is_training, max_sweeps=C.io.INFINITELY_REPEAT if is_training else 1)
def validate():
valid_reader = create_reader(files['valid']['file'], is_training=False)
while True:
data = valid_reader.next_minibatch(minibatch_size, input_map={
x: valid_reader.streams.source,
y: valid_reader.streams.slot_labels
})
if not data:
break
enhance_data(data, enc)
trainer.test_minibatch(data)
trainer.summarize_test_progress()
def evaluate():
test_reader = create_reader(files['test']['file'], is_training=False)
while True:
data = test_reader.next_minibatch(minibatch_size, input_map={
x: test_reader.streams.source,
y: test_reader.streams.slot_labels
})
if not data:
break
# Enhance data
enhance_data(data, enc)
# Test model
trainer.test_minibatch(data)
trainer.summarize_test_progress()
def train():
train_reader = create_reader(files['train']['file'], is_training=True)
step = 0
pp = C.logging.ProgressPrinter(freq=10, tag='Training', log_to_file=training_log_file)
for epoch in range(num_epochs):
epoch_end = (epoch + 1) * epoch_size
while step < epoch_end:
data = train_reader.next_minibatch(minibatch_size, input_map={
x: train_reader.streams.source,
y: train_reader.streams.slot_labels
})
# Enhance data
enhance_data(data, enc)
# Train model
trainer.train_minibatch(data)
pp.update_with_trainer(trainer, with_metric=True)
step += data[y].num_samples
pp.epoch_summary(with_metric=True)
trainer.save_checkpoint("models/inter-project-model-" + str(epoch + 1) + ".cntk")
validate()
print("Epoch: " + str(epoch + 1))
evaluate()
model = create_model()
enc, dec = model(x, t)
trainer = create_trainer()
start_time = time.time()
train()
time = time.time() - start_time
print("--- %s seconds ---" % time)
with open(training_log_file, "a+") as log:
log.write("Total time: " + str(time) + " seconds")