Hugging Face Transformers快速入门指南
Hugging Face Transformers库提供了预训练模型和简单API,支持自然语言处理(NLP)任务的快速实现。以下是核心使用方法:
安装环境
bash
pip install transformers
pip install torch # 推荐安装PyTorch作为后端
加载预训练模型
python
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_name = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
文本预处理
python
text = "Hugging Face makes NLP easy."
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
模型推理
python
outputs = model(**inputs)
predictions = outputs.logits.argmax(-1)
保存与加载模型
python
model.save_pretrained("./saved_model")
tokenizer.save_pretrained("./saved_model")
# 加载时
model = AutoModelForSequenceClassification.from_pretrained("./saved_model")
常见任务示例
- 文本分类:使用
bert-base-uncased等模型 - 问答系统:尝试
distilbert-base-cased-distilled-squad - 文本生成:选择
gpt2或facebook/opt-350m
使用Pipeline快速推理
python
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
result = classifier("I love using Hugging Face!")
注意事项
- 首次运行会自动下载模型至
~/.cache/huggingface - 大模型需注意GPU显存限制
- 可指定
device_map="auto"自动分配计算设备
最新模型列表可查阅Hugging Face模型库,支持按任务类型筛选。