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

相关推荐
Coding茶水间12 小时前
基于深度学习的面部口罩检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
梁正雄12 小时前
5、python 模块与包
linux·服务器·python
I_ltt_Itw,12 小时前
Python协程学习笔记
开发语言·网络·python
WenGyyyL12 小时前
深度学习数学基础(一)——线性代数、线性代数和微积分
人工智能·深度学习·线性代数
爱笑的眼睛1113 小时前
Flask应用API深度开发:从单体架构到微服务设计模式
java·人工智能·python·ai
LHZSMASH!13 小时前
基于动态图卷积与时间自注意力的EEG情绪识别混合网络——深度技术解析
人工智能·深度学习
AI小云13 小时前
【数据操作与可视化】Matplotlib绘图-常用操作
python·数据可视化
木婉清fresh13 小时前
测开python高频面试精选100题
开发语言·python·面试
彼岸花开了吗13 小时前
构建AI智能体:四十、K-Means++与RAG的融合创新:智能聚类与检索增强生成的深度应用
人工智能·python
JHC00000013 小时前
47. 全排列 II
开发语言·python·面试