TypeError: tuple is not a Module subclass when trying to do nn.Sequential(*ourmodel)

Hello everyone. I’m trying to edit an existing pretrained model and add my own.
I followed the discussions here and here and faced with two options, either of which fail!
the first thread says do :

resnet18 = models.resnet18(pretrained=True)
ourmodel = list(resnet18.named_children())[:-1]
ourmodel = nn.Sequential(*ourmodel)

This fails with the error :
**TypeError** : tuple is not a Module subclass

The second option was to do :

resnet18 = models.resnet18(pretrained=True)
ourmodel = list(resnet18.named_children())[:-1]
ourmodel = nn.ModuleList(ourmodel)

which again results in the same exact error :
**TypeError** : tuple is not a Module subclass

Has pytorch’s behavior changed ? I’m on Pytorch 1.0
What should I do ?

Here is the full stacktrace :

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
     27 ourmodel = list(resnet18.named_children())[:-1]
     28 # ourmodel = nn.ModuleList(ourmodel)
---> 29 ourmodel = nn.Sequential(*ourmodel)
     30 
     31 for i, (name,child) in enumerate(ourmodel.named_children()):

~\Anaconda3\lib\site-packages\torch\nn\modules\container.py in __init__(self, *args)
     51         else:
     52             for idx, module in enumerate(args):
---> 53                 self.add_module(str(idx), module)
     54 
     55     def _get_item_by_idx(self, iterator, idx):

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in add_module(self, name, module)
    177         if not isinstance(module, Module) and module is not None:
    178             raise TypeError("{} is not a Module subclass".format(
--> 179                 torch.typename(module)))
    180         elif not isinstance(name, torch._six.string_classes):
    181             raise TypeError("module name should be a string. Got {}".format(

TypeError: tuple is not a Module subclass

Thank you all very much in advance

Replace resnet18.named_children() with resnet18.children()

1 Like

I had issues with the proposed solution as this user had https://github.com/pytorch/pytorch/issues/15129 I ended up just mutating the last layer as the official pytorch TL tutorial did.