English | 简体中文 | 繁體中文 | 한국어 | Español | 日本語 | हिन्दी | Русский | Português | తెలుగు | Français | Deutsch | Italiano | Tiếng Việt | العربية | اردو | বাংলা |
Transformers 是一個為最先進的機器學習模型(涵蓋文字、電腦視覺、音訊、影片及多模態)提供推理和訓練支援的模型定義框架。
它將模型定義集中化,使得該定義在整個生態系中能夠達成共識。transformers 是貫穿各個框架的樞紐:如果一個模型定義受到支援,它將與大多數訓練框架(如 Axolotl、Unsloth、DeepSpeed、FSDP、PyTorch-Lightning 等)、推理引擎(如 vLLM、SGLang、TGI 等)以及利用 transformers 模型定義的周邊建模函式庫(如 llama.cpp、mlx 等)相容。
我們致力於支援最新的頂尖模型,並透過使其模型定義變得簡單、可客製化且高效,來普及它們的應用。
在 Hugging Face Hub 上,有超過 100 萬個 Transformers 模型檢查點 供您使用。
立即探索 Hub,尋找合適的模型,並使用 Transformers 幫助您快速上手。
Transformers 支援 Python 3.10+ 和 PyTorch 2.4+。
使用 venv 或基於 Rust 的高速 Python 套件及專案管理器 uv 來建立並啟用虛擬環境。
# venv
python -m venv .my-env
source .my-env/bin/activate
# uv
uv venv .my-env
source .my-env/bin/activate在您的虛擬環境中安裝 Transformers。
# pip
pip install "transformers[torch]"
# uv
uv pip install "transformers[torch]"如果您想使用函式庫的最新變更或有興趣參與貢獻,可以從原始碼安裝 Transformers。然而,最新版本可能不穩定。如果您遇到任何錯誤,歡迎隨時提交一個 issue。
git clone https://github.com/huggingface/transformers.git
cd transformers
# pip
pip install '.[torch]'
# uv
uv pip install '.[torch]'透過 Pipeline API 快速開始使用 Transformers。Pipeline 是一個高階的推理類別,支援文字、音訊、視覺和多模態任務。它負責處理輸入資料的預處理,並回傳適當的輸出。
實例化一個 pipeline 並指定用於文字生成的模型。該模型會被下載並快取,方便您之後輕鬆複用。最後,傳入一些文字來提示模型。
from transformers import pipeline
pipeline = pipeline(task="text-generation", model="Qwen/Qwen2.5-1.5B")
pipeline("the secret to baking a really good cake is ")
[{'generated_text': 'the secret to baking a really good cake is 1) to use the right ingredients and 2) to follow the recipe exactly. the recipe for the cake is as follows: 1 cup of sugar, 1 cup of flour, 1 cup of milk, 1 cup of butter, 1 cup of eggs, 1 cup of chocolate chips. if you want to make 2 cakes, how much sugar do you need? To make 2 cakes, you will need 2 cups of sugar.'}]與模型進行聊天,使用模式是相同的。唯一的區別是您需要建構一個您與系統之間的聊天歷史(作為 Pipeline 的輸入)。
Tip
你也可以直接在命令列中與模型聊天。
transformers chat Qwen/Qwen2.5-0.5B-Instructimport torch
from transformers import pipeline
chat = [
{"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."},
{"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"}
]
pipeline = pipeline(task="text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct", dtype=torch.bfloat16, device_map="auto")
response = pipeline(chat, max_new_tokens=512)
print(response[0]["generated_text"][-1]["content"])展開下面的範例,查看 Pipeline 如何在不同模態和任務上運作。
自動語音辨識
from transformers import pipeline
pipeline = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3")
pipeline("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac")
{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'}影像分類
from transformers import pipeline
pipeline = pipeline(task="image-classification", model="facebook/dinov2-small-imagenet1k-1-layer")
pipeline("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png")
[{'label': 'macaw', 'score': 0.997848391532898},
{'label': 'sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita',
'score': 0.0016551691805943847},
{'label': 'lorikeet', 'score': 0.00018523589824326336},
{'label': 'African grey, African gray, Psittacus erithacus',
'score': 7.85409429227002e-05},
{'label': 'quail', 'score': 5.502637941390276e-05}]視覺問答
from transformers import pipeline
pipeline = pipeline(task="visual-question-answering", model="Salesforce/blip-vqa-base")
pipeline(
image="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-few-shot.jpg",
question="What is in the image?",
)
[{'answer': 'statue of liberty'}]-
易於使用的最先進模型:
- 在自然語言理解與生成、電腦視覺、音訊、影片和多模態任務上表現卓越。
- 為研究人員、工程師與開發者提供了低門檻的入門途徑。
- 面向使用者的抽象層級少,只需學習三個核心類別。
- 為所有預訓練模型提供了統一的 API 介面。
-
更低的運算成本,更小的碳足跡:
- 分享訓練好的模型,而不是從零開始訓練。
- 減少運算時間和生產成本。
- 擁有數十種模型架構和超過100萬個橫跨所有模態的預訓練檢查點。
-
為模型的每個生命週期階段選擇合適的框架:
- 僅用3行程式碼即可訓練最先進的模型。
- 在PyTorch/JAX/TF2.0框架之間輕鬆切換單一模型。
- 為訓練、評估和生產選擇最合適的框架。
-
輕鬆根據您的需求客製化模型或範例:
- 我們為每個架構提供了範例,以重現其原作者發表的結果。
- 模型內部結構盡可能保持一致地暴露給使用者。
- 模型檔案可以獨立於函式庫使用,便於快速實驗。
- 本函式庫並非一個用於建構神經網路的模組化工具箱。模型檔案中的程式碼為了讓研究人員能快速在模型上迭代,而沒有進行過度的抽象重構,避免了深入額外的抽象層/檔案。
- 訓練 API 針對 Transformers 提供的 PyTorch 模型進行了最佳化。對於通用的機器學習迴圈,您應該使用像 Accelerate 這樣的其他函式庫。
- 範例指令稿僅僅是範例。它們不一定能在您的特定用例上開箱即用,您可能需要修改程式碼才能使其正常運作。
Transformers 不僅僅是一個使用預訓練模型的工具包,它還是一個圍繞它和 Hugging Face Hub 建構的專案社群。我們希望 Transformers 能夠賦能開發者、研究人員、學生、教授、工程師以及其他任何人,去建構他們夢想中的專案。
為了慶祝 Transformers 獲得 10 萬顆星標,我們希望透過 awesome-transformers 頁面來聚焦社群,該頁面列出了100個基於 Transformers 建構的精彩專案。
如果您擁有或使用一個您認為應該被列入其中的專案,請隨時提交 PR 將其加入!
您可以在我們大多數模型的 Hub 模型頁面 上直接進行測試。
展開下面的每個模態,查看一些用於不同用例的範例模型。
音訊
電腦視覺
- Automatic mask generation with SAM
- Depth estimation with DepthPro
- Image classification with DINO v2
- Keypoint detection with SuperPoint
- Keypoint matching with SuperGlue
- Object detection with RT-DETRv2
- Pose Estimation with VitPose
- Universal segmentation with OneFormer
- Video classification with VideoMAE
多模態
- Audio or text to text with Qwen2-Audio
- Document question answering with LayoutLMv3
- Image or text to text with Qwen-VL
- Image captioning BLIP-2
- OCR-based document understanding with GOT-OCR2
- Table question answering with TAPAS
- Unified multimodal understanding and generation with Emu3
- Vision to text with Llava-OneVision
- Visual question answering with Llava
- Visual referring expression segmentation with Kosmos-2
自然語言處理 (NLP)
現在我們有一篇可供您引用的關於 🤗 Transformers 函式庫的 論文:
@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 = "https://www.aclweb.org/anthology/2020.emnlp-demos.6",
pages = "38--45"
}

