-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_nets.py
More file actions
174 lines (144 loc) · 6.54 KB
/
graph_nets.py
File metadata and controls
174 lines (144 loc) · 6.54 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
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
# Copyright 2018 The GraphNets Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
from typing import List
import sonnet as snt
from graph_nets import modules
from graph_nets import utils_tf
NUM_LAYERS = 2 # Hard-code number of layers in the edge/node/global models.
LATENT_SIZE = 32 # Hard-code latent layer sizes for demos.
def make_mlp_model():
"""Instantiates a new MLP, followed by LayerNorm.
The parameters of each new MLP are not shared with others generated by
this function.
Returns:
A Sonnet module which contains the MLP and LayerNorm.
"""
return snt.Sequential([
snt.nets.MLP([LATENT_SIZE] * NUM_LAYERS, activate_final=True),
snt.LayerNorm()
])
def make_mlp_model_custom(layers: List[int]):
"""Instantiates a new MLP, followed by LayerNorm.
The parameters of each new MLP are not shared with others generated by
this function.
Returns:
A Sonnet module which contains the MLP and LayerNorm.
"""
return lambda: snt.Sequential([
snt.nets.MLP(layers, activate_final=True),
snt.LayerNorm()
])
class MLPGraphIndependent(snt.AbstractModule):
"""GraphIndependent with MLP edge, node, and global models."""
def __init__(self, name="MLPGraphIndependent"):
super(MLPGraphIndependent, self).__init__(name=name)
with self._enter_variable_scope():
self._network = modules.GraphIndependent(
edge_model_fn=make_mlp_model,
node_model_fn=make_mlp_model,
global_model_fn=make_mlp_model)
def _build(self, inputs):
return self._network(inputs)
class MLPGraphNetwork(snt.AbstractModule):
"""Full GraphNetwork with MLP edge, node, and global models."""
def __init__(self, name="MLPGraphNetwork"):
super(MLPGraphNetwork, self).__init__(name=name)
with self._enter_variable_scope():
self._network = modules.GraphNetwork(make_mlp_model, make_mlp_model,
make_mlp_model)
def _build(self, inputs):
return self._network(inputs)
class EncodeProcessDecode(snt.AbstractModule):
"""Full encode-process-decode model.
The model we explore includes three components:
- An "Encoder" graph net, which independently encodes the edge, node, and
global attributes (does not compute relations etc.).
- A "Core" graph net, which performs N rounds of processing (message-passing)
steps. The input to the Core is the concatenation of the Encoder's output
and the previous output of the Core (labeled "Hidden(t)" below, where "t" is
the processing step).
- A "Decoder" graph net, which independently decodes the edge, node, and
global attributes (does not compute relations etc.), on each message-passing
step.
Hidden(t) Hidden(t+1)
| ^
*---------* | *------* | *---------*
| | | | | | | |
Input --->| Encoder | *->| Core |--*->| Decoder |---> Output(t)
| |---->| | | |
*---------* *------* *---------*
"""
def __init__(self,
edge_output_size=None,
node_output_size=None,
global_output_size=None,
name="EncodeProcessDecode"):
super(EncodeProcessDecode, self).__init__(name=name)
self._encoder = MLPGraphIndependent()
self._core = MLPGraphNetwork()
self._decoder = MLPGraphIndependent()
# Transforms the outputs into the appropriate shapes.
if edge_output_size is None:
edge_fn = None
else:
edge_fn = lambda: snt.Linear(edge_output_size, name="edge_output")
if node_output_size is None:
node_fn = None
else:
node_fn = lambda: snt.Linear(node_output_size, name="node_output")
if global_output_size is None:
global_fn = None
else:
global_fn = lambda: snt.Linear(global_output_size,
name="global_output")
with self._enter_variable_scope():
self._output_transform = modules.GraphIndependent(edge_fn, node_fn,
global_fn)
def _build(self, input_op, num_processing_steps):
latent = self._encoder(input_op)
latent0 = latent
output_ops = []
for _ in range(num_processing_steps):
core_input = utils_tf.concat([latent0, latent], axis=1)
latent = self._core(core_input)
decoded_op = self._decoder(latent)
output_ops.append(self._output_transform(decoded_op))
return output_ops
class MLPCustomGN(snt.AbstractModule):
"""Full GraphNetwork with MLP edge, node, and global models."""
def __init__(self, layer_size=32, layer_count=3, name="MLPCustomGN"):
super(MLPCustomGN, self).__init__(name=name)
layers = [layer_size] * layer_count
with self._enter_variable_scope():
self._network = modules.GraphNetwork(make_mlp_model_custom(layers), make_mlp_model_custom(layers),
make_mlp_model_custom(layers))
def _build(self, inputs):
return self._network(inputs)
class DDRGraphNetwork(snt.AbstractModule):
"""
A custom graph network to be used for data-driven routing
"""
def __init__(self, layer_size=32, layer_count=3,
name="DDRGraphNetwork"):
super(DDRGraphNetwork, self).__init__(name=name)
with self._enter_variable_scope():
self._block = MLPCustomGN(layer_size, layer_count)
def _build(self, input_op, num_processing_steps):
latent = input_op
latent0 = input_op
for _ in range(num_processing_steps):
gn_input = utils_tf.concat([latent0, latent], axis=1)
latent = self._block(gn_input)
return latent