
該存儲庫提供了培訓日本預預性模型的代碼。該代碼已用於生產日語-GPT2-MEDIUM,日本GPT2-SMALL和RINNA CO. LTD。
目前支持的預期模型包括:GPT-2,Roberta。
| 目錄 |
|---|
| 更新日誌 |
| 使用提示 |
| 通過擁抱面使用我們預告片的模型 |
從頭開始訓練japanese-gpt2-xsmall |
從頭開始訓練japanese-roberta-base |
| 執照 |
如果您使用代碼或通過HuggingFace使用我們的模型遇到任何問題,請打開問題(用英語/日語)。
如果您覺得這項工作有用,請引用以下論文:
@article{rinna_pretrained2021,
title={日本語自然言語処理における事前学習モデルの公開},
author={趙 天雨 and 沢田 慶},
journal={人工知能学会研究会資料 言語・音声理解と対話処理研究会},
volume={93},
pages={169-170},
year={2021},
doi={10.11517/jsaislud.93.0_169}
}
2022/01/25在模型摘要表中更新了rinna/japanese-gpt-1b的鏈接。
2022/01/17更新了引用信息。
2021/11/01更新了Corpora鏈接。
2021/09/13添加了有關將position_ids與japanese-roberta-base使用的提示。有關詳細信息,請參閱第3期。
2021/08/26 [重要]從MIT許可證到Apache 2.0許可證的更新許可證,這是由於使用了Cl-Tohoku/Bert-jyapanese的Wikipedia預處理代碼。有關詳細信息,請參見第1期。
2021/08/23將日本維基百科添加到培訓語料庫中。發布的培訓代碼rinna/japanese-gpt2-small , rinna/japanese-gpt2-xsmall和rinna/japanese-roberta-base 。
2021/08/18將回購名稱從japanese-gpt2更改為japanese-pretrained-models
2021/06/15使用檢查點時修復了最佳PPL跟踪錯誤。
2021/05/04修復了多GPU培訓的隨機播種錯誤。
2021/04/06發布了用於培訓rinna/japanese-gpt2-medium代碼。
rinna/japanese-roberta-base的提示使用[CLS] :要預測一個蒙版的令牌,請確保在句子之前添加[CLS]令牌以使模型正確編碼它,因為它在模型訓練過程中使用。
令牌化後使用[MASK] :a)在輸入字符串中直接鍵入[MASK] ,b)用[MASK]在令牌化後代替令牌將產生不同的令牌序列,從而產生不同的預測結果。令牌化後使用[MASK]更合適(因為它與模型的預定方式一致)。但是,HuggingFace推斷API僅支持輸入[MASK]在輸入字符串中鍵入,並產生較不健壯的預測。
提供position_ids作為一個參數明確:當未為Roberta*模型提供position_ids時,HuggingFace的transformers將自動構造它,但從padding_idx而不是0開始(請參閱create_position_ids_from_input_ids()在HuggingFace的實施中都無法使用rinna/japanese-roberta-base padding_idx令牌不是0 。因此,請確保自己限制position_ids _ids,並從位置ID 0開始。
| 語言模型 | #參數 | #層 | #EMB DIM | #epochs | DEV PPL | 訓練時間* |
|---|---|---|---|---|---|---|
| rinna/japesen-gpt-1b | 1.3b | 24 | 2048 | 10+ | 13.9 | N/A ** |
| rinna/dayer-gpt2中等 | 336m | 24 | 1024 | 4 | 18 | 45天 |
| rinna/japesen-gpt2-small | 110m | 12 | 768 | 3 | 21 | 15天 |
| rinna/japesen-gpt2-xsmall | 37m | 6 | 512 | 3 | 28 | 4天 |
| 蒙版語言模型 | #參數 | #層 | #EMB DIM | #epochs | DEV PPL | 訓練時間* |
|---|---|---|---|---|---|---|
| rinna/日本羅伯塔基地 | 110m | 12 | 768 | 8 | 3.9 | 15天 |
*在8x V100 32GB機器上進行了培訓。
**使用不同的代碼庫和不同的計算環境進行培訓。
rinna/japanese-roberta-base預測蒙版令牌 import torch
from transformers import T5Tokenizer, RobertaForMaskedLM
# load tokenizer
tokenizer = T5Tokenizer.from_pretrained("rinna/japanese-roberta-base")
tokenizer.do_lower_case = True # due to some bug of tokenizer config loading
# load model
model = RobertaForMaskedLM.from_pretrained("rinna/japanese-roberta-base")
model = model.eval()
# original text
text = "4年に1度オリンピックは開かれる。"
# prepend [CLS]
text = "[CLS]" + text
# tokenize
tokens = tokenizer.tokenize(text)
print(tokens) # output: ['[CLS]', '▁4', '年に', '1', '度', 'オリンピック', 'は', '開かれる', '。']']
# mask a token
masked_idx = 5
tokens[masked_idx] = tokenizer.mask_token
print(tokens) # output: ['[CLS]', '▁4', '年に', '1', '度', '[MASK]', 'は', '開かれる', '。']
# convert to ids
token_ids = tokenizer.convert_tokens_to_ids(tokens)
print(token_ids) # output: [4, 1602, 44, 24, 368, 6, 11, 21583, 8]
# convert to tensor
token_tensor = torch.LongTensor([token_ids])
# provide position ids explicitly
position_ids = list(range(0, token_tensor.size(1)))
print(position_ids) # output: [0, 1, 2, 3, 4, 5, 6, 7, 8]
position_id_tensor = torch.LongTensor([position_ids])
# get the top 10 predictions of the masked token
with torch.no_grad():
outputs = model(input_ids=token_tensor, position_ids=position_id_tensor)
predictions = outputs[0][0, masked_idx].topk(10)
for i, index_t in enumerate(predictions.indices):
index = index_t.item()
token = tokenizer.convert_ids_to_tokens([index])[0]
print(i, token)
"""
0 総会
1 サミット
2 ワールドカップ
3 フェスティバル
4 大会
5 オリンピック
6 全国大会
7 党大会
8 イベント
9 世界選手権
"""
japanese-gpt2-xsmall通過在repo目錄下運行以下命令來安裝所需的軟件包:
pip install -r requirements.txt
python -m unidic download
下載培訓語料庫日語CC-100並提取ja.txt文件。
移動ja.txt文件或修改src/corpus/jp_cc100/config.py以將ja.txt的filepath與self.raw_data_dir匹配配置文件中的self.raw_data_dir 。
通過運行:將ja.txt拆分到較小的文件:
cd src/
python -m corpus.jp_cc100.split_to_small_files
self.download_link (在File src/corpus/jp_wiki/config.py中使用鏈接到您首選的Wikipedia dump版本。然後下載培訓語料庫日語Wikipedia,然後通過運行: python -m corpus.jp_wiki.build_pretrain_dataset
python -m corpus.jp_wiki.split_to_small_files
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m task.pretrain_gpt2.train
--n_gpus 4
--save_model True
--enable_log True
--model_size xsmall
--model_config_filepath model/gpt2-ja-xsmall-config.json
--batch_size 20
--eval_batch_size 40
--n_training_steps 1600000
--n_accum_steps 3
--init_lr 0.0007
假設您已經運行了培訓腳本,並將XSMALL大小的GPT-2保存到data/model/pretrain_gpt2/gpt2-ja-xsmall-xxx.checkpoint 。運行以下命令使用它通過p=0.95和k=40 :
CUDA_VISIBLE_DEVICES=0 python -m task.pretrain_gpt2.interact
--checkpoint_path ../data/model/pretrain_gpt2/gpt2-ja-medium-xxx.checkpoint
--gen_type top
--top_p 0.95
--top_k 40
製作您的擁抱面帳戶。創建模型倉庫。克隆到您當地的機器。
通過運行從檢查點創建模型和配置文件:
python -m task.pretrain_gpt2.checkpoint2huggingface
--checkpoint_path ../data/model/gpt2-medium-xxx.checkpoint
--save_dir {huggingface's model repo directory}
python -m task.pretrain_gpt2.check_huggingface
--model_dir {huggingface's model repo directory}
通過運行:檢查GPT-2培訓腳本的可用參數:
python -m task.pretrain_gpt2.train --help
japanese-roberta-base假設您已經完成瞭如上所述完成的數據構建過程,請運行以下命令來訓練基本大小的日本羅伯塔(Japanese Roberta),例如8 V100 GPU:
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 python -m task.pretrain_roberta.train
--n_gpus 8
--save_model True
--enable_log True
--model_size base
--model_config_filepath model/roberta-ja-base-config.json
--batch_size 32
--eval_batch_size 32
--n_training_steps 3000000
--n_accum_steps 16
--init_lr 0.0006
Apache 2.0許可證