Hub 文档
使用 🤗 transformers 在 Hugging Face
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
使用 🤗 transformers 在 Hugging Face
🤗 transformers
是一个由 Hugging Face 和社区维护的库,用于 PyTorch、TensorFlow 和 JAX 的最先进的机器学习。它提供了数千个预训练模型,用于执行文本、视觉和音频等不同模态的任务。我们可能有点偏颇,但我们真的非常喜欢 🤗 transformers
!
在 Hub 中探索 🤗 transformers
Hub 中有超过 25,000 个 transformers
模型,您可以通过在模型页面左侧进行筛选来找到它们。
您可以找到用于许多不同任务的模型
- 从上下文中提取答案(question-answering)。
- 从大量文本创建摘要(summarization)。
- 对文本进行分类(例如,作为垃圾邮件或非垃圾邮件,text-classification)。
- 使用 GPT 等模型生成新文本(text-generation)。
- 识别句子中的词性(动词、主语等)或实体(国家、组织等)(token-classification)。
- 将音频文件转录为文本(automatic-speech-recognition)。
- 对音频文件中的说话者或语言进行分类(audio-classification)。
- 检测图像中的物体(object-detection)。
- 分割图像(image-segmentation)。
- 进行强化学习(reinforcement-learning)!
如果您想在不下载模型的情况下进行测试,可以直接在浏览器中试用这些模型,这要归功于浏览器内小部件!


使用现有模型
所有 transformer
模型只需一行代码即可使用!根据您想要使用它们的方式,您可以使用使用 pipeline
函数的高级 API,也可以使用 AutoModel
来获得更多控制。
# With pipeline, just specify the task and the model id from the Hub.
from transformers import pipeline
pipe = pipeline("text-generation", model="distilbert/distilgpt2")
# If you want more control, you will need to define the tokenizer and model.
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
model = AutoModelForCausalLM.from_pretrained("distilbert/distilgpt2")
您还可以从特定版本(基于提交哈希值、标签名称或分支)加载模型,如下所示
model = AutoModel.from_pretrained(
"julien-c/EsperBERTo-small", revision="v2.0.1" # tag name, or branch name, or commit hash
)
如果您想查看如何加载特定模型,您可以单击“Use in Transformers”,您将获得一个可用于加载它的工作代码片段!如果您需要有关模型架构的更多信息,您还可以单击代码片段底部的“Read model documentation”。


分享您的模型
要阅读有关使用 transformers
分享模型的所有信息,请前往官方文档中的分享模型指南。
transformers
中的许多类,例如模型和分词器,都有一个 push_to_hub
方法,可以轻松地将文件上传到仓库。
# Pushing model to your own account
model.push_to_hub("my-awesome-model")
# Pushing your tokenizer
tokenizer.push_to_hub("my-awesome-model")
# Pushing all things after training
trainer.push_to_hub()
您可以做的事情还有很多,因此我们建议您查看分享模型指南。