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

相关推荐
小途软件1 分钟前
基于深度学习的垃圾识别分类研究与实现
人工智能·pytorch·python·深度学习·语言模型
Salt_07283 分钟前
DAY 58 经典时序预测模型 1
人工智能·python·深度学习·神经网络·机器学习
X_Cosmic10 分钟前
从零开始:YOLO11 训练 DOTA OBB 遥感数据旋转框目标检测
python·yolo·目标检测
Hello.Reader13 分钟前
PyFlink 向量化 UDF(Vectorized UDF)Arrow 批传输原理、pandas 标量/聚合函数、配置与内存陷阱、五种写法一网打尽
python·flink·pandas
曲幽25 分钟前
FastAPI + SQLite:从基础CRUD到安全并发的实战指南
python·sqlite·fastapi·web·jwt·form·sqlalchemy·oauth2
用户43035102506826 分钟前
Python 中除 Ecception 外的三类系统异常
python
搂着猫睡的小鱼鱼28 分钟前
基于Python的淘宝评论爬虫
开发语言·爬虫·python
小途软件29 分钟前
基于深度学习的人脸属性增强器
java·人工智能·pytorch·python·深度学习·语言模型
Hcoco_me39 分钟前
大模型面试题46:在训练7B LLM时,如果使用AdamW优化器,那么它需要的峰值显存是多少?
开发语言·人工智能·深度学习·transformer·word2vec
ai_top_trends40 分钟前
AI 生成工作计划 PPT 是否适合年初规划与年度汇报
人工智能·python·powerpoint