Upgrade to Pro — share decks privately, control downloads, hide ads and more …

実践:日本語文章生成 Transformers ライブラリで学ぶ実装の守破離 / Introduction of Japanese Text Generation with Transformers

実践:日本語文章生成 Transformers ライブラリで学ぶ実装の守破離 / Introduction of Japanese Text Generation with Transformers

「PyCon JP 2022」での登壇「実践:日本語文章生成 Transformers ライブラリで学ぶ実装の守破離」の発表資料
https://2022.pycon.jp/timetable?id=EEA8FG

Shotaro Ishihara

October 12, 2022
Tweet

More Decks by Shotaro Ishihara

Other Decks in Technology

Transcript

  1. 石原祥太郎 日本経済新聞社 日経イノベーション・ラボ 主任研究員 PyCon JP 2022、2022 年 10 月

    14 日 実践:日本語文章生成  Transformers ライブラリ で学ぶ実装の守破離
  2. 24 • 機械学習(教師あり):入力と出力のペアから、 ルールを獲得する試み • 自然言語処理では、2 段階での学習が一般的に ◦ 事前学習 ◦

    ファインチューニング • 大規模言語モデル・基盤モデルと呼ばれることも まとめ:近年の自然言語処理の躍進
  3. 30 import torch from transformers import T5Tokenizer, AutoModelForCausalLM tokenizer =

    T5Tokenizer.from_pretrained("rinna/japanese-gpt-1b") model = AutoModelForCausalLM.from_pretrained("rinna/japanese-gpt-1b") if torch.cuda.is_available(): model = model.to("cuda") 守:既存のモデルを利用 https://huggingface.co/rinna/japanese-gpt-1b
  4. 31 text = "Pythonは、" token_ids = tokenizer.encode(text, add_special_tokens=False, return_tensors="pt") with

    torch.no_grad(): output_ids = model.generate(token_ids.to(model.device)) output = tokenizer.decode(output_ids.tolist()[0]) print(output) 守:既存のモデルを利用 https://huggingface.co/rinna/japanese-gpt-1b
  5. 33 守:手軽な Pipelines https://huggingface.co/docs/transformers/main_classes/pipelines https://huggingface.co/csebuetnlp/mT5_multilingual_XLSum from transformers import pipeline text

    = "PyCon JP は、Python ユーザが集まり、......" pipe = pipeline("summarization", model="csebuetnlp/mT5_multilingual_XLSum") pipe_out = pipe(text) # [{'summary_text': 'Python とは何なのか? 世界中のユーザーが意見を交わすため、オ ンラインで「pyCon JP」を開催する。'}]
  6. 35 破:ファインチューニング https://github.com/huggingface/transformers/tree/main/examples $ python examples/pytorch/summarization/run_summarization.py \ --model_name_or_path t5-small \

    --do_train \ --do_eval \ --dataset_name cnn_dailymail \ --output_dir /tmp/tst-summarization 自前データを整形して Examples のコードを実行
  7. $ python run_mlm_flax.py \ --output_dir="./nikkei-roberta-base" \ --model_type="roberta" \ --config_name="./nikkei-roberta-base" \

    --tokenizer_name="./nikkei-roberta-base" \ --dataset_name="nikkei" 37 離:事前学習から取り組む https://github.com/huggingface/transformers/tree/main/examples 自前データを整形して Examples のコードを実行
  8. 40 • 「日経電子版」で 1 日約 1000 記事を配信 • 記者・編集者は約 1500

    人 • 内製の開発組織があり、研究開発も推進 ◦ PyCon JP 2022 シルバースポンサー ◦ https://hack.nikkei.com/slides/ ◦ https://hack.nikkei.com/publications 日本経済新聞社
  9. 42 • 大規模言語モデルとして「BERT」が 2018 年末に 発表され話題に • 外部企業の協力を経て 2019 年、日経電子版の記

    事を用いた BERT モデルを構築 • ニュース記事の要約などの応用を検証 日経電子版 BERT 2019
  10. 43 新型コロナの流行前の 文章で学習した BERT モデルは、コロナ後の 文章の処理が苦手 言葉は日々変化 石原慧人, 石原祥太郎, 白井穂乃

    (2021). “BertSumを用いた日本語ニュース 記事の抽象型要約手法の検討.” 人工知能学会全国大会論文集.
  11. 44 • 時期の異なる日経電子版 記事で構築した 2 つの 単語分散表現のベクトル 空間を比較 • 「コロナ」の周辺単語が

    コロナ前と以後で変化 言葉は日々変化 高橋寛武, 石原祥太郎, 白井穂乃 (2022). “単語分散表現を用いた新型コロナ ウイルスによる意味変化検出.” 言語処理学会第28回年次大会発表論文集.
  12. 48 • モデルの出力をそのまま使うのではなく ◦ 記者・編集者への候補の提示 ◦ 修正や指摘を取り込んで日々改善 活用・改善のための仕組みづくり Shotaro Ishihara,

    Yuta Matsuda, and Norihiko Sawa (2021). Editors-in-the-loop News Article Summarization Framework with Sentence Selection and Compression. In Proceedings of the 5th IEEE Workshop on Human-in-the-Loop Methods and Future of Work in BigData, pp. 3522-24.
  13. 49 独自のモデルをいつ再学習すべきか? Shotaro Ishihara, Hiromu Takahashi, and Hono Shirai (2022).

    Semantic Shift Stability: Efficient Way to Detect Performance Degradation of Word Embeddings and Pre-trained Language Models. In Proceedings of AACL-IJCNLP 2022. (to appear) https://github.com/Nikkei/seman tic-shift-stability
  14. 51 • 『機械学習エンジニアのためのTransformers』 (オライリー・ジャパン) • 『BERTによる自然言語処理入門: Transformersを 使った実践プログラミング』(オーム社) • 『IT

    Text 自然言語処理の基礎』(オーム社) • 『機械学習工学 (機械学習プロフェッショナルシ リーズ)』(講談社) 更なる学び