samskalicky edited a comment on issue #19535:
URL:
https://github.com/apache/incubator-mxnet/issues/19535#issuecomment-727159044
Heres a silly example of a hierarchical Block/HybridBlock to play around
with:
```
class MyBlock(mx.gluon.nn.Block):
def __init__(self, **kwargs):
super(MyBlock, self).__init__(**kwargs)
def add(self, block):
self._children[block.name + str(len(self._children))] = block
def forward(self, x, *args):
out = (x,) + args
for block in self._children.values():
out = block(*out)
return out
# create the Model
inside = MyBlock()
inside.add(mx.gluon.nn.Dense(10))
net = MyBlock()
net.add(inside)
net.add(mx.gluon.nn.Dense(10))
net.initialize()
x = mx.nd.empty((1,10))
out = net(x)
#hybridize and create cached_graphs
net.hybridize()
out = net(x)
#save cached_graphs
save_cached_graphs(net)
```
The hierarchy should look like this, where a top level Block(0) has a child
Block(1) and a HybridBlock(1) and the child Block(1) has a child HybridBlock(0).
```
- MyBlock(0)
|
- MyBlock(1)
| |
| - Dense(0)
- Dense(1)
```
----------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]