def selfTrain(graphsage, labels, train, test, val, confList, adj_lists, filetime, features, temperature = 1, update_interval = 50, maxiter = 500, tol = 0.5, batch_size = 200 ): opt = TrainOptions().parse() #index = 0 #index_array = np.arange(test.shape[0]) closs = nn.CrossEntropyLoss() optimizer = torch.optim.SGD(filter(lambda p : p.requires_grad, graphsage.parameters()), lr= opt.lr_slf, momentum = opt.momentum_slf ) loss_Data = [] y_pred_last = 0 encoder_scheduler = StepLR(optimizer,step_size=opt.step_size ,gamma= opt.gamma) acNodes = [] nonActNod = [] for ite in range(int(maxiter)): if ite % update_interval == 0: labels_pse = np.empty((len(labels), 1), dtype=np.int64) labels_pse[train] = labels[train] test_scores = graphsage(test) test_scores = test_scores #/ temperature.unsqueeze(1).expand(test_scores.size(0), test_scores.size(1)) test_output, _ = torch.max(F.softmax(test_scores,dim = 1), dim = 1) y_pred = F.softmax(test_scores, dim = 1).data.numpy().argmax(axis=1) #conf_test_nod, unconf_test_nod = vat_sel(graphsage, test, test_scores) conf_test_nod = list((test_output > opt.thres).nonzero().data.numpy().squeeze()) unconf_test_nod = list((test_output <= opt.thres).nonzero().data.numpy().squeeze()) #print(len(list((test_output <= 0.4).nonzero().data.numpy().squeeze()))) labels_pse[ test[conf_test_nod]] = calPseLb(F.softmax(test_scores, dim = 1))[conf_test_nod] if ite % (update_interval*2) == 0: node, nonActN = activeUnL(graphsage, list(val), Variable(torch.LongTensor(labels[np.array(list(val))])).squeeze().data.numpy(), features, adj_lists, opt.num_features, opt.num_hidden, opt.num_cls, filetime, labels) acNodes.extend(node) nonActNod.extend(nonActN) train_set = train +list(acNodes) labels_pse[ acNodes ] = labels[ acNodes ] #print(len(conf_test_nod), len(actNod)) #writetofile(len(actNod), opt.res_path, filetime) #plotGraphStrc(train + acNodes + nonActNod,graphsage(train + acNodes + nonActNod).data, [0]*len(train)+[1]*len(acNodes)+[2]*len(nonActNod), adj_lists, time = filetime, name = 'act-slfT'+str(ite // update_interval)) else: train_set = train + list(test[conf_test_nod])+list(acNodes) labels_pse[ acNodes ] = labels[ acNodes ] writetofile(len(conf_test_nod), opt.res_path, filetime) plotGraphStrc(train + list(test[conf_test_nod]) + list(test[unconf_test_nod]),graphsage(train + list(test[conf_test_nod]) + list(test[unconf_test_nod])).data, [0]*len(train)+[1]*len(conf_test_nod)+[2]*len(unconf_test_nod), adj_lists, time = filetime, name = 'slfT'+str(ite // update_interval)) #plotGraphStrc(train + list(test) ,graphsage(train + list(test)).data, [0]*len(train)+ list(test_output) , adj_lists, time = filetime, name = 'slfT'+str(ite // update_interval)) print('\nIter {}: '.format(ite), end='') writetofile('Iter:'+str(ite), opt.res_path, filetime) print("Validation ACCU:", accuracy_score( labels[test], y_pred)) writetofile("Validation ACCU:" + str(accuracy_score( labels[test], y_pred)), opt.res_path, filetime) print("confidence point ACCU:", accuracy_score( labels[test[conf_test_nod]], y_pred[conf_test_nod])) writetofile("confidence point ACCU:" + str(accuracy_score( labels[test[conf_test_nod]], y_pred[conf_test_nod])), opt.res_path, filetime) # check stop criterion if ite >0: delta_label = np.sum(y_pred != y_pred_last).astype(np.float) / y_pred.shape[0] print('Fraction of documents with label changes: {} %'.format(np.round(delta_label*100, 3))) writetofile("Fraction of documents with label changes: " + str(np.round(delta_label*100, 3)), opt.res_path, filetime) y_pred_last = np.copy(y_pred) if ite > 0 and delta_label <= tol/100: print('\nFraction: {} % < tol: {} %'.format(np.round(delta_label*100, 3), tol)) print('Reached tolerance threshold. Stopping training.') writetofile('Reached tolerance threshold. Stopping training.', opt.res_path, filetime) break scores = graphsage(train_set) #scores = scores #/ temperature #v_loss = vat_loss(graphsage, train_set,scores) loss = closs(scores, Variable(torch.LongTensor(labels_pse[np.array(train_set)])).squeeze()) #+ activeL(graphsage, train_set, Variable(torch.LongTensor(labels_pse[np.array(train_set)])).squeeze(), features, adj_lists, opt.num_features, opt.num_hidden, opt.num_cls) optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() encoder_scheduler.step(ite) #index = index + 1 if (index + 1) * batch_size <= test.shape[0] else 0 loss_Data.append(loss.data) return graphsage, loss_Data
loss = criterion(pre, batch_x4) psnr = batch_PSNR(torch.clamp(pre, 0., 1.), batch_x4, 1.0) loss_list.append(loss.item()) bar.set_description("Epoch: %d Loss: %.6f" % (ep, loss_list[-1])) # Update the parameters optimizer.zero_grad() loss.backward() optimizer.step() print("[Epoch %d][G loss: %7f][PSNR : %7f]" % (ep, loss_list[-1], psnr)) Loss_list.append(np.mean(loss_list)) # Save the training image ''' if ep % opts.record_epoch == 0: img = fusePostProcess(y_f, y_hat, patch1, patch2, single = False) cv2.imwrite(os.path.join(opts.det, 'image', str(ep) + ".png"), img[0, :, :, :]) ''' # Save the training model if (ep % 100 == 0): torch.save(model.state_dict(), './Model/%d.pkl' % (ep)) if __name__ == '__main__': opts = TrainOptions().parse() train(opts) #def test(opts):
#v_loss = vat_loss(graphsage, train_set,scores) loss = closs(scores, Variable(torch.LongTensor(labels_pse[np.array(train_set)])).squeeze()) #+ activeL(graphsage, train_set, Variable(torch.LongTensor(labels_pse[np.array(train_set)])).squeeze(), features, adj_lists, opt.num_features, opt.num_hidden, opt.num_cls) optimizer.zero_grad() loss.backward(retain_graph=True) optimizer.step() encoder_scheduler.step(ite) #index = index + 1 if (index + 1) * batch_size <= test.shape[0] else 0 loss_Data.append(loss.data) return graphsage, loss_Data if __name__ == "__main__": if torch.cuda.is_available(): device = 'cuda' else: device = 'cpu' opt = TrainOptions().parse() if opt.dataset == 'cora': opt.k = 80 opt.lr_pre = 1e-4 opt.epoch = 400 elif opt.dataset =='pubmed': opt.k = 50 opt.lr_pre = 5e-4 elif opt.dataset == 'ppi': opt.lr_pre = 5e-4 opt.k = 500 opt.num_hidden = 150 opt.epoch = 1000 elif opt.dataset == 'reddit': opt.lr_pre = 5e-4 opt.k = 500