HuggingFace Transformers 3.3 概要 (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 10/13/2020 (3.3.1)
* 本ページは、HuggingFace Transformers の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
- Windows PC のブラウザからご参加が可能です。スマートデバイスもご利用可能です。
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション |
E-Mail:sales-info@classcat.com ; WebSite: https://www.classcat.com/ |
Facebook: https://www.facebook.com/ClassCatJP/ |
HuggingFace Transformers : 概要
PyTorch と TensorFlow 2.0 のための最先端の自然言語処理
Transformers は 100+ 言語の分類、情報抽出、質問応答、要約、翻訳、テキスト生成等のテキスト上のタスクを遂行するために数千の事前訓練モデルを提供します。その目的は最先端の NLP を誰でも利用できるように容易にすることです。
Transformers はそれらの事前訓練モデルを素早くダウンロードして与えられたテキスト上で利用し、それらを貴方自身のデータセット上で再調整して、それからそれらを私達の モデル・ハブ 上でコミュニティと共有するための API を提供します。同時に、アーキテクチャを定義する各 python モジュールは素早い研究実験を可能にするためスタンドアロンとして利用できて変更できます。
Transformers は 2 つの最もポピュラーな深層学習ライブラリ PyTorch と TensorFlow により、それらの間のシームレスな統合によって支援され、その一つでモデルを訓練してから推論のために他方でそれをロードすることを許容します。
オンライン・デモ
私達のモデルの殆どを モデルハブ のそれらのページから直接テストすることができます。それらのモデルを利用する推論 API も供給します。
ここに少しのサンプルがあります :
- BERT による Masked word completion (単語補完)
- Electra による固有表現認識
- GPT-2 によるテキスト生成
- RoBERTa による自然言語推論
- BART による要約
- DistilBERT による質問応答
- T5 による翻訳
Hugging Face チームにより構築された Write With Transformer はこのレポのテキスト生成機能の公式デモです。
クイック・ツアー
与えられたテキスト上でモデルを直ちに利用するため、パイプライン API を提供します。パイプラインは事前訓練されたモデルをそのモデル訓練の間に使用された前処理と一緒にグループ化します。ここにポジティブ vs ネガティブ・テキストを分類するためのパイプラインをどのように素早く利用するかがあります :
>>> from transformers import pipeline # Allocate a pipeline for sentiment-analysis >>> classifier = pipeline('sentiment-analysis') >>> classifier('We are very happy to include pipeline into the transformers repository.') [{'label': 'POSITIVE', 'score': 0.9978193640708923}]
コードの 2 番目の行はパイプラインで使用された事前訓練モデルをダウンロードしてキャッシュします、3 番目の行はそれを与えられたテキスト上で評価します。ここでは答えは 99.8% の信頼度で「ポジティブ」です。
これは、あるコンテキストから質問応答を抽出できるもののために使用されるパイプラインの他のサンプルです :
>>> from transformers import pipeline # Allocate a pipeline for question-answering >>> question_answerer = pipeline('question-answering') >>> question_answerer({ ... 'question': 'What is the name of the repository ?', ... 'context': 'Pipeline have been included in the huggingface/transformers repository' ... }) {'score': 0.5135612454720828, 'start': 35, 'end': 59, 'answer': 'huggingface/transformers'}
答えの頭に、ここで使用される事前訓練モデルは (トークン化されたセンテンスの開始位置と終了位置とともに) その信頼度スコアを返します。このチュートリアルでパイプライン API によりサポートされるタスクについて更に学習できます。
貴方の与えられたタスク上で任意の事前訓練されたモデルをダウンロードして利用するには、それらの 3 行のコードを単に必要とするだけです (PyToch バージョン) :
>>> from transformers import AutoTokenizer, AutoModel >>> tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") >>> model = AutoModel.from_pretrained("bert-base-uncased") >>> inputs = tokenizer("Hello world!", return_tensors="pt") >>> outputs = model(**inputs)
or for TensorFlow:
>>> from transformers import AutoTokenizer, TFAutoModel >>> tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") >>> model = TFAutoModel.from_pretrained("bert-base-uncased") >>> inputs = tokenizer("Hello world!", return_tensors="tf") >>> outputs = model(**inputs)
tokenizer は事前訓練されたモデルが想定する前処理の総ての責任を負い、そしてテキストの一つ (or リスト) 上で直接呼び出せます (両者のコードサンプルの 4 番目で見れるように)。それは貴方のモデルに直接渡せる辞書を出力します (それは 5 行目で成されます)。
モデル自身は通常の Pytorch nn.Module や TensorFlow tf.keras.Model で (貴方のバックエンドに依拠します)、これらは普通に利用できます。例えば、このチュートリアル は古典的な PyTorch or TensorFlow 訓練ループでそのようなモデルを統合するか、あるいは新しいデータセット上で素早く再調整するために Trainer API をどのように利用するかを説明しています。
何故私は transformers を使用するべきでしょう?
- 使いやすい最先端のモデル :
- NLU と NLG タスク上の高パフォーマンス。
- 教育者と実践者のためのエントリへの低い障壁。
- 学習すべき 3 つのクラスだけを持つ、ユーザが直面する少ない抽象。
- 事前訓練されたモデルの総てを使用するための統一 API。
- より低い計算コスト、より小さいカーボンフットプリント :
- 研究者は常に再訓練する代わりに訓練モデルを共有できます。
- 実践者は計算時間とプロダクション・コストを減じることができます。
- 幾つかは100 言語以上の、2,000 超の事前訓練モデルを持つ数十のアーキテクチャ。
- モデルのライフタイムの総てのパートのために適切なフレームワークを選択する :
- 3 行のコードで最先端のモデルを訓練します。
- TF2.0/PyTorch フレームワーク間で単一モデルを自在に移動する。訓練、評価、プロダクションのための適切なフレームワークをシームレスに選択する。
- 貴方のニーズにモデルやサンプルを容易にカスタマイズする :
- 言及されるアーキテクチャの公式著者による結果を再生成するための各アーキテクチャのためのサンプル。
- できる限り一貫してモデル内部を公開する。
- モデルファイルは素早い実験のためにライブラリから独立的に利用できる。
何故私は transformers を利用しないのでしょう?
- このライブラリはニューラルネットのためのビルディングブロックのモジュール・ツールボックスではありません。研究者が追加の抽象/ファイルに潜ることなくモデルの各々の上で素早く iterate できるようにように、モデルファイルのコードは意図的な追加の抽象によりリファクタリングされません。
- 訓練 API は任意のモデル上で動作することを意図されていませんがライブラリにより提供されるモデルで動作するように最適化されています。
- 私達は可能な限り多くのユースケースを提示する努力をする一方で、examples フォルダのスクリプトは単なるサンプルです。それらは貴方の特定の問題上でそのままでは動作しないでしょうし、それらを貴方のニーズに適応させるためにコードの数行を変更する必要があることが想定されます。
モデル・アーキテクチャ
Transformers は現在以下のアーキテクチャを提供しています (それらの各々の高位な要約は ここ を参照) :
- ALBERT (from Google Research and the Toyota Technological Institute at Chicago) released with the paper ALBERT: A Lite BERT for Self-supervised Learning of Language Representations, by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.
- BART (from Facebook) released with the paper BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer.
- BERT (from Google) released with the paper BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.
- BERT For Sequence Generation (from Google) released with the paper Leveraging Pre-trained Checkpoints for Sequence Generation Tasks by Sascha Rothe, Shashi Narayan, Aliaksei Severyn.
- Blenderbot (from Facebook) released with the paper Recipes for building an open-domain chatbot by Stephen Roller, Emily Dinan, Naman Goyal, Da Ju, Mary Williamson, Yinhan Liu, Jing Xu, Myle Ott, Kurt Shuster, Eric M. Smith, Y-Lan Boureau, Jason Weston.
- CamemBERT (from Inria/Facebook/Sorbonne) released with the paper CamemBERT: a Tasty French Language Model by Louis Martin*, Benjamin Muller*, Pedro Javier Ortiz Suárez*, Yoann Dupont, Laurent Romary, Éric Villemonte de la Clergerie, Djamé Seddah and Benoît Sagot.
- CTRL (from Salesforce) released with the paper CTRL: A Conditional Transformer Language Model for Controllable Generation by Nitish Shirish Keskar*, Bryan McCann*, Lav R. Varshney, Caiming Xiong and Richard Socher.
- DeBERTa (from Microsoft Research) released with the paper DeBERTa: Decoding-enhanced BERT with Disentangled Attention by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.
- DialoGPT (from Microsoft Research) released with the paper DialoGPT: Large-Scale Generative Pre-training for Conversational Response Generation by Yizhe Zhang, Siqi Sun, Michel Galley, Yen-Chun Chen, Chris Brockett, Xiang Gao, Jianfeng Gao, Jingjing Liu, Bill Dolan.
- DistilBERT (from HuggingFace), released together with the paper DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into DistilGPT2, RoBERTa into DistilRoBERTa, Multilingual BERT into DistilmBERT and a German version of DistilBERT.
- DPR (from Facebook) released with the paper Dense Passage Retrieval for Open-Domain Question Answering by Vladimir Karpukhin, Barlas Oğuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, and Wen-tau Yih.
- ELECTRA (from Google Research/Stanford University) released with the paper ELECTRA: Pre-training text encoders as discriminators rather than generators by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning.
- FlauBERT (from CNRS) released with the paper FlauBERT: Unsupervised Language Model Pre-training for French by Hang Le, Loïc Vial, Jibril Frej, Vincent Segonne, Maximin Coavoux, Benjamin Lecouteux, Alexandre Allauzen, Benoît Crabbé, Laurent Besacier, Didier Schwab.
- Funnel Transformer (from CMU/Google Brain) released with the paper Funnel-Transformer: Filtering out Sequential Redundancy for Efficient Language Processing by Zihang Dai, Guokun Lai, Yiming Yang, Quoc V. Le.
- GPT (from OpenAI) released with the paper Improving Language Understanding by Generative Pre-Training by Alec Radford, Karthik Narasimhan, Tim Salimans and Ilya Sutskever.
- GPT-2 (from OpenAI) released with the paper Language Models are Unsupervised Multitask Learners by Alec Radford*, Jeffrey Wu*, Rewon Child, David Luan, Dario Amodei** and Ilya Sutskever**.
- LayoutLM (from Microsoft Research Asia) released with the paper LayoutLM: Pre-training of Text and Layout for Document Image Understanding by Yiheng Xu, Minghao Li, Lei Cui, Shaohan Huang, Furu Wei, Ming Zhou.
- Longformer (from AllenAI) released with the paper Longformer: The Long-Document Transformer by Iz Beltagy, Matthew E. Peters, Arman Cohan.
- LXMERT (from UNC Chapel Hill) released with the paper LXMERT: Learning Cross-Modality Encoder Representations from Transformers for Open-Domain Question Answering by Hao Tan and Mohit Bansal.
- MarianMT Machine translation models trained using OPUS data by Jörg Tiedemann. The Marian Framework is being developed by the Microsoft Translator Team.
- MBart (from Facebook) released with the paper Multilingual Denoising Pre-training for Neural Machine Translation by Yinhan Liu, Jiatao Gu, Naman Goyal, Xian Li, Sergey Edunov, Marjan Ghazvininejad, Mike Lewis, Luke Zettlemoyer.
- Pegasus (from Google) released with the paper PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization by Jingqing Zhang, Yao Zhao, Mohammad Saleh and Peter J. Liu.
- Reformer (from Google Research) released with the paper Reformer: The Efficient Transformer by Nikita Kitaev, Łukasz Kaiser, Anselm Levskaya.
- RoBERTa (from Facebook), released together with the paper a Robustly Optimized BERT Pretraining Approach by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov. ultilingual BERT into DistilmBERT and a German version of DistilBERT.
- SqueezeBert released with the paper SqueezeBERT: What can computer vision teach NLP about efficient neural networks? by Forrest N. Iandola, Albert E. Shaw, Ravi Krishna, and Kurt W. Keutzer.
- T5 (from Google AI) released with the paper Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
- Transformer-XL (from Google/CMU) released with the paper Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context by Zihang Dai*, Zhilin Yang*, Yiming Yang, Jaime Carbonell, Quoc V. Le, Ruslan Salakhutdinov.
- XLM (from Facebook) released together with the paper Cross-lingual Language Model Pretraining by Guillaume Lample and Alexis Conneau.
- XLM-RoBERTa (from Facebook AI), released together with the paper Unsupervised Cross-lingual Representation Learning at Scale by Alexis Conneau*, Kartikay Khandelwal*, Naman Goyal, Vishrav Chaudhary, Guillaume Wenzek, Francisco Guzmán, Edouard Grave, Myle Ott, Luke Zettlemoyer and Veselin Stoyanov.
- XLNet (from Google/CMU) released with the paper XLNet: Generalized Autoregressive Pretraining for Language Understanding by Zhilin Yang*, Zihang Dai*, Yiming Yang, Jaime Carbonell, Ruslan Salakhutdinov, Quoc V. Le.
- Other community models, contributed by the community.
- 新しいモデルを寄贈することを望みますか?新しいモデルを追加するプロセスに導く詳細なガイドとテンプレートを追加しました。レポジトリの templates フォルダでそれらを見つけることができます。貴方の PR を始める前にフィードバックを集めるために contributing ガイドライン を確認してそしてメンテナーにコンタクトするか issue をオープンすることを確実にしてください。
これらの実装は幾つかのデータセット上でテストされ (examples スクリプト参照) そして元の実装のパフォーマンスに一致するはずです。ドキュメントの Examples セクションでパフォーマンス上の更なる詳細を見つけられます。
更に学習する
セクション | 説明 |
ドキュメント | Full API ドキュメントとチュートリアル |
タスク要約 | ![]() |
前処理チュートリアル | モデルのためにデータを準備するための Tokenizer クラスを使用する |
訓練と再調整 | PyTorch/TensorFlow 訓練ループと Trainer API で![]() |
クイックツアー: 再調整/使用方法スクリプト | 広範囲なタスク上でモデルを再調整するためのサンプル・スクリプト |
モデル共有とアップロード | 貴方の再調整モデルをコミュニティでアップロードして共有する |
マイグレーション | pytorch-transformers or pytorch-pretrained-bert から ![]() |
Citation
(訳注: 原文 を参照してください。)
以上