def run(dataset, model, model_ssl, runs, epochs, lr, weight_decay, early_stopping, permute_masks=None, logger=None): for k in range(runs): model.to(device).reset_parameters() optimizer = Adam(model.parameters(), lr=lr, weight_decay=weight_decay) if torch.cuda.is_available(): torch.cuda.synchronize() data = dataset[0] data = data.to(device) num_nodes = data.num_nodes pivot = int(num_nodes * 0.1) cold_mask_node = range(k * pivot, (k + 1) * pivot) data.test_mask = cold_mask_node train_node = range(num_nodes) train_node = [e for e in train_node if e not in cold_mask_node] data = test_edges(data, cold_mask_node) data.train_mask = random.sample(train_node, 140) print("{}-fold Result".format(k)) # one possibile solution: fix the train_mask_nodes to 140 nodes run_(data, dataset, data.total_edge_index, train_node) data.masked_nodes = data.train_mask train_loader = DataLoader(train_dataset, 140, shuffle=True) data = train_edges(data, data.masked_nodes) scheduler = StepLR(optimizer, step_size=2000, gamma=0.5) for epoch in range(2000): with torch.autograd.set_detect_anomaly(True): train_loss = train_Z(model, optimizer, data, epoch) for epoch in range(5000): with torch.autograd.set_detect_anomaly(True): train_loss = train(model, optimizer, data, epoch) scheduler.step() if torch.cuda.is_available(): torch.cuda.synchronize() loss, acc = evaluate(model, data) print('Val Loss: {:.4f}, Test Accuracy: {:.3f}'.format(loss, acc))
def run(dataset, model, runs, epochs, lr, weight_decay, early_stopping, permute_masks=None, logger=None): batch_size = 30 test_losses, accs, p0, p1, r0, r1, f0, f1 = [], [], [], [], [], [], [], [] for k in range(runs): model.to(device).reset_parameters() optimizer = Adam(model.parameters(), lr=lr, weight_decay=weight_decay) best_val_perf = test_perf = 0 if torch.cuda.is_available(): torch.cuda.synchronize() # t_start = time.perf_counter() data = dataset[0] data = data.to(device) num_nodes = data.num_nodes pivot = int(num_nodes * 0.2) cold_mask_node = range(k * pivot, (k + 1) * pivot) data.test_mask = cold_mask_node train_node = range(num_nodes) train_node = [e for e in train_node if e not in cold_mask_node] data = test_edges(data, cold_mask_node) data.train_mask = random.sample(train_node, 140) epoch_num = int((num_nodes - pivot) / batch_size) print("{}-fold Result".format(k)) for i in range(5): for epoch in range(0, epoch_num): data.masked_nodes = train_node[epoch * batch_size:(epoch + 1) * batch_size] data = train_edges(data, data.masked_nodes) train_loss = train(model, optimizer, data) # log = 'Epoch: {:03d}, Loss: {:.4f}, Test: {:.4f}' # print(log.format(epoch, train_loss, test_auc)) if torch.cuda.is_available(): torch.cuda.synchronize() test_auc, test_recall, test_loss, c_m, edge_index_cs = evaluate( model, data) test_losses.append(test_loss) accs.append(test_auc) p0.append(test_recall[0][0]) p1.append(test_recall[0][1]) r0.append(test_recall[1][0]) r1.append(test_recall[1][1]) f0.append(test_recall[2][0]) f1.append(test_recall[2][1]) run_(data, dataset, train_node) run_cs(data, dataset, edge_index_cs, train_node) # loss, acc = tensor(train_losses), tensor(accs) print(c_m) print( "Train loss: {:.4f}, Test AUC Score: {:.3f},\n precision: {:.4f}, {:.4f},\n recall: {:.4f}, {:.4f},\n fscore: {:.4f}, {:.4f}" .format(train_loss, test_auc, test_recall[0][0], test_recall[0][1], test_recall[1][0], test_recall[1][1], test_recall[2][0], test_recall[2][1])) acc_, acc, p0, p1, r0, r1, f0, f1 = tensor(test_losses), tensor( accs), tensor(p0), tensor(p1), tensor(r0), tensor(r1), tensor( f0), tensor(f1) print("\n===============\n") print("Final Result") print( 'Loss: {:.4f}, Test AUC: {:.3f} ± {:.3f}\n precision: {:.4f}, {:.4f},\n recall: {:.4f}, {:.4f},\n fscore: {:.4f}, {:.4f}' .format(acc_.mean().item(), acc.mean().item(), acc.std().item(), p0.mean(), p1.mean(), r0.mean(), r1.mean(), f0.mean(), f1.mean()))
def run(dataset, model, runs, epochs, lr, weight_decay, early_stopping, permute_masks=None, logger=None): batch_size = 30 losses, accs, losses_wo, accs_wo = [], [], [], [] for k in range(runs): model.to(device).reset_parameters() optimizer = Adam(model.parameters(), lr=lr, weight_decay=weight_decay) best_val_perf = test_perf = 0 if torch.cuda.is_available(): torch.cuda.synchronize() data = dataset[0] # gdc = T.GDC(self_loop_weight=1, normalization_in='sym', # normalization_out='col', # diffusion_kwargs=dict(method='ppr', alpha=0.05), # sparsification_kwargs=dict(method='topk', k=128, # dim=0), exact=True) # data = gdc(data) data = data.to(device) num_nodes = data.num_nodes pivot = int(num_nodes * 0.1) cold_mask_node = range(k * pivot, (k + 1) * pivot) data.test_masked_nodes = cold_mask_node train_node = range(num_nodes) train_node = [e for e in train_node if e not in cold_mask_node] data = test_edges(data, cold_mask_node) data.train_masked_nodes = random.sample(train_node, 140) epoch_num = int((num_nodes - pivot) / batch_size) print("{}-fold Result".format(k)) data = train_edges(data, data.train_masked_nodes) loss_wo, acc_wo = run_(data, dataset, data.train_edge_index, train_node) losses_wo.append(loss_wo) accs_wo.append(acc_wo) for epoch in range(1000): with torch.autograd.set_detect_anomaly(True): train_loss = train(model, optimizer, data, epoch) if torch.cuda.is_available(): torch.cuda.synchronize() loss, acc = evaluate(model, data) losses.append(loss) accs.append(acc) print('Val Loss: {:.4f}, Test Accuracy: {:.3f}'.format(loss, acc)) losses, accs, losses_wo, accs_wo = tensor(losses), tensor(accs), tensor( losses_wo), tensor(accs_wo) print('w/o Mean Val Loss: {:.4f}, Mean Test Accuracy: {:.3f} ± {:.3f}'. format(losses_wo.mean().item(), accs_wo.mean().item(), accs_wo.std().item())) print('Mean Val Loss: {:.4f}, Mean Test Accuracy: {:.3f} ± {:.3f}'.format( losses.mean().item(), accs.mean().item(), accs.std().item()))
def run(dataset, model, model_ssl, runs, epochs, lr, weight_decay, early_stopping, permute_masks=None, logger=None): batch_size = 30 for k in range(runs): district = k model.to(device).reset_parameters() model_ssl.to(device).reset_parameters() optimizer = Adam(model.parameters(), lr=lr, weight_decay=weight_decay) if torch.cuda.is_available(): torch.cuda.synchronize() data = dataset[0] # gdc = T.GDC(self_loop_weight=1, normalization_in='sym', # normalization_out='col', # diffusion_kwargs=dict(method='ppr', alpha=0.05), # sparsification_kwargs=dict(method='topk', k=128, # dim=0), exact=True) # data = gdc(data) data = data.to(device) num_nodes = data.num_nodes pivot = int(num_nodes * 0.1) cold_mask_node = range(k * pivot, (k + 1) * pivot) data.test_mask = cold_mask_node train_node = range(num_nodes) train_node = [e for e in train_node if e not in cold_mask_node] data = test_edges(data, cold_mask_node) data.train_mask = random.sample(train_node, 140) print("{}-fold Result".format(k)) # one possibile solution: fix the train_mask_nodes to 140 nodes run_(data, dataset, data.total_edge_index, train_node) ''' for i in range(20): for epoch in range(0, epoch_num): data.masked_nodes = train_node[epoch*batch_size:(epoch+1)*batch_size] data = train_edges(data, data.masked_nodes) with torch.autograd.set_detect_anomaly(True): train_loss =train(model, optimizer, data) # log = 'Epoch: {:03d}, Loss: {:.4f}, Test: {:.4f}' # print(log.format(epoch, train_loss, test_auc)) ''' data.masked_nodes = data.train_mask data = train_edges(data, data.masked_nodes) scheduler = StepLR(optimizer, step_size=2000, gamma=0.5) for epoch in range(2000): with torch.autograd.set_detect_anomaly(True): train_loss = train_Z(model, optimizer, data, epoch) for epoch in range(5000): with torch.autograd.set_detect_anomaly(True): train_loss = train(model, optimizer, data, epoch) scheduler.step() if torch.cuda.is_available(): torch.cuda.synchronize() loss, acc = evaluate(model, data) print('Val Loss: {:.4f}, Test Accuracy: {:.3f}'.format(loss, acc))
def run(dataset, model, runs, epochs, lr, weight_decay, early_stopping, permute_masks=None, logger=None): batch_size = 30 losses, accs , losses_wo, accs_wo= [], [], [], [] perm = torch.randperm(dataset[0].num_nodes) for k in range(runs): model.to(device).reset_parameters() optimizer = Adam(model.parameters(), lr=lr, weight_decay=weight_decay) best_val_perf = test_perf = 0 if torch.cuda.is_available(): torch.cuda.synchronize() writer = SummaryWriter('runs/{}_{}'.format(k, tt)) data = dataset[0] data = data.to(device) num_nodes = data.num_nodes if os.path.isfile('{}_{}.pkl'.format(str(dataset)[:-2], k)): data = pickle.load(open('{}_{}.pkl'.format(str(dataset)[:-2], k), 'rb')) else: pivot= int(num_nodes*0.1) cold_mask_node = perm[list(range(k*pivot, (k+1)*pivot))] data.test_masked_nodes =cold_mask_node train_node = range(num_nodes) train_node = [e for e in train_node if e not in cold_mask_node] #or unknown] data = test_edges(data, cold_mask_node) val_mask_node = random.sample(train_node, int(pivot*0.5)) data.val_masked_nodes = torch.tensor(val_mask_node) data = val_edges(data, val_mask_node) train_node = [e for e in train_node if e not in val_mask_node] #or unknown] data.train_nodes = train_node data.train_masked_nodes = torch.tensor(random.sample(train_node,int(num_nodes*0.1))) data = train_edges(data, data.train_masked_nodes) with open('{}_{}.pkl'.format(str(dataset)[:-2], k), 'wb') as f: pickle.dump(data, f) print("{}-fold Result".format(k)) train_node=data.train_nodes loss_wo, acc_wo = run_(data, dataset, data.train_edge_index, train_node,writer) losses_wo.append(loss_wo) accs_wo.append(acc_wo) scheduler = StepLR(optimizer, step_size=2000, gamma=0.5) for epoch in range(2000): with torch.autograd.set_detect_anomaly(True): train_gan(dataset, data, writer) for epoch in range(5000): with torch.autograd.set_detect_anomaly(True): train_loss =train(model, optimizer,data,epoch) scheduler.step() if torch.cuda.is_available(): torch.cuda.synchronize() loss, acc = evaluate(model, data) losses.append(loss) accs.append(acc) print('Val Loss: {:.4f}, Test Accuracy: {:.3f}'.format(loss,acc)) losses, accs, losses_wo, accs_wo = tensor(losses), tensor(accs), tensor(losses_wo), tensor(accs_wo) print('w/o Mean Val Loss: {:.4f}, Mean Test Accuracy: {:.3f} ± {:.3f}'. format(losses_wo.mean().item(), accs_wo.mean().item(), accs_wo.std().item() )) print('Mean Val Loss: {:.4f}, Mean Test Accuracy: {:.3f} ± {:.3f}'. format(losses.mean().item(), accs.mean().item(), accs.std().item() ))