pytorch 遇到 ImportError cannot import name ‘Iterator‘ from ‘torchtext.data‘

pytorch 遇到 ImportError: cannot import name ‘Iterator’ from ‘torchtext.data’

自己的深度学习容器中的运行环境

各个版本

torch version: 1.11.0+cu102
torchtext 0.12
python 3.6
torchvision 0.12.0 
torchaudio 0.11.0 

遇到问题

下面的代码在torchtext 0.12 下无法运行

from torchtext.data import Iterator, BucketIterator

产生报错

ImportError: cannot import name 'Iterator' from 'torchtext.data' (/usr/local/lib/python3.8/dist-packages/torchtext/data/__init__.py)

后来google一下,发现应该使用torchtext.legacy 包

from torchtext.legacy.data import Iterator, BucketIterator

遇到问题

Cannot find reference 'legacy' in '__init__.py | __init__.py'

这个在torchtext 0.9下存在的,但是在torchtext==0.12下这个legacy被移除了。

在官方新版本的release torchtext==0.12.0中找到了相关的说明,移除了legacy包:https://github.com/pytorch/text/releases

Backward Incompatible changes

We have removed the legacy folder in this release which provided access to legacy datasets and abstractions. For additional information, please refer to the corresponding github issue (#1422) and PR (#1437)

那应该怎么做才能使用原来的Iterator 和 BucketIterator呢?

解决方法

把torch降低到1.8,把torchtext版本降级为0.9

pip install torch==1.8 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install torchtext==0.9  -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

如果出现torchvision 0.12.0 和 torchaudio 0.11.0 版本不兼容的问题,需要降低它们的版本(匹配 torch 1.8)

pip install torchvision==0.9 torchaudio==0.8 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

需要可行的版本如下

torch 1.8
torchtext 0.9
python 3.6

// 下面两者不是torchtext必需的,这个是为了兼容torch
// 所以如果torch降级,vision和audio也是需要降低的
torchvision 0.9
torchaudio 0.8

这样再运行上面的Iterator 和 BucketIterator不会有问题。到这里,问题应该得以解决,下面的内容读者可以忽略。当然,如果想看笔者debug的过程,可以继续阅读。

笔者具体踩坑的过程

下面是笔者debug的具体过程

初始环境版本

torch version: 1.11.0+cu102
torchtext 0.12
python 3.6
torchvision 0.12.0 
torchaudio 0.11.0 

为了正常使用,把torchtext版本降级为0.8.0(这里笔者手误,想要torchtext==0.9.0,结果输入成torchtext == 0.8.0)

pip install torchtext==0.8.0  -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

然后restart pycharm,报错问题解决了,但是又出现了新的问题:不兼容的问题! 遇到不兼容的问题请参与笔者的另一篇博文:pytorch 中 torchtext 遇到问题_torchtext.so undefined symbol解决方法(亲测有效)

OSError: /usr/local/lib/python3.8/dist-packages/torchtext/_torchtext.so: undefined symbol: _ZNK3c104Type14isSubtypeOfExtESt10shared_ptrIS0_EPSo

自己的torch版本

torch version: 1.11.0+cu102

查到的解决方法是降低torch的版本到1.8

 pip install torch==1.8 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

然后遇到torch和vision,audio不兼容的问题

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
torchvision 0.12.0 requires torch==1.11.0, but you have torch 1.8.0 which is incompatible.
torchaudio 0.11.0 requires torch==1.11.0, but you have torch 1.8.0 which is incompatible.
Successfully installed torch-1.8.0

为了配套torch1.8,安装torchvision==0.9 和torchaudio ==0.8

pip install torchvision==0.9 torchaudio==0.8 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

然后显示成功安装

Successfully installed torchaudio-0.8.0 torchvision-0.9.0

查看torch版本

torch version: 1.8.0

运行代码,还是出现问题,在官方文档中找到问题所在:是torch和torchtext版本不兼容的问题:https://github.com/pytorch/text/

兼容版本如下:
在这里插入图片描述

来源:官网https://github.com/pytorch/text/

我需要修改为

torch 1.8
torchtext 0.9
python 3.6

修改torchtext版本

pip install torchtext==0.9  -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

然后问题得以解决。

Reference

torchtext版本:https://github.com/pytorch/text/releases

其他

升级torchtext的命令,目前可升级的最新版本是0.12.0

pip install -U torchtext  -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

大陆用户使用pip install安装时,最好使用国内的源加速。

-i http://pypi.douban.com/simple --trusted-host pypi.douban.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值