Example #1
0
    def _layer_initialize(self):
        agg_layers = []
        adversarial_layers = []
        for i in range(self.model_config['layer_depth']):
            # initialize the two aggregators
            agg_one = GCN(self.model_config['attr_one_dim'],
                          self.model_config['attr_two_dim']).to(
                              self.model_config['device'])
            agg_two = GCN(self.model_config['attr_two_dim'],
                          self.model_config['attr_one_dim']).to(
                              self.model_config['device'])
            agg_layers.append([agg_one, agg_two])

            # initialize the adversarial operations
            adv_one = AdversarialLearning(agg_one,
                                          self.model_config['attr_two_dim'],
                                          self.model_config['disc_hid_dim'],
                                          self.model_config['learning_rate'],
                                          self.model_config['weight_decay'],
                                          self.model_config['dropout'],
                                          self.model_config['device'],
                                          outfeat=1)
            adv_two = AdversarialLearning(agg_two,
                                          self.model_config['attr_one_dim'],
                                          self.model_config['disc_hid_dim'],
                                          self.model_config['learning_rate'],
                                          self.model_config['weight_decay'],
                                          self.model_config['dropout'],
                                          self.model_config['device'],
                                          outfeat=1)
            adversarial_layers.append([adv_one, adv_two])
        return agg_layers, adversarial_layers
 def __layer_initialize(self):
     gcn_layers = []
     adversarial_layers = []
     for i in range(self.layer_depth):
         if i % 2 == 0:
             one_gcn_layer = GCN(self.v_attr_dimensions,
                                 self.u_attr_dimensions).to(self.device)
             gcn_layers.append(one_gcn_layer)
             adversarial_layers.append(
                 AdversarialLearning(one_gcn_layer,
                                     self.u_attr_dimensions,
                                     self.v_attr_dimensions,
                                     self.dis_hidden_dim,
                                     self.learning_rate,
                                     self.weight_decay,
                                     self.dropout,
                                     self.device,
                                     outfeat=1))
         else:
             one_gcn_layer = GCN(self.u_attr_dimensions,
                                 self.v_attr_dimensions).to(self.device)
             gcn_layers.append(one_gcn_layer)
             adversarial_layers.append(
                 AdversarialLearning(one_gcn_layer,
                                     self.v_attr_dimensions,
                                     self.u_attr_dimensions,
                                     self.dis_hidden_dim,
                                     self.learning_rate,
                                     self.weight_decay,
                                     self.dropout,
                                     self.device,
                                     outfeat=1))
     return gcn_layers, adversarial_layers
    def __init__(self,
                 bipartite_graph_data_loader,
                 args,
                 device,
                 layer_depth=3,
                 rank=-1,
                 dataset="cora"):
        self.rank = rank
        self.dataset = dataset
        self.epochs = args.epochs
        self.dis_hidden_dim = args.dis_hidden
        self.learning_rate = args.lr
        self.weight_decay = args.weight_decay
        self.dropout = args.dropout
        self.u_attr_dimensions = bipartite_graph_data_loader.get_u_attr_dimensions(
        )
        self.v_attr_dimensions = bipartite_graph_data_loader.get_v_attr_dimensions(
        )
        bipartite_graph_data_loader = bipartite_graph_data_loader
        self.device = device

        self.layer_depth = layer_depth
        self.bipartite_graph_data_loader = bipartite_graph_data_loader
        self.batch_size = bipartite_graph_data_loader.batch_size
        self.device = device

        self.batch_num_u = bipartite_graph_data_loader.get_batch_num_u()
        self.batch_num_v = bipartite_graph_data_loader.get_batch_num_v()
        self.u_attr = bipartite_graph_data_loader.get_u_attr_array()
        self.v_attr = bipartite_graph_data_loader.get_v_attr_array()
        self.u_adj = bipartite_graph_data_loader.get_u_adj()
        self.v_adj = bipartite_graph_data_loader.get_v_adj()
        self.u_num = len(self.u_attr)
        self.v_num = len(self.v_attr)
        self.f_loss = open("BGNN-Adv-loss.txt", "a")

        self.gcn_explicit = GCN(self.v_attr_dimensions,
                                self.u_attr_dimensions).to(device)
        self.gcn_implicit = GCN(self.u_attr_dimensions,
                                self.v_attr_dimensions).to(device)
        self.gcn_merge = GCN(self.v_attr_dimensions,
                             self.u_attr_dimensions).to(device)

        self.learning_type = args.learning_type
