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

相关推荐
diegoXie30 分钟前
Python / R 向量顺序分割与跨步分割
开发语言·python·r语言
七牛云行业应用33 分钟前
解决OSError: No space left... 给DeepSeek Agent装上无限云硬盘
python·架构设计·七牛云·deepseek·agent开发
BoBoZz191 小时前
CutWithScalars根据标量利用vtkContourFilter得到等值线
python·vtk·图形渲染·图形处理
失散131 小时前
Python——1 概述
开发语言·python
萧鼎1 小时前
Python 图像哈希库 imagehash——从原理到实践
开发语言·python·哈希算法
qq_251533591 小时前
使用 Python 提取 MAC 地址
网络·python·macos
Data_agent3 小时前
学术爬虫实战:构建知网论文关键词共现网络的技术指南
python·算法
哥布林学者5 小时前
吴恩达深度学习课程四:计算机视觉 第一周:卷积基础知识(三)简单卷积网络
深度学习·ai
_一路向北_5 小时前
爬虫框架:Feapder使用心得
爬虫·python
皇族崛起5 小时前
【3D标注】- Unreal Engine 5.7 与 Python 交互基础
python·3d·ue5