-
Notifications
You must be signed in to change notification settings - Fork 6
/
graph_models.py
182 lines (116 loc) · 5.27 KB
/
graph_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
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
import torch
from torch_geometric.nn import GINEConv, global_max_pool
from torch import nn
import torch.nn.functional as F
class GraphEBM(nn.Module):
def __init__(self, inp_dim, out_dim, mem):
super(GraphEBM, self).__init__()
h = 128
self.edge_map = nn.Linear(1, h // 2)
self.edge_map_opt = nn.Linear(1, h // 2)
self.conv1 = GINEConv(nn.Sequential(nn.Linear(1, 128)), edge_dim=h)
self.conv2 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.conv3 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.fc1 = nn.Linear(128, 128)
self.fc2 = nn.Linear(128, 1)
def forward(self, inp, opt_edge):
edge_embed = self.edge_map(inp.edge_attr)
opt_edge_embed = self.edge_map_opt(opt_edge)
edge_embed = torch.cat([edge_embed, opt_edge_embed], dim=-1)
h = self.conv1(inp.x, inp.edge_index, edge_attr=edge_embed)
h = self.conv2(h, inp.edge_index, edge_attr=edge_embed)
h = self.conv3(h, inp.edge_index, edge_attr=edge_embed)
mean_feat = global_max_pool(h, inp.batch)
energy = self.fc2(F.relu(self.fc1(mean_feat)))
return energy
class GraphIterative(nn.Module):
def __init__(self, inp_dim, out_dim, mem):
super(GraphIterative, self).__init__()
h = 128
self.edge_map = nn.Linear(1, h // 2)
self.edge_map_opt = nn.Linear(1, h // 2)
self.conv1 = GINEConv(nn.Sequential(nn.Linear(1, 128)), edge_dim=h)
self.conv2 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.conv3 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.decode = nn.Linear(256, 1)
def forward(self, inp, opt_edge):
edge_embed = self.edge_map(inp.edge_attr)
opt_edge_embed = self.edge_map_opt(opt_edge)
edge_embed = torch.cat([edge_embed, opt_edge_embed], dim=-1)
h = self.conv1(inp.x, inp.edge_index, edge_attr=edge_embed)
h = self.conv2(h, inp.edge_index, edge_attr=edge_embed)
h = self.conv3(h, inp.edge_index, edge_attr=edge_embed)
edge_index = inp.edge_index
hidden = h
h1 = hidden[edge_index[0]]
h2 = hidden[edge_index[1]]
h = torch.cat([h1, h2], dim=-1)
output = self.decode(h)
return output
class GraphRecurrent(nn.Module):
def __init__(self, inp_dim, out_dim):
super(GraphRecurrent, self).__init__()
h = 128
self.edge_map = nn.Linear(1, h)
self.conv1 = GINEConv(nn.Sequential(nn.Linear(1, 128)), edge_dim=h)
self.lstm = nn.LSTM(h, h)
# self.conv2 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.conv3 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.decode = nn.Linear(256, 1)
def forward(self, inp, state=None):
edge_embed = self.edge_map(inp.edge_attr)
h = self.conv1(inp.x, inp.edge_index, edge_attr=edge_embed)
h, state = self.lstm(h[None], state)
h = self.conv3(h[0], inp.edge_index, edge_attr=edge_embed)
edge_index = inp.edge_index
hidden = h
h1 = hidden[edge_index[0]]
h2 = hidden[edge_index[1]]
h = torch.cat([h1, h2], dim=-1)
output = self.decode(h)
return output, state
class GraphPonder(nn.Module):
def __init__(self, inp_dim, out_dim):
super(GraphPonder, self).__init__()
h = 128
self.edge_map = nn.Linear(1, h)
self.conv1 = GINEConv(nn.Sequential(nn.Linear(1, 128)), edge_dim=h)
self.conv2 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.conv3 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.decode = nn.Linear(256, 1)
def forward(self, inp, iters=1):
edge_embed = self.edge_map(inp.edge_attr)
h = self.conv1(inp.x, inp.edge_index, edge_attr=edge_embed)
outputs = []
for i in range(iters):
h = F.relu(self.conv2(h, inp.edge_index, edge_attr=edge_embed))
output = self.conv3(h, inp.edge_index, edge_attr=edge_embed)
edge_index = inp.edge_index
hidden = output
h1 = hidden[edge_index[0]]
h2 = hidden[edge_index[1]]
output = torch.cat([h1, h2], dim=-1)
output = self.decode(output)
outputs.append(output)
return outputs
class GraphFC(nn.Module):
def __init__(self, inp_dim, out_dim):
super(GraphFC, self).__init__()
h = 128
self.edge_map = nn.Linear(1, h)
self.conv1 = GINEConv(nn.Sequential(nn.Linear(1, 128)), edge_dim=h)
self.conv2 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.conv3 = GINEConv(nn.Sequential(nn.Linear(128, 128)), edge_dim=h)
self.decode = nn.Linear(256, 1)
def forward(self, inp):
edge_embed = self.edge_map(inp.edge_attr)
h = self.conv1(inp.x, inp.edge_index, edge_attr=edge_embed)
h = self.conv2(h, inp.edge_index, edge_attr=edge_embed)
h = self.conv3(h, inp.edge_index, edge_attr=edge_embed)
edge_index = inp.edge_index
hidden = h
h1 = hidden[edge_index[0]]
h2 = hidden[edge_index[1]]
h = torch.cat([h1, h2], dim=-1)
output = self.decode(h)
return output