当我使用 pytorch 训练模型时,我尝试打印整个网络结构
所以我将所有层打包在一个列表中
然后我使用 nn.Sequential(*list)
但它不起作用,并且 TypeError: list 不是 Module 子类
最佳答案
请提供您创建的图层列表,您确定您没有在其中犯过任何错误吗?尝试检查您的列表是否实际上是 [] 而不是 [[..]]。我注意到的另一件事是您将 list
作为变量名,这不是一个好主意 - list
是 Python 关键字。
我尝试编写一个解包列表的示例代码,它对我来说很好。
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)
这个运行没有任何错误,结果是:
Sequential(
(0): Linear(in_features=2, out_features=2, bias=True)
(1): Linear(in_features=2, out_features=2, bias=True)
)
希望这会有所帮助。 :)
关于python - pytorch nn.Sequential(*list) TypeError : list is not a Module subclass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58656046/