Exemplo n.º 1
0
def run(is_train, hidden, data_name, random_state, use_cuda, save_path):

    if is_train is False and save_path is None:
        raise RuntimeError("There is no file for test.")

    device = "cpu"
    if use_cuda:
        device = "cuda"

    # Load Train data
    train_data = load_data(data_name=data_name,
                           train=True,
                           random_state=random_state)
    dim_features = train_data[0].shape[1]
    num_labels = max(train_data[2]).item() + 1

    model = GCN(dim_features, hidden, num_labels)

    # Train Phase
    if is_train:
        print("---Training Start---")
        train(model, train_data, device, save_path)
        print("---Training Done--- \n")

    if save_path is not None:
        # Load model
        model.load_state_dict(torch.load(save_path))

    # Test Phase
    print("---Test Start---")
    test_data = load_data(data_name=data_name, train=False, random_state=42)
    print("Test Accuracy: %2.2f %%" % get_acc(model, test_data, device))
    print("---Test Done--- \n")
Exemplo n.º 2
0
def main():
    from pre_process import preprocess
    feature, a_hat, labels = preprocess()
    print("loaded")

    selected, unselected = depart(len(labels), 1 - Config.test_ratio)
    labels_selected = labels[selected]
    labels_unselected = labels[unselected]

    feature = torch.from_numpy(feature).float().cuda()
    tensor_selected = torch.tensor(labels_selected).long().cuda()
    a_hat = torch.tensor(a_hat).float().cuda()
    net = GCN(a_hat, feature.shape[1], Config.num_classes, Config.hidden_size,
              Config.n_hidden_layer).cuda()

    print(net)

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(net.parameters(), lr=Config.lr)
    net.train()
    for e in range(Config.num_epochs):
        optimizer.zero_grad()
        output = net(feature)
        loss = criterion(output[selected], tensor_selected)
        loss.backward()
        optimizer.step()

        trained_accuracy = evaluate(output[selected], labels_selected)
        untrained_accuracy = evaluate(output[unselected], labels_unselected)
        print(
            "[Epoch %d]: trained acc: %.7f, untrained acc: %.7f, loss: %.7f" %
            (e, trained_accuracy, untrained_accuracy,
             loss.detach().cpu().numpy()))
Exemplo n.º 3
0
def gen_model(args):
    if args.use_labels:
        model = GCN(
            in_feats + n_classes, args.n_hidden, n_classes, args.n_layers, F.relu, args.dropout, args.use_linear
        )
    else:
        model = GCN(in_feats, args.n_hidden, n_classes, args.n_layers, F.relu, args.dropout, args.use_linear)
    return model
    def test_memorize_minibatch(self):
        for db_name in self.db_names:
            db_info = get_db_info(db_name)
            train_data, val_data, _ = get_train_val_test_datasets(
                dataset_name=db_name,
                train_test_split='use_full_train',
                encoders=dict(CATEGORICAL='CategoricalOrdinalEnc',
                              SCALAR='ScalarRobustScalerEnc',
                              DATETIME='DatetimeScalarEnc',
                              LATLONG='LatLongScalarEnc',
                              TEXT='TextSummaryScalarEnc'),
            )
            train_loader = get_dataloader(
                dataset=train_data,
                batch_size=256,
                sampler_class_name='SequentialSampler',
                num_workers=0,
                max_nodes_per_graph=False)

            writer = DummyWriter()
            model = GCN(writer,
                        db_info=db_info,
                        hidden_dim=256,
                        n_init_layers=3,
                        activation_class_name='SELU',
                        activation_class_kwargs={},
                        loss_class_kwargs={},
                        loss_class_name='CrossEntropyLoss',
                        p_dropout=0.0,
                        drop_whole_embeddings=True,
                        n_layers=3,
                        readout_class_name='AvgPooling',
                        readout_kwargs={})
            if torch.cuda.is_available():
                model.cuda()
                model.device = torch.device('cuda:0')
            else:
                model.device = torch.device('cpu')
            model.train()
            optimizer = AdamW(model.parameters(), lr=0.001, weight_decay=0.0)

            bdgl, features, label = next(iter(train_loader))
            recursive_to((bdgl, features, label), model.device)
            for _ in tqdm(range(200)):
                optimizer.zero_grad()
                output = model(bdgl, features)
                loss = model.loss_fxn(output, label)
                if loss < 1e-4:
                    break
                loss.backward()
                optimizer.step()
            else:
                tqdm.write(f'Loss: {loss}')
                self.fail("Didn't memorize minibatch")
Exemplo n.º 5
0
def main():
    
    # Load data
    start = time.time()
    N, _adj, _feats, _labels, train_adj, train_feats, train_nodes, val_nodes, test_nodes, y_train, y_val, y_test, val_mask, test_mask = utils.load_data(args.dataset)
    print('Loaded data in {:.2f} seconds!'.format(time.time() - start))
    
    # Prepare Train Data
    start = time.time()
    _, parts = utils.partition_graph(train_adj, train_nodes, args.num_clusters_train)
    parts = [np.array(pt) for pt in parts]
    train_features, train_support, y_train = utils.preprocess_multicluster(train_adj, parts, train_feats, y_train, args.num_clusters_train, args.batch_size)    
    print('Train Data pre-processed in {:.2f} seconds!'.format(time.time() - start))
    
    # Prepare Test Data
    if args.test == 1:    
        y_test, test_mask = y_val, val_mask
        start = time.time()
        _, test_features, test_support, y_test, test_mask = utils.preprocess(_adj, _feats, y_test, np.arange(N), args.num_clusters_test, test_mask) 
        print('Test Data pre-processed in {:.2f} seconds!'.format(time.time() - start))
    
    # Shuffle Batches
    batch_idxs = list(range(len(train_features)))
    
    # model
    model = GCN(fan_in=_in, fan_out=_out, layers=args.layers, dropout=args.dropout, normalize=True, bias=False).float()
    model.cuda()

    # Loss Function
    criterion = torch.nn.CrossEntropyLoss()
    
    # Optimization Algorithm
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
        
    # Learning Rate Schedule    
    scheduler = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr=args.lr, steps_per_epoch=int(args.num_clusters_train/args.batch_size), epochs=args.epochs+1, anneal_strategy='linear')
    model.train()

    
    # Train
    for epoch in range(args.epochs + 1):
        np.random.shuffle(batch_idxs)
        avg_loss = 0
        start = time.time()
        for batch in batch_idxs:
            loss = train(model.cuda(), criterion, optimizer, train_features[batch], train_support[batch], y_train[batch], dataset=args.dataset)
            if args.lr_scheduler == 1:
                scheduler.step()
            avg_loss += loss.item()
        
        # Write Train stats to tensorboard
        writer.add_scalar('time/train', time.time() - start, epoch)
        writer.add_scalar('loss/train', avg_loss/len(train_features), epoch)
        
    if args.test == 1:    
        # Test on cpu
        f1 = test(model.cpu(), test_features, test_support, y_test, test_mask, device='cpu')
        print('f1: {:.4f}'.format(f1))
