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

相关推荐
Full Stack Developme几秒前
Spring Security 与 Apache Shiro 两大安全框架比较
spring boot·python·安全
杰瑞哥哥5 分钟前
快速搭建Web前端(streamlit使用指南)
python·信息可视化·web·模型部署
Hcoco_me6 分钟前
大模型面试题39:KV Cache 完全指南
人工智能·深度学习·自然语言处理·transformer·word2vec
小途软件7 分钟前
基于计算机视觉的课堂行为编码研究
人工智能·python·深度学习·计算机视觉·语言模型·自然语言处理·django
盼小辉丶7 分钟前
PyTorch实战——pix2pix详解与实现
pytorch·深度学习·生成模型
智航GIS7 分钟前
9.2 多进程入门
数据库·python
小途软件8 分钟前
基于计算机视觉的桥梁索力测试方法
人工智能·python·语言模型·自然语言处理·django
yousuotu15 分钟前
基于Python实现水果新鲜度分类
开发语言·python·分类
Data_agent16 分钟前
微店商品列表API接口指南
大数据·数据库·python
吴老弟i18 分钟前
基于 VSCode 实现 Python 开发与调试 | 环境配置搭建 | PIP Anaconda
vscode·python·pip