-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnn.py
More file actions
149 lines (124 loc) · 5.24 KB
/
nn.py
File metadata and controls
149 lines (124 loc) · 5.24 KB
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
import torch
import torch_geometric
EMB_SIZE=64
class GNNPolicy(torch.nn.Module):
'''
from Gasse's model. see https://github.com/ds4dm/ecole/blob/master/examples/branching-imitation/example.ipynb
'''
def __init__(self,nGroup):
super().__init__()
emb_size = EMB_SIZE
cons_nfeats = 2
edge_nfeats = 1
var_nfeats = 7
group_nfeats = nGroup
# GROUP EMBEDDING
self.group_embedding = torch.nn.Sequential(
torch.nn.LayerNorm(group_nfeats),
torch.nn.Linear(group_nfeats, emb_size),
torch.nn.ReLU(),
)
# CONSTRAINT EMBEDDING
self.cons_embedding = torch.nn.Sequential(
torch.nn.LayerNorm(cons_nfeats),
torch.nn.Linear(cons_nfeats, emb_size),
torch.nn.ReLU(),
torch.nn.Linear(emb_size, emb_size),
torch.nn.ReLU(),
)
# EDGE EMBEDDING
self.edge_embedding = torch.nn.Sequential(
torch.nn.LayerNorm(edge_nfeats),
)
# VARIABLE EMBEDDING
self.var_embedding = torch.nn.Sequential(
torch.nn.LayerNorm(var_nfeats),
torch.nn.Linear(var_nfeats, emb_size),
torch.nn.ReLU(),
torch.nn.Linear(emb_size, emb_size),
torch.nn.ReLU(),
)
self.conv_v_to_c = BipartiteGraphConvolution()
self.conv_c_to_v = BipartiteGraphConvolution()
self.conv_v_to_c2 = BipartiteGraphConvolution()
self.conv_c_to_v2 = BipartiteGraphConvolution()
self.output_module = torch.nn.Sequential(
torch.nn.Linear(emb_size, emb_size),
torch.nn.ReLU(),
torch.nn.Linear(emb_size, 1, bias=False),
)
def forward(
self, constraint_features, edge_indices, edge_features, variable_features,group_features
):
reversed_edge_indices = torch.stack([edge_indices[1], edge_indices[0]], dim=0) # cons ID -> var ID
group_features = self.group_embedding(group_features)
# First step: linear embedding layers to a common dimension (64)
constraint_features = self.cons_embedding(constraint_features)
edge_features = self.edge_embedding(edge_features)
variable_features = self.var_embedding(variable_features) + group_features
# Two half convolutions
constraint_features = self.conv_v_to_c(
variable_features, reversed_edge_indices, edge_features, constraint_features
)
variable_features = self.conv_c_to_v(
constraint_features, edge_indices, edge_features, variable_features
)
constraint_features = self.conv_v_to_c2(
variable_features, reversed_edge_indices, edge_features, constraint_features
)
variable_features = self.conv_c_to_v2(
constraint_features, edge_indices, edge_features, variable_features
)
# A final MLP on the variable features
output = self.output_module(variable_features).squeeze(-1)
return output
class BipartiteGraphConvolution(torch_geometric.nn.MessagePassing):
"""
The bipartite graph convolution is already provided by pytorch geometric and we merely need
to provide the exact form of the messages being passed.
"""
def __init__(self):
super().__init__("add")
emb_size = EMB_SIZE
self.feature_module_left = torch.nn.Sequential(
torch.nn.Linear(emb_size, emb_size)
)
self.feature_module_edge = torch.nn.Sequential(
torch.nn.Linear(1, emb_size, bias=False)
)
self.feature_module_right = torch.nn.Sequential(
torch.nn.Linear(emb_size, emb_size, bias=False)
)
self.feature_module_final = torch.nn.Sequential(
torch.nn.LayerNorm(emb_size),
torch.nn.ReLU(),
torch.nn.Linear(emb_size, emb_size),
)
self.post_conv_module = torch.nn.Sequential(torch.nn.LayerNorm(emb_size))
# output_layers
self.output_module = torch.nn.Sequential(
torch.nn.Linear(2 * emb_size, emb_size),
torch.nn.ReLU(),
torch.nn.Linear(emb_size, emb_size),
)
def forward(self, left_features, edge_indices, edge_features, right_features):
"""
This method sends the messages, computed in the message method.
if flow = source_to_target (default setting), edge_indices are [j,i], where i is the central node
"""
output = self.propagate(
edge_indices,
size=(left_features.shape[0], right_features.shape[0]),
node_features=(left_features, right_features),
edge_features=edge_features,
)
return self.output_module(
torch.cat([self.post_conv_module(output), right_features], dim=-1)
)
def message(self, node_features_i, node_features_j, edge_features):
output = self.feature_module_final(
self.feature_module_left(node_features_i)
+ self.feature_module_edge(edge_features)
+ self.feature_module_right(node_features_j)
)
return output