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

トークナイザーの仕組みを少しだけ深ぼって理解する

Shota Totsuka
January 29, 2025
5

 トークナイザーの仕組みを少しだけ深ぼって理解する

Shota Totsuka

January 29, 2025
Tweet

Transcript

  1. 自己紹介 ©Blueish 2024. All rights reserved. 戸塚 翔太|BLUEISH Shota Totsuka

    ・LLMアプリ開発者(Go/Python …etc) ・生成AI, 機械学習 ・趣味: スキー/スノボ, 最近はDifyにContribute ・静岡県(浜松)に住んでます  近くの方がいれば、一緒に勉強会しましょう! Xアカウント @totsukash
  2. ©Blueish 2024. All rights reserved. - 自然言語(人間が読めるテキスト)を数値の配列に変換する処理を行う - 文章を単語に分割し、ベクトルに変換する -

    テキストの中で一つの意味を表す文字の並びのことを「トークン」と呼ぶ - 多くのLLM APIではトークン単位で課金される トークナイザーとは トークナイザーについて
  3. ©Blueish 2024. All rights reserved. トークナイザーについて ©Blueish 2024. All rights

    reserved. - トークナイザーは単語とidを紐づける辞書のようなもの - 大きすぎても小さすぎてもダメ - あらかじめ用意したデータセットに従ってトークナイザーを作成 → そこに無い語句は「未知語(Unknown)」となる トークナイザーのサイズ
  4. ©Blueish 2024. All rights reserved. トークナイザーについて ©Blueish 2024. All rights

    reserved. - 一般的に使われない単語が入る - 無駄な語句によりサイズが大きくなり、メモリ負荷などコストが高くなる 大きすぎる場合
  5. ©Blueish 2024. All rights reserved. トークナイザーについて ©Blueish 2024. All rights

    reserved. - 1文字単位の分割など、細かく区切りすぎる - 単語の意味が理解できず、モデルの精度低下を招く 小さすぎる場合
  6. ©Blueish 2024. All rights reserved. トークナイザーの種類 ©Blueish 2024. All rights

    reserved. - 「バイト対符号化」と呼ばれる手法 - 最も頻出する文字列のペアを新しい単位として結合していく圧縮アルゴリズム - あらかじめ指定した回数だけ結合 - 例:「low lower lowest」という文章から学習する場合 - 最初:「l o w _ l o w e r _ l o w e s t」 - 「l」「o」が頻出→「lo」として結合 - 次に「lo w」が頻出→「low」として結合 BPE(Byte-Pair Encoding)
  7. ©Blueish 2024. All rights reserved. トークナイザーの種類 ©Blueish 2024. All rights

    reserved. - BERTで採用されたサブワード分割 - BPEと異なり、頻度ではなく尤度で分割を決定 - 全て文字に分割後、確率の高いペアから結合 - 例: walking → 「walk」「##ing」 - walk:単語の始まり - ##ing:途中から始まる部分 Word Piece
  8. ©Blueish 2024. All rights reserved. トークナイザーの種類 ©Blueish 2024. All rights

    reserved. - Mecabなど形態素解析器でテキストを分割 → 単語の区切りができる - その後BPEやWord Pieceを使用 形態素解析
  9. ©Blueish 2024. All rights reserved. トークナイザーの種類 ©Blueish 2024. All rights

    reserved. - Mecabは日本語に最適化されているため、多言語での使用が難しい - Google/sentencepiece で公開されている - 文法規則ではなく、頻度ベースで最適な分割単位を”学習” Sentence Piece
  10. ©Blueish 2024. All rights reserved. トークナイザーの種類 ©Blueish 2024. All rights

    reserved. 1. Unicode Normalization - テキストをUnicodeの標準形式に正規化 2. Trainer - コーパスから頻出するサブワードを学習し、効率的な分割規則を作成 3. Encoder - 学習済みの分割規則に基づいて、入力テキストをサブワードに分割し、トークンIDの列に変換 4. Decoder - トークンIDの列を受け取り、対応するsubwordを連結して元のテキスト形式に復元 Sentence Piece 手順
  11. ©Blueish 2024. All rights reserved. - プロンプトはトークナイザーを介してLLMが理解できる形式(埋め込みベクトル)に 変換される - その手法はBPE,

    Word Piece, Sentence Pieceなど複数ある まとめ 参考情報 - https://dalab.jp/archives/journal/japanese-llm-tokenizer/ - [SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing](https://arxiv.org/abs/1808.06226)