前回の続き。
既存の日本語GPT-2モデルにrinnaという有名なやつがあるので
それにのっからしてもらう感じでやってみる。
# 要りそうなもの
!git clone https://github.com/huggingface/transformers -b v4.4.2
!pip install transformers==4.4.2
!pip install datasets==1.2.1
!pip install sentencepiece==0.1.91
# 転移学習させる
!python ./transformers/examples/language-modeling/run_clm.py \
--model_name_or_path=rinna/japanese-gpt2-medium \
--train_file=train.txt \
--validation_file=train.txt \
--do_train \
--do_eval \
--num_train_epochs=1 \
--save_steps=5000 \
--save_total_limit=3 \
--per_device_train_batch_size=1 \
--per_device_eval_batch_size=1 \
--output_dir=output1/ \
--use_fast_tokenizer=False
transformersは明示的に4.4.2を入れてあげないと、多分今の環境ではコケます。
上記の場合train.txtが元のキャラクターの発言などを収めたテキストファイル。
output1/は学習モデルの出力先フォルダです。
# テスト出力
import transformers
from transformers import T5Tokenizer, AutoModelForCausalLM
tokenizer = T5Tokenizer.from_pretrained("rinna/japanese-gpt2-medium")
model = AutoModelForCausalLM.from_pretrained("output1/")
input = tokenizer.encode("こんにちは", return_tensors="pt")
output = model.generate(input, do_sample=True, max_length=50, num_return_sequences=5)
print(tokenizer.batch_decode(output))
テスト出力は上記の感じで。
エポック数増やすともっといい感じになるのですが
多分メモリ足んなくなるんとちゃいますかね……?
Colab Proとか使える人はチャレンジしてみてください。
以下、宜しく。
参考:Huggingface Transformers 入門 (28) - rinnaの日本語GPT-2モデルのファインチューニング
コメント