def load_saved(config, data): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print('Using : ', device) G = data.Graph model = GCN(G, config) model_name = config['output_path'] + config['dataset_name'] + '_GCN_DGL' model.load_state_dict(torch.load(model_name, map_location=device)) model.eval() model = model.to(device) features = data.x.to(device) _, norm_labels = data.y.max(dim=1) norm_labels = norm_labels.to(device) model.eval() test_pred = model(features) test_index = data.test_index.to(device) pred_label = test_pred[test_index] true_label = norm_labels[test_index] pred_label = np.argmax(pred_label.cpu().detach().numpy(), axis=1) true_label = true_label.cpu().detach().numpy() from GNN.Utils import draw_confusion_matrix filename = config['output_path'] + config[ 'dataset_name'] + "_GCN_DGL" + str(config['epoch']) + "_CM.png" draw_confusion_matrix(true_label, pred_label, data.classname, filename) return
def load_saved(config, data): #from CVE.CVE_FC.CVE_FC import Net device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print('Using : ', device) model = Net(config) model.load_state_dict( torch.load(config['output_path'] + config['dataset_name'] + '_FC', map_location=device)) model.eval() model = model.to(device) features = data.x.to(device) _, norm_labels = data.y.max(dim=1) norm_labels = norm_labels.to(device) x_train = features[data.train_index] y_train = norm_labels[data.train_index] x_val = features[data.val_index] y_val = norm_labels[data.val_index] x_test = features[data.test_index] y_test = norm_labels[data.test_index] test_correct = 0.0 test_count = 0.0 for x_test_batch, y_test_batch in batch(x_test, y_test): test_pred = model(x_test_batch) test_cor, test_cou = correct(test_pred, y_test_batch) test_correct += test_cor test_count += test_cou test_accuracy = test_correct / test_count print('Test Accuracy: {:.4f}'.format(test_accuracy)) test_pred = model(x_test) pred_label = np.argmax(test_pred.cpu().detach().numpy(), axis=1) true_label = y_test.cpu().detach().numpy() from GNN.Utils import draw_confusion_matrix filename = config['output_path'] + config['dataset_name'] + "_FC" + str( config['epoch']) + "_CM.png" draw_confusion_matrix(true_label, pred_label, data.classname, filename) return
def load_saved(config, data): save_file = config['output_path'] + 'NGM_Keras.h5' model = tf.keras.models.load_model(save_file) y_softmax = model.predict(data.Feature[data.test_index]) y_test_1d = np.argmax(data.Label[data.test_index], axis=1) y_pred_1d = np.argmax(y_softmax, axis=1) from GNN.Utils import draw_confusion_matrix filename = config['output_path'] + "/NGM_Keras_CM.png" draw_confusion_matrix(y_test_1d, y_pred_1d, data.classname, filename) return
def load_saved(config,data): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print('Using : ', device) G = data.Graph num_heads = 5 num_layers = len(config['hidden_neurons']) num_out_heads = 5 heads = ([num_heads] * num_layers) + [num_out_heads] model = GAT(G, num_layers, config['input_features'], config['hidden_neurons'][0], config['out_features'], heads, # heads F.elu, 0.6, # indrop 0.6, # atten drop, 0.2, # alpha False # residual ) model.load_state_dict(torch.load(config['output_path'] + config['dataset_name'] + '_GAT_DGL', map_location=device)) model.eval() model = model.to(device) features = data.x.to(device) _, norm_labels = data.y.max(dim=1) norm_labels = norm_labels.to(device) model.eval() test_pred = model(features) test_index = data.test_index.to(device) pred_label = test_pred[test_index] true_label = norm_labels[test_index] pred_label = np.argmax(pred_label.cpu().detach().numpy(), axis=1) true_label = true_label.cpu().detach().numpy() from GNN.Utils import draw_confusion_matrix filename = config['output_path'] + config['dataset_name'] + "_GAT_DGL" + str(config['epoch']) + "_CM.png" draw_confusion_matrix(true_label, pred_label, data.classname, filename) return
def train(config, data): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print('Using : ', device) model = Net(config) print(model) model = model.to(device) features = data.x.to(device) _, norm_labels = data.y.max(dim=1) norm_labels = norm_labels.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=0.001) criterion = nn.CrossEntropyLoss() train_accs = list() val_accs = list() epochs = config['epoch'] x_train = features[data.train_index] y_train = norm_labels[data.train_index] x_val = features[data.val_index] y_val = norm_labels[data.val_index] x_test = features[data.test_index] y_test = norm_labels[data.test_index] for epoch in range(epochs): #Training total_loss = 0 t_correct = 0.0 t_count = 0.0 model.train() for x_train_batch, y_train_batch in batch(x_train, y_train, config['batch_size']): optimizer.zero_grad() x = model(x_train_batch) x_outputs = F.softmax(x, dim=1) loss = criterion(x_outputs, y_train_batch) loss.backward() optimizer.step() total_loss += loss.item() t_cor, t_cou = correct(x_outputs, y_train_batch) t_correct += t_cor t_count += t_cou t_acc = t_correct / t_count train_accs.append(t_acc) #validation v_correct = 0.0 v_count = 0.0 #model.eval() for x_val_batch, y_val_batch in batch(x_val, y_val): val_pred = model(x_val_batch) v_cor, v_cou = correct(val_pred, y_val_batch) v_correct += v_cor v_count += v_cou v_acc = v_correct / v_count val_accs.append(v_acc) print("Epoch: {0}, Loss: {1}, Train Acc: {2}, Val Acc: {3}".format( epoch + 1, total_loss, t_acc, v_acc)) #Testing test_correct = 0.0 test_count = 0.0 print("Saving model") torch.save(model.state_dict(), config['output_path'] + config['dataset_name'] + '_FC') model.eval() for x_test_batch, y_test_batch in batch(x_test, y_test): test_pred = model(x_test_batch) test_cor, test_cou = correct(test_pred, y_test_batch) test_correct += test_cor test_count += test_cou test_accuracy = test_correct / test_count print('Test Accuracy: {:.4f}'.format(test_accuracy)) from GNN.Utils import plot_train_val_accuracy filename = config['dataset_name'] + '_FC_' + 'epoch_' + str( epochs) + '_class_' + str(config['out_features']) plot_train_val_accuracy(config['output_path'], { 'name': filename, 'train_accs': train_accs, 'val_accs': val_accs }) model.eval() test_pred = model(x_test) pred_label = np.argmax(test_pred.cpu().detach().numpy(), axis=1) true_label = y_test.cpu().detach().numpy() # print(pred_label) # print(true_label) from GNN.Utils import draw_confusion_matrix filename = config['output_path'] + config['dataset_name'] + "_FC" + str( epochs) + "_CM.png" draw_confusion_matrix(true_label, pred_label, data.classname, filename) return
def train(config, data): #global_step = tf.Variable(0, name='global_step', trainable=False) #dunno why yet alpha1 = tf.constant(1e-1, dtype=np.float32, name='a1') alpha2 = tf.constant(1e-1, dtype=np.float32, name='a2') alpha3 = tf.constant(1e-1, dtype=np.float32, name='a3') in_u1 = tf.placeholder(tf.float32, [ None, config['input_features'], ], name="ull") in_v1 = tf.placeholder(tf.float32, [ None, config['input_features'], ], name="vll") in_u2 = tf.placeholder(tf.float32, [ None, config['input_features'], ], name="ulu") in_v2 = tf.placeholder(tf.float32, [ None, config['input_features'], ], name="vlu") in_u3 = tf.placeholder(tf.float32, [ None, config['input_features'], ], name="uuu") in_v3 = tf.placeholder(tf.float32, [ None, config['input_features'], ], name="vuu") test_inputs = tf.placeholder(tf.float32, [ None, config['input_features'], ], name="test_in") test_labels = tf.placeholder(tf.float32, [None, config['out_features']], name="test_labels") labels_u1 = tf.placeholder(tf.float32, [None, config['out_features']], name="lull") labels_v1 = tf.placeholder(tf.float32, [None, config['out_features']], name="lvll") labels_u2 = tf.placeholder(tf.float32, [None, config['out_features']], name="lulu") cu1 = tf.placeholder(tf.float32, [ None, ], name="Cull") cv1 = tf.placeholder(tf.float32, [ None, ], name="Cvll") cu2 = tf.placeholder(tf.float32, [ None, ], name="Culu") weights_ll = tf.placeholder(tf.float32, [ None, ], name="wll") weights_lu = tf.placeholder(tf.float32, [ None, ], name="wlu") weights_uu = tf.placeholder(tf.float32, [ None, ], name="wuu") model = getModel(config) scores_u1 = model(in_u1) scores_v1 = model(in_v1) scores_u2 = model(in_u2) scores_v2 = model(in_v2) scores_u3 = model(in_u3) scores_v3 = model(in_v3) test_scores = model(test_inputs) #loss function --------------- part1 = tf.nn.softmax_cross_entropy_with_logits( logits=scores_u1, labels=tf.nn.softmax(scores_v1)) part2 = tf.nn.softmax_cross_entropy_with_logits( logits=scores_v1, labels=tf.nn.softmax(scores_u1)) part3 = tf.nn.softmax_cross_entropy_with_logits(logits=scores_u1, labels=labels_u1) part4 = tf.nn.softmax_cross_entropy_with_logits(logits=scores_v1, labels=labels_v1) part5 = tf.nn.softmax_cross_entropy_with_logits( logits=scores_u2, labels=tf.nn.softmax(scores_v2)) part6 = tf.nn.softmax_cross_entropy_with_logits( logits=scores_v2, labels=tf.nn.softmax(scores_u2)) part7 = tf.nn.softmax_cross_entropy_with_logits(logits=scores_u2, labels=labels_u2) # l1 = tf.reduce_sum(alpha1 * weights_ll * ((part1 + part2) / 2.0) + cu1 * part3 + cv1 * part4) # l2 = tf.reduce_sum(alpha2 * weights_lu * ((part5 + part6) / 2.0) + cu2 * part7) l1 = tf.reduce_mean(alpha1 * weights_ll * ((part1 + part2) / 2.0) + cu1 * part3 + cv1 * part4) l2 = tf.reduce_mean(alpha2 * weights_lu * ((part5 + part6) / 2.0) + cu2 * part7) part8 = tf.nn.softmax_cross_entropy_with_logits( logits=scores_u3, labels=tf.nn.softmax(scores_v3)) part9 = tf.nn.softmax_cross_entropy_with_logits( logits=scores_v3, labels=tf.nn.softmax(scores_u3)) #l3 = tf.reduce_sum(alpha3 * weights_uu * ((part8 + part9) / 2.0)) l3 = tf.reduce_mean(alpha3 * weights_uu * ((part8 + part9) / 2.0)) loss_function = l1 + l2 + l3 #loss end --------------- #opt_op = tf.train.RMSPropOptimizer(0.5).minimize(loss_function) opt_op = tf.train.AdamOptimizer(0.001).minimize(loss_function) #performance ------------ train_correct_prediction = tf.concat([ tf.equal(tf.argmax(scores_u1, 1), tf.argmax(labels_u1, 1)), tf.equal(tf.argmax(scores_v1, 1), tf.argmax(labels_v1, 1)), tf.equal(tf.argmax(scores_u2, 1), tf.argmax(labels_u2, 1)) ], axis=0) train_corr_sum = tf.reduce_sum(tf.cast(train_correct_prediction, "float"), name="train_accuracy") train_pred_num = tf.size(train_correct_prediction) test_correct_prediction = tf.equal(tf.argmax(test_scores, 1), tf.argmax(test_labels, 1)) test_corr_sum = tf.reduce_sum(tf.cast(test_correct_prediction, "float"), name="test_accuracy") test_pred_num = tf.size(test_correct_prediction) #end performance init_op = tf.global_variables_initializer() session_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess = tf.Session(config=session_conf) K.set_session(sess) sess.run(init_op) #epochs=config['epoch'] epochs = 12 train_accuracies = list() val_accuracies = list() for epoch in range(epochs): print("======= EPOCH " + str(epoch + 1) + " ========") train_batches = batch_iter_train(data, config['batch_size']) #train_batches = batch_incremental(data,config['batch_size']) val_batches = batch_iter_val(data, config['batch_size']) total_loss = 0 train_total_corr = 0 train_total_pred = 0 for batch in train_batches: #current_step = tf.train.global_step(sess, global_step) (u1, v1, u2, v2, u3, v3, lu1, lv1, lu2, w_ll, w_lu, w_uu, c_ull, c_vll, c_ulu) = batch _, loss, tc, tp = sess.run( [opt_op, loss_function, train_corr_sum, train_pred_num], feed_dict={ in_u1: u1, in_v1: v1, in_u2: u2, in_v2: v2, in_u3: u3, in_v3: v3, labels_u1: lu1, labels_v1: lv1, labels_u2: lu2, weights_ll: w_ll, weights_lu: w_lu, weights_uu: w_uu, cu1: c_ull, cv1: c_vll, cu2: c_ulu }) train_total_corr += tc train_total_pred += tp total_loss += loss train_accuracy = train_total_corr / train_total_pred train_accuracies.append(train_accuracy) val_accuracy = calculate_accuracy(sess, test_inputs, test_labels, test_corr_sum, test_pred_num, val_batches) val_accuracies.append(val_accuracy) print("Epoch {0} Loss: {1} Train accuracy {2} Val Accuracy {3}".format( epoch, total_loss, train_accuracy, val_accuracy)) test_batches = batch_iter_test(data, config['batch_size']) test_accuracy = calculate_accuracy(sess, test_inputs, test_labels, test_corr_sum, test_pred_num, test_batches) print("Test Accuracy {0}".format(test_accuracy)) from GNN.Utils import plot_train_val_accuracy filename = config['dataset_name'] + '_NGM_Keras_' + 'epoch_' + str( epochs) + '_class_' + str(config['out_features']) plot_train_val_accuracy( config['output_path'], { 'name': filename, 'train_accs': train_accuracies, 'val_accs': val_accuracies }) save_file = config['output_path'] + 'NGM_Keras.h5' model.save(save_file) y_softmax = model.predict(data.Feature[data.test_index]) y_test_1d = np.argmax(data.Label[data.test_index], axis=1) y_pred_1d = np.argmax(y_softmax, axis=1) from GNN.Utils import draw_confusion_matrix filename = config['output_path'] + "/NGM_Keras" + str(epochs) + "_CM.png" draw_confusion_matrix(y_test_1d, y_pred_1d, data.classname, filename) return
def train(config,data): device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') #device = torch.device('cpu') print('Using : ',device) G = data.Graph num_heads=5 num_layers=len(config['hidden_neurons']) num_out_heads=5 heads = ([num_heads] * num_layers) + [num_out_heads] model = GAT(G, num_layers, config['input_features'], config['hidden_neurons'][0], config['out_features'], heads, #heads F.elu, 0.6, #indrop 0.6, #atten drop, 0.2, #alpha False #residual ) print(model) model=model.to(device) #change the parameters and learning stuff optimizer = torch.optim.Adam(model.parameters(), lr=config['learning_rate'],weight_decay=5e-4) criterion = nn.CrossEntropyLoss() _, norm_labels = data.y.max(dim=1) features=data.x.to(device) norm_labels = norm_labels.to(device) train_index=data.train_index.to(device) val_index=data.val_index.to(device) test_index=data.test_index.to(device) train_accs = list() val_accs = list() test_accs = list() epochs = config['epoch'] model.train() for epoch in range(epochs): optimizer.zero_grad() x = model(features) outputs=F.softmax(x,dim=1) loss = criterion(outputs[train_index], norm_labels[train_index]) loss.backward() optimizer.step() train_acc=accuracy(outputs[train_index],norm_labels[train_index]) val_acc=accuracy(outputs[val_index],norm_labels[val_index]) test_acc=accuracy(outputs[test_index],norm_labels[test_index]) train_accs.append(train_acc) val_accs.append(val_acc) test_accs.append(test_acc) print("Epoch: {0}, Loss: {1}, Train Acc: {2}, Val Acc: {3}".format(epoch+1,loss.item(),train_acc, val_acc)) print("Saving model") torch.save(model.state_dict(), config['output_path'] + config['dataset_name'] + '_GAT_DGL') model.eval() test_pred=model(features) test_accuracy=accuracy(test_pred[test_index],norm_labels[test_index]) print('Test Accuracy: {:.4f}'.format(test_accuracy)) from GNN.Utils import plot_train_val_accuracy filename = config['dataset_name'] + '_GAT_DGL_' + 'epoch_' + str(epochs) + '_class_' + str(config['out_features']) plot_train_val_accuracy(config['output_path'],{'name': filename, 'train_accs': train_accs, 'val_accs': val_accs, 'test_accs': test_accs}) pred_label = test_pred[test_index] true_label = norm_labels[test_index] pred_label = np.argmax(pred_label.cpu().detach().numpy(), axis=1) true_label = true_label.cpu().detach().numpy() from GNN.Utils import draw_confusion_matrix filename = config['output_path'] + config['dataset_name'] + "_GAT_DGL" + str(epochs) + "_CM.png" draw_confusion_matrix(true_label, pred_label, data.classname, filename) return
def train(config, data): (data, classname) = make_data(data) print(data) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = Net().to(device) data = data.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=0.001) criterion = nn.CrossEntropyLoss() _, norm_labels = data.y.max(dim=1) train_accs = list() val_accs = list() test_accs = list() epochs = 100 model.train() for epoch in range(epochs): optimizer.zero_grad() x = model(data) outputs = F.softmax(x, dim=1) loss = criterion(outputs[data.train_index], norm_labels[data.train_index]) loss.backward() optimizer.step() train_acc = accuracy(outputs[data.train_index], norm_labels[data.train_index]) val_acc = accuracy(outputs[data.val_index], norm_labels[data.val_index]) test_acc = accuracy(outputs[data.test_index], norm_labels[data.test_index]) train_accs.append(train_acc) val_accs.append(val_acc) test_accs.append(test_acc) print("Epoch: {0}, Loss: {1}, Train Acc: {2}, Val Acc: {3}".format( epoch + 1, loss.item(), train_acc, val_acc)) model.eval() test_pred = model(data) test_accuracy = accuracy(test_pred[data.test_index], norm_labels[data.test_index]) print('Test Accuracy: {:.4f}'.format(test_accuracy)) from GNN.Utils import plot_train_val_accuracy plot_train_val_accuracy( config['output_path'], { 'name': 'GCN_PYG_' + layer.__name__, 'train_accs': train_accs, 'val_accs': val_accs, 'test_accs': test_accs }) test_pred = model(data) pred_label = test_pred[data.test_index] pred_label = np.argmax(pred_label.cpu().detach().numpy(), axis=1) true_label = norm_labels[data.test_index].cpu().detach().numpy() from GNN.Utils import draw_confusion_matrix filename = config['output_path'] + config[ 'dataset_name'] + "_GCN_PYG" + str(epochs) + "_CM.png" draw_confusion_matrix(true_label, pred_label, classname, filename) return