Transformers 库指南
transformers 是 Hugging Face 开发的旗舰级 Python 库,它提供了一个统一的 API 来下载、加载和微调最先进的预训练模型。
#核心设计理念
Transformers 库围绕三个核心对象构建:
- Tokenizer:将文本转换为模型可以理解的数字(Input IDs)。
- Model:神经网络架构及其权重。
- Configuration:存储模型架构参数。
#极简使用:Pipeline API
pipeline() 是库中最简单直接的工具,适合快速部署和测试。
pythonfrom transformers import pipeline # 1. 情感分析 (Sentiment Analysis) classifier = pipeline("sentiment-analysis") print(classifier("We are very happy to show you the 🤗 Transformers library.")) # 2. 文本生成 (Text Generation) generator = pipeline("text-generation", model="gpt2") print(generator("In this course, we will teach you how to", max_length=30)) # 3. 命名实体识别 (NER) ner = pipeline("ner", grouped_entities=True) print(ner("My name is Sylvain and I work at Hugging Face in Brooklyn."))
#标准使用:Auto Classes
当你需要更多控制权时,可以使用 AutoTokenizer 和 AutoModel 类。它们能根据模型名称自动加载正确的架构。
pythonfrom transformers import AutoTokenizer, AutoModelForSequenceClassification import torch checkpoint = "distilbert-base-uncased-finetuned-sst-2-english" # 加载分词器 tokenizer = AutoTokenizer.from_pretrained(checkpoint) # 加载带分类头的模型 model = AutoModelForSequenceClassification.from_pretrained(checkpoint) sequences = ["I've been waiting for a HuggingFace course my whole life.", "So hate this!"] # 编码输入 inputs = tokenizer(sequences, padding=True, truncation=True, return_tensors="pt") # 前向传播 with torch.no_grad(): outputs = model(**inputs) # 获取逻辑值 (Logits) predictions = torch.nn.functional.softmax(outputs.logits, dim=-1) print(predictions)
#常用模型后缀
根据任务选择合适的 AutoModelFor... 类:
AutoModelForCausalLM:因果语言模型(如 GPT-2, Llama)。AutoModelForMaskedLM:掩码语言模型(如 BERT, RoBERTa)。AutoModelForSequenceClassification:序列分类。AutoModelForTokenClassification:词元分类(如 NER)。AutoModelForQuestionAnswering:问答系统。
#模型保存与加载
python# 保存本地 tokenizer.save_pretrained("./my_saved_model") model.save_pretrained("./my_saved_model") # 从本地加载 tokenizer = AutoTokenizer.from_pretrained("./my_saved_model") model = AutoModelForSequenceClassification.from_pretrained("./my_saved_model")
#进阶功能:Trainer API
Trainer 是一个高性能、全功能的训练循环实现,它抽象了复杂的 PyTorch 代码。
pythonfrom transformers import Trainer, TrainingArguments training_args = TrainingArguments( output_dir="./results", num_train_epochs=3, per_device_train_batch_size=16, evaluation_strategy="epoch", ) trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset, tokenizer=tokenizer, ) trainer.train()
#性能优化
- Quantization (量化):使用
bitsandbytes库加载 4-bit 或 8-bit 模型以节省显存。 - Flash Attention:通过
attn_implementation="flash_attention_2"加速长序列计算。 - Device Map:使用
device_map="auto"实现多显卡模型切分。
提示:Transformers 库支持 PyTorch、TensorFlow 和 JAX 三大深度学习框架。