繁体   English   中英

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

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

当我使用 pytorch 训练 model 时,我尝试打印整个网络结构

所以我将所有层打包在一个列表中,然后我使用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)
)

希望这可以帮助。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM