-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplgnn.py
152 lines (100 loc) · 3.78 KB
/
simplgnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 4 11:16:16 2020
@author: REZA
"""
import torch
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
import networkx as nx
import imageio
from celluloid import Camera
from IPython.display import HTML
plt.rcParams['animation.ffmpeg_path'] = '/usr/local/bin/ffmpeg'
class GCNConv(nn.Module):
def __init__(self, A, in_channels, out_channels):
super(GCNConv, self).__init__()
self.A_hat = A+torch.eye(A.size(0))
self.D = torch.diag(torch.sum(A,1))
self.D = self.D.inverse().sqrt()
#self.D = self.D.pow(-0.5)
self.A_hat = torch.mm(torch.mm(self.D, self.A_hat), self.D)
self.W = nn.Parameter(torch.rand(in_channels,out_channels, requires_grad=True))
def forward(self, X):
out = torch.relu(torch.mm(torch.mm(self.A_hat, X), self.W))
return out
class Net(torch.nn.Module):
def __init__(self,A, nfeat, nhid, nout):
super(Net, self).__init__()
self.conv1 = GCNConv(A,nfeat, nhid)
self.conv2 = GCNConv(A,nhid, nout)
def forward(self,X):
H = self.conv1(X)
H2 = self.conv2(H)
return H2
data=torch.Tensor([[0,1,1,0,1,0,0,1],
[1,0,1,0,0,1,1,0],
[1,1,0,1,1,0,1,0],
[0,0,1,0,1,0,1,1],
[1,0,1,1,0,1,1,0],
[0,1,0,0,1,0,0,1],
[0,1,1,1,1,0,0,0],
[1,0,0,1,0,1,0,0]
])
target=torch.tensor([0,-1,-1,-1,-1, -1, -1, 1])
graph = nx.Graph()
for i in range (data.shape[0]):
graph.add_node(i)
for i in range (data.shape[0]):
for j in range(data.shape[0]):
if data[i][j]==1:
graph.add_edge(i,j)
nx.draw_networkx(graph)
print(nx.info(graph))
features=torch.Tensor([[10, 230, 147,0,0,0,0,0],
[5, 52, 200,0,0,0,0,0],
[3, 76, 260,0,0,0,0,0],
[21, 42, 100,0,0,0,0,0],
[0, 12, 16,0,0,0,0,0],
[1, 330, 280,0,0,0,0,0],
[33, 178, 15,0,0,0,0,0],
[11, 90, 96,0,0,0,0,0]
])
X=torch.zeros(8,8)
T=Net(data,features.size(0), 10, 2)
#features=torch.eye(data.size(0))
criterion = torch.nn.CrossEntropyLoss(ignore_index=-1)
optimizer = optim.SGD(T.parameters(), lr=0.01, momentum=0.9)
loss=criterion(T(features),target)
print(loss)
#%% Plot animation using celluloid
fig = plt.figure()
camera = Camera(fig)
for i in range(200):
optimizer.zero_grad()
loss=criterion(T(features), target)
loss.backward()
optimizer.step()
l=(T(features));
plt.scatter(l.detach().numpy()[:,0],
l.detach().numpy()[:,1],c=[0, 0, 0, 0 ,1 ,1 ,0, 1])
for i in range(l.shape[0]):
text_plot = plt.text(l[i,0], l[i,1], str(i+1))
camera.snap()
if i%20==0:
print("Cross Entropy Loss: =", loss.item())
animation = camera.animate(blit=False, interval=150)
animation.save('./reza.gif',writer='ffmpeg', fps=60)
HTML(animation.to_html5_video())
'''
features=torch.Tensor([[10., 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., 0.],
[0., 0., 0., 0., 0., 0., 0., 10.]
])
'''