-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
82 lines (61 loc) · 2.48 KB
/
models.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
# -*- coding:utf-8 -*-
import torch.nn.functional as F
from torch import nn
from torch_geometric.nn import GATConv, GCNConv, SAGEConv
class GCN_LP(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels):
super(GCN_LP, self).__init__()
self.conv1 = GCNConv(in_channels, hidden_channels)
self.conv2 = GCNConv(hidden_channels, out_channels)
def encode(self, data):
x, edge_index = data.x, data.edge_index
x = F.relu(self.conv1(x, edge_index))
x = F.dropout(x, p=0.5, training=self.training)
x = self.conv2(x, edge_index)
return x
def decode(self, z, edge_label_index):
src = z[edge_label_index[0]]
dst = z[edge_label_index[1]]
r = (src * dst).sum(dim=-1)
return r
def forward(self, data, edge_label_index):
z = self.encode(data)
return self.decode(z, edge_label_index)
class SAGE_LP(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels):
super(SAGE_LP, self).__init__()
self.conv1 = SAGEConv(in_channels, hidden_channels)
self.conv2 = SAGEConv(hidden_channels, out_channels)
def encode(self, data):
x, edge_index = data.x, data.edge_index
x = F.relu(self.conv1(x, edge_index))
x = F.dropout(x, p=0.5, training=self.training)
x = self.conv2(x, edge_index)
return x
def decode(self, z, edge_label_index):
src = z[edge_label_index[0]]
dst = z[edge_label_index[1]]
r = (src * dst).sum(dim=-1)
return r
def forward(self, data, edge_label_index):
z = self.encode(data)
return self.decode(z, edge_label_index)
class GAT_LP(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels):
super(GAT_LP, self).__init__()
self.conv1 = GATConv(in_channels, hidden_channels, heads=8, concat=False)
self.conv2 = GATConv(hidden_channels, out_channels, heads=8, concat=False)
def encode(self, data):
x, edge_index = data.x, data.edge_index
x = F.relu(self.conv1(x, edge_index))
x = F.dropout(x, p=0.5, training=self.training)
x = self.conv2(x, edge_index)
return x
def decode(self, z, edge_label_index):
src = z[edge_label_index[0]]
dst = z[edge_label_index[1]]
r = (src * dst).sum(dim=-1)
return r
def forward(self, data, edge_label_index):
z = self.encode(data)
return self.decode(z, edge_label_index)