def main(): args = ArgsInit().args if args.use_gpu: device = torch.device("cuda:" + str(args.device)) if torch.cuda.is_available() else torch.device("cpu") else: device = torch.device("cpu") dataset = OGBNDataset(dataset_name=args.dataset) args.num_tasks = dataset.num_tasks args.nf_path = dataset.extract_node_features(args.aggr) evaluator = Evaluator(args.dataset) valid_data_list = [] for i in range(args.num_evals): parts = dataset.random_partition_graph(dataset.total_no_of_nodes, cluster_number=args.valid_cluster_number) valid_data = dataset.generate_sub_graphs(parts, cluster_number=args.valid_cluster_number) valid_data_list.append(valid_data) model = DeeperGCN(args) model.load_state_dict(torch.load(args.model_load_path)['model_state_dict']) model.to(device) result = multi_evaluate(valid_data_list, dataset, model, evaluator, device) print(result) model.print_params(final=True)
def main(): args = ArgsInit().args if args.use_gpu: device = torch.device("cuda:" + str(args.device)) if torch.cuda.is_available( ) else torch.device("cpu") else: device = torch.device("cpu") args.device = device dataset = OGBNDataset(dataset_name=args.dataset) args.num_tasks = dataset.num_tasks args.nf_path = dataset.extract_node_features(args.aggr) evaluator = Evaluator(args.dataset) valid_data_list = [] for i in range(args.num_evals): parts = dataset.random_partition_graph( dataset.total_no_of_nodes, cluster_number=args.valid_cluster_number) valid_data = dataset.generate_sub_graphs( parts, cluster_number=args.valid_cluster_number) valid_data_list.append(valid_data) if args.backbone == 'deepergcn': # model = DeeperGCN(args).to(device) pass # elif args.backbone == 'deq': # model = DEQGCN(args).to(device) # elif args.backbone == 'revwt': # model = WTRevGCN(args).to(device) elif args.backbone == 'rev': model = RevGCN(args).to(device) pass else: raise Exception("unkown backbone") if torch.cuda.is_available(): model.load_state_dict( torch.load(args.model_load_path)['model_state_dict']) else: model.load_state_dict( torch.load(args.model_load_path, map_location=torch.device('cpu'))['model_state_dict']) model.to(device) with torch.cuda.amp.autocast(): result = multi_evaluate(valid_data_list, dataset, model, evaluator, device) print(result) model.print_params(final=True) peak_memuse = torch.cuda.max_memory_allocated(device) / float(1024**3) print('Peak memuse {:.2f} G'.format(peak_memuse))
def main(): args = ArgsInit().save_exp() if args.use_gpu: device = torch.device("cuda:" + str(args.device)) if torch.cuda.is_available( ) else torch.device("cpu") else: device = torch.device("cpu") logging.info('%s' % device) dataset = OGBNDataset(dataset_name=args.dataset) # extract initial node features nf_path = dataset.extract_node_features(args.aggr) args.num_tasks = dataset.num_tasks args.nf_path = nf_path logging.info('%s' % args) evaluator = Evaluator(args.dataset) criterion = torch.nn.BCEWithLogitsLoss() valid_data_list = [] for i in range(args.num_evals): parts = dataset.random_partition_graph( dataset.total_no_of_nodes, cluster_number=args.valid_cluster_number) valid_data = dataset.generate_sub_graphs( parts, cluster_number=args.valid_cluster_number) valid_data_list.append(valid_data) sub_dir = 'random-train_{}-test_{}-num_evals_{}'.format( args.cluster_number, args.valid_cluster_number, args.num_evals) logging.info(sub_dir) model = DeeperGCN(args).to(device) optimizer = optim.Adam(model.parameters(), lr=args.lr) results = { 'highest_valid': 0, 'final_train': 0, 'final_test': 0, 'highest_train': 0 } start_time = time.time() for epoch in range(1, args.epochs + 1): # do random partition every epoch train_parts = dataset.random_partition_graph( dataset.total_no_of_nodes, cluster_number=args.cluster_number) data = dataset.generate_sub_graphs(train_parts, cluster_number=args.cluster_number, ifmask=True) epoch_loss = train(data, dataset, model, optimizer, criterion, device) logging.info('Epoch {}, training loss {:.4f}'.format( epoch, epoch_loss)) model.print_params(epoch=epoch) result = multi_evaluate(valid_data_list, dataset, model, evaluator, device) if epoch % 5 == 0: logging.info('%s' % result) train_result = result['train']['rocauc'] valid_result = result['valid']['rocauc'] test_result = result['test']['rocauc'] if valid_result > results['highest_valid']: results['highest_valid'] = valid_result results['final_train'] = train_result results['final_test'] = test_result save_ckpt(model, optimizer, round(epoch_loss, 4), epoch, args.model_save_path, sub_dir, name_post='valid_best') if train_result > results['highest_train']: results['highest_train'] = train_result logging.info("%s" % results) end_time = time.time() total_time = end_time - start_time logging.info('Total time: {}'.format( time.strftime('%H:%M:%S', time.gmtime(total_time))))
def main(): args = ArgsInit().save_exp() logging.getLogger().setLevel(logging.INFO) writer = SummaryWriter(log_dir=args.save) if args.use_gpu: device = torch.device("cuda:" + str(args.device)) if torch.cuda.is_available( ) else torch.device("cpu") else: device = torch.device("cpu") logging.info('%s' % device) dataset = OGBNDataset(dataset_name=args.dataset) # extract initial node features nf_path = dataset.extract_node_features(args.aggr) args.num_tasks = dataset.num_tasks args.nf_path = nf_path logging.info('%s' % args) evaluator = Evaluator(args.dataset) criterion = torch.nn.BCEWithLogitsLoss() valid_data_list = [] for i in range(args.num_evals): parts = dataset.random_partition_graph( dataset.total_no_of_nodes, cluster_number=args.valid_cluster_number) valid_data = dataset.generate_sub_graphs( parts, cluster_number=args.valid_cluster_number) valid_data_list.append(valid_data) sub_dir = 'random-train_{}-test_{}-num_evals_{}'.format( args.cluster_number, args.valid_cluster_number, args.num_evals) logging.info(sub_dir) if args.backbone == 'deepergcn': # model = DeeperGCN(args).to(device) pass # elif args.backbone == 'deq': # model = DEQGCN(args).to(device) # elif args.backbone == 'revwt': # model = WTRevGCN(args).to(device) elif args.backbone == 'rev': model = RevGCN(args).to(device) else: raise Exception("unkown backbone") logging.info('# of params: {}'.format( sum(p.numel() for p in model.parameters()))) logging.info('# of learnable params: {}'.format( sum(p.numel() for p in model.parameters() if p.requires_grad))) optimizer = optim.Adam(model.parameters(), lr=args.lr) results = { 'highest_valid': 0, 'final_train': 0, 'final_test': 0, 'highest_train': 0 } start_time = time.time() for epoch in range(1, args.epochs + 1): # do random partition every epoch train_parts = dataset.random_partition_graph( dataset.total_no_of_nodes, cluster_number=args.cluster_number) data = dataset.generate_sub_graphs(train_parts, cluster_number=args.cluster_number) epoch_loss = train(data, dataset, model, optimizer, criterion, device, epoch=epoch) logging.info('Epoch {}, training loss {:.4f}'.format( epoch, epoch_loss)) if epoch == 1: peak_memuse = torch.cuda.max_memory_allocated(device) / float(1024 **3) logging.info('Peak memuse {:.2f} G'.format(peak_memuse)) torch.cuda.empty_cache() model.print_params(epoch=epoch) with torch.cuda.amp.autocast(): result = multi_evaluate(valid_data_list, dataset, model, evaluator, device, epoch=epoch) if epoch % 5 == 0: logging.info('%s' % result) train_result = result['train']['rocauc'] valid_result = result['valid']['rocauc'] test_result = result['test']['rocauc'] writer.add_scalar('stats/train_rocauc', train_result, epoch) writer.add_scalar('stats/valid_rocauc', valid_result, epoch) writer.add_scalar('stats/test_rocauc', test_result, epoch) if valid_result > results['highest_valid']: results['highest_valid'] = valid_result results['final_train'] = train_result results['final_test'] = test_result save_ckpt(model, optimizer, round(epoch_loss, 4), epoch, args.model_save_path, sub_dir, name_post='valid_best') if train_result > results['highest_train']: results['highest_train'] = train_result logging.info("%s" % results) end_time = time.time() total_time = end_time - start_time logging.info('Total time: {}'.format( time.strftime('%d-%H:%M:%S', time.gmtime(total_time))))