【Python】GPT-2で日本語文章を自動で生成する | ジコログ
$ pip3 install torch torchvision torchaudio
※PyTorchをインストール
$ pip install transformers
Pythonで実行する
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
results = classifier(
[
"We are very happy to show you the 🤗 Transformers library.",
"We hope you don't hate it.",
]
)
for result in results:
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
$ pip install sentencepiece
テスト用ファイルをダウンロードする
import sentencepiece as spm
# 学習によるモデル作成
sp = spm.SentencePieceProcessor()
spm.SentencePieceTrainer.train(
"--input=wagahaiwa_nekodearu.txt --model_prefix=trained_model --vocab_size=8000 --character_coverage=0.9995"
)
分割して動作確認
import sentencepiece as spm
TEXT = "猫になったんだよな君は"
# 学習済みモデルの読み込み
sp = spm.SentencePieceProcessor()
sp.load("trained_model.model")
# 分割した結果を表示
result = sp.EncodeAsPieces(TEXT)
print(result)
# 分割して単語IDとともに表示
for i in sp.EncodeAsIds(TEXT):
print(i, sp.IdToPiece(i))
【AI】rinnaの「GPT-2」と「BERT」を使って文章を自動生成する! | ガジェラン
rinna GPT-2モデルの生成パラメータ
※パラメータ説明(パラメータ別出力比較付き・結構便利)
Huggingface Transformers 入門 (27) – rinnaの日本語GPT-2モデルの推論|npaka|note
※パラメータ説明(ちょっと細かい)
rinna社のbert事前学習モデルを使ってみた – Qiita
※関数化している
コメントを残す