You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/11/13 00:33:45 UTC

[GitHub] sandeep-krishnamurthy closed pull request #12805: Make output of multi input hybrid block unique

sandeep-krishnamurthy closed pull request #12805: Make output of multi input hybrid block unique
URL: https://github.com/apache/incubator-mxnet/pull/12805
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py
index 6cb9fc690b5..883686c6369 100644
--- a/python/mxnet/gluon/block.py
+++ b/python/mxnet/gluon/block.py
@@ -923,6 +923,13 @@ def forward(self, x, *args):
             "Symbol or NDArray, but got %s"%type(x)
         params = {i: j.var() for i, j in self._reg_params.items()}
         with self.name_scope():
+            # This is required to handle case where there are more than one
+            # inputs passing through same block.
+            # Without this change, prefix will be same for both the inputs
+            # passing through this block producing same name output.
+            # So, we append name of input to make it unique.
+            if self.name_scope()._name_scope:
+                self.name_scope()._name_scope._prefix += x.name + "_"
             return self.hybrid_forward(symbol, x, *args, **params)
 
     def hybrid_forward(self, F, x, *args, **kwargs):
diff --git a/tests/python/unittest/test_gluon.py b/tests/python/unittest/test_gluon.py
index a6932d252dd..c033023bdd7 100644
--- a/tests/python/unittest/test_gluon.py
+++ b/tests/python/unittest/test_gluon.py
@@ -2552,6 +2552,41 @@ def hybrid_forward(self, F, x):
             net = Net(act0, act1, shape, slice)
             check_layer_forward_withinput(net, x)
 
+@with_seed()
+def test_multi_input_hybrid_block():
+    class Net(gluon.HybridBlock):
+        def __init__(self):
+            super(Net, self).__init__()
+            with self.name_scope():
+                self.dense = mx.gluon.nn.Dense(units=5)
+
+        def hybrid_forward(self, F, x, y):
+            # We apply same dense layer in the block
+            # for both the inputs.
+            # We expect output names should be different.
+            out_x = self.dense(x)
+            out_y = self.dense(y)
+            return out_x + out_y
+
+    # Create Hybrid block, export the model.
+    net1 = Net()
+    net1.initialize()
+    net1.hybridize()
+    data1 = mx.nd.random_normal(shape=(10,1))
+    data2 = mx.nd.random_normal(shape=(10,1))
+    out1 = net1(data1, data2)
+    net1.export('multi-input-net', epoch=0)
+
+    # Import the model and pass the same input. We expect:
+    # 1. No conflicts to import the model.
+    # 2. Same outputs.
+    net2 = gluon.SymbolBlock.imports(
+        'multi-input-net-symbol.json', ['data0', 'data1'],
+        'multi-input-net-0000.params')
+    out2 = net2(data1, data2)
+
+    assert_almost_equal(out1.asnumpy(), out2.asnumpy())
+
 if __name__ == '__main__':
     import nose
     nose.runmodule()


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services