I was trying to build a tree like neural net, where there are nested modules in ModuleList/ModuleDict objects. However I encountered a maximum recursion bug in the top node, the ‘root’ of the tree. To make it simple, I created a minimal example to reproduce this error (Pythorch 1.2):

class TreeNode_Test(nn.Module):
    def __init__(self):
        super(TreeNode_Test, self).__init__()
        self.nodesInLevels = nn.ModuleList([self]) 

myModel = TreeNode_Test()
myModel # when calling this or myModel.nodesInLevels I ll get max recursion error:


  File "C:\Users\mk23\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1042, in __repr__
    mod_str = repr(module)

  File "C:\Users\mk23\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1042, in __repr__
    mod_str = repr(module)

  File "C:\Users\mk23\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1036, in __repr__
    extra_repr = self.extra_repr()

RecursionError: maximum recursion depth exceeded


Any ideas?