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前后有两个_,

相关推荐
struggle20253 分钟前
PennyLane 是一个用于量子计算、量子机器学习和量子化学的跨平台 Python 库。由研究人员构建,用于研究
python·量子计算
扑克中的黑桃A4 分钟前
Python-素数
python
扑克中的黑桃A5 分钟前
Python学习的自我理解和想法(4)
python
扑克中的黑桃A14 分钟前
Python-打印杨辉三角(进阶版)
python
盼小辉丶39 分钟前
TensorFlow深度学习实战(21)——Transformer架构详解与实现
深度学习·tensorflow·transformer
laity171 小时前
激活IDM的几种方法
python
gsls2008081 小时前
使用xdocreport导出word
前端·python·word
Johny_Zhao2 小时前
基于CentOS Stream 8的物联网数据采集与展示方案
linux·网络·python·mqtt·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维
这里有鱼汤2 小时前
想成为下一个吉姆·西蒙斯,这十种经典K线形态你一定要记住
后端·python
Scoful2 小时前
快速用 uv 模拟发布一个 Python 依赖包到 TestPyPI 上,以及常用命令
开发语言·python·uv