Exemplo n.º 6
0
def train(config):

    # Set random seed
    tf.random.set_seed(config.seed)
    np.random.seed(config.seed)

    # Load dataset
    adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(
        config.dataset
    )

    # Feature preprocessing
    norm_feat = preprocess_features(features)
    sparse_norm_feat = sparse_matrix_to_sparse_tensor(norm_feat)

    if config.model.name == "gcn":
        support = [preprocess_adj(adj)]
        num_supports = 1
    elif config.model.name == "gcn_cheby":
        support = chebyshev_polynomials(adj, config.model.max_degree)
        num_supports = 1 + config.model.max_degree
    else:
        raise ValueError("Invalid argument for model: {0}".format(config.model.name))

    model = GCN(
        input_dim=sparse_norm_feat.shape,
        output_dim=y_train.shape[1],
        supports=support,
        num_features_nonzero=tuple(sparse_norm_feat.values.shape.as_list()),
        config=config.model,
    )

    train_generator = dataset_generator(sparse_norm_feat, y_train, train_mask)
    val_generator = dataset_generator(sparse_norm_feat, y_val, val_mask)

    # Train
    history = model.fit_generator(
        train_generator,
        steps_per_epoch=1,
        epochs=config.training.epochs,
        verbose=2,
        validation_data=val_generator,
        validation_steps=1,
    )

    # Evaluate
    test_loss, test_acc = model.test_on_batch(
        sparse_norm_feat, np.column_stack((y_test, test_mask))
    )

    print("Test set results: cost= {:.5f}, accuracy={:.5f}".format(test_loss, test_acc))
Exemplo n.º 7
0
def main():
    config = get_args()
    dataset = Data(config)

    num_features = dataset.features.shape[1]
    num_classes = dataset.labels.max().item() + 1

    model = GCN(config=config, num_features=num_features, num_classes=num_classes)
    solver = Solver(config, model, dataset)

    if torch.cuda.is_available():
        model = model.to('cuda')

    criterion, best_model = solver.train()
    solver.test(criterion, best_model)
def gen_model(in_feats, n_classes, args):
    norm = args.norm
    use_attn_dst = not args.no_attn_dst
    if args.use_labels:
        in_feats_ = in_feats + n_classes
    else:
        in_feats_ = in_feats

    if args.model == "gcn":
        model = GCN(in_feats_, args.n_hidden, n_classes, args.n_layers, F.relu,
                    args.dropout, args.use_linear)

    if args.model == "gcn-ha":
        model = GCNHA(
            in_feats_,
            n_classes,
            K=args.K,
            n_hidden=args.n_hidden,
            n_layers=args.n_layers,
            n_heads=args.n_heads,
            activation=F.relu,
            dropout=args.dropout,
            input_drop=args.input_drop,
            attn_drop=args.attn_drop,
        )

    if args.model == "gat":
        model = GAT(
            in_feats_,
            n_classes,
            n_hidden=args.n_hidden,
            n_layers=args.n_layers,
            n_heads=args.n_heads,
            activation=F.relu,
            dropout=args.dropout,
            input_drop=args.input_drop,
            attn_drop=args.attn_drop,
            edge_drop=args.edge_drop,
            use_attn_dst=use_attn_dst,
            use_symmetric_norm=(norm == "sym"),
        )

    if args.model == "gat-ha":
        model = GATHA(
            in_feats_,
            n_classes,
            K=args.K,
            n_hidden=args.n_hidden,
            n_layers=args.n_layers,
            n_heads=args.n_heads,
            activation=F.relu,
            dropout=args.dropout,
            input_drop=args.input_drop,
            edge_drop=args.edge_drop,
            attn_drop=args.attn_drop,
            use_attn_dst=use_attn_dst,
            norm=norm,
        )

    return model
def build_model(model_name, num_layers, mlp_layers, hidden_dim, num_classes,
                dropout_rate, num_heads, learn_eps, sparse):
  """Create gnn model and initialize parameters weights."""
  # Convert hidden_dim to integers
  for i in range(len(hidden_dim)):
    hidden_dim[i] = int(hidden_dim[i])

  # Only GCN, GAT and GIN are available.
  if model_name == 'gcn':
    model = GCN(
        num_layers=num_layers,
        hidden_dim=hidden_dim,
        num_classes=num_classes,
        dropout_rate=dropout_rate,
        sparse=sparse,
        bias=True)
  elif model_name == 'gat':
    model = GAT(
        num_layers=num_layers,
        hidden_dim=hidden_dim,
        num_classes=num_classes,
        dropout_rate=dropout_rate,
        num_heads=num_heads,
        sparse=sparse)
  elif model_name == 'gin':
    model = GIN(
        num_layers=num_layers,
        mlp_layers=mlp_layers,
        hidden_dim=hidden_dim,
        num_classes=num_classes,
        dropout_rate=dropout_rate,
        learn_eps=learn_eps,
        sparse=sparse)

  return model
Exemplo n.º 10
0
 def train(self):
     """
         Trains the model based on configurations specified in __init__
     """
     # Model and optimizer
     model = GCN(nfeat=self.features.shape[1],
                 nhid=self.hidden,
                 nclass=self.labels.max().item() + 1,
                 dropout=self.dropout)
     optimizer = optim.Adam(model.parameters(),
                            lr=self.lr,
                            weight_decay=self.weight_decay)
     # Train model
     for _ in range(self.epochs):
         train(model, self.features, self.adj, self.idx_train, self.labels,
               optimizer)
     return model
Exemplo n.º 11
0
def run(n_trials=100):
    start_time = time.time()
    # Load data
    adj, features, labels, idx_train, idx_test = load_data()

    epochs = 200
    lr = 0.01
    weight_decay = 5e-4
    hidden = 16
    dropout = 0.5
    for i in range(n_trials):

        # Model and optimizer
        model = GCN(nfeat=features.shape[1],
                    nhid=hidden,
                    nclass=labels.max().item() + 1,
                    dropout=dropout)
        optimizer = optim.Adam(model.parameters(),
                               lr=lr,
                               weight_decay=weight_decay)
        # Train model
        for _ in range(epochs):
            train(model, features, adj, idx_train, labels, optimizer)
        done_training_time = time.time()

        # Testing
        metrics = test(model, features, adj, idx_test, labels)
        params = model.state_dict()
        for k in params:
            params[k] = params[k].reshape(-1).data.tolist()
        final_results = {**params, **metrics}
        done_testing_time = time.time()

        # Cache to file
        records_filename = "records.csv"
        write_results_to_file(records_filename, final_results)

        # report_progress(i, start_time, done_training_time,
        #                   done_testing_time, end_time)
    end_time = time.time()
    speed = n_trials / (end_time - start_time)
    print(f"{n_trials} tasks completed in {end_time - start_time}.")
    print(f"{round(speed, 3)} tasks/second for non-parallel implementation.")
Exemplo n.º 12
0
def main(dataset, times):

    adj, features, labels, idx_train, idx_val, idx_test = load_data(dataset)

    features = features.to(device)
    adj = adj.to(device)
    labels = labels.to(device)
    idx_train = idx_train.to(device)
    idx_val = idx_val.to(device)
    idx_test = idx_test.to(device)

    nclass = labels.max().item() + 1

    acc_lst = list()
    for seed in random.sample(range(0, 100000), times):
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed(seed)

        # Model and optimizer
        # weight_decay 权值衰减,防止过拟合,调节模型复杂度对损失函数的影响
        model = GCN(nfeat=features.shape[1],
                    nhid=args.hidden,
                    nclass=nclass,
                    dropout=args.dropout)
        optimizer = optim.Adam(model.parameters(),
                               lr=args.lr,
                               weight_decay=args.weight_decay)
        model.to(device)

        # Train model
        t_total = time.time()
        for epoch in range(args.epochs):
            train(epoch, model, optimizer, adj, features, labels, idx_train,
                  idx_val)
        print(f"Total time elapsed: {time.time() - t_total:.4f}s")

        # Testing
        acc_lst.append(test(model, adj, features, labels, idx_test))

    print(acc_lst)
    print(np.mean(acc_lst))
