
huggingface/transformers-pytorch-gpuHugging Face Transformers 镜像是基于 Transformers 库构建的 Docker 镜像,提供了数千个预训练模型,支持文本、视觉、音频等多模态任务的深度学习推理与微调。该镜像集成了 CPU 和 GPU 的 PyTorch 后端,旨在简化预训练模型的部署与使用流程,适用于开发者、研究人员和学生快速构建基于 Transformer 架构的 AI 应用。
pipeline API,3 行代码即可完成模型加载与推理AutoTokenizer 和 AutoModel 接口,简化不同模型的调用流程bashdocker pull huggingface/transformers:latest-pytorch
bashdocker run -it --rm \ -v $(pwd):/workspace \ huggingface/transformers:latest-pytorch \ python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I love Hugging Face!'))"
bashdocker run -it --rm \ --gpus all \ -v $(pwd):/workspace \ huggingface/transformers:latest-pytorch \ python -c "from transformers import pipeline; print(pipeline('image-classification', device=0)('[***]"
bashdocker run -it --rm \ --gpus all \ -v $(pwd):/workspace \ -p 8888:8888 \ huggingface/transformers:latest-pytorch \ jupyter notebook --ip=0.0.0.0 --allow-root
创建 docker-compose.yml 文件:
yamlversion: '3.8' services: transformers: image: huggingface/transformers:latest-pytorch runtime: nvidia # 仅 GPU 环境需要 volumes: - ./data:/workspace/data # 挂载数据目录 - ./models:/root/.cache/huggingface/hub # 持久化模型缓存 environment: - TRANSFORMERS_CACHE=/root/.cache/huggingface/hub - MODEL_NAME=google-bert/bert-base-uncased command: python /workspace/script.py
启动服务:
bashdocker-compose up
pythonfrom transformers import pipeline # 加载情感分析 pipeline classifier = pipeline("sentiment-analysis") result = classifier("Hugging Face Transformers 镜像简化了模型部署流程!") print(result) # 输出:[{'label': 'POSITIVE', 'score': 0.9997}]
pythonfrom transformers import AutoTokenizer, AutoModelForSequenceClassification # 从 Hugging Face Hub 加载模型 model_name = "distilbert/distilbert-base-uncased-finetuned-sst-2-english" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained(model_name) # 输入文本处理 inputs = tokenizer("Docker 镜像让 AI 模型部署更简单", return_tensors="pt") outputs = model(**inputs) # 解析结果 predictions = outputs.logits.argmax(dim=-1) print("情感标签:", "POSITIVE" if predictions[0] == 1 else "NEGATIVE")
| 变量名 | 说明 | 默认值 |
|---|---|---|
TRANSFORMERS_CACHE | 模型缓存目录 | /root/.cache/huggingface/hub |
HF_HOME | Hugging Face 配置文件目录 | /root/.cache/huggingface |
PYTORCH_CUDA_ALLOC_CONF | PyTorch GPU 内存分配配置 | max_split_size_mb:128 |
| 参数 | 说明 | 示例 |
|---|---|---|
--gpus all | Docker 启用所有 GPU | docker run --gpus all ... |
-v | 挂载本地目录到容器 | -v $(pwd)/models:/root/.cache/huggingface/hub |
-e | 设置环境变量 | -e MODEL_NAME=mistralai/Mistral-7B-Instruct-v0.2 |
该镜像支持 Hugging Face Transformers 库中的所有模型架构,包括但不限于:
完整模型列表可参考 Hugging Face 模型文档。
如需在研究中使用该镜像,请引用 Transformers 库论文:
bibtex@inproceedings{wolf-etal-2020-transformers, title = "Transformers: State-of-the-Art Natural Language Processing", author = "Thomas Wolf and Lysandre Debut and Victor Sanh and Julien Chaumond and Clement Delangue and Anthony Moi and Pierric Cistac and Tim Rault and Rémi Louf and Morgan Funtowicz and Joe Davison and Sam Shleifer and Patrick von Platen and Clara Ma and Yacine Jernite and Julien Plu and Canwen Xu and Teven Le Scao and Sylvain Gugger and Mariama Drame and Quentin Lhoest and Alexander M. Rush", booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing: System Demonstrations", month = oct, year = "2020", address = "Online", publisher = "Association for Computational Linguistics", url = "[***]", pages = "38--45" }
官方文档:Hugging Face Transformers 文档
模型仓库:Hugging Face Hub
manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务