-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
90 lines (81 loc) · 3.23 KB
/
model.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
from torch.nn.functional import dropout
from torch_geometric.nn import GCNConv, GINConv, SAGEConv, RGCNConv, FastRGCNConv, GATConv, global_add_pool
from torch_geometric.utils import negative_sampling
from torch_geometric.nn import GCNConv, GINConv
from sklearn.metrics import roc_auc_score
from torch.nn.functional import binary_cross_entropy_with_logits
import matplotlib.pyplot as plt
import torch
class Net(torch.nn.Module):
def __init__(self, in_channels, hid_channels, out_channels):
super(Net, self).__init__()
torch.manual_seed(0)
# 1st type of graph layer
self.conv1 = GCNConv(in_channels, hid_channels)
self.conv2 = GCNConv(hid_channels, hid_channels)
self.conv3 = GCNConv(hid_channels, out_channels)
# 2nd type of graph layer
self.conv4 = SAGEConv(in_channels=in_channels, out_channels=hid_channels)
self.conv5 = SAGEConv(in_channels=hid_channels, out_channels=hid_channels)
self.conv6 = SAGEConv(in_channels=hid_channels, out_channels=out_channels)
# 3rd type of graph layer
self.conv7 = GATConv(in_channels, hid_channels, heads=1, dropout=0.6)
self.conv8 = GATConv(hid_channels*1, out_channels, concat=False, heads=8, dropout=0.6)
self.conv9 = GATConv(hid_channels*1, hid_channels, concat=False, heads=4, dropout=0.6)
def encode(self, x, edge_index, index = 0):
if index == 0:
x = self.conv1(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv2(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv3(x, edge_index)
elif index == 1:
x = self.conv4(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv5(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv6(x, edge_index)
elif index == 2:
x = self.conv7(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv9(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv8(x, edge_index)
elif index == 3:
x = self.conv4(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv2(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv6(x, edge_index)
elif index == 4:
x = self.conv7(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv5(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv8(x, edge_index)
elif index == 5:
x = self.conv1(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv9(x, edge_index)
x = x.relu()
x = dropout(x, p=0.5, training=self.training)
x = self.conv3(x, edge_index)
return x
def decode(self, z, pos_edge_index, neg_edge_index):
edge_index = torch.cat([pos_edge_index, neg_edge_index], dim=1)
logits = (z[edge_index[0]] * z[edge_index[1]]).sum(dim=-1)
return logits
def decode_all(self, z):
prob_adj = z @ z.t()
return (prob_adj > 0).nonzero(as_tuple=False).t(), prob_adj