transformers
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTransformers
Transformers
Overview
概述
The Hugging Face Transformers library provides access to thousands of pre-trained models for tasks across NLP, computer vision, audio, and multimodal domains. Use this skill to load models, perform inference, and fine-tune on custom data.
Hugging Face Transformers库提供了对数千个预训练模型的访问,这些模型适用于自然语言处理(NLP)、计算机视觉、音频和多模态领域的任务。使用此技能可以加载模型、执行推理并在自定义数据上进行微调。
Installation
安装
Install transformers and core dependencies:
bash
uv pip install torch transformers datasets evaluate accelerateFor vision tasks, add:
bash
uv pip install timm pillowFor audio tasks, add:
bash
uv pip install librosa soundfile安装transformers及核心依赖:
bash
uv pip install torch transformers datasets evaluate accelerate对于视觉任务,需额外安装:
bash
uv pip install timm pillow对于音频任务,需额外安装:
bash
uv pip install librosa soundfileAuthentication
身份验证
Many models on the Hugging Face Hub require authentication. Set up access:
python
from huggingface_hub import login
login() # Follow prompts to enter tokenOr set environment variable:
bash
export HUGGINGFACE_TOKEN="your_token_here"Get tokens at: https://huggingface.co/settings/tokens
Hugging Face Hub上的许多模型需要身份验证。请按以下步骤设置访问权限:
python
from huggingface_hub import login
login() # Follow prompts to enter token或者设置环境变量:
bash
export HUGGINGFACE_TOKEN="your_token_here"Quick Start
快速开始
Use the Pipeline API for fast inference without manual configuration:
python
from transformers import pipeline使用Pipeline API无需手动配置即可快速执行推理:
python
from transformers import pipelineText generation
Text generation
generator = pipeline("text-generation", model="gpt2")
result = generator("The future of AI is", max_length=50)
generator = pipeline("text-generation", model="gpt2")
result = generator("The future of AI is", max_length=50)
Text classification
Text classification
classifier = pipeline("text-classification")
result = classifier("This movie was excellent!")
classifier = pipeline("text-classification")
result = classifier("This movie was excellent!")
Question answering
Question answering
qa = pipeline("question-answering")
result = qa(question="What is AI?", context="AI is artificial intelligence...")
undefinedqa = pipeline("question-answering")
result = qa(question="What is AI?", context="AI is artificial intelligence...")
undefinedCore Capabilities
核心功能
1. Pipelines for Quick Inference
1. 快速推理管道
Use for simple, optimized inference across many tasks. Supports text generation, classification, NER, question answering, summarization, translation, image classification, object detection, audio classification, and more.
When to use: Quick prototyping, simple inference tasks, no custom preprocessing needed.
See for comprehensive task coverage and optimization.
references/pipelines.md适用于多种任务的简单、优化推理。支持文本生成、分类、命名实体识别(NER)、问答、摘要、翻译、图像分类、目标检测、音频分类等。
适用场景:快速原型开发、简单推理任务、无需自定义预处理的场景。
有关全面的任务覆盖和优化,请参阅。
references/pipelines.md2. Model Loading and Management
2. 模型加载与管理
Load pre-trained models with fine-grained control over configuration, device placement, and precision.
When to use: Custom model initialization, advanced device management, model inspection.
See for loading patterns and best practices.
references/models.md通过对配置、设备放置和精度的细粒度控制来加载预训练模型。
适用场景:自定义模型初始化、高级设备管理、模型检查。
有关加载模式和最佳实践,请参阅。
references/models.md3. Text Generation
3. 文本生成
Generate text with LLMs using various decoding strategies (greedy, beam search, sampling) and control parameters (temperature, top-k, top-p).
When to use: Creative text generation, code generation, conversational AI, text completion.
See for generation strategies and parameters.
references/generation.md使用各种解码策略(贪心、束搜索、采样)和控制参数(temperature、top-k、top-p),通过大语言模型(LLM)生成文本。
适用场景:创意文本生成、代码生成、对话AI、文本补全。
有关生成策略和参数,请参阅。
references/generation.md4. Training and Fine-Tuning
4. 训练与微调
Fine-tune pre-trained models on custom datasets using the Trainer API with automatic mixed precision, distributed training, and logging.
When to use: Task-specific model adaptation, domain adaptation, improving model performance.
See for training workflows and best practices.
references/training.md使用Trainer API在自定义数据集上微调预训练模型,支持自动混合精度、分布式训练和日志记录。
适用场景:任务特定模型适配、领域适配、提升模型性能。
有关训练工作流和最佳实践,请参阅。
references/training.md5. Tokenization
5. 分词
Convert text to tokens and token IDs for model input, with padding, truncation, and special token handling.
When to use: Custom preprocessing pipelines, understanding model inputs, batch processing.
See for tokenization details.
references/tokenizers.md将文本转换为模型输入所需的令牌和令牌ID,支持填充、截断和特殊令牌处理。
适用场景:自定义预处理管道、理解模型输入、批量处理。
有关分词细节,请参阅。
references/tokenizers.mdCommon Patterns
常见模式
Pattern 1: Simple Inference
模式1:简单推理
For straightforward tasks, use pipelines:
python
pipe = pipeline("task-name", model="model-id")
output = pipe(input_data)对于简单任务,使用管道:
python
pipe = pipeline("task-name", model="model-id")
output = pipe(input_data)Pattern 2: Custom Model Usage
模式2:自定义模型使用
For advanced control, load model and tokenizer separately:
python
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("model-id")
model = AutoModelForCausalLM.from_pretrained("model-id", device_map="auto")
inputs = tokenizer("text", return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=100)
result = tokenizer.decode(outputs[0])如需高级控制,分别加载模型和分词器:
python
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("model-id")
model = AutoModelForCausalLM.from_pretrained("model-id", device_map="auto")
inputs = tokenizer("text", return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=100)
result = tokenizer.decode(outputs[0])Pattern 3: Fine-Tuning
模式3:微调
For task adaptation, use Trainer:
python
from transformers import Trainer, TrainingArguments
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=8,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
)
trainer.train()如需任务适配,使用Trainer:
python
from transformers import Trainer, TrainingArguments
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=8,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
)
trainer.train()Reference Documentation
参考文档
For detailed information on specific components:
- Pipelines: - All supported tasks and optimization
references/pipelines.md - Models: - Loading, saving, and configuration
references/models.md - Generation: - Text generation strategies and parameters
references/generation.md - Training: - Fine-tuning with Trainer API
references/training.md - Tokenizers: - Tokenization and preprocessing
references/tokenizers.md
有关特定组件的详细信息:
- 管道:- 所有支持的任务和优化
references/pipelines.md - 模型:- 加载、保存和配置
references/models.md - 生成:- 文本生成策略和参数
references/generation.md - 训练:- 使用Trainer API进行微调
references/training.md - 分词器:- 分词和预处理
references/tokenizers.md