Exemplo n.º 13
0
def build_model(model_name, num_layers, hidden_dim, num_classes, dropout_rate):
    """Create gnn model and initialize parameters weights."""
    # Convert hidden_dim to integers
    for i in range(len(hidden_dim)):
        hidden_dim[i] = int(hidden_dim[i])

    # Only gcn available now
    if model_name == 'gcn':
        model = GCN(num_layers=num_layers,
                    hidden_dim=hidden_dim,
                    num_classes=num_classes,
                    dropout_rate=dropout_rate,
                    bias=True)
    elif model_name == 'gat':
        raise NotImplementedError

    return model
Exemplo n.º 14
0
    def __init__(self, nfeat, hidden_sizes, nclass, nnodes, lambda_, device):
        super(BaseMeta, self).__init__()

        self.hidden_sizes = hidden_sizes
        self.nfeat = nfeat
        self.nclass = nclass
        
        self.lambda_ = lambda_        
        self.device = device
        
        self.gcn = GCN(nfeat=nfeat,
                       nhid=hidden_sizes[0],
                       nclass=nclass).to(self.device)

        self.nnodes = nnodes
        self.adj_changes = Parameter(torch.FloatTensor(nnodes, nnodes)).to(self.device)
        self.adj_changes.data.fill_(0)
Exemplo n.º 15
0
def main():
    data_generator = DataGenerator(args)
    meta_model = GCN(nfeat=args.in_f_d,
                     nhid=args.hidden,
                     nclass=args.nclasses,
                     dropout=args.dropout).to(device)
    proto_model = GCN_Proto(args, nfeat=args.hidden, dropout=args.dropout).to(device)
    structure_model = GCN_Structure(args, nfeat=args.hidden, nhid=args.structure_dim, dropout=args.dropout).to(
        device)

    if args.train:
        meta_optimiser = torch.optim.Adam(
            list(meta_model.parameters()) + list(proto_model.parameters()) + list(structure_model.parameters()),
            lr=args.meta_lr, weight_decay=args.weight_decay)
        train(args, meta_model, meta_optimiser, proto_model, structure_model,
              metatrain_iterations=args.metatrain_iterations,
              data_generator=data_generator, fit_function=meta_gradient_step,
              fit_function_kwargs={'train': True, 'inner_train_steps': args.inner_train_steps,
                                   'inner_lr': args.inner_lr, 'batch_n': args.batch_n, 'device': device})

    else:
        if args.test_load_epoch > 0:
            meta_model.load_state_dict(
                torch.load(args.logdir + '/' + exp_string + '/' + 'model_epoch_{}'.format(args.test_load_epoch)))
            proto_model.load_state_dict(
                torch.load(
                    args.logdir + '/' + exp_string + '/' + 'proto_model_epoch_{}'.format(args.test_load_epoch)))
            structure_model.load_state_dict(
                torch.load(
                    args.logdir + '/' + exp_string + '/' + 'structure_model_epoch_{}'.format(
                        args.test_load_epoch)))
        meta_optimiser = torch.optim.Adam(list(meta_model.parameters()) + list(proto_model.parameters()),
                                          lr=args.meta_lr, weight_decay=args.weight_decay)
        evaluate(args, meta_model, meta_optimiser, proto_model, structure_model, data_generator=data_generator,
                 fit_function=meta_gradient_step,
                 fit_function_kwargs={'train': False, 'inner_train_steps': args.inner_train_steps,
                                      'inner_lr': args.inner_lr_test, 'batch_n': args.test_sample_g_n,
                                      'device': device})
Exemplo n.º 16
0
def main(args):
    start_time_str = time.strftime("_%m_%d_%H_%M_%S", time.localtime())
    log_path = os.path.join(args.log_dir, args.model + start_time_str)
    if not os.path.exists(log_path):
        os.mkdir(log_path)

    logging.basicConfig(filename=os.path.join(log_path, 'log_file'),
                        filemode='w',
                        format='| %(asctime)s |\n%(message)s',
                        datefmt='%b %d %H:%M:%S',
                        level=logging.INFO)
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))

    logging.info(args)
    # load data
    if args.model in ['EGNN', 'ECConv', 'GTEA-ST']:
        data = Dataset(data_dir=args.data_dir, batch_size=args.batch_size, use_static=True)
    else:
        data = Dataset(data_dir=args.data_dir, batch_size=args.batch_size)

    
    

    g = data.g

    # features = torch.FloatTensor(data.features)
    # labels = torch.LongTensor(data.labels)

    train_loader = data.train_loader
    val_loader = data.val_loader
    test_loader = data.test_loader

    num_nodes = data.num_nodes
    node_in_dim = args.node_in_dim

    num_edges = data.num_edges
    edge_in_dim = data.edge_in_dim
    edge_timestep_len = data.edge_timestep_len    

    num_train_samples = data.num_train_samples
    num_val_samples = data.num_val_samples
    num_test_samples = data.num_test_samples

    logging.info("""----Data statistics------'
      #Nodes %d
      #Edges %d
      #Node_feat %d
      #Edge_feat %d
      #Edge_timestep %d
      #Train samples %d
      #Val samples %d
      #Test samples %d""" %
          (num_nodes, num_edges, 
           node_in_dim, edge_in_dim, edge_timestep_len,
              num_train_samples,
              num_val_samples,
              num_test_samples))
    


    device = torch.device("cuda:"+str(args.gpu) if torch.cuda.is_available() and args.gpu >=0 else "cpu")
    infer_device = device if args.infer_gpu else torch.device('cpu')

    # g = g.to(device)


    # create  model   

    if args.model == 'GCN':

        model = GCN(num_nodes=num_nodes,
                        in_feats=node_in_dim, 
                        n_hidden=args.node_hidden_dim, 
                        n_layers=args.num_layers,
                        activation=F.relu,
                        dropout=args.dropout)
    elif args.model == 'GraphSAGE':

        model = GraphSAGE(num_nodes=num_nodes,
                            in_feats=node_in_dim, 
                            n_hidden=args.node_hidden_dim, 
                            n_layers=args.num_layers,
                            activation=F.relu,
                            dropout=args.dropout)
    elif args.model == 'GAT':

        model = GAT(num_nodes=num_nodes,                 
                 in_dim=node_in_dim,
                 hidden_dim=args.node_hidden_dim,
                 num_layers=args.num_layers,
                 num_heads=args.num_heads)
    elif args.model == 'ECConv':
        model = ECConv(num_nodes=num_nodes,                 
                 node_in_dim=node_in_dim,
                 edge_in_dim=edge_in_dim,
                 hidden_dim=args.node_hidden_dim,
                 num_layers=args.num_layers,
                 drop_prob=args.dropout,
                 device=device)
    elif args.model == 'EGNN':
        model = EGNN(num_nodes=num_nodes,                 
                 node_in_dim=node_in_dim,
                 edge_in_dim=edge_in_dim,
                 hidden_dim=args.node_hidden_dim,
                 num_layers=args.num_layers,
                 drop_prob=args.dropout,
                 device=device)
    elif args.model == 'GTEA-ST':
        model = GTEAST(num_nodes=num_nodes,                 
                 node_in_dim=node_in_dim,
                 edge_in_dim=edge_in_dim,
                 node_hidden_dim=args.node_hidden_dim,
                 num_layers=args.num_layers,
                 drop_prob=args.dropout,
                 device=device)

    elif args.model == 'TGAT':
        model = TGAT(num_nodes=num_nodes, 
                        node_in_dim=node_in_dim, 
                        node_hidden_dim=args.node_hidden_dim, 
                        edge_in_dim=edge_in_dim-1, 
                        time_hidden_dim=args.time_hidden_dim, 
                        num_class=0, 
                        num_layers=args.num_layers, 
                        num_heads=args.num_heads, 
                        device=device, 
                        drop_prob=args.dropout)
    elif args.model == 'GTEA-LSTM':
        model = GTEALSTM(num_nodes=num_nodes,
                           node_in_dim=node_in_dim, 
                           node_hidden_dim=args.node_hidden_dim,
                           edge_in_dim=edge_in_dim, 
                           num_class=0, 
                           num_layers=args.num_layers, 
                           num_time_layers=args.num_lstm_layers, 
                           bidirectional=args.bidirectional,
                           device=device, 
                           drop_prob=args.dropout)
    elif args.model == 'GTEA-LSTM+T2V':
        model = GTEALSTMT2V(num_nodes=num_nodes,
                           node_in_dim=node_in_dim, 
                           node_hidden_dim=args.node_hidden_dim,
                           edge_in_dim=edge_in_dim-1, 
                           time_hidden_dim=args.time_hidden_dim,
                           num_class=0, 
                           num_layers=args.num_layers, 
                           num_time_layers=args.num_lstm_layers, 
                           bidirectional=args.bidirectional,
                           device=device, 
                           drop_prob=args.dropout)
    elif args.model == 'GTEA-Trans':
        model = GTEATrans(num_nodes=num_nodes,
                           node_in_dim=node_in_dim, 
                           node_hidden_dim=args.node_hidden_dim,
                           edge_in_dim=edge_in_dim, 
                           num_class=0, 
                           num_layers=args.num_layers, 
                           num_heads=args.num_heads,
                           num_time_layers=args.num_lstm_layers, 
                           device=device, 
                           drop_prob=args.dropout)
    elif args.model == 'GTEA-Trans+T2V':
        model = GTEATransT2V(num_nodes=num_nodes,
                           node_in_dim=node_in_dim, 
                           node_hidden_dim=args.node_hidden_dim,
                           edge_in_dim=edge_in_dim-1, 
                           time_hidden_dim=args.time_hidden_dim,
                           num_class=0, 
                           num_layers=args.num_layers, 
                           num_heads=args.num_heads,
                           num_time_layers=args.num_lstm_layers, 
                           device=device, 
                           drop_prob=args.dropout)
    else:
        logging.info('Model {} not found.'.format(args.model))
        exit(0)

    # send model to device
    model.to(device)

    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)

    checkpoint_path = os.path.join(log_path, str(args.model) + '_checkpoint.pt')
    
    trainer = Trainer(g=g,
                     model=model, 
                     optimizer=optimizer, 
                     epochs=args.epochs, 
                     train_loader=train_loader, 
                     val_loader=val_loader, 
                     test_loader=test_loader,
                     patience=args.patience, 
                     batch_size=args.batch_size,
                     num_neighbors=args.num_neighbors, 
                     num_layers=args.num_layers, 
                     num_workers=args.num_workers, 
                     device=device,
                     infer_device=infer_device, 
                     log_path=log_path,
                     checkpoint_path=checkpoint_path)

    logging.info('Start training')

    best_val_result, test_result = trainer.train()

    # recording the result
    line = [start_time_str[1:]] + [args.model] + ['K=' + str(args.use_K)] + \
    [str(x) for x in best_val_result] + [str(x) for x in test_result] + [str(args)]
    line = ','.join(line) + '\n'

    with open(os.path.join(args.log_dir, str(args.model) + '_result.csv'), 'a') as f:
        f.write(line)
