-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmodel_geniepath.py
85 lines (68 loc) · 2.75 KB
/
model_geniepath.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
import torch
import torch.nn.functional as F
from torch_geometric.nn import GATConv
dim = 256
lstm_hidden = 256
heads = 1
layer_num = 4
class Breadth(torch.nn.Module):
def __init__(self, in_dim, out_dim):
super(Breadth, self).__init__()
self.gatconv = GATConv(in_dim, out_dim, heads=heads)
def forward(self, x, edge_index):
x = torch.tanh(self.gatconv(x, edge_index))
return x
class Depth(torch.nn.Module):
def __init__(self, in_dim, hidden):
super(Depth, self).__init__()
self.lstm = torch.nn.LSTM(in_dim, hidden, 1, bias=False)
def forward(self, x, h, c):
x, (h, c) = self.lstm(x, (h, c))
return x, (h, c)
class GeniePathLayer(torch.nn.Module):
def __init__(self, in_dim):
super(GeniePathLayer, self).__init__()
self.breadth_func = Breadth(in_dim, dim)
self.depth_func = Depth(dim, lstm_hidden)
def forward(self, x, edge_index, h, c):
x = self.breadth_func(x, edge_index)
x = x[None, :]
x, (h, c) = self.depth_func(x, h, c)
x = x[0]
return x, (h, c)
class GeniePath(torch.nn.Module):
def __init__(self, in_dim, out_dim, device):
super(GeniePath, self).__init__()
self.device = device
self.lin1 = torch.nn.Linear(in_dim, dim)
self.gplayers = torch.nn.ModuleList([GeniePathLayer(dim) for i in range(layer_num)])
self.lin2 = torch.nn.Linear(dim, out_dim)
def forward(self, x, edge_index):
x = self.lin1(x)
h = torch.zeros(1, x.shape[0], lstm_hidden).to(self.device)
c = torch.zeros(1, x.shape[0], lstm_hidden).to(self.device)
for i, l in enumerate(self.gplayers):
x, (h, c) = self.gplayers[i](x, edge_index, h, c)
x = self.lin2(x)
return x
class GeniePathLazy(torch.nn.Module):
def __init__(self, in_dim, out_dim, device):
super(GeniePathLazy, self).__init__()
self.device = device
self.lin1 = torch.nn.Linear(in_dim, dim)
self.breaths = torch.nn.ModuleList([Breadth(dim, dim) for i in range(layer_num)])
self.depths = torch.nn.ModuleList([Depth(dim * 2, lstm_hidden) for i in range(layer_num)])
self.lin2 = torch.nn.Linear(dim, out_dim)
def forward(self, x, edge_index):
x = self.lin1(x)
h = torch.zeros(1, x.shape[0], lstm_hidden).to(self.device)
c = torch.zeros(1, x.shape[0], lstm_hidden).to(self.device)
h_tmps = []
for i, l in enumerate(self.breaths):
h_tmps.append(self.breaths[i](x, edge_index))
x = x[None, :]
for i, l in enumerate(self.depths):
in_cat = torch.cat((h_tmps[i][None, :], x), -1)
x, (h, c) = self.depths[i](in_cat, h, c)
x = self.lin2(x[0])
return x