Pytorch故障汇总(持续更新)


以下是个人使用pytorch时遇到的故障和解决办法,分享食用

1.问题:ModuleNotFoundError:no module named ‘tools.nnwrap’

说明:使用pip install torch或pip3 install torch命令安装torch库,下完了安装时提示的错误
解决:使用官方提供安装命令安装,地址如下:
https://pytorch.org/
选择合适配置复制命令到cmd上运行即可(windows)
在这里插入图片描述

2.问题:No module named ‘torch._C’

说明:import torch后使用torch方法时报的错误
解决:torch版本与Python版本兼容问题,使用Python3.5环境运行代码,亲测3.6不行。

3.问题:init() got an unexpected keyword argument ‘target_tensor’

说明:使用Data.TensorDataset方法时传入参数有误报的错误
解决:新版Data.TensorDataset中data_tensor 和target_tensor去掉,输入变成了可变参数。即直接输入参数,不需要指代,例如:

train_dataset = Data.TensorDataset(data_tensor=x, target_tensor=y)
#改为
train_dataset = Data.TensorDataset(x,y)

4.问题:DataLoader worker (pid(s) xxxxx, xxxxx) exited unexpectedly

说明:使用Data.DataLoader方法时缺少相关语句报的错误
解决:根据报错提示,添加缺少语句即可。
缺少语句为if name == ‘main’:

RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

举例:

import torch
import torch.utils.data as Data
torch.manual_seed(1)

BATCH_SIZE = 5

x = torch.linspace(1, 10, 10)
y = torch.linspace(10, 1, 10)

#先转换成torch能识别的Dataset
torch_dataset = Data.TensorDataset(x, y)

#把dataset放入DataLoader
loader = Data.DataLoader(
    dataset=torch_dataset,
    batch_size=BATCH_SIZE,
    shuffle=True,
    num_workers=2
)

if __name__ == '__main__':
 for epoch in range(3):                                                             #训练所有数据,一共3次
    for step,(batch_x, batch_y) in enumerate(loader):                               #每一步loader释放一小批数据用来学习
        #训练的地方

        #打印数据
        print('Epoch:', epoch, '|Step:',step,'|batch x:', batch_x.numpy(), '|batch y', batch_y.numpy())

5.问题:ValueError: too many values to unpack (expected 2)

说明:获取方法的输出值报的错误,我这里是CNN网络
解决:保证方法输出与声明输出的变量一致即可,比如方法返回2个输出,声明两个变量来赋值,例如:
test_output, last_layer = cnn(test_x)

6.问题:Can’t get attribute _rebuild_parameter on module torch._utils

说明:Pytorch版本过低加载模型报错,Can’t get attribute ‘_rebuild_parameter’ on <module ‘torch._utils’ from ‘/opt/conda/lib/python3.6/site-packages/torch/_utils.py’>
解决:升级Pytorch版本,使用合适cpu或gpu版本的torch

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值