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

相关推荐
HyperAI超神经9 小时前
在线教程丨 David Baker 团队开源 RFdiffusion3,实现全原子蛋白质设计的生成式突破
人工智能·深度学习·学习·机器学习·ai·cpu·gpu
一瞬祈望12 小时前
⭐ 深度学习入门体系(第 7 篇): 什么是损失函数?
人工智能·深度学习·cnn·损失函数
阿正的梦工坊12 小时前
Kronecker积详解
人工智能·深度学习·机器学习
秃了也弱了。12 小时前
python实现定时任务:schedule库、APScheduler库
开发语言·python
Dfreedom.12 小时前
从 model(x) 到__call__:解密深度学习框架的设计基石
人工智能·pytorch·python·深度学习·call
weixin_4250230012 小时前
Spring Boot 配置文件优先级详解
spring boot·后端·python
汤姆yu13 小时前
基于深度学习的水稻病虫害检测系统
人工智能·深度学习
小徐Chao努力13 小时前
【Langchain4j-Java AI开发】06-工具与函数调用
java·人工智能·python
无心水13 小时前
【神经风格迁移:全链路压测】33、全链路监控与性能优化最佳实践:Java+Python+AI系统稳定性保障的终极武器
java·python·性能优化
_codemonster14 小时前
BERT中的padding操作
人工智能·深度学习·bert