def main(): prog_args = configs.arg_parse() path = os.path.join(prog_args.logdir, io_utils.gen_prefix(prog_args)) writer = SummaryWriter(path) if prog_args.gpu: os.environ["CUDA_VISIBLE_DEVICES"] = prog_args.cuda print("CUDA", prog_args.cuda) else: print("Using CPU") # use --bmname=[dataset_name] for Reddit-Binary, Mutagenicity if prog_args.bmname is not None: benchmark_task(prog_args, writer=writer) elif prog_args.pkl_fname is not None: pkl_task(prog_args) elif prog_args.dataset is not None: if prog_args.dataset == "syn1": syn_task1(prog_args, writer=writer) elif prog_args.dataset == "syn2": syn_task2(prog_args, writer=writer) elif prog_args.dataset == "syn3": syn_task3(prog_args, writer=writer) elif prog_args.dataset == "syn4": syn_task4(prog_args, writer=writer) elif prog_args.dataset == "syn5": syn_task5(prog_args, writer=writer) elif prog_args.dataset == "enron": enron_task(prog_args, writer=writer) elif prog_args.dataset == "ppi_essential": ppi_essential_task(prog_args, writer=writer) writer.close()
def main(): prog_args = configs.arg_parse() path = os.path.join(prog_args.logdir, io_utils.gen_prefix(prog_args)) writer = SummaryWriter(path) if prog_args.gpu: os.environ["CUDA_VISIBLE_DEVICES"] = prog_args.cuda print("CUDA", prog_args.cuda) else: print("Using CPU") syn_task1(prog_args, 30, writer=writer) syn_task2(prog_args, writer=writer) syn_task1(prog_args, 300, writer=writer) syn_task1(prog_args, 3000, writer=writer) writer.close()
def main(): prog_args = configs.arg_parse() path = os.path.join(prog_args.logdir, io_utils.gen_prefix(prog_args)) writer = SummaryWriter(path) if prog_args.gpu: os.environ["CUDA_VISIBLE_DEVICES"] = prog_args.cuda print("CUDA", prog_args.cuda) else: print("Using CPU") # use --bmname=[dataset_name] for Reddit-Binary, Mutagenicity if prog_args.dataset == "FFMpeg": FFMpeg(prog_args, writer=writer) elif prog_args.dataset == "fan": fan(prog_args) elif prog_args.dataset == "reveal": reveal(prog_args, writer=writer) writer.close()
def main(): # Parsing defaults for all program parameters unless provided by user prog_args = parse_train_args.arg_parse() syn_task1(prog_args, writer=None) # writer=writer path = os.path.join(prog_args.logdir, io_utils.gen_prefix(prog_args)) print("Tensorboard writer path :\n", path) # writer = SummaryWriter(path) if prog_args.gpu: # os.environ["CUDA_VISIBLE_DEVICES"] = prog_args.cuda # env = os.environ.get('CUDA_VISIBLE_DEVICES') # print("Environment is set :", env) print('\nCUDA_VISIBLE_DEVICES') print('------------------------------------------') print("CUDA", prog_args.cuda) else: print('\n------------------------------------------') print("Using CPU") print('Batch size : {} and Dropout rate : {}'.format( prog_args.batch_size, prog_args.dropout)) print('------------------------------------------')
for epoch in range(args.num_epochs): total_loss = 0 model.train() for batch in loader: # print ('batch:', batch.feat) opt.zero_grad() pred = model(batch) pred = pred[train_mask] # print ('pred:', pred) label = labels[train_mask] # print ('label:', label) loss = model.loss(pred, label) print('loss:', loss) loss.backward() opt.step() total_loss += loss.item() * 1 total_loss /= num_train writer.add_scalar("loss", total_loss, epoch) if epoch % 10 == 0: test_acc = test(loader, model, args, labels, test_mask) print("Epoch {}. Loss: {:.4f}. Test accuracy: {:.4f}".format( epoch, total_loss, test_acc)) writer.add_scalar("test accuracy", test_acc, epoch) prog_args = arg_parse() path = os.path.join(prog_args.logdir, io_utils.gen_prefix(prog_args)) syn_task1(prog_args, writer=SummaryWriter(path))
def main(): # Parsing defaults for all program parameters unless provided by user prog_args = parse_explainer_args.arg_parse() # More params on top of train.py prog_args.writer = None # Check is for None and default is True path = os.path.join(prog_args.logdir, io_utils.gen_prefix(prog_args)) print("Tensorboard writer path :\n", path) print("No. of epochs :", prog_args.num_epochs) # writer = SummaryWriter(path) if prog_args.gpu: # os.environ["CUDA_VISIBLE_DEVICES"] = prog_args.cuda # env = os.environ.get('CUDA_VISIBLE_DEVICES') # print("Environment is set :", env) print('\nCUDA_VISIBLE_DEVICES') print('------------------------------------------') print("CUDA", prog_args.cuda) else: print('\n------------------------------------------') print("Using CPU") # Loading previously saved computational graph data (model checkpoint) model_dict = io_utils.load_ckpt(prog_args) model_optimizer = model_dict['optimizer'] print("Model optimizer :", model_optimizer) print("Model optimizer state dictionary :\n", model_optimizer.state_dict()['param_groups']) # model.load_state_dict(checkpoint['model_state_dict']) # optimizer.load_state_dict(checkpoint['optimizer_state_dict']) # epoch = checkpoint['epoch'] # loss = checkpoint['loss'] print( '------------------------------------------------------------------------------------' ) print("Keys in loaded model dictionary :", list(model_dict)) print("Keys in loaded model optimizer dictionary:", list(model_optimizer.state_dict())) print("All loaded labels :\n", model_dict['cg']['label']) print() print('mask_act:{}, mask_bias:{}, explainer_suffix:{}'.format( prog_args.mask_act, prog_args.mask_bias, prog_args.explainer_suffix)) # Determine explainer mode graph_mode = (prog_args.graph_mode or prog_args.multigraph_class >= 0 or prog_args.graph_idx >= 0) # Trained data stored in computational graph dictionary cg_dict = model_dict['cg'] input_dim = cg_dict['feat'].shape[2] num_classes = cg_dict['pred'].shape[2] print("\nLoaded model from subdirectory \"{}\" ...".format( prog_args.ckptdir)) print("input dim :", input_dim, "; num classes :", num_classes) print("Labels of retrieved data :\n", cg_dict['label']) print( '------------------------------------------------------------------------------------' ) print("Multigraph class :", prog_args.multigraph_class) print("Graph Index :", prog_args.graph_idx) print("Explainer graph mode :", graph_mode) print("Input dimension :", input_dim) print("Hidden dimension :", prog_args.hidden_dim) print("Output dimension :", prog_args.output_dim) print("Number of classes :", num_classes) print("Number of GCN layers :", prog_args.num_gc_layers) print("Batch Normalization :", prog_args.bn) model = models.GcnEncoderNode(input_dim=input_dim, hidden_dim=prog_args.hidden_dim, embedding_dim=prog_args.output_dim, label_dim=num_classes, num_layers=prog_args.num_gc_layers, bn=prog_args.bn, args=prog_args) print("\nGcnEncoderNode model :\n", model) # load state_dict (obtained by model.state_dict() when saving checkpoint) # Loading Model for Inference print("Model checked result :", model.load_state_dict(model_dict['model_state'])) print( '------------------------------------------------------------------------------------\n' ) # Explaining single node prediction print('Explaining single default node :', prog_args.explain_node) # The number of epochs used for explanation training is much smaller than the 1K epochs used for node label # trainings and predictions in the GCN. The former is trained only based on the k-hop labels which depends # on the number GCN layers (at a smaller scale, so the number of epochs can be lower without reducing the # accuracy). Whereas, the latter will affect the node predictions and thus, it will affect the accuracy of # the node explanations. print('GNN Explainer is trained based on {} epochs.'.format( prog_args.num_epochs)) print("Writer :", prog_args.writer) # Create explainer explainer = explain.Explainer( model=model, adj=cg_dict["adj"], feat=cg_dict["feat"], label=cg_dict["label"], pred=cg_dict["pred"], train_idx=cg_dict["train_idx"], args=prog_args, writer=prog_args.writer, print_training=True, graph_mode=graph_mode, graph_idx=prog_args.graph_idx, ) if prog_args.explain_node is not None: # Returned masked adjacency, edges and features of the subgraph masked_adj, masked_edges, masked_features = explainer.explain( prog_args.explain_node, unconstrained=False) print("Returned masked adjacency matrix :\n", masked_adj) print("Returned masked edges matrix :\n", masked_edges) print("Returned masked features matrix :\n", masked_features) else: print("Please provide node for explanation.")