使用torch.load时,出现错误:Can‘t get attribute ‘Net‘ on module ‘__main__‘

(1)原文:

torch.load 出现 AttributeError: Can‘t get attribute ‘Net‘ on module ‘__main__‘_torch.load attributeerror: can't get attribute-CSDN博客

(2)问题展示(这是错误代码!):

import torch
model = torch.load('model_method1.pth')

#报错信息:AttributeError: Can't get attribute 'Net' on <module '__main__' from 'D:\\在D盘的pytorch项目\\PycharmProjects\\pytorch\\Minist\\test.py'>

(3)解决方法:

1.找到使用torch.save的那个python文件(我的是“MNIST1_train.py”)

2.把这个文件里面的网络类(我的是Net类)导入到需要torch.load的文件(我的是test.py)

3.在test.py中加入代码

有问题请联系:chufeng0105@qq.com

import torch
from MNIST1_train import Net

model = torch.load('model_method1.pth')

(4)成功展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值