我的博客

配置 pytorch + transformers -下载预训练模型 Ununtu 18.04

目录
  1. 可选的准备工作
    1. 使用清华 pip 源
    2. 配置 python 虚环境
  2. 安装
    1. pytorch 其他版本或者其他 cuda 版本
  3. 下载预训练模型
  4. 安装 apex
  5. 错误解决
    1. ~/.local/bin/ 不在 PATH 中
    2. 没有安装 python3-venv

环境:

ubuntu 18.04

Nvidia 2080 ti

CUDA 10.2

pytorch 1.3

可选的准备工作

使用清华 pip 源

pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

配置 python 虚环境

1
2
3
python3 -m pip install --user --upgrade pip # 升级 pip
python3 -m pip install --user virtualenv #
sudo apt-get install python3-venv

创建虚环境

1
python3 -m venv huggingface-transform

激活虚环境

1
2
cd huggingface-transform/
source bin/activate

安装

先装 pytorch,参考官网: https://pytorch.org/get-started/locally/

python 3.6 + GPU

1
pip3 install torch torchvision

然后

1
pip install transformers

就好了。

pytorch 其他版本或者其他 cuda 版本

可以看到官网目前只给出了 1.4.0 的 cuda 10.1 和 cuda9.2 的安装命令,但是实际上还有很多版本和 cuda 的支持。

可以来这个页面 ctrl + f 搜索一下

可以看到还有 cuda10.0 的版本

cu100/torch-1.4.0%2Bcu100-cp27-cp27m-linux_x86_64.whl
cu100/torch-1.4.0%2Bcu100-cp27-cp27mu-linux_x86_64.whl
cu100/torch-1.4.0%2Bcu100-cp35-cp35m-linux_x86_64.whl
cu100/torch-1.4.0%2Bcu100-cp36-cp36m-linux_x86_64.whl
cu100/torch-1.4.0%2Bcu100-cp37-cp37m-linux_x86_64.whl
cu100/torch-1.4.0%2Bcu100-cp38-cp38-linux_x86_64.whl

也有 cu10.1 的。

例如想装 cuda 10.0 的可以用命令:

1
pip install torch==1.4.0+cu100 torchvision==0.5.0+cu100 -f https://download.pytorch.org/whl/torch_stable.html

下载预训练模型

github 仓库(https://github.com/huggingface/transformers)里似乎没有下载地址了。

但是代码里有(链接):

1
2
3
4
5
6
7
8
9
10
BERT_PRETRAINED_MODEL_ARCHIVE_MAP = {
"bert-base-uncased": "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased-pytorch_model.bin",
"bert-large-uncased": "https://s3.amazonaws.com/models.huggingface.co/bert/bert-large-uncased-pytorch_model.bin",
"bert-base-cased": "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-cased-pytorch_model.bin",
"bert-large-cased": "https://s3.amazonaws.com/models.huggingface.co/bert/bert-large-cased-pytorch_model.bin",
"bert-base-multilingual-uncased": "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-multilingual-uncased-pytorch_model.bin",
"bert-base-multilingual-cased": "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-multilingual-cased-pytorch_model.bin",
"bert-base-chinese": "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-chinese-pytorch_model.bin",
......
}

bert-base-uncased-pytorch_model.bin 420 MB md5:ea97b42698d3b5f6d8e8011eba3d1611

bert-base-cased-pytorch_model.bin 416 MB md5:968fbdcf7682a3608a32ed407794da31

bert-base-chinese-pytorch_model.bin 393 MB md5:ce2fe62236165d623c30d7068509e7bb

我用校园网下载速度还挺快的最快有 10 MB / s

安装 apex

第一个是官方的,第二个是修改版的,按照页面说明安装即可

https://github.com/NVIDIA/apex

https://github.com/ptrblck/apex/tree/apex_no_distributed

错误解决

~/.local/bin/ 不在 PATH 中

1
2
WARNING: The script virtualenv is installed in '/home/xxx/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

编辑 .profile 把这个提示的路径添加进去

没有安装 python3-venv

he virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

apt-get install python3-venv

You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.


这里已经告诉解决方法了 apt-get install python3-venv

评论无需登录,可以匿名,欢迎评论!