From df05809140ffe54b084e8e118c3b595d87eceb98 Mon Sep 17 00:00:00 2001 From: Eric Hunsberger Date: Wed, 4 Mar 2020 17:07:29 -0400 Subject: [PATCH] Remove problematic source tensor sorting This is no longer required, and is problematic for models that have an output that is used other places in the model (since the sorting puts all outputs at the end). --- CHANGES.rst | 3 +++ nengo_dl/converter.py | 12 ------------ nengo_dl/tests/test_converter.py | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 8e13cb486..4051d9568 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -75,6 +75,8 @@ Release history - Added ``tensorflow-cpu`` distributions to installation checks (so Nengo DL will not attempt to reinstall TensorFlow if ``tensorflow-cpu`` is already installed). (`#142`_) +- Fixed bug when applying the Converter to Keras models that re-use intermediate + layers as output layers. (`#137`_) **Deprecated** @@ -88,6 +90,7 @@ Release history .. _#129: https://github.com/nengo/nengo-dl/pull/129 .. _#130: https://github.com/nengo/nengo-dl/pull/130 .. _#134: https://github.com/nengo/nengo-dl/pull/134 +.. _#137: https://github.com/nengo/nengo-dl/pull/137 .. _#139: https://github.com/nengo/nengo-dl/pull/139 .. _#140: https://github.com/nengo/nengo-dl/pull/140 .. _#142: https://github.com/nengo/nengo-dl/pull/142 diff --git a/nengo_dl/converter.py b/nengo_dl/converter.py index 2b3e6986c..f04058e95 100644 --- a/nengo_dl/converter.py +++ b/nengo_dl/converter.py @@ -817,18 +817,6 @@ def convert(self, node_id): # that need to be built into the Nengo network source_tensors = self.trace_tensors(self.layer.outputs) - def sort_key(x): - # sort tensors so that order of model inputs/outputs is preserved - for i, y in enumerate(self.layer.inputs): - if x is y: - return -(len(self.layer.inputs) - i) - for i, y in enumerate(self.layer.outputs): - if x is y: - return i + 1 - return 0 - - source_tensors = sorted(source_tensors, key=sort_key) - for tensor in source_tensors: # look up the layer/node to be converted model_layer, model_node_id, _ = self.get_history(tensor) diff --git a/nengo_dl/tests/test_converter.py b/nengo_dl/tests/test_converter.py index b99f7f88a..e494dfedc 100644 --- a/nengo_dl/tests/test_converter.py +++ b/nengo_dl/tests/test_converter.py @@ -701,3 +701,20 @@ def test_layer_dicts(): assert conv.outputs[x0].target is conv.layers[x0] assert conv.outputs[x1].target is conv.layers[x1] + + +def test_mid_model_output(Simulator): + """Check that converter supports output tensors from the middle of the model. + + Previous converter put output tensors last in build order, so having an output + tensor that needed to be built before non-output tensors was problematic. + https://github.com/nengo/nengo-dl/pull/137 + """ + + # model must have at least three layers, with one layer in between outputs + inp = tf.keras.Input(shape=(1,)) + x0 = tf.keras.layers.ReLU()(inp) + x1 = tf.keras.layers.ReLU()(x0) + x2 = tf.keras.layers.ReLU()(x1) + + _test_convert(inp, [x0, x2], inp_vals=[np.ones((4, 1))])