Layers and Modules

To Q1, If changing MySequential to store blocks in a Python list, I got warning when call net.initialize():

UserWarning: “MySequential.blocklist” is an unregistered container with Blocks. Note that Blocks inside the list, tuple or dict will not be registered automatically. Make sure to register them using register_child() or switching to nn.Sequential/nn.HybridSequential instead.

Looks like the “list of blocks” is unregistered container and cannot be registered automatically. And the warning suggest that we can register_child manually. Following that, we can then get the same result as using the _children dictionary.

Here is the code:

class MySequential(nn.Block):

    def __init__(self, **kwargs):
        self.blocklist = []
    def add(self, block):
        self.blocklist = self.blocklist + [block]      # Change to list
        self.register_child(block,    # To fix issue: manually register child

    def forward(self, x):
        for block in self.blocklist:
            x = block(x)
        return x
1 Like
  1. What kinds of problems will occur if you change MySequential to store blocks in a Python

My answer -> list can contain not unique object, casing problems in forward passing