Example #4
0
    def __init__(self,
                 args,
                 batch_size=128,
                 target='mnistm',
                 learning_rate=0.0002,
                 interval=10,
                 optimizer='adam',
                 checkpoint_dir=None,
                 save_epoch=10):
        self.batch_size = batch_size
        self.target = target
        self.checkpoint_dir = checkpoint_dir
        self.save_epoch = save_epoch
        self.interval = interval
        self.lr = learning_rate
        self.best_correct = 0
        self.args = args
        if self.args.use_target:
            self.ndomain = self.args.ndomain
        else:
            self.ndomain = self.args.ndomain - 1

        # load source and target domains
        self.datasets, self.dataset_test, self.dataset_size = dataset_read(
            target, self.batch_size)
        self.niter = self.dataset_size / self.batch_size
        print('Dataset loaded!')

        # define the feature extractor and GCN-based classifier
        self.G = Generator(self.args.net)
        self.GCN = GCN(nfeat=args.nfeat, nclasses=args.nclasses)
        self.G.cuda()
        self.GCN.cuda()
        print('Model initialized!')

        if self.args.load_checkpoint is not None:
            self.state = torch.load(self.args.load_checkpoint)
            self.G.load_state_dict(self.state['G'])
            self.GCN.load_state_dict(self.state['GCN'])
            print('Model load from: ', self.args.load_checkpoint)

        # initialize statistics (prototypes and adjacency matrix)
        if self.args.load_checkpoint is None:
            self.mean = torch.zeros(args.nclasses * self.ndomain,
                                    args.nfeat).cuda()
            self.adj = torch.zeros(args.nclasses * self.ndomain,
                                   args.nclasses * self.ndomain).cuda()
            print('Statistics initialized!')
        else:
            self.mean = self.state['mean'].cuda()
            self.adj = self.state['adj'].cuda()
            print('Statistics loaded!')

        # define the optimizer
        self.set_optimizer(which_opt=optimizer, lr=self.lr)
        print('Optimizer defined!')
Example #5
0
    def _get_models(self):
        # bow_feat = self.loader.bow_mx
        topo_feat = self.loader.topo_mx

        # model1 = GCN(nfeat=bow_feat.shape[1],
        #              hlayers=self._conf["kipf"]["hidden"],
        #              nclass=self.loader.num_labels,
        #              dropout=self._conf["kipf"]["dropout"])
        # opt1 = optim.Adam(model1.parameters(), lr=self._conf["kipf"]["lr"],
        #                   weight_decay=self._conf["kipf"]["weight_decay"])
        #
        # model2 = GCNCombined(nbow=bow_feat.shape[1],
        #                      nfeat=topo_feat.shape[1],
        #                      hlayers=self._conf["hidden_layers"],
        #                      nclass=self.loader.num_labels,
        #                      dropout=self._conf["dropout"])
        # opt2 = optim.Adam(model2.parameters(), lr=self._conf["lr"], weight_decay=self._conf["weight_decay"])
        #
        # model3 = GCN(nfeat=topo_feat.shape[1],
        #              hlayers=self._conf["multi_hidden_layers"],
        #              nclass=self.loader.num_labels,
        #              dropout=self._conf["dropout"],
        #              layer_type=None,
        #              is_regression=self.loader.regression)
        # opt3 = optim.Adam(model3.parameters(), lr=self._conf["lr"], weight_decay=self._conf["weight_decay"])
        #
        model4 = GCN(nfeat=topo_feat.shape[1],
                     hlayers=self._conf["multi_hidden_layers"],
                     nclass=self.loader.num_labels,
                     dropout=self._conf["dropout"],
                     layer_type=AsymmetricGCN,
                     is_regression=self.loader.regression)
        opt4 = optim.Adam(model4.parameters(), lr=self._conf["lr"], weight_decay=self._conf["weight_decay"])

        return {
            # "kipf": {
            #     "model": model1, "optimizer": opt1,
            #     "arguments": [self.loader.bow_mx, self.loader.adj_mx],
            #     "labels": self.loader.labels,
            # },
            # "our_combined": {
            #     "model": model2, "optimizer": opt2,
            #     "arguments": [self.loader.bow_mx, self.loader.topo_mx, self.loader.adj_rt_mx],
            #     "labels": self.loader.labels,
            # },
            # "our_topo_sym": {
            #     "model": model3, "optimizer": opt3,
            #     "arguments": [self.loader.topo_mx, self.loader.adj_mx],
            #     "labels": self.loader.labels,
            # },
            "our_topo_asymm": {
                "model": model4, "optimizer": opt4,
                "arguments": [self.loader.topo_mx, self.loader.adj_rt_mx],
                "labels": self.loader.labels,
            },
        }