Exemplo n.º 17
0
mean_time_list = []
total_time_list = []

# repeat multiple runs
for i in range(args.repeat):
    t_total = time.time()

    # Load data
    adj_train, adj_val, adj_test, features, labels, idx_train, idx_val, \
        idx_test = load_data(args.dataset, args.label_rate, inductive, loop, norm)

    # Model and optimizer
    if args.model == 'GCN':
        model = GCN(nfeat=features.shape[1],
                    nhid=args.hidden,
                    nclass=labels.max().item() + 1,
                    dropout=args.dropout,
                    node_dropout=args.node_dropout,
                    edge_dropout=args.edge_dropout)
    elif args.model == 'GCN1':
        model = GCN1(nfeat=features.shape[1],
                     nhid=args.hidden,
                     nclass=labels.max().item() + 1,
                     dropout=args.dropout,
                     edge_dropout=args.edge_dropout)
    elif args.model == 'GCN_Linear':
        model = GCN_Linear(nfeat=features.shape[1],
                           nhid=args.hidden,
                           nclass=labels.max().item() + 1,
                           dropout=args.dropout)
    elif args.model == 'Linear_GCN':
        model = Linear_GCN(nfeat=features.shape[1],
Exemplo n.º 18
0
    tf.sparse_placeholder(tf.float32),
    'features':
    tf.sparse_placeholder(tf.float32,
                          shape=tf.constant(features[2], dtype=tf.int64)),
    'labels':
    tf.placeholder(tf.float32, shape=(None, y_train.shape[1])),
    'labels_mask':
    tf.placeholder(tf.int32),
    'dropout':
    tf.placeholder_with_default(0., shape=()),
    'num_features_nonzero':
    tf.placeholder(tf.int32)  # helper variable for sparse dropout
}

# Create model
model = GCN(placeholders, input_dim=features[2][1], logging=True)

# Initialize session
sess = tf.Session()


def construct_feed_dict(features, support_, labels, labels_mask, placeholders):
    """Construct feed dictionary."""
    feed_dict = dict()
    feed_dict.update({placeholders['labels']: labels})
    feed_dict.update({placeholders['labels_mask']: labels_mask})
    feed_dict.update({placeholders['features']: features})
    feed_dict.update({placeholders['support']: support_})
    feed_dict.update({placeholders['num_features_nonzero']: features[1].shape})
    return feed_dict
Exemplo n.º 19
0
model_ts = GCNLink(nfeat=nfeat,
                   nhid=args.hidden,
                   nout=args.embed_dim,
                   dropout=args.dropout)

model_cluster = GCNClusterNet(nfeat=nfeat,
                              nhid=args.hidden,
                              nout=args.embed_dim,
                              dropout=args.dropout,
                              K=args.K,
                              cluster_temp=args.clustertemp)

#keep a couple of initializations here so that the random seeding lines up
#with results reported in the paper -- removing these is essentially equivalent to
#changing the seed
_ = GCN(nfeat, args.hidden, args.embed_dim, args.dropout)
_ = nn.Parameter(torch.rand(K, args.embed_dim))

#uses GCNs to predict the cluster membership of each node
model_gcn = GCNDeep(nfeat=nfeat,
                    nhid=args.hidden,
                    nout=args.K,
                    dropout=args.dropout,
                    nlayers=2)

#uses GCNs to predict the probability that each node appears in the solution
model_gcn_x = GCNDeepSigmoid(nfeat=nfeat,
                             nhid=args.hidden,
                             nout=1,
                             dropout=args.dropout,
                             nlayers=2)
Exemplo n.º 20
0
dataset_test = MyDataset(dataset, file_list_test, and_or=and_or, args=args)
dataloader_train = DataLoader(dataset_train,
                              batch_size=args.batch_size,
                              shuffle=True,
                              num_workers=args.dataloader_workers)
dataloader_test = DataLoader(dataset_test,
                             batch_size=1,
                             shuffle=False,
                             num_workers=args.dataloader_workers)

print('Number of training samples:', len(dataset_train))

# Model and optimizer
model = GCN(input_size=args.features,
            hidden_size=args.hidden_size,
            output_size=args.embedding_size,
            dropout=args.dropout,
            indep_weights=indep_weights)
mlp = MLP(input_size=2 * args.embedding_size,
          hidden_size=args.hidden_size_mlp,
          output_size=2,
          dropout=args.dropout)
