Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytorch nn.Sequential(*list) TypeError: list is not a Module subclass

Tags:

python

pytorch

when I use pytorch to train a model, I tried to print the whole net structure

so I packed all the layers in a list then I use nn.Sequential(*list)

but it doesn't work, and the TypeError: list is not a Module subclass

like image 425
ElevenFlogs Avatar asked Oct 30 '25 02:10

ElevenFlogs


1 Answers

Please provide the list of layers that you have created, are you sure you haven' done any error in that. Try checking if your list is actually [] and not [[..]]. The other thing that I noticed is that you have list as a variable name, which isn't a good idea - list is a Python keyword.

I tried writing a sample code of unpacking a list and it works fine for me.

import torch
import torch.nn as nn                                                                           net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))                                           
layers = [nn.Linear(2, 2), nn.Linear(2, 2)]                                                  
net = nn.Sequential(*layers)
print(net)

This ran without any error, and the result was:

Sequential(
  (0): Linear(in_features=2, out_features=2, bias=True)
  (1): Linear(in_features=2, out_features=2, bias=True)
)

Hope this helps. :)

like image 69
Anant Mittal Avatar answered Oct 31 '25 18:10

Anant Mittal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!