Example #6
0
def main(sample_name, epochs=200, get_probs=False):
    # Training settings
    valid = False
    no_cuda = False
    seed = 42
    lr = 1e-2
    weight_decay = 1e-5
    hidden = 32
    dropout = 0.5

    cuda = not no_cuda and torch.cuda.is_available()

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

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

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

    if 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()
        y_test = y_test.cuda()

#   Training model
    torch.set_grad_enabled(True)
    t_total = time.time()
    model.eval()
    print("------- Training GCN")
    for epoch in range(epochs):
        if epoch == epochs - 1:
            valid = True
        train(model, optimizer, epoch, adj, features, labels, idx_train, idx_val, valid)
    print("Optimization Finished!")
    print("Total time elapsed: {:.4f}s".format(time.time() - t_total))
    # Testing
    info = gcn_inference(sample_name, model, adj, features, y_test, idx_test, get_probs=get_probs)
    return info
Example #7
0
 def __init__(self, name, params, method_name, graph, file_tags):
     super(GCNModel, self).__init__(params["dimension"], method_name, graph)
     # Training settings
     self._adj, self._features, self._labels, self._idx_train, self._idx_val, self._idx_test = \
         new_load_data(graph, file_tags, len(graph.nodes()), name=name)
     self._seed = 42
     self._epochs = params["epochs"]
     self._lr = params["lr"]
     self._weight_decay = params["weight_decay"]
     self._hidden = params["hidden"]
     self._dropout = params["dropout"]
     self._model = GCN(nfeat=self._features.shape[1], nhid=self._hidden, nclass=self._d, dropout=self._dropout)
     self._optimizer = optim.Adam(self._model.parameters(), lr=self._lr, weight_decay=self._weight_decay)