optimizer = optim.Adam(itertools.chain(model.parameters(), mlp.parameters()),
                       lr=args.lr,
                       weight_decay=args.weight_decay)

creterion = torch.nn.TripletMarginLoss(margin=args.margin, p=2)
CE = torch.nn.CrossEntropyLoss()

loss_list = deque(maxlen=100)
loss_list_CE = deque(maxlen=100)
Exemplo n.º 21
0
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()

random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.cuda:
    torch.cuda.manual_seed(args.seed)

# Load feature vectors and labels of original graph
features, labels = load_data('pubmed')

model = GCN(nfeat=features.shape[1],
            nhid=args.hidden,
            nclass=int(labels.max()) + 1,
            dropout=args.dropout,
            alpha=args.alpha)
optimizer = optim.Adam(model.parameters(),
                       lr=args.lr,
                       weight_decay=args.weight_decay)

if args.cuda:
    model.cuda()
    features = features.cuda()
    labels = labels.cuda()

features, labels = Variable(features), Variable(labels)


def train(epoch, features, adj, labels, idx_train, idx_val):
Exemplo n.º 22
0
# processed_adj = np.dot(d_inv,adj)
# processed_adj = np.dot(adj,d_inv)
# adj = sp.csr_matrix(processed_adj)
# adj = sparse_mx_to_torch_sparse_tensor(adj)
# adj = torch.from_numpy(adj).float()


def accuracy(output, labels):
    preds = output.max(1)[1].type_as(labels)
    correct = preds.eq(labels).double()
    correct = correct.sum()
    return correct / len(labels)


# model creation and loss seting
model = GCN(nfeat=1, nhid=256, nclass=10).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)

###Training Part
start_time = time.time()
for epoch in range(num_epochs):

    for i, (adj, features, masks,
            batch_labels) in tqdm(enumerate(train_loader)):
        # input = feature[1].view(784,1)
        features = features.to(device)
        batch_labels = batch_labels.to(device)
        adj = adj.to(device)
        t = time.time()
Exemplo n.º 23
0
def train_step(model_file):
    # Set random seed
    seed = 123
    np.random.seed(seed)
    tf.set_random_seed(seed)
    dirname = FLAGS.dataset + model_file + '/'
    print(dirname)
    # Load data
    namelist_path = FLAGS.dataset + 'sub_list_new.txt'
    adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(
        FLAGS.hemisphere, namelist=namelist_path, MPM=True)
    # neighbor_mask = 50*np.load( FLAGS.modeldir+'neighbor_mask_{}.npy'.format(FLAGS.hemisphere))
    namelist = [
        str(name).replace("\n", "")
        for name in open(namelist_path, 'r').readlines()
    ]
    # mpm = np.max(np.load( FLAGS.modeldir+'MPM_{}.npy'.format(FLAGS.hemisphere))[:,:210], axis=1)
    # train_mask = np.array([False]*len(mpm))
    # train_mask[mpm>0.5] = True
    # Some preprocessing
    adj = sparse_to_tuple(adj)

    for i in range(len(features)):
        features[i] = preprocess_features(features[i])

    support = chebyshev_polynomials(adj, FLAGS.max_degree)
    num_supports = 1 + FLAGS.max_degree

    # Define placeholders/
    placeholders = {
        'support':
        [tf.sparse_placeholder(tf.float32) for _ in range(num_supports)],
        'features':
        tf.sparse_placeholder(tf.float32,
                              shape=tf.constant(features[0].shape,
                                                dtype=tf.float64)),
        'labels':
        tf.placeholder(tf.float32, shape=(None, y_train.shape[1])),
        'labels_mask':
        tf.placeholder(tf.int32),
        'dropout':
        tf.placeholder_with_default(FLAGS.dropout, shape=()),
        'num_features_nonzero':
        tf.placeholder(tf.int32),  # helper variable for sparse dropout
        'gradient_adj': [tf.sparse_placeholder(tf.float32)],
        'gradient':
        tf.placeholder(tf.bool)
    }

    # Create model
    model_name = 'model_{}'.format(FLAGS.hemisphere)
    print('model name:', model_name)
    model = GCN(placeholders,
                input_dim=features[0].shape[1],
                layer_num=FLAGS.layer_num,
                logging=True,
                name=model_name)

    # Initialize session
    sess = tf.Session()

    # Init variables
    sess.run(tf.global_variables_initializer())

    # Define model evaluation function
    def evaluate(features, support, labels, mask, adj, gradient, placeholders):
        t_test = time.time()
        feed_dict_val = construct_feed_dict(features, support, labels, mask,
                                            adj, gradient, placeholders)
        outs_val = sess.run([model.loss, model.dice, model.outputs],
                            feed_dict=feed_dict_val)
        return outs_val[0], outs_val[1], outs_val[2], (time.time() - t_test)

    def dynamic_learning_rate(epoch):
        # learning_rate = FLAGS.basic_learning_rate * 10**(-3 * epoch/FLAGS.epochs)
        learning_rate = FLAGS.basic_learning_rate * (1 - epoch / FLAGS.epochs)
        return learning_rate

    # def dynamic_training_mask(mpm, epoch):
    #     if (epoch/FLAGS.epochs)>0.5:
    #         train_mask = [True]*len(mpm)
    #     else:
    #         thr = 65-130*epoch/FLAGS.epochs
    #         train_mask = np.array([False]*len(mpm))
    #         train_mask[mpm>thr] = True
    #     return train_mask

    cost_train, acc_train, dc_train = [], [], []
    cost_val, acc_val, dc_val = [], [], []
    # Train model
    print('length', len(features))
    for epoch in range(FLAGS.epochs):
        numlist = np.arange(0, FLAGS.train_num, 1)
        np.random.shuffle(numlist)
        FLAGS.learning_rate = dynamic_learning_rate(epoch)
        # train_mask = dynamic_training_mask(mpm, epoch)
        print('_____leaning rate', FLAGS.learning_rate, 'epoch:', epoch)
        for i in numlist:
            t = time.time()
            print('training sample: ', i)
            # Construct feed dictionary
            feed_dict = construct_feed_dict(features[i], support, y_train,
                                            train_mask, adj, False,
                                            placeholders)

            # Training step
            outs = sess.run([model.opt_op, model.loss, model.dice],
                            feed_dict=feed_dict)
            cost_train.append(outs[1])
            dice_train.append(outs[2])

            # Validation
            validnum = np.random.randint(FLAGS.validate_num) + FLAGS.train_num
            cost_val, dice_val, _, tt = evaluate(features[validnum], support,
                                                 y_val, val_mask, adj, False,
                                                 placeholders)
            cost_val.append(cost_val)
            dc_val.append(dice_val)
            # Print results
            print("Epoch:", '%04d' % (epoch + 1), "train_loss=",
                  "{:.5f}".format(outs[1]), "train_dice=",
                  "{:.3f}".format(outs[2]), "val_loss=",
                  "{:.5f}".format(cost_val), "val_dice=",
                  "{:.3f}".format(dice_val), "time=", "{:.5f}".format(tt))

            # if epoch > FLAGS.early_stopping and cost_val[-1] > np.mean(cost_val[-(FLAGS.early_stopping+1):-1]):
            #     print("Early stopping...")
            #     break
        model.save(sess=sess, path=dirname)

    np.savetxt(dirname + 'cost_val.txt', cost_val, fmt='%9.5f', delimiter=',')
    np.savetxt(dirname + 'cost_train.txt',
               cost_train,
               fmt='%9.5f',
               delimiter=',')
    np.savetxt(dirname + 'dc_val.txt', dc_val, fmt='%9.5f', delimiter=',')
    np.savetxt(dirname + 'dc_train.txt', dc_train, fmt='%9.5f', delimiter=',')
    print("Optimization Finished!")

    # model.load(sess)
    # testave = []
    # # Testing
    # for i in range(len(features)):
    #     test_cost, test_acc, test_dice, prediction, test_duration = evaluate(features[i], support, y_test, test_mask, neighbor_mask, placeholders)
    #     print("Test set results:", "cost=", "{:.5f}".format(test_cost),
    #         "accuracy=", "{:.3f}_{:.3f}".format(test_acc, test_dice), "time=", "{:.5f}".format(test_duration))
    #     if i>=FLAGS.train_num:
    #         testave.append(test_dice)
    #     # print(prediction.shape)
    #     path = dirname+'{}.npy'.format(namelist[i])
    #     np.save(path, prediction)
    # np.savetxt(dirname+'dc_test.txt', testave, fmt='%7.4f', delimiter=',')
    # print('average_test_dice:', np.array(testave).mean())

    # Testing and resting
    # #load data
    # f = open('/DATA/232/lma/data/HCP_test/sub_list.txt','r')
    # namelist = [str(int(name)) for name in f.readlines()]
    # feature_path_list = []
    # for name in namelist:
    #     feature_path_list.append('/DATA/232/lma/data/HCP_test/{}/{}_{}_probtrackx_omatrix2/finger_print_fiber.npz'.format(name, name, FLAGS.hemisphere))

    # adj, features2, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(FLAGS.hemisphere,pathlist= feature_path_list)
    # # Some preprocessing
    # for i in range(len(features2)):
    #     features2[i] = preprocess_features(features2[i])
    # # prediction
    # testave = []
    # for i in range(len(features2)):
    #     test_cost, test_acc, test_dice, prediction, test_duration = evaluate(features2[i], support, y_test, test_mask, neighbor_mask,placeholders)
    #     print("Test set results:", "cost=", "{:.5f}".format(test_cost),
    #         "accuracy=", "{:.5f}".format(test_dice), "time=", "{:.5f}".format(test_duration))
    #     testave.append(test_dice)
    #     # print(prediction.shape)
    #     path = dirname+'test_{}.npy'.format(namelist[i])
    #     np.save(path, prediction)
    # print('average_test/retest_acc:', np.array(testave).mean())

    # #Resesting
    # f = open('/DATA/232/lma/data/HCP_retest/sub_list.txt','r')
    # namelist = [str(int(name)) for name in f.readlines()]
    # feature_path_list = []
    # for name in namelist:
    #     feature_path_list.append('/DATA/232/lma/data/HCP_retest/{}/{}_{}_probtrackx_omatrix2/finger_print_fiber.npz'.format(name, name, FLAGS.hemisphere))

    # adj, features3, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(FLAGS.hemisphere, pathlist= feature_path_list)
    # # Some preprocessing
    # for i in range(len(features3)):
    #     features3[i] = preprocess_features(features3[i])
    # #prediction
    # testave = []
    # for i in range(len(features3)):
    #     test_cost, test_acc, test_dice, prediction, test_duration = evaluate(features3[i], support, y_test, test_mask, neighbor_mask, placeholders)
    #     print("Test set results:", "cost=", "{:.5f}".format(test_cost),
    #         "accuracy=", "{:.5f}".format(test_dice), "time=", "{:.5f}".format(test_duration))
    #     testave.append(test_dice)
    #     # print(prediction.shape)
    #     path = dirname+'retest_{}.npy'.format(namelist[i])
    #     np.save(path, prediction)
    # print('average_test/retest_acc:', np.array(testave).mean())
    return None
