AttributeError: module ‘torch.nn‘ has no attribute ‘module‘

bash 复制代码
import torch 
import torch.nn as nn

class LinearModel(nn.Module):
    def _init_(self,ndim):
        super(LinearModel,self)._init_()
        self.ndim=ndim
        
        self.weight=nn.Parameter(torch.randn(ndim,1))#定义权重
        self.bias=nn.Parameter(torch.randn(1)) #定义偏置
         
    def forward(self,x):
    # y = wx +b
        return x.mm(self.weight)+self.bias
bash 复制代码
lm=LinearModel(5)

如上,调用时报错:
init () takes 1 positional argument but 2 were given

纠错发现是少打了下划线

init前后有两个_,

相关推荐
爱加糖的橙子7 分钟前
Dify升级到Dify v1.10.1-fix修复CVE-2025-55182漏洞
人工智能·python·ai
Pyeako34 分钟前
python网络爬虫
开发语言·爬虫·python·requsets库
diegoXie34 分钟前
【Python】 中的 * 与 **:Packing 与 Unpacking
开发语言·windows·python
_codemonster1 小时前
深度学习实战(基于pytroch)系列(四十八)AdaGrad优化算法
人工智能·深度学习·算法
AI即插即用1 小时前
即插即用系列 | Attention GhostUNet++:基于多维注意力和 Ghost 模块的高效 CT 图像脂肪与肝脏分割网络
网络·图像处理·人工智能·深度学习·神经网络·计算机视觉·视觉检测
程序员杰哥2 小时前
如何用Postman做接口自动化测试?
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
xxxxxmy2 小时前
同向双指针(滑动窗口)
python·算法·滑动窗口·同向双指针
测试19982 小时前
selenium自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
勇气要爆发2 小时前
【第一阶段—基础准备】第五章:Python模块和包管理(基础篇)—变形金刚的装备库
开发语言·python