Example #8
0
    def __init__(self, bipartite_graph_data_loader, args, device, rank=-1, dataset="cora"):
        self.rank = rank
        self.dataset = dataset
        u_attr_dimensions = bipartite_graph_data_loader.get_u_attr_dimensions()
        v_attr_dimensions = bipartite_graph_data_loader.get_v_attr_dimensions()
        decoder_hidfeat = args.decoder_hidfeat
        learning_rate = args.lr
        weight_decay = args.weight_decay
        dropout = args.dropout
        gcn_output_dim = args.gcn_output_dim

        self.gcn_explicit = GCN(v_attr_dimensions, gcn_output_dim).to(device)
        self.gcn_implicit = GCN(u_attr_dimensions, gcn_output_dim).to(device)
        self.gcn_merge = GCN(v_attr_dimensions, gcn_output_dim).to(device)
        self.gcn_opposite = GCN(u_attr_dimensions, gcn_output_dim).to(device)

        self.mlp_explicit = MLPLearning(self.gcn_explicit, gcn_output_dim, u_attr_dimensions, decoder_hidfeat,
                                        learning_rate, weight_decay, dropout, device)
        self.mlp_implicit = MLPLearning(self.gcn_implicit, gcn_output_dim, v_attr_dimensions, decoder_hidfeat,
                                        learning_rate, weight_decay, dropout, device)
        self.mlp_merge = MLPLearning(self.gcn_merge, gcn_output_dim, u_attr_dimensions, decoder_hidfeat,
                                     learning_rate, weight_decay, dropout, device)
        self.mlp_opposite = MLPLearning(self.gcn_opposite, gcn_output_dim, v_attr_dimensions, decoder_hidfeat,
                                        learning_rate, weight_decay, dropout, device)

        self.bipartite_graph_data_loader = bipartite_graph_data_loader
        self.batch_size = bipartite_graph_data_loader.batch_size
        self.device = device
        self.epochs = args.epochs

        self.batch_num_u = bipartite_graph_data_loader.get_batch_num_u()
        self.batch_num_v = bipartite_graph_data_loader.get_batch_num_v()
        self.u_attr = bipartite_graph_data_loader.get_u_attr_array()
        self.v_attr = bipartite_graph_data_loader.get_v_attr_array()
        self.u_adj = bipartite_graph_data_loader.get_u_adj()
        self.v_adj = bipartite_graph_data_loader.get_v_adj()
        self.u_num = len(self.u_attr)
        self.v_num = len(self.v_attr)
Example #9
0
def get_model(adj, features, labels, idxs, args):
    # Model and optimizer
    model = GCN(nfeat=features.shape[1],
                nhid=args.hidden,
                nclass=labels.max().item() + 1,
                dropout=args.dropout)
    if args.use_gpu:
        model = model.cuda()

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

    adj_norm = normalize_adj(adj, args)

    # train
    for epoch in range(args.epochs):
        train(epoch, model, optimizer, adj_norm, features, labels, idxs, args)

    # test
    acc_test = test(model, adj_norm, features, labels, idxs, args)
    return model, acc_test
Example #10
0
                   0] = 4

    if word_ind_sorted[3] != adj[0].shape[0] - 1:
        all_phrase_val[epoch,
                       int(word_ind_sorted[3] + 1):int(adj[0].shape[0]), 0] = 5

    y_val[epoch, :] = np.float32(label[current_ind, :]).reshape(1, -1)

feed_dict_val = construct_feed_dict_sgc(features_val_feed, eigvec_val, y_val,
                                        val_mask, all_phrase_val, class_weight,
                                        placeholders)

with tf.device('/cpu:0'):

    # Create model
    model = GCN(placeholders, input_dim=features[0].shape[1], logging=False)

    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                            log_device_placement=False))

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

    cost_val_f1 = []
    cost_val_loss = []

    for epoch_h in range(0, 200):
        t = time.time()
        shuffle_ind_per_epoch = np.asarray(
            range(0, int(num_batch / 10 * 9 - 1)))
        np.random.shuffle(shuffle_ind_per_epoch)
Example #11
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()
print(adj)

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

if args.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()


def train(epoch):
bob_features = features.send(bob)
bob_labels = labels[idx_bob].send(bob)
bob_idx = idx_bob.send(bob)

alice_adj = adj.send(alice)
alice_features = features.send(alice)
alice_labels = labels[idx_alice].send(alice)
alice_idx = idx_alice.send(alice)

# 以列表嵌套list的形式存放数据
remote_dataset[0] = [bob_adj, bob_features, bob_labels, bob_idx]
remote_dataset[1] = [alice_adj, alice_features, alice_labels, alice_idx]

# model and optimizer
local_model = GCN(nfeat=features.shape[1],
                  nhid=args.nhid,
                  nclass=args.nclass,
                  dropout=args.dropout)

bob_alone_model = GCN(nfeat=features.shape[1],
                      nhid=args.nhid,
                      nclass=args.nclass,
                      dropout=args.dropout)
alice_alone_model = GCN(nfeat=features.shape[1],
                        nhid=args.nhid,
                        nclass=args.nclass,
                        dropout=args.dropout)

