【nn.Sequential 因为报错,原因是括号的位置导致问题】

本文解决了一个因括号位置不当导致的TypeError问题,在PyTorch模型定义中,nn.Sequential的使用需要正确放置括号以确保所有层被正确包含。通过调整括号位置,使模型能够成功构建并运行。

TypeError: torch.FloatTensor is not a Module subclass

这个错误搞了好久,竟然是一个括号的问题!记录一下

class Longtao(nn.Module):
    def __init__(self):
        super(Longtao, self).__init__()
        self.model = nn.Sequential
        (
            #  ↑ 括号导致报错
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),

            nn.Flatten(),
            nn.Linear(64 * 4 * 4, 64),
            nn.Linear(64, 10)
        )

##该部分代码存在问题

class Longtao(nn.Module):
    def __init__(self):
        super(Longtao, self).__init__()
        self.model = nn.Sequential(
            #  括号导致报错  注意将这个括号放到上面去!!!
            nn.Conv2d(3, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, 5, 1, 2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 5, 1, 2),
            nn.MaxPool2d(2),

            nn.Flatten(),
            nn.Linear(64 * 4 * 4, 64),
            nn.Linear(64, 10)
        )
        def forward(self, x):
        	x = self.model(x)
        	return x
if __name__ == '__main__':
    longge = Longtao()
    input = torch.ones((64, 3, 32, 32))
    output = longge(input)
    print(output.shape)

至此,修改完那个括号,,代码可以正常运行!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值