![](/img/trans.png)
[英]PyTorch: access weights of a specific module in nn.Sequential()
[英]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.