bob_fed_model = GCN(nfeat=features.shape[1],
                    nhid=args.nhid,
                    nclass=args.nclass,
                    dropout=args.dropout)
Example #13
0
        acc_val = validate(model, features, adj, labels, idx_val)
        if acc_val > best_acc:
            best_acc = acc_val
        print('best val accuracy {}'.format(best_acc))
        print(' -- ' * 5)
        print('\n\n')
        train_loss.append(loss)
    test(model, features, adj, labels, idx_test)
    plt.figure()
    plt.plot(train_loss)
    plt.savefig('./train_loss.png')

    #GCN

    adj, features, labels, idx_train, idx_val, idx_test, idx2node = load_nx_adj_data(
        G, label_file, exist_fea=basic_file)
    model = GCN(nfeat=features.shape[1],
                nhid=128,
                nclass=labels.max().item() + 1,
                dropout=0.5)
    optimizer = optim.Adam(model.parameters(),
                           lr=lr,
                           weight_decay=weight_decay)
    embedding = model.get_embeddings(features, adj, idx2node)
    for epoch in range(epochs):
        train(epoch, model, optimizer, adj, features, labels, idx_train)
    embedding = model.get_embeddings(features, adj, idx2node)
    print(" Optimization Finished ! ")
    embedding.to_pickle('./Result/' + name + 'GCN_embedding' + str(DIM) +
                        '.pickle')
