-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.py
467 lines (448 loc) · 23.1 KB
/
model.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
#!/usr/bin/python3.5
import tensorflow as tf
import numpy as np
from sklearn.metrics import roc_auc_score
import os
class grainedDKTModel:
def __init__(
self,
batch_size=16,
vec_length_in=0, # number of questions in dataset
vec_length_out=0, # output size
initial_learning_rate=0.001,
final_learning_rate=0.00001,
n_hidden=200, # number of hidden units in the hidden layer
embedding_size=200,
keep_prob=0.5,
epsilon=0.001,
is_training=True,
random_embedding=False,
multi_granined = True,
multi_granined_out = True,
n_categories=0):
# Rules
assert keep_prob > 0 and keep_prob <= 1, "keep_prob parameter should be in (0, 1]"
# Inputs: to be received from the outside
Xs = tf.placeholder(tf.int32, shape=[batch_size, None], name='Xs_input')
if multi_granined_out:
Ys = tf.placeholder(tf.float32, shape=[batch_size, None, vec_length_out], name='Ys_input')
else:
Ys = tf.placeholder(tf.float32, shape=[batch_size, None, vec_length_out], name='Ys_input')
targets = tf.placeholder(tf.float32, shape=[batch_size, None], name='targets_input')
sequence_length = tf.placeholder(tf.int32, shape=[batch_size], name='sequence_length_input')
categories = tf.placeholder(tf.int32, shape=[batch_size, None], name='input_categories')
# Global parameters initialized
global_step = tf.Variable(0, trainable=False, name='global_step')
learning_rate = tf.train.polynomial_decay(initial_learning_rate, global_step, 5000, final_learning_rate, name='learning_rate')
# LSTM parameters initialized
w = tf.Variable(tf.truncated_normal([n_hidden, vec_length_out], stddev=1.0/np.sqrt(vec_length_out)), name='Weight') # Weight
b = tf.Variable(tf.truncated_normal([vec_length_out], stddev=1.0/np.sqrt(vec_length_out)), name='Bias') # Bias
embeddings = tf.Variable(tf.random_uniform([2 * vec_length_in + 2, embedding_size], -1.0, 1.0), name='X_Embeddings')
cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden) # rnn cell
### incorrect try 00
# rnn_layers = [tf.nn.rnn_cell.BasicLSTMCell(size) for size in [n_hidden, n_categories, n_hidden]] # rnn layer
# cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers)
###
initial_state = cell.zero_state(batch_size, tf.float32)
# LSTM Training options initialized
if random_embedding:
if multi_granined:
category_id_embedding = tf.one_hot(categories, n_categories)
skill_id_embedding = tf.nn.embedding_lookup(embeddings, Xs, name='Xs_embedded') # Xs embedded
inputsX = tf.concat([category_id_embedding, skill_id_embedding], 2)
else:
inputsX = tf.nn.embedding_lookup(embeddings, Xs, name='Xs_embedded') # Xs embedded
else:
# indices = Xs
if multi_granined:
category_id_embedding = tf.one_hot(categories, n_categories)
skill_id_embedding = tf.one_hot(Xs, 2 * vec_length_in + 2)
inputsX = tf.concat([category_id_embedding, skill_id_embedding], 2)
else:
inputsX = tf.one_hot(Xs, 2 * vec_length_in + 2)
outputs, state = tf.nn.dynamic_rnn(cell, inputsX, sequence_length, initial_state=initial_state, dtype=tf.float32)
if is_training and keep_prob < 1:
outputs = tf.nn.dropout(outputs, keep_prob)
outputs_flat = tf.reshape(outputs, shape=[-1, n_hidden], name='Outputs')
# print "output shape = {0}, output flat shape = {1}, state shape = {2}".format(tf.shape(outputs), tf.shape(outputs_flat), tf.shape(state))
logits = tf.reshape(tf.nn.xw_plus_b(outputs_flat, w, b), shape=[batch_size,-1, vec_length_out], name='Logits')
# could be other ways like totalloss = alpha * small_category_loss + beta * big_category_loss
# that'll be two preds: for the gross / fine grained skill
# wouldn't it be easily implemented if we do:
# Ys = [ 0.... beta ... 0 | 0 ... alpha ... ]
# and then do pred.tf.reduce_sum ?
pred = tf.reduce_max(logits*Ys, axis=2)
loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=pred, labels=targets)
mask = tf.sign(tf.abs(pred))
loss_masked = mask*loss
loss_masked_by_s = tf.reduce_sum(loss_masked, axis=1)
mean_loss = tf.reduce_mean(loss_masked_by_s / tf.to_float(sequence_length), name='mean_loss')
# Optimizer defined
optimizer = tf.train.AdamOptimizer(
learning_rate=learning_rate, \
epsilon=epsilon, name='train_op') \
.minimize(mean_loss,global_step=global_step)
# Saver defined
saver = tf.train.Saver()
# test inputsX, test batch_size, test sequence_length, test initial state, etc. could be different
# LSTM Validation options
test_outputs, test_state = tf.nn.dynamic_rnn(cell,inputsX,sequence_length,initial_state)
test_outputs_flat = tf.reshape(test_outputs, shape=[-1,n_hidden], name='test_output')
test_logits = tf.reshape(tf.nn.xw_plus_b(test_outputs_flat,w,b),shape=[batch_size,-1,vec_length_out], name='test_logits')
test_pred = tf.sigmoid(tf.reduce_max(test_logits*Ys, axis=2), name='test_predict')
test_predall = tf.nn.softmax(test_logits, name='test_predall')
# assigning the attributes
self._isTraining = is_training
self._Xs = Xs
self._Ys = Ys
self._targets = targets
self._seqlen = sequence_length
self._loss = mean_loss
self._train = optimizer
self._saver = saver
self._pred = test_pred
self._predall = test_predall
self._categories = categories
@property
def Xs(self):
return self._Xs
@property
def Ys(self):
return self._Ys
@property
def categories(self):
return self._categories
@property
def targets(self):
return self._targets
@property
def seq_len(self):
return self._seqlen
@property
def loss(self):
return self._loss
@property
def train_op(self):
return self._train
@property
def saver(self):
return self._saver
@property
def predict(self):
return self._pred
@property
def predall(self):
return self._predall
class BatchGenerator:
def __init__(self, data, batch_size, id_encoding, vec_length_in, vec_length_out, n_categories, random_embedding=False, skill_to_category_dict=None, multi_granined_out=True):
self.data = sorted(data, key = lambda x: x[0])
self.batch_size = batch_size
self.id_encoding = id_encoding
self.vec_length_in = vec_length_in
self.vec_length_out = vec_length_out
self.skill_to_category_dict = skill_to_category_dict
self.data_size = len(data)
self.random_embedding = random_embedding
self.multi_granined_out = multi_granined_out
self.cursor = 0 # cursor of the current batch's starting index
self.n_categories = n_categories
def one_hot(self, hot, size):
vec = np.zeros(size)
vec[hot] = 1.0
return vec
def combined_one_hot(self, hot1, size1, hot2, size2):
vec = np.zeros(size1 + size2)
vec[hot1] = 1.0
vec[hot2 + size1] = 1.0
return vec
def reset(self):
self.cursor = 0
def next_batch(self):
qa_sequences = []
len_sequences = []
max_sequence_len = 0
for i in range(self.batch_size):
tmp_sequence = self.data[self.cursor][1]
tmp_sequence_len = len(tmp_sequence)
qa_sequences.append(tmp_sequence)
len_sequences.append(tmp_sequence_len)
if tmp_sequence_len > max_sequence_len:
max_sequence_len = tmp_sequence_len
self.cursor = (self.cursor + 1) % self.data_size
# initialize the Xs and Ys
Xs = np.zeros((self.batch_size, max_sequence_len),dtype=np.int32)
if self.multi_granined_out:
Ys = np.zeros((self.batch_size, max_sequence_len, self.vec_length_out + self.n_categories), dtype=np.int32)
else:
Ys = np.zeros((self.batch_size, max_sequence_len, self.vec_length_out), dtype=np.int32)
targets = np.zeros((self.batch_size, max_sequence_len),dtype=np.int32)
categories = np.zeros((self.batch_size, max_sequence_len),dtype=np.int32)
for i, sequence in enumerate(qa_sequences):
padding_length = max_sequence_len - len(sequence)
# s in sequence: s[0] - question id; s[1] - correctness
# Xs[i] = np.pad([2 + self.id_encoding[s[0]] + s[1] * self.vec_length_in for s in sequence[:-1]],
# (1, padding_length), 'constant', constant_values=(1,0))
Xs[i] = np.pad([2 + self.id_encoding[s[0]] + s[1] * self.vec_length_in for s in sequence[:-1]],
(1, padding_length), 'constant', constant_values=(1,0))
if self.multi_granined_out:
Ys[i] = np.pad([self.combined_one_hot(self.skill_to_category_dict[s[0]], self.n_categories, self.id_encoding[s[0]]%self.vec_length_out, self.vec_length_out) for s in sequence],
((0, padding_length), (0, 0)), 'constant', constant_values=0)
else:
Ys[i] = np.pad([self.one_hot(self.id_encoding[s[0]]%self.vec_length_out, self.vec_length_out) for s in sequence],
((0, padding_length), (0, 0)), 'constant', constant_values=0)
targets[i] = np.pad([s[1] for s in sequence],
(0, padding_length), 'constant', constant_values=0)
categories[i] = np.pad([self.skill_to_category_dict[s[0]] for s in sequence[:-1]],
(1, padding_length), 'constant', constant_values=(1,0))
# print self.multi_granined_out
# print Xs.shape
# print Ys.shape
return Xs, Ys, targets, len_sequences, categories
def run(session,
train_batchgen, test_batchgen,
option="epoch", n_epoch=1, n_step=5001,
keep_prob = 0.5,
report_loss_interval=100, report_score_interval=500,
model_saved_path='./model.ckpt',
random_embedding=False,
embedding_size=200,
multi_granined=True,
n_categories=0,
steps_to_test=0,
out_folder='./',
out_file='out.csv',
n_hidden_units = 200,
record_performance=True,
initial_learning_rate=0.001,
final_learning_rate=0.00001,
multi_granined_out=True):
assert option in ['step', 'epoch'], "Run with either epochs or steps"
if steps_to_test == 0:
steps_to_test = test_batchgen.data_size//test_batchgen.batch_size
# assert steps_to_test > 0, "Test set too small"
performance_table_path = os.path.join(out_folder, out_file)
out_file_csv = open(performance_table_path, 'a')
out_file_csv.close()
if os.stat(performance_table_path).st_size == 0:
with open(performance_table_path, 'a') as out_file_csv:
out_file_csv.write("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}\n".format( \
'n_hidden_units', 'step', 'epoch', 'batch_size', 'embedding_size', \
'keep_prob', 'random_embedding', 'multi_granined', 'multi_granined_out', \
'initial_learning_rate', 'final_learning_rate', 'AUC'))
def calc_score(m):
auc_sum = 0.
test_batchgen.reset()
for i in range(steps_to_test):
test_batch_Xs, test_batch_Ys, test_batch_labels, test_batch_sequence_lengths, test_batch_caegories = test_batchgen.next_batch()
test_feed_dict= {m.Xs : test_batch_Xs, m.Ys : test_batch_Ys,
m.seq_len : test_batch_sequence_lengths,
m.targets : test_batch_labels,
m.categories : test_batch_caegories}
pred = session.run([m.predict], feed_dict=test_feed_dict)
label_list = test_batch_labels.reshape(-1)
pred_list = np.array(pred).reshape(-1)
# print [abs(d) for d in np.array(label_list) - np.array(pred_list)]
# accuracy = sum([abs(d) for d in np.array(label_list) - np.array(pred_list)]) / len(pred_list)
# print accuracy
auc_sum += roc_auc_score(label_list,pred_list)
auc = auc_sum / steps_to_test
return auc
if multi_granined_out:
vec_length_out = train_batchgen.vec_length_out + n_categories
else:
vec_length_out = train_batchgen.vec_length_out
m = grainedDKTModel(train_batchgen.batch_size, train_batchgen.vec_length_in, vec_length_out, \
random_embedding=random_embedding, multi_granined=multi_granined, n_categories=n_categories, \
keep_prob=keep_prob, n_hidden=n_hidden_units, embedding_size=embedding_size, \
initial_learning_rate=initial_learning_rate, final_learning_rate=final_learning_rate,
multi_granined_out = multi_granined_out)
with session.as_default():
tf.global_variables_initializer().run()
if option == 'step':
sum_loss = 0
for step in range(n_step):
batch_Xs, batch_Ys, batch_labels, batch_sequence_lengths, batch_caegories = train_batchgen.next_batch()
feed_dict = {m.Xs : batch_Xs, m.Ys : batch_Ys,
m.seq_len : batch_sequence_lengths,
m.targets : batch_labels,
m.categories : batch_caegories}
_, batch_loss = session.run([m.train_op,m.loss], feed_dict=feed_dict)
sum_loss += batch_loss
if step % report_loss_interval == 0:
average_loss = sum_loss / min(report_loss_interval, step+1)
print ('Average loss at step (%d/%d): %f' % (step, n_step, average_loss))
sum_loss = 0
if step and step % report_score_interval == 0:
auc = calc_score(m)
print('AUC score: {0}'.format(auc))
save_path = m.saver.save(session, model_saved_path)
print('Model saved in {0}'.format(save_path))
with open(performance_table_path, 'a') as out_file_csv:
out_file_csv.write("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}\n".format( \
n_hidden_units, step + 1, '', test_batchgen.batch_size, embedding_size, keep_prob, random_embedding, multi_granined, multi_granined_out, initial_learning_rate, final_learning_rate, auc))
elif option == 'epoch':
steps_per_epoch = train_batchgen.data_size//train_batchgen.batch_size
for epoch in range(n_epoch):
train_batchgen.reset()
print ('Start epoch (%d/%d)' % (epoch, n_epoch))
sum_loss = 0
for step in range(steps_per_epoch):
batch_Xs, batch_Ys, batch_labels, batch_sequence_lengths, batch_caegories = train_batchgen.next_batch()
feed_dict = {m.Xs : batch_Xs, m.Ys : batch_Ys,
m.seq_len : batch_sequence_lengths,
m.targets : batch_labels,
m.categories : batch_caegories}
_, batch_loss = session.run([m.train_op,m.loss], feed_dict=feed_dict)
sum_loss += batch_loss
if step % report_loss_interval == 0:
average_loss = sum_loss / min(report_loss_interval, step+1)
print ('Average loss at step (%d/%d): %f' % (step, steps_per_epoch, average_loss))
sum_loss = 0
if step % report_score_interval == 0:
auc = calc_score(m)
print('AUC score: {0}'.format(auc))
save_path = m.saver.save(session, model_saved_path)
# https://www.tensorflow.org/programmers_guide/saved_model
m.saver.restore(session, model_saved_path) # test
print('Model saved in {0}'.format(save_path))
print ('End epoch (%d/%d)' % (epoch, n_epoch))
auc = calc_score(m)
print('AUC score: {0}'.format(auc))
save_path = m.saver.save(session, model_saved_path)
print('Model saved in {0}'.format(save_path))
with open(performance_table_path, 'a') as out_file_csv:
out_file_csv.write("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}\n".format( \
n_hidden_units, '', epoch + 1, test_batchgen.batch_size, embedding_size, keep_prob, random_embedding, multi_granined, multi_granined_out, initial_learning_rate, final_learning_rate, auc))
pass
def run_epoch(session,
train_batchgen,
n_epoch=1,
keep_prob = 0.5,
report_loss_interval=100,
model_saved_path='./model.ckpt',
random_embedding=False,
embedding_size=200,
multi_granined=True,
n_categories=0,
n_hidden_units = 200,
record_performance=True,
initial_learning_rate=0.001,
final_learning_rate=0.00001,
multi_granined_out=True):
if multi_granined_out:
vec_length_out = train_batchgen.vec_length_out + n_categories
else:
vec_length_out = train_batchgen.vec_length_out
m = grainedDKTModel(train_batchgen.batch_size, train_batchgen.vec_length_in, vec_length_out, \
random_embedding=random_embedding, multi_granined=multi_granined, n_categories=n_categories, \
keep_prob=keep_prob, n_hidden=n_hidden_units, embedding_size=embedding_size, \
initial_learning_rate=initial_learning_rate, final_learning_rate=final_learning_rate,
multi_granined_out = multi_granined_out)
with session.as_default():
tf.global_variables_initializer().run()
steps_per_epoch = train_batchgen.data_size // train_batchgen.batch_size
for epoch in range(n_epoch):
train_batchgen.reset()
print ('Start epoch (%d/%d)' % (epoch, n_epoch))
sum_loss = 0
for step in range(steps_per_epoch):
batch_Xs, batch_Ys, batch_labels, batch_sequence_lengths, batch_caegories = train_batchgen.next_batch()
feed_dict = {m.Xs : batch_Xs, m.Ys : batch_Ys,
m.seq_len : batch_sequence_lengths,
m.targets : batch_labels,
m.categories : batch_caegories}
_, batch_loss = session.run([m.train_op,m.loss], feed_dict=feed_dict)
sum_loss += batch_loss
if step % report_loss_interval == 0:
average_loss = sum_loss / min(report_loss_interval, step+1)
print ('Average loss at step (%d/%d): %f' % (step, steps_per_epoch, average_loss))
sum_loss = 0
print ('End epoch (%d/%d)' % (epoch, n_epoch))
save_path = m.saver.save(session, model_saved_path)
print('Model saved in {0}'.format(save_path))
# tf.reset_default_graph()
pass
def run_predict(session,
test_batchgen,
update=True,
steps_to_test=0,
n_epoch=1,
keep_prob = 0.5,
report_loss_interval=100,
model_saved_path='./model.ckpt',
checkpoint_dir = "./",
random_embedding=False,
embedding_size=200,
multi_granined=True,
n_categories=0,
n_hidden_units = 200,
record_performance=True,
initial_learning_rate=0.001,
final_learning_rate=0.00001,
multi_granined_out=True):
if steps_to_test == 0:
steps_to_test = test_batchgen.data_size//test_batchgen.batch_size
if steps_to_test == 0:
steps_to_test = 1
# print steps_to_test
if multi_granined_out:
vec_length_out = test_batchgen.vec_length_out + n_categories
else:
vec_length_out = test_batchgen.vec_length_out
def calc_score(graph):
auc_sum = 0.
test_batchgen.reset()
Xs = graph.get_tensor_by_name("Xs_input:0")
Ys = graph.get_tensor_by_name("Ys_input:0")
seq_len = graph.get_tensor_by_name("sequence_length_input:0")
targets = graph.get_tensor_by_name("targets_input:0")
categories = graph.get_tensor_by_name("input_categories:0")
predict_tensor = graph.get_tensor_by_name("test_predict:0")
predall_tensor = graph.get_tensor_by_name("test_predall:0")
train_op = graph.get_tensor_by_name("train_op:0")
loss = graph.get_tensor_by_name("mean_loss:0")
n_valid_auc = 0
accuracy_sum = 0.
n_valid_cnt = 0
for i in range(steps_to_test):
test_batch_Xs, test_batch_Ys, test_batch_labels, test_batch_sequence_lengths, test_batch_categories = test_batchgen.next_batch()
test_feed_dict= {Xs : test_batch_Xs, Ys : test_batch_Ys,
seq_len : test_batch_sequence_lengths,
targets : test_batch_labels,
categories : test_batch_categories}
pred, pred_all = session.run([predict_tensor, predall_tensor], feed_dict=test_feed_dict)
label_list = test_batch_labels.reshape(-1)
pred_list = np.array(pred).reshape(-1)
pred_each_part = pred_all[-1][-1]
# print [abs(d) for d in np.array(label_list) - np.array(pred_list)]
accuracy_sum += sum([abs(d) for d in np.array(label_list) - np.array(pred_list)])
n_valid_cnt += len(pred_list)
# print accuracy
try:
auc_sum += roc_auc_score(label_list,pred_list)
n_valid_auc += 1
except:
print ("no valid auc available in this case")
auc = auc_sum / max(n_valid_auc, 1)
accuracy = accuracy_sum / max(n_valid_cnt, 1)
if update:
# update the model
for i in range(steps_to_test):
test_batch_Xs, test_batch_Ys, test_batch_labels, test_batch_sequence_lengths, test_batch_categories = test_batchgen.next_batch()
feed_dict= {Xs : test_batch_Xs, Ys : test_batch_Ys,
seq_len : test_batch_sequence_lengths,
targets : test_batch_labels,
categories : test_batch_categories}
_, batch_loss = session.run([train_op,loss], feed_dict=feed_dict)
return accuracy, auc, pred_each_part
with session.as_default():
# tf.global_variables_initializer().run()
saver = tf.train.import_meta_graph('{0}.meta'.format(model_saved_path))
saver.restore(session, tf.train.latest_checkpoint(checkpoint_dir))
graph = tf.get_default_graph()
accuracy, auc, pred_each_part = calc_score(graph)
if update:
saver.save(session, model_saved_path)
print('accuracy: {0}'.format(accuracy))
print('AUC score: {0}'.format(auc))
return accuracy, auc, pred_each_part