tossy diary

日常の記録を残す

HuggingFace Transformers Course(1章)

2017年に「[1706.03762] Attention Is All You Need」が出されて以降、Transformer(パーツはAttention)を使った手法が現在の自然言語処理の主流の手法となっている。 Transformerの実装ではHugging Faceのライブラリを使うことが多いのでこの機会に体形立てて学習しておくと良いだろう。 オンラインコースの参考になった部分について紹介していく。 各章の最後には章の内容に関するクイズがあるので理解を深めることができる。

huggingface.co

1章 : Transformer models

1章はTransformerモデルの説明が書かれている。

NLP(Natural Language Processing)とは何かというところからNLPで解決したい各タスクに対してTransformersライブラリのpipelineを使ったコードとともに記載されている。 例えば、以下のようなタスクに対して適用できる。

  • 特徴選択 : feature-extraction (get the vector representation of a text)
  • マスク当て : fill-mask
  • 固有表現抽出 : ner (named entity recognition)
  • 質問応答 : question-answering
  • 感情分析 : sentiment-analysis
  • 文章要約 : summarization
  • 文章生成 : text-generation
  • 翻訳 : translation
  • ゼロショット分類 : zero-shot-classification

Tranformerの歴史

Transformerの歴史の図 https://huggingface.co/course/static/chapter1/transformers_chrono.png

Transformerモデルは以下のような3つの種類に分けることができる。

  • GPT-like(auto-regressive)
  • BERT-like(auto-encoding)
  • BART/T5-like(sequence-to-sequence)

GPT-likeのモデルはTransformerのDecoderを用いている。 文章生成のような生成タスクにおいてDecoderを使うと良い。

BERT-likeのモデルはTransformerのEncoderを用いている。 BERT-baseは12層のTransformerのEncoder部分(BertLayerモジュール)を使っている。 文章分類や固有表現抽出のようなインプットの理解が必要なタスクに使うと良い。

BART/T5-likeのモデルはTransformerのEncoder-Decoderの両方を用いている。 翻訳や文章要約のようなインプットを必要とする生成タスクではEncoder-Decoderの両方を用いる

以下の表がモデルと例、タスクを整理しておりわかりやすい。 f:id:sktshk:20210619230008p:plain

EncoderとDecoder

Encoderは図の左の部分、Decoderは図の右の部分。

オリジナルのTransformerのアーキテクチャの図は以下。 https://huggingface.co/course/static/chapter1/transformers.png