"projects/vscode:/vscode.git/clone" did not exist on "a2c6af925005d0a994546b046b2ed478079b2bcd"
test.py 1.11 KB
Newer Older
1
"""
GaiYu0's avatar
GaiYu0 committed
2
ipython3 test.py -- --features 1 16 16 --gpu -1 --n-classes 5 --n-iterations 10 --n-nodes 10 --radius 3
3
4
5
6
7
"""

import argparse
import networkx as nx
import torch as th
GaiYu0's avatar
GaiYu0 committed
8
import torch.nn.functional as F
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import torch.optim as optim
import gnn

parser = argparse.ArgumentParser()
parser.add_argument('--features', nargs='+', type=int)
parser.add_argument('--gpu', type=int)
parser.add_argument('--n-classes', type=int)
parser.add_argument('--n-iterations', type=int)
parser.add_argument('--n-nodes', type=int)
parser.add_argument('--radius', type=int)
args = parser.parse_args()

if args.gpu < 0:
    cuda = False
else:
    cuda = True
    th.cuda.set_device(args.gpu)

g = nx.barabasi_albert_graph(args.n_nodes, 1).to_directed() # TODO SBM
y = th.multinomial(th.ones(args.n_classes), args.n_nodes, replacement=True)
GaiYu0's avatar
GaiYu0 committed
29
model = gnn.GNN(g, args.features, args.radius, args.n_classes)
30
if cuda:
GaiYu0's avatar
GaiYu0 committed
31
32
    model.cuda()
opt = optim.Adam(model.parameters())
33
34

for i in range(args.n_iterations):
GaiYu0's avatar
GaiYu0 committed
35
36
37
    y_bar = model()
    loss = F.cross_entropy(y_bar, y)
    opt.zero_grad()
38
    loss.backward()
GaiYu0's avatar
GaiYu0 committed
39
    opt.step()
40
41

    print('[iteration %d]loss %f' % (i, loss))