Exemplo n.º 24
0
import sklearn.metrics as metrics
from os.path import expanduser
import gzip
import pickle
import pathlib
import  tensorflow as tf;
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
print('tf version:', tf.__version__)
assert tf.__version__.startswith('2.')


if __name__ == '__main__':
    
    filedir = os.path.dirname(__file__)
    model_dir = f'{filedir}/../trained_models/{args.problem}/TRIG-GCN'
    model = GCN( output_dim=2)
    model.load_weights(f'{model_dir}/model.ckpt')

    ####### data #######

    for data_dir in ['test_small', 'test_medium']:
        data_path = f'{filedir}/../datasets/{args.problem}/{data_dir}'   
        data_files = list(pathlib.Path(data_path).glob('sample_*.pkl'))
        data_files = [str(data_file) for data_file in data_files][:100]
        os.makedirs(f'{filedir}/../ret_model', exist_ok=True)
        logfile = f'{filedir}/../ret_model/{args.problem}_{data_dir}_TRIG_GCN.txt'
        nsamples = len(data_files)
        
        log(f'test dataset: <{data_path}>, number of instances: {nsamples}', logfile)
        log(f'log write to: <{logfile}>', logfile)
        t1 = time.time()
Exemplo n.º 25
0
dataloader_test = DataLoader(dataset_test,
                             batch_size=1,
                             shuffle=False,
                             num_workers=args.dataloader_workers)

print('Number of training samples:', len(dataset_train))
sys.stdout.flush()

# Model and optimizer
# a dummy example to determine the dimension of input data
adj0, features0, labels0, idx_train0, idx_val0, idx_test0, add_children0, or_children0 = load_data(
    '0', dataset, and_or=and_or, override_path=ds_path)
model = GCN(
    nfeat=features0.shape[1],
    nhid=args.hidden,
    # nclass=labels.max().item() + 1,
    nclass=100,
    nhidlayers=args.hidden_layers,
    dropout=args.dropout,
    indep_weights=indep_weights)
mlp = MLP(dropout=args.dropout)
optimizer = optim.Adam(itertools.chain(model.parameters(), mlp.parameters()),
                       lr=args.lr,
                       weight_decay=args.weight_decay)

creterion = torch.nn.TripletMarginLoss(margin=args.margin, p=2)
CE = torch.nn.CrossEntropyLoss()

loss_list = RunningAvg(window_size=200)
loss_list_CE = RunningAvg(window_size=200)
acc_list = RunningAvg(window_size=200)
loss_by_iter = []
Exemplo n.º 26
0
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()

np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.cuda:
    torch.cuda.manual_seed(args.seed)

# Load data
adj, features, labels, idx_train, idx_val, idx_test = load_data()

# Model and optimizer
model = GCN(nfeat=features.shape[1],
            nhid=args.hidden,
            nclass=labels.max().item() + 1,
            dropout=args.dropout,
            nnz=adj._nnz())
optimizer = optim.Adam(model.parameters(),
                       lr=args.lr,
                       weight_decay=args.weight_decay)
gamma = 0
eta = 10**-6  #should be decreasing with epochs

print('!!!!!!!!!!!!CHECK!!!!!!!!!!!!')
print('save (location) of plots')
print('Gamma:', gamma)
print('Eta:', eta)
print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')

