nn.ModuleList使用案例以及module’ object has no attributes ‘linear‘

上网找的一个代码是这么写的:nn.ModuleList([nn.linear for i in range(10)])
我以为nn.linear是正常的呢!
没想到直接报错module’ object has no attributes ‘linear’

这里还是要注意大小写的

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.linears = nn.ModuleList([nn.Linear(128,128) for i in range(10)])

    # ModuleList can act as an iterable, or be indexed using ints
    def forward(self, x):
        for i, l in enumerate(self.linears):
            x = self.linears[i](x) 
        return x

输出网络:

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
t = MyModel().to(device)
print(t)

代码输出:

MyModel(
  (linears): ModuleList(
    (0): Linear(in_features=128, out_features=128, bias=True)
    (1): Linear(in_features=128, out_features=128, bias=True)
    (2): Linear(in_features=128, out_features=128, bias=True)
    (3): Linear(in_features=128, out_features=128, bias=True)
    (4): Linear(in_features=128, out_features=128, bias=True)
    (5): Linear(in_features=128, out_features=128, bias=True)
    (6): Linear(in_features=128, out_features=128, bias=True)
    (7): Linear(in_features=128, out_features=128, bias=True)
    (8): Linear(in_features=128, out_features=128, bias=True)
    (9): Linear(in_features=128, out_features=128, bias=True)
  )
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在你的引用中,报错"module' object has no attributes 'linear'"是由于你错误地将nn.linear作为一个模块而不是类来使用。正确的写法应该是nn.Linear。 而在你的引用中,MyModel类中的self.linears使用nn.ModuleList来创建一个包含10个nn.Linear模块的列表。这样可以在forward()方法中使用for循环遍历并应用这些线性模块。 至于你的引用中的错误,"AttributeError: 'Tensor' object has no attribute 'gpu'"和"TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first."是由于你试图在GPU上进行操作,但是你的张量对象没有gpu属性或者无法将cuda类型的张量转换为numpy类型。你可以使用Tensor.cpu()将张量复制到主机内存中来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [nn.ModuleList使用案例以及moduleobject has no attributeslinear](https://blog.csdn.net/weixin_49599247/article/details/129418756)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [torch中常见的错误信息](https://blog.csdn.net/weixin_61745097/article/details/127825548)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值