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

相关推荐
2301_764441337 小时前
Aella Science Dataset Explorer 部署教程笔记
笔记·python·全文检索
爱笑的眼睛117 小时前
GraphQL:从数据查询到应用架构的范式演进
java·人工智能·python·ai
江上鹤.1487 小时前
Day40 复习日
人工智能·深度学习·机器学习
BoBoZz197 小时前
ExtractSelection 选择和提取数据集中的特定点,以及如何反转该选择
python·vtk·图形渲染·图形处理
liwulin05067 小时前
【PYTHON-YOLOV8N】如何自定义数据集
开发语言·python·yolo
行如流水7 小时前
BLIP和BLIP2解析
深度学习
木头左8 小时前
LSTM量化交易策略中时间序列预测的关键输入参数分析与Python实现
人工智能·python·lstm
电子硬件笔记8 小时前
Python语言编程导论第七章 数据结构
开发语言·数据结构·python
fegggye8 小时前
PyO3 Class 详解 - 在 Python 中使用 Rust 类
pytorch·rust
cskywit8 小时前
MobileMamba中的小波分析
人工智能·深度学习