-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain.py
134 lines (125 loc) · 7.45 KB
/
train.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
from typing_extensions import final
from model import SGKT
import tensorflow as tf
import numpy as np
import os
from tqdm import tqdm
from sklearn.metrics import roc_auc_score, precision_recall_fscore_support, accuracy_score
from data_process import DataGenerator
def train(args,train_dkt):
run_config = tf.ConfigProto()
run_config.gpu_options.allow_growth = True
with tf.Session(config=run_config) as sess:
print(args.model)
model = SGKT(args)
saver = tf.train.Saver()
if train_dkt:
sess.run(tf.global_variables_initializer())
model_dir = save_model_dir(args)
best_valid_auc = 0
for epoch in tqdm(range(args.num_epochs)):
train_generator = DataGenerator(args.train_seqs, args.max_step, batch_size=args.batch_size,
feature_size=args.feature_answer_size - 2,
hist_num=args.hist_neighbor_num)
valid_generator = DataGenerator(args.valid_seqs, args.max_step, batch_size=args.batch_size,
feature_size=args.feature_answer_size - 2,
hist_num=args.hist_neighbor_num)
print("epoch:", epoch)
overall_loss = 0
train_generator.shuffle()
preds, binary_preds, targets = list(), list(), list()
train_step = 0
while not train_generator.end:
train_step += 1
[features_answer_index,target_answers,seq_lens,hist_neighbor_index] = train_generator.next_batch()
binary_pred, pred, loss = model.train(sess,features_answer_index,target_answers,seq_lens,hist_neighbor_index)
overall_loss += loss
for seq_idx, seq_len in enumerate(seq_lens):
preds.append(pred[seq_idx, 0:seq_len])
binary_preds.append(binary_pred[seq_idx, 0:seq_len])
targets.append(target_answers[seq_idx, 0:seq_len])
train_loss = overall_loss / train_step
preds = np.concatenate(preds)
binary_preds = np.concatenate(binary_preds)
targets = np.concatenate(targets)
auc_value = roc_auc_score(targets, preds)
accuracy = accuracy_score(targets, binary_preds)
precision, recall, f_score, _ = precision_recall_fscore_support(targets, binary_preds)
print("\ntrain loss = {0},auc={1}, accuracy={2}".format(train_loss, auc_value, accuracy))
write_log(args,model_dir,auc_value, accuracy, epoch, name='train_')
# valid
valid_generator.reset()
preds, binary_preds, targets = list(), list(), list()
valid_step = 0
while not valid_generator.end:
valid_step += 1
[features_answer_index,target_answers,seq_lens,hist_neighbor_index] = valid_generator.next_batch()
binary_pred, pred = model.evaluate(sess,features_answer_index,target_answers,seq_lens,hist_neighbor_index,valid_step)
for seq_idx, seq_len in enumerate(seq_lens):
preds.append(pred[seq_idx, 0:seq_len])
binary_preds.append(binary_pred[seq_idx, 0:seq_len])
targets.append(target_answers[seq_idx, 0:seq_len])
# compute metrics
preds = np.concatenate(preds)
binary_preds = np.concatenate(binary_preds)
targets = np.concatenate(targets)
auc_value = roc_auc_score(targets, preds)
accuracy = accuracy_score(targets, binary_preds)
precision, recall, f_score, _ = precision_recall_fscore_support(targets, binary_preds)
print("\nvalid auc={0}, accuracy={1}, precision={2}, recall={3}".format(auc_value, accuracy, precision,
recall))
write_log(args,model_dir,auc_value, accuracy, epoch, name='valid_')
if auc_value > best_valid_auc:
print('%3.4f to %3.4f' % (best_valid_auc, auc_value))
best_valid_auc = auc_value
best_epoch = epoch
checkpoint_dir = os.path.join(args.checkpoint_dir, model_dir)
save(best_epoch,sess,checkpoint_dir,saver)
print(model_dir+"\t"+str(best_valid_auc))
else:
if self.load():
print('CKPT loaded')
else:
raise Exception('CKPT need')
test_data_generator = DataGenerator(args.test_seqs, args.max_step, batch_size=args.batch_size,
feature_size=args.feature_answer_size - 2,
hist_num=args.hist_neighbor_num)
test_data_generator.reset()
preds, binary_preds, targets = list(), list(), list()
while not test_data_generator.end:
[features_answer_index, target_answers, seq_lens, hist_neighbor_index] = test_data_generator.next_batch()
binary_pred, pred = model.evaluate(sess, features_answer_index, target_answers, seq_lens,
hist_neighbor_index)
for seq_idx, seq_len in enumerate(seq_lens):
preds.append(pred[seq_idx, 0:seq_len])
binary_preds.append(binary_pred[seq_idx, 0:seq_len])
targets.append(target_answers[seq_idx, 0:seq_len])
preds = np.concatenate(preds)
binary_preds = np.concatenate(binary_preds)
targets = np.concatenate(targets)
auc_value = roc_auc_score(targets, preds)
accuracy = accuracy_score(targets, binary_preds)
precision, recall, f_score, _ = precision_recall_fscore_support(targets, binary_preds)
print("\ntest auc={0}, accuracy={1}, precision={2}, recall={3}".format(auc_value, accuracy, precision,
recall))
print(model_dir)
write_log(args, model_dir, auc_value, accuracy, epoch, name='test_')
def save(global_step,sess,checkpoint_dir,saver):
model_name = 'SGKT'
if not os.path.exists(checkpoint_dir):
os.mkdir(checkpoint_dir)
saver.save(sess, os.path.join(checkpoint_dir, model_name), global_step=global_step)
print('Save checkpoint at %d' % (global_step))
def save_model_dir(args):
return '{}_{}_{}lr_{}hop_{}sn_{}qn_{}hn_{}nn_{}_{}bound_{}keep_{}'.format(args.dataset,
args.model,args.lr,args.n_hop,args.skill_neighbor_num,args.question_neighbor_num,args.hist_neighbor_num,\
args.next_neighbor_num,args.sim_emb,args.att_bound,args.dropout_keep_probs,args.tag)
def write_log(args,model_dir,auc, accuracy, epoch, name='train_'):
log_path = os.path.join(args.log_dir, name+model_dir+'.csv')
if not os.path.exists(log_path):
log_file = open(log_path, 'w')
log_file.write('Epoch\tAuc\tAccuracy\n')
else:
log_file = open(log_path, 'a')
log_file.write(str(epoch) + '\t' + str(auc) + '\t' + str(accuracy) + '\n')
log_file.flush()