def train_10_fold(): batch_size = 128 test_size = 128 skf = StratifiedKFold(n_splits=10) file_path = "../dataset/g4_128.npy" dataset = np.load(open(file_path, 'r')) X, y = np.array(dataset['X']), np.array(dataset['y'], dtype=np.int) with tf.name_scope('Input'): X_left = tf.placeholder(tf.float32, [None, dim * dim * bin_vec_dim]) X_right = tf.placeholder(tf.float32, [None, dim * dim * bin_vec_dim]) Y = tf.placeholder(tf.float32, [None, 2]) dropout = tf.placeholder(tf.float32, name='dropout') sample_weights = tf.placeholder(tf.float32, [batch_size]) py_x = model(X_left, X_right, dropout) cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) reg_term = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) train_op = tf.train.AdamOptimizer().minimize(cost + reg_term) predict_op = tf.argmax(py_x, 1) indices = np.random.permutation(X.shape[0]) X = X[indices] y = y[indices] fold_index = 0 avg_accuracy = 0. avg_recall = 0. avg_precision = 0. avg_f1_score = 0. fout = open('result/sda_base_10_fold.txt', 'w') if os.path.exists('result') is not True: os.mkdir("result") if os.path.exists("old_models_10_fold") is not True: os.mkdir("old_models_10_fold") for train_idx, test_idx in skf.split(X, y): print('*' * 40 + str(fold_index) + '*' * 40) fold_path = os.path.join("old_models_10_fold/sda_base", str(fold_index)) if os.path.exists(fold_path) is not True: os.mkdir(fold_path) X_train, X_test = X[train_idx], X[test_idx] y_train, y_test = y[train_idx], y[test_idx] train_X_left, train_X_right, train_Y = \ graph_mat_data.make_pairs_10_fold_old_model(X_train, y_train, neg_ratio=1.3, pos_ratio=1.0, add_all_neg=False) test_X_left, test_X_right, test_Y = \ graph_mat_data.make_pairs_10_fold(X_test, y_test, neg_ratio=1.0, pos_ratio=1.0, add_all_neg=True) # compute the class weights classes_numbers = np.bincount(np.argmax(train_Y, axis=1)) classes_weights = np.array([ classes_numbers[1] * 1.0 / (classes_numbers[0] + classes_numbers[1]), classes_numbers[0] * 1.0 / (classes_numbers[0] + classes_numbers[1]) ], dtype=np.float32) classes_weights = np.reshape(classes_weights, newshape=[2, 1]) t_beg = time.clock() with tf.Session() as sess: merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(logdir, sess.graph) tf.global_variables_initializer().run() dense_test_X_left = from_sparse_arrs(test_X_left[0:test_size]) dense_test_X_right = from_sparse_arrs(test_X_right[0:test_size]) step = 0 for epoch in xrange(6): indices = np.random.permutation(train_X_left.shape[0]) train_X_left = train_X_left[indices] train_X_right = train_X_right[indices] train_Y = train_Y[indices] for start, end in zip( range(0, np.shape(train_X_left)[0], batch_size), range(batch_size, np.shape(train_X_left)[0] + 1, batch_size)): dense_train_X_left = from_sparse_arrs( train_X_left[start:end]) dense_train_X_right = from_sparse_arrs( train_X_right[start:end]) batch_samples_weights = np.matmul(train_Y[start:end], classes_weights) batch_samples_weights = np.reshape(batch_samples_weights, newshape=[batch_size]) sess.run(train_op, feed_dict={ X_left: dense_train_X_left, X_right: dense_train_X_right, Y: train_Y[start:end], dropout: keep_prob, sample_weights: batch_samples_weights }) step += 1 if step % 100 == 0 and step != 0: print('Epoch: %d, Iter: %d\n' % (epoch, step)) predict_Y = sess.run(predict_op, feed_dict={ X_left: dense_test_X_left, X_right: dense_test_X_right, dropout: 1.0 }) print( epoch, np.mean( np.argmax(test_Y[0:test_size], axis=1) == predict_Y)) t_end = time.clock() print('time cost: %.2f' % (t_end - t_beg)) saver = tf.train.Saver() saver.save(sess, os.path.join(fold_path, 'mode.ckpt')) print "model saved." overall_accuracy = 0. overall_predict_Y = [] iter = 0 for start, end in zip( range(0, np.shape(test_X_left)[0], batch_size), range(batch_size, np.shape(test_X_left)[0] + 1, batch_size)): dense_test_X_left = from_sparse_arrs(test_X_left[start:end]) dense_test_X_right = from_sparse_arrs(test_X_right[start:end]) predict_Y = sess.run(predict_op, feed_dict={ X_left: dense_test_X_left, X_right: dense_test_X_right, dropout: 1.0 }) # no dropout overall_predict_Y.extend(predict_Y.tolist()) accuracy = np.mean( np.argmax(test_Y[start:end], axis=1) == predict_Y) iter += 1 overall_accuracy += accuracy print('Overall accuracy: %.5f' % (overall_accuracy / iter)) t_end = time.clock() print('Time cost: %.2f' % (t_end - t_beg)) fout.write('*' * 80 + '\n') fout.write('Fold %d:\n' % (fold_index)) fout.write('Overall accuracy: %.5f\n' % (overall_accuracy / iter)) fout.write('Time cost: %.2f\n' % (t_end - t_beg)) recall, precision, f1_score = stat(np.argmax( test_Y[:len(overall_predict_Y)], axis=1), np.array(overall_predict_Y, dtype=np.int32), fout=fout) fout.flush() avg_accuracy += overall_accuracy / iter avg_recall += recall avg_precision += precision avg_f1_score += f1_score print('*' * 80) fold_index += 1 avg_accuracy /= 10.0 avg_precision /= 10.0 avg_recall /= 10.0 avg_f1_score /= 10.0 print( 'Avg accuracy: %.4f, avg recall: %.4f, avg precision: %.4f, avg f1 ' 'score: %.4f' % (avg_accuracy, avg_recall, avg_precision, avg_f1_score)) fout.write('*' * 80 + '\n') fout.write( 'Avg accuracy: %.4f, avg recall: %.4f, avg precision: %.4f, avg f1 ' 'score: %.4f' % (avg_accuracy, avg_recall, avg_precision, avg_f1_score)) fout.close()
def train_10_fold_balanced(): global reg_term with tf.name_scope('input'): X_left = tf.placeholder(tf.float32, [None, dim, dim, bin_vec_dim]) X_right = tf.placeholder(tf.float32, [None, dim, dim, bin_vec_dim]) Y = tf.placeholder(tf.float32, [None, 2]) dropout = tf.placeholder(tf.float32) phase = tf.placeholder(tf.bool, name='phase') sample_weights = tf.placeholder(tf.float32, [batch_size]) py_x = classification(X_left, X_right, dropout, phase) cost = tf.reduce_mean( tf.losses.softmax_cross_entropy(logits=py_x, onehot_labels=Y, weights=sample_weights)) tf.summary.scalar('cost', cost) cost = tf.reduce_mean(cost + beta * reg_term) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost) predict_op = tf.argmax(py_x, 1) skf = StratifiedKFold(n_splits=10) file_path = "../dataset/g4_128.npy" dataset = np.load(open(file_path, 'r'), allow_pickle=True) X, y = np.array(dataset['X']), np.array(dataset['y'], dtype=np.int) # shuffle indices = np.random.permutation(X.shape[0]) X = X[indices] y = y[indices] fold_index = 0 avg_accuracy = 0. avg_recall = 0. avg_precision = 0. avg_f1_score = 0. if os.path.exists('result') is not True: os.mkdir("result") fout = open('result/10_fold_balanced.txt', 'w') if os.path.exists("10_fold_balanced") is not True: os.mkdir("10_fold_balanced") for train_idx, test_idx in skf.split(X, y): print('*' * 40 + str(fold_index) + '*' * 40) fold_path = os.path.join("10_fold_balanced", str(fold_index)) if os.path.exists(fold_path) is not True: os.mkdir(fold_path) X_train, X_test = X[train_idx], X[test_idx] y_train, y_test = y[train_idx], y[test_idx] train_X_left, train_X_right, train_Y = \ graph_mat_data.make_pairs_10_fold(X_train, y_train, neg_ratio=10.0, pos_ratio=1.0, add_all_neg=True) test_X_left, test_X_right, test_Y = \ graph_mat_data.make_pairs_10_fold(X_test, y_test, neg_ratio=1.0, pos_ratio=1.0, add_all_neg=True) # compute the class weights classes_numbers = np.bincount(np.argmax(train_Y, axis=1)) classes_weights = np.array([ classes_numbers[1] * 2.0 / (classes_numbers[0] + classes_numbers[1]), classes_numbers[0] * 1.0 / (classes_numbers[0] + classes_numbers[1]) ], dtype=np.float32) classes_weights = np.reshape(classes_weights, newshape=[2, 1]) t_beg = time.clock() # tf.reset_default_graph() # reset the model with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(logdir, sess.graph) saver = tf.train.Saver(max_to_keep=3) step = 0 for epoch in xrange(4): # re-shuffle for each epoch indices = np.random.permutation(train_X_left.shape[0]) train_X_left = train_X_left[indices] train_X_right = train_X_right[indices] train_Y = train_Y[indices] # for small test dense_test_X_left = from_sparse_arrs(test_X_left[0:test_size]) dense_test_X_right = from_sparse_arrs( test_X_right[0:test_size]) for start, end in zip( range(0, np.shape(train_X_left)[0], batch_size), range(batch_size, np.shape(train_X_left)[0] + 1, batch_size)): it_beg = time.clock() dense_train_X_left = from_sparse_arrs( train_X_left[start:end]) dense_train_X_right = from_sparse_arrs( train_X_right[start:end]) batch_samples_weights = np.matmul(train_Y[start:end], classes_weights) batch_samples_weights = np.reshape(batch_samples_weights, newshape=[batch_size]) _ = sess.run( [train_op], feed_dict={ X_left: dense_train_X_left, X_right: dense_train_X_right, Y: train_Y[start:end], sample_weights: batch_samples_weights, dropout: keep_prob, phase: 1 }) print('epoch %d, iteration %d, time %.2f\n' % (epoch, step, time.clock() - it_beg)) step += 1 if step % 100 == 0 and step != 0: batch_samples_weights = np.matmul( test_Y[:test_size], classes_weights) batch_samples_weights = np.reshape( batch_samples_weights, newshape=[test_size]) predict_Y, summary = sess.run( [predict_op, merged], feed_dict={ X_left: dense_test_X_left, X_right: dense_test_X_right, Y: test_Y[:test_size], sample_weights: batch_samples_weights, dropout: 1.0, phase: 0 }) # no dropout train_writer.add_summary(summary, step) print( epoch, np.mean( np.argmax(test_Y[:test_size], axis=1) == predict_Y)) saver.save(sess, os.path.join(fold_path, 'mode.ckpt')) print "model saved." t_end = time.clock() print('Time cost: %.2f' % (t_end - t_beg)) # validation overall_accuracy = 0. overall_predict_Y = [] iter = 0 for start, end in zip( range(0, np.shape(test_X_left)[0], batch_size), range(batch_size, np.shape(test_X_left)[0] + 1, batch_size)): dense_test_X_left = from_sparse_arrs(test_X_left[start:end]) dense_test_X_right = from_sparse_arrs(test_X_right[start:end]) predict_Y = sess.run(predict_op, feed_dict={ X_left: dense_test_X_left, X_right: dense_test_X_right, dropout: 1.0, phase: 0 }) # no dropout overall_predict_Y.extend(predict_Y.tolist()) accuracy = np.mean( np.argmax(test_Y[start:end], axis=1) == predict_Y) iter += 1 overall_accuracy += accuracy print('Overall accuracy: %.5f' % (overall_accuracy / iter)) t_end = time.clock() print('Time cost: %.2f' % (t_end - t_beg)) fout.write('*' * 80 + '\n') fout.write('Fold %d:\n' % (fold_index)) fout.write('Overall accuracy: %.5f\n' % (overall_accuracy / iter)) fout.write('Time cost: %.2f\n' % (t_end - t_beg)) recall, precision, f1_score = stat(np.argmax( test_Y[:len(overall_predict_Y)], axis=1), np.array(overall_predict_Y, dtype=np.int32), fout=fout) fout.flush() avg_accuracy += overall_accuracy / iter avg_recall += recall avg_precision += precision avg_f1_score += f1_score print('*' * 80) fold_index += 1 avg_accuracy /= 10.0 avg_precision /= 10.0 avg_recall /= 10.0 avg_f1_score /= 10.0 print( 'Avg accuracy: %.4f, avg recall: %.4f, avg precision: %.4f, avg f1 ' 'score: %.4f' % (avg_accuracy, avg_recall, avg_precision, avg_f1_score)) fout.write('*' * 80 + '\n') fout.write( 'Avg accuracy: %.4f, avg recall: %.4f, avg precision: %.4f, avg f1 ' 'score: %.4f' % (avg_accuracy, avg_recall, avg_precision, avg_f1_score)) fout.close()
def train_10_fold(): global cls_reg_term, reg_term t_beg = time.clock() batch_size = 256 test_size = 256 skf = StratifiedKFold(n_splits=10) file_path = "../dataset/g4_128.npy" dataset = np.load(open(file_path, 'r'), allow_pickle=True) X, y = np.array(dataset['X']), np.array(dataset['y'], dtype=np.int32) with tf.name_scope('Input'): X_left = tf.placeholder(tf.float32, [None, dim * dim * bin_vec_dim]) X_right = tf.placeholder(tf.float32, [None, dim * dim * bin_vec_dim]) Y = tf.placeholder(tf.float32, [None, 2]) dropout = tf.placeholder(tf.float32, name='dropout') sample_weights = tf.placeholder(tf.float32, [batch_size]) with tf.variable_scope('encoder-decoder'): z = decoder(encoder(X_left, dropout)) cost = tf.reduce_mean(tf.nn.l2_loss(z - X_left)) tf.summary.scalar('ae_cost', cost, collections=['ae']) encoding_trian_op = tf.train.AdamOptimizer().minimize(cost + beta_1 * reg_term) with tf.variable_scope('encoder-decoder', reuse=True): h_left_op = encoder(X_left, dropout) h_right_op = encoder(X_right, dropout) h_left = tf.placeholder(tf.float32, [None, h_dim]) h_right = tf.placeholder(tf.float32, [None, h_dim]) with tf.variable_scope('classification'): py_x = binary_classification(h_left, h_right, dropout) cls_cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) tf.summary.scalar('cls_cost', cls_cost, ['cls']) cls_train_op = tf.train.AdamOptimizer().minimize(cls_cost + beta_2 * cls_reg_term) predict_op = tf.argmax(py_x, 1) indices = np.random.permutation(X.shape[0]) X = X[indices] y = y[indices] fold_index = 0 avg_accuracy = 0. avg_recall = 0. avg_precision = 0. avg_f1_score = 0. fout = open('result/sda_unsup_10_fold.txt', 'w') if os.path.exists('result') is not True: os.mkdir("result") if os.path.exists("old_models_10_fold") is not True: os.mkdir("old_models_10_fold") for train_idx, test_idx in skf.split(X, y): print('*' * 40 + str(fold_index) + '*' * 40) fold_path = os.path.join("old_models_10_fold/sda_unsup", str(fold_index)) if os.path.exists(fold_path) is not True: os.mkdir(fold_path) X_train, X_test = X[train_idx], X[test_idx] y_train, y_test = y[train_idx], y[test_idx] train_X_left, train_X_right, train_Y = \ graph_mat_data.make_pairs_10_fold_old_model(X_train, y_train, neg_ratio=1.3, pos_ratio=1.0, add_all_neg=False) test_X_left, test_X_right, test_Y = \ graph_mat_data.make_pairs_10_fold(X_test, y_test, neg_ratio=1.0, pos_ratio=1.0, add_all_neg=True) classes_numbers = np.bincount(np.argmax(train_Y, axis=1)) classes_weights = np.array([ classes_numbers[1] * 2.0 / (classes_numbers[0] + classes_numbers[1]), classes_numbers[0] * 1.0 / (classes_numbers[0] + classes_numbers[1]) ], dtype=np.float32) classes_weights = np.reshape(classes_weights, newshape=[2, 1]) t_beg = time.clock() t_end = time.clock() # print('Preparing time: %.2f' % (t_end - t_beg)) with tf.Session() as sess: merged = tf.summary.merge_all(key='ae') train_writer = tf.summary.FileWriter(logdir, sess.graph) tf.global_variables_initializer().run() tf.local_variables_initializer().run() dense_test_X_left = from_sparse_arrs(test_X_left[0:test_size]) dense_test_X_right = from_sparse_arrs(test_X_right[0:test_size]) it = 0 for epoch in range(300): indices = np.random.permutation(X_train.shape[0]) shuffle_train_X = X_train[indices] for start, end in zip( xrange(0, np.shape(X_train)[0], batch_size), xrange(batch_size, np.shape(X_train)[0] + 1, batch_size)): dense_train_X_left = from_sparse_arrs( shuffle_train_X[start:end]) # dense_train_X_right = from_sparse_arrs(train_X_right[start:end]) _ = sess.run([encoding_trian_op], feed_dict={ X_left: dense_train_X_left, dropout: keep_prob }) # train_writer.add_summary(summary, it) if (it % 100 == 0): print('Epoch: %d, Iter: %d\n' % (epoch, it)) h_left_val = sess.run(z, feed_dict={ X_left: dense_test_X_left, dropout: 1.0 }) print('l2 cost: %.3f\n' % (np.mean( np.linalg.norm(h_left_val - dense_test_X_left, axis=1)))) it += 1 t_end = time.clock() print('Unsup phase time: %.2f' % (t_end - t_beg)) merged = tf.summary.merge_all('cls') best_test_acc = 0. it = 0 for epoch in range(3): indices = np.random.permutation(train_X_left.shape[0]) train_X_left = train_X_left[indices] train_X_right = train_X_right[indices] train_Y = train_Y[indices] it_beg = time.clock() for start, end in zip( range(0, np.shape(train_X_left)[0], batch_size), range(batch_size, np.shape(train_X_left)[0] + 1, batch_size)): batch_samples_weights = np.matmul(train_Y[start:end], classes_weights) batch_samples_weights = np.reshape(batch_samples_weights, newshape=[batch_size]) dense_train_X_left = from_sparse_arrs( train_X_left[start:end]) dense_train_X_right = from_sparse_arrs( train_X_right[start:end]) h_left_val = sess.run(h_left_op, feed_dict={ X_left: dense_train_X_left, dropout: 1.0 }) h_right_val = sess.run(h_right_op, feed_dict={ X_right: dense_train_X_right, dropout: 1.0 }) _ = sess.run( [cls_train_op], feed_dict={ h_left: h_left_val, h_right: h_right_val, Y: train_Y[start:end], dropout: keep_prob, sample_weights: batch_samples_weights }) # train_writer.add_summary(summary, it) if (it % 100 == 0): print('Epoch: %d, Iter: %d Time: %.2f\n' % (epoch, it, time.clock() - it_beg)) it_beg = time.clock() it += 1 h_left_val = sess.run(h_left_op, feed_dict={ X_left: dense_test_X_left, dropout: 1.0 }) h_right_val = sess.run(h_right_op, feed_dict={ X_right: dense_test_X_right, dropout: 1.0 }) predict_Y = sess.run(predict_op, feed_dict={ h_left: h_left_val, h_right: h_right_val, dropout: 1.0 }) stat(np.argmax(test_Y[0:test_size], axis=1), np.array(predict_Y, dtype=np.float32)) test_acc = np.mean( np.argmax(test_Y[0:test_size], axis=1) == predict_Y) print(epoch, test_acc) saver = tf.train.Saver() saver.save(sess, os.path.join(fold_path, 'mode.ckpt')) print "model saved." t_end = time.clock() print('time cost: %.2f' % (t_end - t_beg)) overall_accuracy = 0. overall_predict_Y = [] iter = 0 # saver = tf.train.Saver() # saver.restore(sess, os.path.join(fold_path, 'mode.ckpt')) for start, end in zip( range(0, np.shape(test_X_left)[0], batch_size), range(batch_size, np.shape(test_X_left)[0] + 1, batch_size)): dense_test_X_left = from_sparse_arrs(test_X_left[start:end]) dense_test_X_right = from_sparse_arrs(test_X_right[start:end]) h_left_val = sess.run(h_left_op, feed_dict={ X_left: dense_test_X_left, dropout: 1.0 }) h_right_val = sess.run(h_right_op, feed_dict={ X_right: dense_test_X_right, dropout: 1.0 }) predict_Y = sess.run(predict_op, feed_dict={ h_left: h_left_val, h_right: h_right_val, Y: train_Y[start:end], dropout: 1.0 }) overall_predict_Y.extend(predict_Y.tolist()) accuracy = np.mean( np.argmax(test_Y[start:end], axis=1) == predict_Y) iter += 1 overall_accuracy += accuracy print('Overall accuracy: %.5f' % (overall_accuracy / iter)) t_end = time.clock() print('Time cost: %.2f' % (t_end - t_beg)) fout.write('*' * 80 + '\n') fout.write('Fold %d:\n' % (fold_index)) fout.write('Overall accuracy: %.5f\n' % (overall_accuracy / iter)) fout.write('Time cost: %.2f\n' % (t_end - t_beg)) recall, precision, f1_score = stat(np.argmax( test_Y[:len(overall_predict_Y)], axis=1), np.array(overall_predict_Y, dtype=np.int32), fout=fout) fout.flush() avg_accuracy += overall_accuracy / iter avg_recall += recall avg_precision += precision avg_f1_score += f1_score print('*' * 80) fold_index += 1 avg_accuracy /= 10.0 avg_precision /= 10.0 avg_recall /= 10.0 avg_f1_score /= 10.0 print( 'Avg accuracy: %.4f, avg recall: %.4f, avg precision: %.4f, avg f1 ' 'score: %.4f' % (avg_accuracy, avg_recall, avg_precision, avg_f1_score)) fout.write('*' * 80 + '\n') fout.write( 'Avg accuracy: %.4f, avg recall: %.4f, avg precision: %.4f, avg f1 ' 'score: %.4f' % (avg_accuracy, avg_recall, avg_precision, avg_f1_score)) fout.close()