if args.cuda:
Exemplo n.º 27
0
def execute(params, budget=None, max_epoch=243, device='cpu', seed=42):

    np.random.seed(seed)
    torch.manual_seed(seed)
    if device == "cuda":
        torch.cuda.manual_seed(seed)

    # Load data
    if params['dataset'] == "cora":
        adj, features, labels, idx_train, idx_val, idx_test = load_data(
            dataset=params['dataset'], train_percent=0.052)
    if params['dataset'] == "citeseer":
        adj, features, labels, idx_train, idx_val, idx_test = load_citeseer(
            train_percent=0.036)

    # Model and optimizer
    model = GCN(nfeat=features.shape[1],
                nhid=params['hidden'],
                nclass=labels.max().item() + 1,
                dropout=params['dropout'])
    optimizer = optim.Adam(model.parameters(),
                           lr=params['lr'],
                           weight_decay=params['weight_decay'])

    if device == "cuda":
        model.cuda()
        features = features.cuda()
        adj = adj.cuda()
        labels = labels.cuda()
        idx_train = idx_train.cuda()
        idx_val = idx_val.cuda()
        idx_test = idx_test.cuda()

    # train model
    if device == "cuda":
        start = torch.cuda.Event(enable_timing=True)
        end = torch.cuda.Event(enable_timing=True)
        start.record()
    else:
        t1 = time.time_ns()

    model.train()
    num_epoch = int(budget) if budget != None else max_epoch
    for epoch in range(num_epoch):
        optimizer.zero_grad()
        output = model(features, adj)
        loss_train = F.nll_loss(output[idx_train], labels[idx_train])
        acc_train = accuracy(output[idx_train], labels[idx_train])
        loss_train.backward()
        optimizer.step()

    # evaluation
    model.eval()
    output = model(features, adj)
    loss_val = F.nll_loss(output[idx_val], labels[idx_val])
    acc_val = accuracy(output[idx_val], labels[idx_val])

    if device == "cuda":
        end.record()
        torch.cuda.synchronize()
        total_time = start.elapsed_time(end) / 1e3
        sys.stdout.flush()
        acc_val = acc_val.item()
    else:
        t2 = time.time_ns()
        total_time = (t2 - t1) / 1e9

    print()
    print(
        f"dataset={params['dataset']}, num_epoch={num_epoch}, device={next(model.parameters()).device}"
    )
    print("Validation results:", "loss= {:.4f}".format(loss_val.item()),
          "accuracy= {:.4f}".format(acc_val))
    print("Total training time: {:.4f} sec".format(total_time))

    return 1 - acc_val
Exemplo n.º 28
0
random.seed(42)
np.random.seed(42)
torch.manual_seed(42)
if use_gpu:
    torch.cuda.manual_seed(42)

model, optimizer = None, None
best_acc = 0

# Define the model and optimizer
if (opt.model == 'basic'):
    print("| Constructing basic GCN model...")
    model = GCN(
            nfeat = features.shape[1],
            nhid = opt.num_hidden,
            nclass = labels.max().item() + 1,
            dropout = opt.dropout,
            init = opt.init_type
    )
else:
    raise NotImplementedError

if (opt.optimizer == 'sgd'):
    optimizer = optim.SGD(
            model.parameters(),
            lr = opt.lr,
            weight_decay = opt.weight_decay,
            momentum = 0.9
    )
