-
Notifications
You must be signed in to change notification settings - Fork 0
/
GVAECiteSeer.py
256 lines (131 loc) · 5.51 KB
/
GVAECiteSeer.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python
# coding: utf-8
import torch
from torch_geometric.datasets import Planetoid
import torch_geometric.transforms as T
from torch_geometric.nn import GCNConv
from torch_geometric.utils import train_test_split_edges #depricated
dataset = Planetoid("./data", "CiteSeer", transform = T.NormalizeFeatures())
dataset.data # X = [3327, 3703] for each node we have 3703 labels
data = dataset[0]
data.train_mask = data.val_mask = data.test_mask = None
data
data = train_test_split_edges(data)
data = T.RandomLinkSplit(data)
data
type(data)
# neg_edge -> edges that are not in graph , pos_edge -> edges that are present in graph
# ## Define The Encoder
class GCNEncoder(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(GCNEncoder, self).__init__() # in this case we have only one graph, it is useful to cache
self.conv1 = GCNConv(in_channels, 2*out_channels, cached=True) #cached only for transductive learning - caches the normalization of the adjacences matrices
self.conv2 = GCNConv(2*out_channels, out_channels, cached=True) #cached only for transductive learning
def forward(self, x, edge_index):
x = self.conv1(x, edge_index).relu()
return self.conv2(x, edge_index)
# ## Define The AutoEncoder
from torch_geometric.nn import GAE
# paramets
out_channels = 2
num_features = dataset.num_features
epochs = 100
# model
model = GAE(GCNEncoder(num_features, out_channels))
#move to GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
x = data.num_val.x.to(device)
train_pos_edge_index = data.num_val.train_pos_edge_index.to(device)
# optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
model
#dir(model)
def train():
model.train()
optimizer.zero_grad()
z = model.encode(x, train_pos_edge_index)
loss = model.recon_loss(z, train_pos_edge_index)
loss.backward()
optimizer.step()
return float(loss)
def test(pos_edge_index, neg_edge_index):
model.eval()
with torch.no_grad():
z = model.encode(x, pos_edge_index)
return model.test(z, pos_edge_index, neg_edge_index)
for epoch in range(1, epochs+1):
loss = train()
auc, ap = test(data.num_val.test_pos_edge_index, data.num_val.test_neg_edge_index)
print('Epoch: {:03d}, AUC: {:.4f}, AP: {:.4f}'.format(epoch, auc, ap))
Z = model.encode(x, train_pos_edge_index)
Z
# ## Use Tensorboard
from torch.utils.tensorboard import SummaryWriter
# paramets
out_channels = 20
num_features = dataset.num_features
epochs = 1000
# model
model = GAE(GCNEncoder(num_features, out_channels))
#move to GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
x = data.num_val.x.to(device)
train_pos_edge_index = data.num_val.train_pos_edge_index.to(device)
# optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
writer = SummaryWriter('runs/GAE_experiment_'+'20d_1000_epochs')
for epoch in range(1, epochs + 1):
loss = train()
auc, ap = test(data.num_val.test_pos_edge_index, data.num_val.test_neg_edge_index)
print('Epoch: {:03d}, AUC: {:.4f}, AP: {:.4f}'.format(epoch, auc, ap))
writer.add_scalar('auc train',auc,epoch) # new line
writer.add_scalar('ap train',ap,epoch) # new line
'''
# ## Graph Variational AutoEncoder (GVAE)
from torch_geometric.nn import VGAE
dataset = Planetoid("./data", "CiteSeer", transform = T.NormalizeFeatures())
data2 = dataset[0]
data2.train_mask = data2.val_mask = data2.test_mask = data2.y = None
data2 = T.RandomLinkSplit(data2)
class VariationalGCNEncoder(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(VariationalGCNEncoder, self).__init__()
self.conv1 = GCNConv(in_channels, 2 * out_channels, cached=True) # cache only for transductive learning
self.conv_mu = GCNConv(2 * out_channels, out_channels, cached=True)
self.conv_logstd = GCNConv(2 * out_channels, out_channels, cached=True)
def forward(self, x, edge_index):
x = self.conv1(x, edge_index).relu()
return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)
out_channels = 2
num_features = dataset.num_features
epochs = 100
model = VGAE(VariationalGCNEncoder(num_features, out_channels)) #instantiate the VGAE by passing encoder
device = torch.device('cuda'if torch.cuda.is_available() else 'cpu')
model = model.to(device)
x = data2.num_val.x.to(device)
# train_pos_edge_index = data.train_pos_edge_index.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
def train():
model.train()
optimizer.zero_grad()
z = model.encode(x, data2.num_val.train_pos_edge_index)
loss = model.recon_loss(z, data2.num_val.train_pos_edge_index)
loss = loss + (1 / data2.num_val.num_nodes) * model.kl_loss() #new_line
loss.backward()
optimizer.step()
return float(loss)
def test(pos_edge_index, neg_edge_index):
model.eval()
with torch.no_grad():
z = model.encode(x, data2.num_val.train_pos_edge_index)
return model.test(z, pos_edge_index, neg_edge_index)
writer = SummaryWriter('runs/VGAE_experiment_'+'2d_100_epochs')
for epoch in range(1, epochs+1):
loss = train()
auc, ap = test(data2.num_val.test_pos_edge_index, data2.num_val.test_neg_edge_index)
print('Epoch: {:03d}, AUC: {:.4f}, AP: {:.4f}'.format(epoch, auc, ap))
writer.add_scalar('auc_train', auc, epoch)
writer.add_scalar('ap_train', ap, epoch)
'''