欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux安装Transformers、Pytorch

程序员文章站 2022-06-11 22:00:36
...

在安装Transformers之前需要安装Tensorflow 2.0或Pytorch

pip install --index-url https://pypi.douban.com/simple transformers

踩坑记录:环境中已有pytorch,可成功执行import torch;然而安装transformers后,执行import transformers报错AttributeError: module ‘torch’ has no attribute ‘distributed’ ,执行import torch报错NameError: name ‘_C’ is not defined

解决方法:卸载后重新安装

pip uninstall transformers
pip uninstall torch
pip uninstall torchvision

注意:卸载pytorch时注意提示信息,需要手动删除一些文件

安装Pytorch

官网:https://pytorch.org/
安装最新版本:根据设备环境选择变量得到安装命令

安装以前版本(以1.6.0为例):
点击“Previous versions of PyTorch”
得到安装命令为

pip install torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html

然后由于网络连接超时报错,故下载安装包进行离线安装

打开https://download.pytorch.org/whl/torch_stable.html,下载所需的安装包

cu101/torch-1.6.0%2Bcu101-cp36-cp36m-linux_x86_64.whl
cu101/torchvision-0.7.0%2Bcu101-cp36-cp36m-linux_x86_64.whl

将安装包放在/tmp目录下

cd /tmp
pip install torch-1.6.0+cu101-cp36-cp36m-linux_x86_64.whl
pip install torchvision-0.7.0+cu101-cp36-cp36m-linux_x86_64.whl