🚀 中文詞レベルRoBERTaミニモデルセット
このシリーズは、[UER - py](https://github.com/dbiir/UER - py/)で事前学習された5つの中国語の単語レベルのRoBERTaモデルです。関連論文はこちらを参照してください。また、これらのモデルはTencentPretrainでも事前学習できます。このフレームワークはUER - pyを継承し、10億レベルのパラメータモデルをサポートし、マルチモーダル事前学習フレームワークに拡張されています。詳細はこの論文を参照してください。
多くの中国語の事前学習重みは文字レベルに基づいています。文字モデルと比較して、単語レベルのモデルはシーケンスが短いため速度が速く、実験によると性能も優れています。そのため、5種類の異なるサイズの中国語の単語レベルのRoBERTaモデルを公開しました。結果の再現を容易にするために、公開コーパスと形態素解析ツールを使用し、完全な学習詳細を提供しています。
これら5つのモデルは[UER - pyモデルライブラリページ](https://github.com/dbiir/UER - py/wiki/Modelzoo)からダウンロードできます。また、以下のHuggingFaceリンクから取得することもできます。
|
リンク |
単語レベルRoBERTa - Tiny |
[L = 2/H = 128 (ミニ)][2_128] |
単語レベルRoBERTa - Mini |
[L = 4/H = 256 (ミニ)][4_256] |
単語レベルRoBERTa - Small |
[L = 4/H = 512 (スモール)][4_512] |
単語レベルRoBERTa - Medium |
[L = 8/H = 512 (ミディアム)][8_512] |
単語レベルRoBERTa - Base |
[L = 12/H = 768 (ベース)][12_768] |
[文字モデル](https://huggingface.co/uer/chinese_roberta_L - 2_H - 128)と比較して、単語レベルのモデルはほとんどのタスクで優れた性能を発揮します。以下は、6つの中国語タスクの開発セットでのスコア比較です。
モデル |
平均点 |
書評感情 |
中国語感情 |
テキストマッチング |
ニュース分類(CLUE) |
アプリ分類(CLUE) |
自然推論(CLUE) |
RoBERTa - Tiny(文字) |
72.3 |
83.4 |
91.4 |
81.8 |
62.0 |
55.0 |
60.3 |
RoBERTa - Tiny(単語) |
74.4(+2.1) |
86.7 |
93.2 |
82.0 |
66.4 |
58.2 |
59.6 |
RoBERTa - Mini(文字) |
75.9 |
85.7 |
93.7 |
86.1 |
63.9 |
58.3 |
67.4 |
RoBERTa - Mini(単語) |
76.9(+1.0) |
88.5 |
94.1 |
85.4 |
66.9 |
59.2 |
67.3 |
RoBERTa - Small(文字) |
76.9 |
87.5 |
93.4 |
86.5 |
65.1 |
59.4 |
69.7 |
RoBERTa - Small(単語) |
78.4(+1.5) |
89.7 |
94.7 |
87.4 |
67.6 |
60.9 |
69.8 |
RoBERTa - Medium(文字) |
78.0 |
88.7 |
94.8 |
88.1 |
65.6 |
59.5 |
71.2 |
RoBERTa - Medium(単語) |
79.1(+1.1) |
90.0 |
95.1 |
88.0 |
67.8 |
60.6 |
73.0 |
RoBERTa - Base(文字) |
79.7 |
90.1 |
95.2 |
89.2 |
67.0 |
60.9 |
75.5 |
RoBERTa - Base(単語) |
80.4(+0.7) |
91.1 |
95.7 |
89.4 |
68.0 |
61.5 |
76.8 |
各タスクは以下のハイパーパラメータの組み合わせから最適な設定を選択し、学習時のシーケンス長は128です。
- 学習エポック数:3/5/8
- バッチサイズ:32/64
- 学習率:3e - 5/1e - 4/3e - 4
💻 使用例
基本的な使用法
マスク言語モデリングに直接使用できます(単語レベルRoBERTa - Mediumを例に)。
>>> from transformers import pipeline
>>> unmasker = pipeline('fill - mask', model='uer/roberta - medium - word - chinese - cluecorpussmall')
>>> unmasker("[MASK]的首都是北京。")
[
{'sequence': '中国 的首都是北京。',
'score': 0.21525809168815613,
'token': 2873,
'token_str': '中国'},
...(他の予測結果)
]
高度な使用法
PyTorchでテキスト特徴を取得する方法:
from transformers import AlbertTokenizer, BertModel
tokenizer = AlbertTokenizer.from_pretrained('uer/roberta - medium - word - chinese - cluecorpussmall')
model = BertModel.from_pretrained("uer/roberta - medium - word - chinese - cluecorpussmall")
text = "用你喜欢的任何文本替换我。"
encoded_input = tokenizer(text, return_tensors='pt')
output = model(**encoded_input)
TensorFlowでの使用方法:
from transformers import AlbertTokenizer, TFBertModel
tokenizer = AlbertTokenizer.from_pretrained('uer/roberta - medium - word - chinese - cluecorpussmall')
model = TFBertModel.from_pretrained("uer/roberta - medium - word - chinese - cluecorpussmall")
text = "用你喜欢的任何文本替换我。"
encoded_input = tokenizer(text, return_tensors='tf')
output = model(encoded_input)
注:BertTokenizerはsentencepieceをサポートしていないため、ここではAlbertTokenizerを使用しています。
📦 インストール
学習データ
学習コーパスとしてCLUECorpusSmallを使用し、Googleのsentencepieceを用いて形態素解析を行います。形態素解析モデルはCLUECorpusSmall上で学習されます。
>>> import sentencepiece as spm
>>> spm.SentencePieceTrainer.train(...学習パラメータ...)
学習プロセス
モデルは[UER - py](https://github.com/dbiir/UER - py/)を使用して騰訊クラウド上で事前学習されます。まずシーケンス長128で100万ステップ学習し、次にシーケンス長512で25万ステップ追加学習します。異なるサイズのモデルは同じハイパーパラメータを使用します。
単語レベルRoBERTa - Mediumを例に:
第一フェーズ:
python3 preprocess.py --corpus_path corpora/cluecorpussmall.txt \
--spm_model_path models/cluecorpussmall_spm.model \
--dataset_path cluecorpussmall_word_seq128_dataset.pt \
--processes_num 32 --seq_length 128 \
--dynamic_masking --data_processor mlm
python3 pretrain.py --dataset_path ...(学習コマンドの詳細)
第二フェーズ:
python3 preprocess.py ...(512長の前処理)
python3 pretrain.py ...(512長の学習)
最後にHuggingface形式に変換します。
python3 scripts/convert_bert_from_uer_to_huggingface.py ...(変換コマンド)
📚 ドキュメント
引用文献
@article{devlin2018bert,
title={BERT: 面向语言理解的深度双向Transformer预训练},
author={Devlin等},
journal={arXiv预印本},
year={2018}
}
...(他の文献引用)
[2_128]:https://huggingface.co/uer/roberta - tiny - word - chinese - cluecorpussmall
[4_256]:https://huggingface.co/uer/roberta - mini - word - chinese - cluecorpussmall
[4_512]:https://huggingface.co/uer/roberta - small - word - chinese - cluecorpussmall
[8_512]:https://huggingface.co/uer/roberta - medium - word - chinese - cluecorpussmall
[12_768]:https://huggingface.co/uer/roberta - base - word - chinese - cluecorpussmall