elif (opt.optimizer == 'adam'):
    optimizer = optim.Adam(
Exemplo n.º 29
0
def train_k_fold(model_name,
                 support,
                 placeholders,
                 is_pool=False,
                 is_skip_connection=True,
                 locality1=1,
                 locality2=2,
                 locality_sizes=None):
    """model_name: name of model (using option defined for FLAGS.model in top
       locality1 & locality2: values of k for 2 GC blocks of gcn_cheby(simple gcn model)
       locality_sizes: locality sizes included in each GC block for res_gcn_cheby(our proposed model)
    """
    # Create model
    logging = False
    if model_name == 'res_gcn_cheby':
        model = ResGCN(placeholders,
                       input_dim=num_features_selected,
                       logging=logging,
                       locality_sizes=locality_sizes,
                       is_pool=is_pool,
                       is_skip_connection=is_skip_connection)

    elif model_name == 'gcn':
        model = GCN(placeholders,
                    input_dim=num_features_selected,
                    logging=logging)

    elif model_name == 'gcn_cheby':
        locality = [locality1, locality2]  # locality sizes of different blocks
        model = GCN(placeholders,
                    input_dim=num_features_selected,
                    logging=logging,
                    is_simple=True,
                    is_skip_connection=is_skip_connection,
                    locality=locality)

    elif model_name == 'dense':
        model = MLP(placeholders,
                    input_dim=num_features_selected,
                    logging=logging)

    else:
        raise ValueError('Invalid argument for model: ' + str(model_name))

    # Define model evaluation function
    def evaluate(features, support, labels, mask, placeholders):
        t_test = time.time()
        feed_dict_val = construct_feed_dict(features, support, labels, mask,
                                            placeholders)
        outs_val = sess.run([model.loss, model.accuracy, merged_summary],
                            feed_dict=feed_dict_val)
        return outs_val[0], outs_val[1], outs_val[2], (time.time() - t_test)

    ##...........change to dense_features
    num_nodes = dense_features.shape[0]
    num_folds = 10
    fold_size = int(num_nodes / num_folds)

    # list of results including accuracy, auc, confusion matrix
    train_accuracy = []
    val_accuracy = []
    test_accuracy = []
    test_confusion_matrices = []
    test_auc = []

    # index of fold for validation set and test set
    val_part = 0
    test_part = 1

    # storing number of epochs of each fold
    num_epochs = []

    # shape of features
    print('whole features shape: ', dense_features.shape)

    # Num_folds cross validation
    for fold in range(num_folds):
        print('Starting fold {}'.format(fold + 1))

        # rotating folds of val and test
        val_part = (val_part + 1) % 10
        test_part = (test_part + 1) % 10

        # defining train, val and test mask
        train_mask = np.ones((num_nodes, ), dtype=np.bool)
        val_mask = np.zeros((num_nodes, ), dtype=np.bool)
        test_mask = np.zeros((num_nodes, ), dtype=np.bool)
        train_mask[val_part * fold_size:min((val_part + 1) *
                                            fold_size, num_nodes)] = 0
        train_mask[test_part * fold_size:min((test_part + 1) *
                                             fold_size, num_nodes)] = 0
        val_mask[val_part * fold_size:min((val_part + 1) *
                                          fold_size, num_nodes)] = 1
        test_mask[test_part * fold_size:min((test_part + 1) *
                                            fold_size, num_nodes)] = 1

        # defining train, val and test labels
        y_train = np.zeros(one_hot_labels.shape)
        y_val = np.zeros(one_hot_labels.shape)
        y_test = np.zeros(one_hot_labels.shape)
        y_train[train_mask, :] = one_hot_labels[train_mask, :]
        y_val[val_mask, :] = one_hot_labels[val_mask, :]
        y_test[test_mask, :] = one_hot_labels[test_mask, :]

        # number of samples in each set
        print('# of training samples: ', np.sum(train_mask))
        print('# of validation samples: ', np.sum(val_mask))
        print('# of testing samples: ', np.sum(test_mask))

        tmp_labels = [item + 1 for item in all_labels]
        train_labels = train_mask * tmp_labels
        val_labels = val_mask * tmp_labels
        test_labels = test_mask * tmp_labels

        # distribution of train, val and test set over classes
        train_class = [
            train_labels.tolist().count(i) for i in range(1, num_class + 1)
        ]
        print('train class distribution:', train_class)
        val_class = [
            val_labels.tolist().count(i) for i in range(1, num_class + 1)
        ]
        print('val class distribution:', val_class)
        test_class = [
            test_labels.tolist().count(i) for i in range(1, num_class + 1)
        ]
        print('test class distribution:', test_class)

        # saving initial boolean masks for later use
        init_train_mask = train_mask
        init_val_mask = val_mask
        init_test_mask = test_mask

        # changing mask for having weighted loss
        train_mask = node_weights * train_mask
        val_mask = node_weights * val_mask
        test_mask = node_weights * test_mask

        labeled_ind = [i for i in range(num_nodes) if train_mask[i]]

        # feature selection
        features_selected = feature_selection(dense_features, all_labels,
                                              labeled_ind,
                                              num_features_selected)
        features_selected = sparse_to_tuple(sp.coo_matrix(features_selected))
        # Initialize session
        config = tf.ConfigProto(device_count={'GPU': 1})
        sess = tf.Session(config=config)

        # Session with GPU
        # config = tf.ConfigProto()
        # config.gpu_options.allow_growth = True
        # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9)
        # sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

        # Initialize variables
        sess.run(tf.global_variables_initializer())

        # loss and accuracy scalar curves
        if model_name == 'res_gcn_cheby':
            l1 = locality_sizes[0]
            l2 = locality_sizes[1]
        else:
            l1 = locality1
            l2 = locality2
        tf.summary.scalar(name='{}_{}_loss_fold_{}'.format(l1, l2, fold + 1),
                          tensor=model.loss)
        tf.summary.scalar(name='{}_{}_accuracy_fold_{}'.format(
            l1, l2, fold + 1),
                          tensor=model.accuracy)
        merged_summary = tf.summary.merge_all()

        # defining train, test and val writers in /tmp/model_name/ path
        train_writer = tf.summary.FileWriter(
            logdir='/tmp/' + model_name +
            '_{}_{}/train_fold_{}/'.format(l1, l2, fold + 1))
        test_writer = tf.summary.FileWriter(
            logdir='/tmp/' + model_name +
            '_{}_{}/test_fold_{}/'.format(l1, l2, fold + 1))
        val_writer = tf.summary.FileWriter(
            logdir='/tmp/' + model_name +
            '_{}_{}/val_fold_{}/'.format(l1, l2, fold + 1))
        # Train model
        cost_val = []
        train_results = []
        for epoch in range(FLAGS.epochs):
            t = time.time()

            # Construct feed dictionary for training
            feed_dict = construct_feed_dict(features_selected, support,
                                            y_train, train_mask, placeholders)
            feed_dict.update({placeholders['dropout']: FLAGS.dropout})

            # Training step
            train_results = sess.run(
                [model.opt_op, model.loss, model.accuracy, merged_summary],
                feed_dict=feed_dict)
            train_writer.add_summary(train_results[-1], epoch)

            # Evaluation on val set
            val_cost, val_acc, val_summary, duration = evaluate(
                features_selected, support, y_val, val_mask, placeholders)
            cost_val.append(val_cost)
            val_writer.add_summary(val_summary, epoch)

            # Evaluation on test set
            test_cost, test_acc, test_summary, test_duration = evaluate(
                features_selected, support, y_test, test_mask, placeholders)
            test_writer.add_summary(test_summary, epoch)

            # Print results of train, val and test
            print("Epoch:", '%04d' % (epoch + 1), "train_loss=",
                  "{:.5f}".format(train_results[1]), "train_acc=",
                  "{:.5f}".format(train_results[2]), "val_loss=",
                  "{:.5f}".format(val_cost), "val_acc=",
                  "{:.5f}".format(val_acc), "time=",
                  "{:.5f}".format(time.time() - t))
            print("Test set results:", "test_loss=",
                  "{:.5f}".format(test_cost), "test_accuracy=",
                  "{:.5f}".format(test_acc))

            # Check val loss for early stopping
            if epoch > max(FLAGS.early_stopping,
                           FLAGS.start_stopping) and cost_val[-1] > np.mean(
                               cost_val[-(FLAGS.early_stopping + 1):-1]):
                print("Early stopping on epoch {}...".format(epoch + 1))
                break

        num_epochs.append(epoch)
        print("Optimization Finished!")

        # Collecting final results of train, test & val
        train_accuracy.append(train_results[2])
        val_accuracy.append(val_acc)
        test_accuracy.append(test_acc)

        # Visualizing layers' embedding
        # if model_name == 'res_gcn_cheby':
        # path = '/tmp/' + model_name + '_{}_{}'.format(l1, l2) + '/layers/' + \
        #        'fold_{}/'.format(fold)
        # layer_writer = tf.summary.FileWriter(logdir=path)
        # write_meta_data_labels(all_labels, path)
        # visualize_node_embeddings_resgcn(features, support, placeholders, sess, model, layer_writer, FLAGS.is_pool,
        #                                  path, len(locality_sizes))
        # layer_writer.close()
        # activations = get_activations(features, support, placeholders, sess, model)
        # l1_act = activations[0][1]
        # l2_act = activations[1][1]
        # graph_visualize(adj, dense_features, all_labels, 15, l1_act)
        # graph_visualize(adj, dense_features, all_labels, 15, l2_act)

        # Confusion matrix on test set

        feed_dict = dict()
        feed_dict.update({placeholders['features']:
                          features_selected})  ##...........Feature_modi
        feed_dict.update({
            placeholders['support'][i]: support[i]
            for i in range(len(support))
        })
        feed_dict.update(
            {placeholders['num_features_nonzero']:
             features_selected[1].shape})  ##...........Feature_modi
        model_outputs = sess.run(model.outputs, feed_dict=feed_dict)
        prediction = np.argmax(model_outputs, axis=1)[init_test_mask]
        confusion_mat = confusion_matrix(
            y_true=np.asarray(all_labels)[init_test_mask],
            y_pred=prediction,
            labels=[i for i in range(num_class)])
        test_confusion_matrices.append(confusion_mat)
        print('Confusion matrix of test set:')
        print(confusion_mat)

        # Roc auc score on test set
        # auc = roc_auc_score(y_true=one_hot_labels[init_test_mask, :], y_score=model_outputs[init_test_mask, :])
        # test_auc.append(auc)
        # print('Test auc: {:.4f}'.format(auc))
        print('--------')

        # Closing writers
        train_writer.close()
        test_writer.close()
        val_writer.close()
        sess.close()

    if model_name == 'gcn_cheby':
        print('Results of k1={} k2={}'.format(locality1, locality2))

    elif model_name == 'gcn':
        print('Results of re-parametrization model')

    elif model_name == 'res_gcn_cheby':
        print('Results of res_gcn with localities of: ', locality_sizes)

    else:
        print('Results of 3 layer dense neural network')

    print('Average number of epochs: {:.3f}'.format(np.mean(num_epochs)))
    print('Accuracy on {} folds'.format(num_folds))
    print('train:', train_accuracy)
    print('val', val_accuracy)
    print('test', test_accuracy)
    print()

    # print('Test auc on {} folds'.format(num_folds))
    # print(test_auc)
    # print()
    #
    # test_avg_auc = np.mean(test_auc)
    # print('Average test auc on {} folds'.format(num_folds))
    # print(test_avg_auc, '±', np.std(test_auc))

    return train_accuracy, val_accuracy, test_accuracy
Exemplo n.º 30
0
    node_heat_map = np.array(node_heat_map[:mol.GetNumAtoms()]).reshape(-1, 1)
    pos_node_heat_map = MinMaxScaler(feature_range=(0, 1)).fit_transform(
        node_heat_map * (node_heat_map >= 0)).reshape(-1, )
    neg_node_heat_map = MinMaxScaler(feature_range=(-1, 0)).fit_transform(
        node_heat_map * (node_heat_map < 0)).reshape(-1, )
    return pos_node_heat_map + neg_node_heat_map


dataset = load_bbbp(hp.N)
random.Random(hp.shuffle_seed).shuffle(dataset)
split_idx = int(np.floor(len(dataset) * hp.train_frac))
test_dataset = dataset[split_idx:]
loader = DataLoader(test_dataset, batch_size=1, shuffle=False)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = GCN(hp.H_0, hp.H_1, hp.H_2, hp.H_3).to(device)
model.load_state_dict(torch.load('gcn_state_dict.pt'))
model.eval()

optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

print(model)
model.train()
total_loss = 0
for data in tqdm(loader):
    # breakpoint()
    data = data.to(device)
    optimizer.zero_grad()
    out = model(data)
    loss = F.binary_cross_entropy(out, data.y)
    loss.backward()