コード例 #1
0
    def setUp(self):
        self.dim = 3
        W = np.array([[1.0, 2.0, 1.0], [2.0, 1.0, 2.0], [-1.0, -2.0, -1.0]])

        A = np.array([-1.0, 2.0, 3.0])

        b = 2

        # Build graph
        self.graph = gnn.Graph(self.dim)

        # Add nodes
        self.graph.add_node(0, np.array([-1.0, 0.0, 4.0]))
        self.graph.add_node(1, np.array([9.0, -5.0, 2.0]))
        self.graph.add_node(2, np.array([3.0, -10.0, 6.0]))
        self.graph.add_node(3, np.array([-8.0, 4.0, -9.0]))

        # Add edges
        self.graph.add_edge(0, 1)
        self.graph.add_edge(1, 2)
        self.graph.add_edge(2, 0)
        self.graph.add_edge(0, 3)

        self.network = gnn.GNN(self.dim, agg_T=2, init_W=W, init_A=A, init_b=b)
コード例 #2
0
K = args.n_communities

training_dataset = SBMMixtureDataset(args.n_graphs, args.n_nodes, K)
training_loader = DataLoader(training_dataset,
                             args.batch_size,
                             collate_fn=training_dataset.collate_fn,
                             drop_last=True)

ones = th.ones(args.n_nodes // K)
y_list = [
    th.cat([x * ones for x in p]).long().to(dev)
    for p in permutations(range(K))
]

feats = [1] + [args.n_features] * args.n_layers + [K]
model = gnn.GNN(feats, args.radius, K).to(dev)
optimizer = getattr(optim, args.optim)(model.parameters(), lr=args.lr)


def compute_overlap(z_list):
    ybar_list = [th.max(z, 1)[1] for z in z_list]
    overlap_list = []
    for y_bar in ybar_list:
        accuracy = max(th.sum(y_bar == y).item()
                       for y in y_list) / args.n_nodes
        overlap = (accuracy - 1 / K) / (1 - 1 / K)
        overlap_list.append(overlap)
    return sum(overlap_list) / len(overlap_list)


def from_np(f, *args):
コード例 #3
0
import gnn as g
import warnings
import os, sys
from config import *
from load import *
warnings.filterwarnings("ignore")

if __name__ == '__main__':
    print("Column Names:")
    print("GraphID, Total Loss, Reward, Overtime, Undertime")
    if train:
        print('Start Pre-Training')
        if nepoch_pre > 0:
            ### Pre Train ###
            # Load Model
            gnn = g.GNN().cuda()
            gnn.sharpness = sharpness
            gnn.noiselevel = noiselevel
            print('Num of parameters:',
                  sum(p.numel() for p in gnn.parameters() if p.requires_grad))
            optimizer = optim.Adam(gnn.parameters(), lr=lr_pre)
            try:
                gnn.load_state_dict(torch.load(modeldir + pre_modelname))
                gnn.eval()
            except:
                print('No available GNN model exists')
            for i_epoch in range(nepoch_pre):
                for i_batch, graph in enumerate(dataloader):
                    gnn.zero_grad()
                    time, edge_index = gnn(graph, train_be[i_batch],
                                           train_bh[i_batch],
コード例 #4
0
train_dataset, validate_dataset = gnn.dataset_split(dataset)
print("Splitted train / validate:", len(train_dataset), '/',
      len(validate_dataset))

test_dataset = gnn.read_dataset(dataset_path, "test")
print("Test dataset:", len(test_dataset))

# weight
W_sgd = np.load("W_sgd.npy")
A_sgd = np.load("A_sgd.npy")

W_momentum = np.load("W_momentum.npy")
A_momentum = np.load("A_momentum.npy")

gnet_sgd = gnn.GNN(W_sgd, A_sgd)
gnet_momentum = gnn.GNN(W_momentum, A_momentum)

# mean loss
sgd_loss_t = gnet_sgd.batch_loss(train_dataset) / len(train_dataset)
sgd_loss = gnet_sgd.batch_loss(validate_dataset) / len(validate_dataset)
momentum_loss_t = gnet_momentum.batch_loss(train_dataset) / len(train_dataset)
momentum_loss = gnet_momentum.batch_loss(validate_dataset) / len(
    validate_dataset)

# confusion matrix
conf_sgd_t = gnet_sgd.validate(train_dataset)
conf_momentum_t = gnet_momentum.validate(train_dataset)

conf_sgd = gnet_sgd.validate(validate_dataset)
conf_momentum = gnet_momentum.validate(validate_dataset)
コード例 #5
0
    def test_aggregate(self):
        dataset = gnn.read_dataset(dataset_path, "train")
        train_dataset, validate_dataset = gnn.dataset_split(dataset)
        test_dataset = gnn.read_dataset(dataset_path, "test")

        # adjacency matrix of dataset[0] is
        # array([[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        #        [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
        #        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        #        [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
        #        [0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
        #        [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
        #        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        #        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        #        [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
        #        [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1],
        #        [1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0]])
        # so when we use R^{11x8} matrix
        # array([[1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0],
        #        [1, 0, 0, 0, 0, 0, 0, 0]])
        # for X, we get
        # array([[ 2.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 2.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 2.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 2.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 3.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 4.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
        # for first aggregation. And we choose
        # array([[ 1.,  1., -1.,  0.,  0.,  0.,  0.,  0.],
        #        [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
        #        [ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.],
        #        [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
        #        [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],
        #        [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
        #        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]])
        # for W in order to check if 'combine' phase is working well.
        # When we use this W, we get
        # array([[ 2.,  2.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 2.,  2.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 2.,  2.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 2.,  2.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 3.,  3.,  0.,  0.,  0.,  0.,  0.,  0.],
        #        [ 4.,  4.,  0.,  0.,  0.,  0.,  0.,  0.]])
        # for updated X because X[:, 1] is copied from X[:, 0], and
        # X[:, 2] is copied from X[:, 0] too but the sign be inverted
        # so X[:, 2] must be vanished because of ReLU.
        # Then we get
        # array([ 18., 18.,  0.,  0.,  0.,  0.,  0.,  0.])
        # for READOUT.

        # dimension of node signal
        D = 8

        # number of nodes
        N = dataset[0][0]

        # adjacency matrix
        G = dataset[0][1]

        # weight
        W = np.eye(D)
        W[0, 1] = 1
        W[0, 2] = -1

        # we don't use this parameter in this test
        A = np.ones(D + 1)
        A[D] = 0

        # signal on nodes
        X = np.zeros((N, D))
        X[:, 0] = np.ones(N)

        gnet = gnn.GNN(W, A)

        assert_array_equal(
            gnet.aggregate(X, G),
            np.array([[2., 0., 0., 0., 0., 0., 0., 0.],
                      [2., 0., 0., 0., 0., 0., 0., 0.],
                      [0., 0., 0., 0., 0., 0., 0., 0.],
                      [1., 0., 0., 0., 0., 0., 0., 0.],
                      [2., 0., 0., 0., 0., 0., 0., 0.],
                      [2., 0., 0., 0., 0., 0., 0., 0.],
                      [0., 0., 0., 0., 0., 0., 0., 0.],
                      [1., 0., 0., 0., 0., 0., 0., 0.],
                      [1., 0., 0., 0., 0., 0., 0., 0.],
                      [3., 0., 0., 0., 0., 0., 0., 0.],
                      [4., 0., 0., 0., 0., 0., 0., 0.]]))
        assert_array_equal(
            gnet.combine(gnet.aggregate(X, G), W),
            np.array([[2., 2., 0., 0., 0., 0., 0., 0.],
                      [2., 2., 0., 0., 0., 0., 0., 0.],
                      [0., 0., 0., 0., 0., 0., 0., 0.],
                      [1., 1., 0., 0., 0., 0., 0., 0.],
                      [2., 2., 0., 0., 0., 0., 0., 0.],
                      [2., 2., 0., 0., 0., 0., 0., 0.],
                      [0., 0., 0., 0., 0., 0., 0., 0.],
                      [1., 1., 0., 0., 0., 0., 0., 0.],
                      [1., 1., 0., 0., 0., 0., 0., 0.],
                      [3., 3., 0., 0., 0., 0., 0., 0.],
                      [4., 4., 0., 0., 0., 0., 0., 0.]]))
        assert_array_equal(gnet.readout(gnet.combine(gnet.aggregate(X, G), W)),
                           np.array([18., 18., 0., 0., 0., 0., 0., 0.]))
コード例 #6
0
dataset_path = "../datasets"

dataset = gnn.read_dataset(dataset_path, "train")
print("Whole training dataset:", len(dataset))

train_dataset, validate_dataset = gnn.dataset_split(dataset)
print("Splitted train / validate:", len(train_dataset), '/', len(validate_dataset))

test_dataset = gnn.read_dataset(dataset_path, "test")
print("Test dataset:", len(test_dataset))



# weight
W_momentum = np.load("W_momentum.npy")
A_momentum = np.load("A_momentum.npy")

gnet_momentum = gnn.GNN(W_momentum, A_momentum)

test_data_list = []
for idx, data in sorted(test_dataset.items()):
    print(idx)
    test_data_list.append(data)

y_pred = gnet_momentum.predict_list(test_data_list)

with open("prediction.txt", "w") as f:
    for y in y_pred:
        f.write(str(y)+'\n')
コード例 #7
0
# dimension of node signal
D = 8

# choose one data you like
N, G, y = dataset[1004]

# weight
W = np.random.normal(0, 0.4, (D, D))
A = np.random.normal(0, 0.4, D + 1)
A[D] = 0

# signal on nodes
X = np.zeros((N, D))
X[:, 0] = np.ones(N)

gnet = gnn.GNN(W, A)

print("loss for label 0:", gnet.batch_loss([(N, G, 0)], W, A))
print("loss for label 1:", gnet.batch_loss([(N, G, 1)], W, A))

gnet.calc_gradient([(N, G, 1)])
loss = gnet.gradient_descent([(N, G, 1)], alpha=1e-3, repeat=1000)

print("loss for label 1 after fitting:", gnet.batch_loss([(N, G, 1)]))

np.save("loss", loss)

plt.figure()
plt.plot(loss)
plt.xlabel("repeat T times")
plt.ylabel("loss")