Transformers 介绍

发布时间: 更新时间: 总字数:611 阅读时间:2m 作者: IP上海 分享 网址

transformers是为PyTorch, TensorFlow和JAX打造的先进的机器学习工具

介绍

  • https://huggingface.co/docs/transformers/v4.27.2/zh/index
  • Transformers 提供下载、训练先进的预训练模型的API和工具
  • 功能
    • 自然语言处理:文本分类、命名实体识别、问答、语言建模、摘要、翻译、多项选择和文本生成
    • 机器视觉:图像分类、目标检测和语义分割
    • 音频:自动语音识别和音频分类
    • 多模态:表格问答、光学字符识别等
  • 支持众多模型,如ALBERT、BAR等
  • pipeline() 推理,支持快速实现不同模态的多种任务,更多参考

文本分类(sentiment-analysis)示例

  • 安装 transformers datasets 依赖
pip install transformers datasets
  • 安装机器学习框架
pip install torch

当运行时,会默认从 huggingface 官网下载模型和数据,缓存目录:~/.cache/huggingface/hub/

#!/usr/bin/env python

import warnings
warnings.filterwarnings("ignore")

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
raw_inputs = [
    "I've been waiting for a HuggingFace course my whole life.",
    "I hate this so much!",
]

results = classifier(raw_inputs)
for result in results:
    print(f"label: {result['label']}, with score: {round(result['score'], 4)}")

from transformers import AutoTokenizer
checkpoint = 'distilbert-base-uncased-finetuned-sst-2-english'
tokenizer = AutoTokenizer.from_pretrained(checkpoint)

inputs = tokenizer(raw_inputs, padding=True, truncation=True, return_tensors='pt')
print(inputs)

第一次执行过程:

$ python test_sentiment_analysis.py
No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).
Using a pipeline without specifying a model name and revision in production is not recommended.
Downloading (…)lve/main/config.json: 100%|...| 629/629 [00:00<00:00, 158kB/s]
Downloading pytorch_model.bin: 100%|...| 268M/268M [01:43<00:00, 2.58MB/s]
Downloading (…)okenizer_config.json: 100%|...| 48.0/48.0 [00:00<00:00, 22.5kB/s]
Downloading (…)solve/main/vocab.txt: 100%|...| 232k/232k [00:00<00:00, 1.43MB/s]
label: POSITIVE, with score: 0.9998
label: NEGATIVE, with score: 0.5309

说明

  • pipeline 封装了三个步骤:
    • Tokenizer 预处理
      • 分词器:分词、分字以及特殊字符(包括:起始、终止、间隔等),称为 token
      • 为每个 token 映射一个 id(每个词的 id 唯一,包含特征向量)
      • 生成辅助信息,如 attention_mask
    • 通过模型传递输入
    • 后处理
transformers full nlp pipeline

图片参考

  • Tokenizer

参考

  1. https://github.com/huggingface/transformers
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数