由于作者使用PyTorch作为深度学习库,因此本文仅介绍以PyTorch为后端神经网络包情况下transformers包的安装内容。
此外,本部分还有 🤗 Datasets 包相关缓存路径和离线模式的问题,由于我不用datasets包,所以
pip install transformers
如果仅使用CPU,可以直接通过如下命令行同时安装PyTorch:
pip install transformers[torch]
pip install git+https://github.com/huggingface/transformers
这个命令行下载的是最新的master版本,而不是稳定版。master版保持最新更新。
如果您需要:1. 使用源码的master版本。2. 贡献给 🤗 Transformers,测试代码变化。
您就需要进行editable install,即clone GitHub项目并安装transformers包:
git clone https://github.com/huggingface/transformers.git
cd transformers
pip install -e .
这一命令会链接你下载的文件夹到Python包路径,Python会在常规包路径的基础上增加查看你所下载的这个路径。举例来说,如果你的Python包一般下到 ~/anaconda3/envs/main/lib/python3.7/site-packages/
,Python在此基础上还会搜索你下载的这个文件夹: ~/transformers/
。
注意如果你想保留这个包,这个下载的文件夹就不能删。
对 pip install
命令的 -e
参数的更多解释可参考我之前撰写的博文:pip详解(持续更新ing…)_诸神缄默不语的博客-CSDN博客
如果你想更新到最新版本,运行如下代码即可:
cd ~/transformers/
git pull
conda install -c huggingface transformers
python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('we love you'))"
python -c
属于命令行参数,直接运行其后的Python语句。也可以用 command
参数。通过 man python
命令可以查看其介绍:
Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments to the command).
[{'label': 'POSITIVE', 'score': 0.9998704791069031}]
预训练模型会被自动下载并缓存到: ~/.cache/huggingface/transformers/
(shell environment variable TRANSFORMERS_CACHE
)
(在Windows上则是: C:\Users\username\.cache\huggingface\transformers
)
你可以改变以下shell environment variables(按优先级排序)以指定不同的缓存文件夹:
(如果使用过transformers老版,并修改了 shell environment variables PYTORCH_TRANSFORMERS_CACHE
或 PYTORCH_PRETRAINED_BERT_CACHE
),就会使用这些shell environment variables,除非修改了 shell environment variable TRANSFORMERS_CACHE
我没有试验过修改缓存路径,参考,有3种方式修改缓存路径:
import os
os.environ['TRANSFORMERS_CACHE'] = '文件夹名'
在不方便使用网络的情况下,🤗 Transformers也可以只使用本地文件。设置环境变量 TRANSFORMERS_OFFLINE=1
以开启该模式。
举例,原本运行代码的脚本为:
python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ...
离线模式则为:
HF_DATASETS_OFFLINE=1 TRANSFORMERS_OFFLINE=1 \
python examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --dataset_name wmt16 --dataset_config ro-en ...
python -m pip install huggingface_hub
from huggingface_hub import hf_hub_download
hf_hub_download(repo_id="bigscience/T0_3B", filename="config.json", cache_dir="指定下载路径")
Original: https://blog.csdn.net/PolarisRisingWar/article/details/123180118
Author: 诸神缄默不语
Title: huggingface.transformers安装教程
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/731717/
转载文章受原作者版权保护。转载请注明原作者出处!