-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
infomax_transductive.py
68 lines (51 loc) · 1.78 KB
/
infomax_transductive.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
import os.path as osp
import torch
from torch_geometric.datasets import Planetoid
from torch_geometric.nn import DeepGraphInfomax, GCNConv
dataset = 'Cora'
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', dataset)
dataset = Planetoid(path, dataset)
class Encoder(torch.nn.Module):
def __init__(self, in_channels, hidden_channels):
super().__init__()
self.conv = GCNConv(in_channels, hidden_channels)
self.prelu = torch.nn.PReLU(hidden_channels)
def forward(self, x, edge_index):
x = self.conv(x, edge_index)
x = self.prelu(x)
return x
def corruption(x, edge_index):
return x[torch.randperm(x.size(0), device=x.device)], edge_index
if torch.cuda.is_available():
device = torch.device('cuda')
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
device = torch.device('mps')
else:
device = torch.device('cpu')
model = DeepGraphInfomax(
hidden_channels=512,
encoder=Encoder(dataset.num_features, 512),
summary=lambda z, *args, **kwargs: z.mean(dim=0).sigmoid(),
corruption=corruption,
).to(device)
data = dataset[0].to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
def train():
model.train()
optimizer.zero_grad()
pos_z, neg_z, summary = model(data.x, data.edge_index)
loss = model.loss(pos_z, neg_z, summary)
loss.backward()
optimizer.step()
return loss.item()
def test():
model.eval()
z, _, _ = model(data.x, data.edge_index)
acc = model.test(z[data.train_mask], data.y[data.train_mask],
z[data.test_mask], data.y[data.test_mask], max_iter=150)
return acc
for epoch in range(1, 301):
loss = train()
print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}')
acc = test()
print(f'Accuracy: {acc:.4f}')