abhinavs95 commented on issue #14373: Passing parameters to HybridBlocks and 
not using them
URL: 
https://github.com/apache/incubator-mxnet/issues/14373#issuecomment-498451771
 
 
   It seems like this is expected behavior, @eric-haibin-lin could you have a 
look and confirm?
   
   @whamza15 Since the error pops up due to deferred initialization, you can 
avoid it by specifying the input shape when creating the layers. Here is the 
full example:
   ```
   import mxnet.gluon as gl
   import mxnet as mx
   
   
   class EmbeddingBlock(gl.HybridBlock):
       def __init__(self, num_toks, dim, **kwargs):
           super(EmbeddingBlock, self).__init__(**kwargs)
           self.emb = gl.nn.Embedding(num_toks, dim)
       
       def hybrid_forward(self, F, x, valid_length):
           # NOTE valid_length is not used
           return self.emb(x)
   
   
   class Net(gl.HybridBlock):
       def __init__(self, **kwargs):
           super(Net, self).__init__(**kwargs)
           self.dense = gl.nn.Dense(3, in_units=160, flatten=False)
           self.e1 = EmbeddingBlock(10,100)
           self.e2 = EmbeddingBlock(20,60)
       
       def hybrid_forward(self, F, x1, vl1, x2, vl2):
           o = F.concat(self.e1(x1,vl1), self.e2(x2,vl2), dim=-1)
           return self.dense(o)
   
    
   net = Net()
   net.initialize()
   net.hybridize()
   x1 = mx.nd.array(range(8)).reshape(2,-1)
   vl1 = mx.nd.array([3,2])
   x2 = mx.nd.array(range(8)).reshape(2,-1)
   vl2 = mx.nd.array([3,2])
   
   net(x1, vl1, x2, vl2)
   ```
   

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


With regards,
Apache Git Services

Reply via email to