Example #14
0
def train(features, labels, adj):
    # Settings
    flags = tf.app.flags
    FLAGS = flags.FLAGS
    flags.DEFINE_string('dataset', 'cora', 'Dataset string.')  # 'cora', 'citeseer', 'pubmed'
    flags.DEFINE_string('model', 'gcn', 'Model string.')  # 'gcn', 'gcn_cheby', 'dense'
    flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
    flags.DEFINE_integer('epochs', 200, 'Number of epochs to train.')
    flags.DEFINE_integer('hidden1', 16, 'Number of units in hidden layer 1.')
    flags.DEFINE_float('dropout', 0.5, 'Dropout rate (1 - keep probability).')
    flags.DEFINE_float('weight_decay', 5e-4, 'Weight for L2 loss on embedding matrix.')
    flags.DEFINE_integer('early_stopping', 10, 'Tolerance for early stopping (# of epochs).')
    flags.DEFINE_integer('max_degree', 3, 'Maximum Chebyshev polynomial degree.')

    features = sp.lil_matrix(features)
    features = preprocess_features(features)
    support = [preprocess_adj(adj)]
    num_supports = 1
    idx_train = range(len(labels))
    train_mask = sample_mask(idx_train, labels.shape[0])

    # Define placeholders
    placeholders = {
        'support': [tf.sparse_placeholder(tf.float32) for _ in range(num_supports)],
        'features': tf.sparse_placeholder(tf.float32, shape=tf.constant(features[2], dtype=tf.int64)),
        'labels': tf.placeholder(tf.float32, shape=(None, labels.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()

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

    # Train model
    epochs = 200
    # early_stopping = 10
    for epoch in range(epochs):

        t = time.time()
        # Construct feed dictionary
        feed_dict = construct_feed_dict(features, support, labels, train_mask, placeholders)
        feed_dict.update({placeholders['dropout']: 0.5})

        # Training step
        outs = sess.run([model.opt_op, model.loss, model.accuracy], feed_dict=feed_dict)

        # Validation
        # cost, acc, duration = evaluate(features, support, y_val, val_mask, placeholders)
        # cost_val.append(cost)

        # Print results
        print("Epoch:", '%04d' % (epoch + 1), "train_loss=", "{:.5f}".format(outs[1]),
              "train_acc=", "{:.5f}".format(outs[2]), "time=", "{:.5f}".format(time.time() - t))
        # "val_loss=", "{:.5f}".format(cost), "val_acc=", "{:.5f}".format(acc),

        # if epoch > early_stopping and cost_val[-1] > np.mean(cost_val[-(FLAGS.early_stopping + 1):-1]):
        #     print("Early stopping...")
        #     break

    print("Optimization Finished!")
Example #15
0
    def _get_models(self):
        bow_feat = self.loader.bow_mx
        topo_feat = self.loader.topo_mx

        model1 = GCN(nfeat=bow_feat.shape[1],
                     hlayers=[self.conf["kipf"]["hidden"]],
                     nclass=self.loader.num_labels,
                     dropout=self.conf["kipf"]["dropout"])
        opt1 = optim.Adam(model1.parameters(),
                          lr=self.conf["kipf"]["lr"],
                          weight_decay=self.conf["kipf"]["weight_decay"])

        model2 = GCNCombined(nbow=bow_feat.shape[1],
                             nfeat=topo_feat.shape[1],
                             hlayers=self.conf["hidden_layers"],
                             nclass=self.loader.num_labels,
                             dropout=self.conf["dropout"])
        opt2 = optim.Adam(model2.parameters(),
                          lr=self.conf["lr"],
                          weight_decay=self.conf["weight_decay"])

        model3 = GCN(nfeat=topo_feat.shape[1],
                     hlayers=self.conf["multi_hidden_layers"],
                     nclass=self.loader.num_labels,
                     dropout=self.conf["dropout"],
                     layer_type=None)
        opt3 = optim.Adam(model3.parameters(),
                          lr=self.conf["lr"],
                          weight_decay=self.conf["weight_decay"])

        model4 = GCN(nfeat=topo_feat.shape[1],
                     hlayers=self.conf["multi_hidden_layers"],
                     nclass=self.loader.num_labels,
                     dropout=self.conf["dropout"],
                     layer_type=AsymmetricGCN)
        opt4 = optim.Adam(model4.parameters(),
                          lr=self.conf["lr"],
                          weight_decay=self.conf["weight_decay"])

        return {
            "kipf": {
                "model": model1,
                "optimizer": opt1,
                "arguments": [self.loader.bow_mx, self.loader.adj_mx],
                "labels": self.loader.labels,
            },
            "our_combined": {
                "model":
                model2,
                "optimizer":
                opt2,
                "arguments": [
                    self.loader.bow_mx, self.loader.topo_mx,
                    self.loader.adj_rt_mx
                ],
                "labels":
                self.loader.labels,
            },
            "topo_sym": {
                "model": model3,
                "optimizer": opt3,
                "arguments": [self.loader.topo_mx, self.loader.adj_mx],
                "labels": self.loader.labels,
            },
            "topo_asym": {
                "model": model4,
                "optimizer": opt4,
                "arguments": [self.loader.topo_mx, self.loader.adj_rt_mx],
                "labels": self.loader.labels,
            },
        }
Example #16
0
cuda = useCuda and torch.cuda.is_available()
weightAdj = True

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

filePath = '../data/kg_train_edges_weighted.txt'
label, adj, features, numHerbs, numSymps = load_data(filePath,
                                                     weighted=weightAdj)
herbPairRule = getHerPairMatrix('../data/herb_pair.txt')

print(label.shape)

model = GCN(nfeat=features.shape[1], nhid=hidden, dimension=d, dropout=dropout)

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

if cuda:
    model.cuda()
    features = features.cuda()
    adj = adj.cuda()


def getoutputHC(output):
    all = []
    outputHC = torch.zeros(numHerbs,
                           numHerbs,
                           dtype=torch.float,
                           requires_grad=True)
Example #17
0
    def build_architecture(self):

        self.split_train_val_test(train_s=Train, val_s=Val, test_s=Test)
        # self._train_mask = self._data.train_mask
        # self._val_mask = self._data.val_mask
        # self._test_mask = self._data.test_mask

        if self._topo_edges is None:
            self.build_topological_edges()

        if self._params['net'] == 'gcn':
            self._net = GCNNet(self._num_features, self._num_classes,
                                h_layers=self._params['hidden_sizes'], dropout=self._params['dropout_rate'],
                                activation=self._params['activation'])
        elif self._params['net'] == 'siam':
            self._net = SiamNet(self._num_features, self._num_classes,
                                h_layers=self._params['hidden_sizes'], dropout=self._params['dropout_rate'],
                                activation=self._params['activation'])
        elif self._params['net'] == 'gat':
            self._net = GatNet(self._num_features, self._num_classes,
                               h_layers=self._params['hidden_sizes'], dropout=self._params['dropout_rate'],
                               activation=self._params['activation'])
        elif self._params['net'] == 'siam_gat':
            self._net = SiamGAT(self._num_features, self._num_classes,
                               h_layers=self._params['hidden_sizes'], dropout=self._params['dropout_rate'],
                               activation=self._params['activation'], heads=self._params['gat_heads'])
        elif self._params['net'] == 'combined':
            self.build_topo_matrix()
            self._net = GCNCombined(nbow=self._num_features,
                             nfeat=self._topo_mx.shape[1],
                             hlayers=self._params['hidden_sizes'],
                             nclass=self._num_classes,
                             dropout=self._params['dropout_rate'])
        elif self._params['net'] == 'combined_gcn':
            self.build_topo_matrix()
            self._net = CombinedGCN(self._num_features, self._num_classes,
                                    h_layers=self._params['hidden_sizes'], dropout=self._params['dropout_rate'],
                                    activation=self._params['activation'],
                                    num_topology=self._topo_mx.shape[1])
        elif self._params['net'] == 'symmetric':
            self.build_topo_matrix()
            self._net = GCN(nfeat=self._topo_mx.shape[1],
                             hlayers=self._params['hidden_sizes'],
                             nclass=self._num_classes,
                             dropout=self._params['dropout_rate'],
                             layer_type=None)
        elif self._params['net'] == 'asymmetric':
            self.build_topo_matrix()
            self._net = GCN(nfeat=self._topo_mx.shape[1],
                             hlayers=self._params['hidden_sizes'],
                             nclass=self._num_classes,
                             dropout=self._params['dropout_rate'],
                             layer_type=AsymmetricGCN)
        else:
            self._net = SiamNet(self._num_features, self._num_classes,
                                h_layers=self._params['hidden_sizes'], dropout=self._params['dropout_rate'],
                                activation=self._params['activation'])
        self._net.to(self._device)

        # self._criterion = nn.CrossEntropyLoss()
        self._criterion = nn.NLLLoss()
        self._optimizer = optim.Adam(self._net.parameters(), lr=self._params['learning_rate'],
                                     weight_decay=self._params['weight_decay'])
Example #18
0
            A = scipy.linalg.block_diag(*As)

            yield self._transform(x, A, y)

# Define placeholders
placeholders = {
    'support': [tf.sparse_placeholder(tf.float32) for _ in range(num_supports)],
    'features': tf.sparse_placeholder(tf.float32, shape=(None, 2)),
    'labels': tf.placeholder(tf.float32, shape=(None, 2)),
    '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=2, logging=True)

# Initialize session
sess = tf.Session()


# 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], feed_dict=feed_dict_val)
    return outs_val[0], outs_val[1], (time.time() - t_test)


# Init variables
sess.run(tf.global_variables_initializer())
Example #19
0
  policy_estimator = PolicyEstimator(learning_rate=0.001)
  value_estimator = ValueEstimator(learning_rate=0.001)

  num_supports = 1
  placeholders = {
    'adj': tf.placeholder(tf.float32, shape=(None, None)) , #unnormalized adjancy matrix
    'support': [tf.sparse_placeholder(tf.float32) for _ in range(num_supports)],
    'features': tf.sparse_placeholder(tf.float32),
    'labels': tf.placeholder(tf.float32, shape=(None, 2)),
    '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
    'learning_rate': tf.placeholder(tf.float32)
  }
  gcn = GCN(placeholders, input_dim=feats, logging=True,FLAGS=FLAGS)



  sess.run(tf.global_variables_initializer())
  stats = actor_critic(sess, gcn, placeholders, env, policy_estimator,
       value_estimator, num_episodes=1000, discount_factor=0.99)