0
1

More than 1 year has passed since last update.

huggingface/transformers覚書

Last updated at Posted at 2021-11-21

huggingface/transformersに関してつまづいた点をまとめた自分用のメモです。
大体は公式ドキュメント公式フォーラムで解決することが多いです。

TensorFlowで学習したモデルをpytorch形式にして読み込みたい

参考記事:公式ドキュメント
transformers-cli convertで一発。

EarlyStoppingさせたい

参考記事:公式ドキュメント
EarlyStopping以外のCallbackも実装されている。

BertJapaneseTokenizerでNEologdなどの辞書を使いたい

参考記事:BertJapaneseTokenizerで辞書を指定・変更する
tokenizer_config.jsonに下記を加えれば良い。

"mecab_kwargs":{
    "mecab_dic": null,
    "mecab_option": "-d 使用したい辞書のpath"
}

mecab_dicnullにしておくこと。
現在のバージョンでは、デフォルト値の場合mecab_dic+mecab_optionで設定を読み込んでしまう。
MeCab本体のインストールやmecabrcの保存場所に注意。

学習済みのSentencePieceを使いたい

参考記事1:huggingface / transformersを使って日本語BERTの事前学習を実施してオリジナルな言語モデルを作ってみる
参考記事2:SentencePieceでの日本語分かち書きをTransformersのパイプラインに組み込む
自作記事:huggingface/transformersのAutoTokenizerから学習済みSentencePiece Tokenizerを呼び出す
AlbertTokenizerで読み込めばOK。special tokenやunknown token idの設定に注意。

PipelineでTokenizerのmax_lengthを設定したい

preprocessやpostprocessなどのパラメータは、Pipelineの__init____call__時に引数として渡すことで設定可能

classifier = TextClassificationPipeline(model=model, tokenizer=tokenizer, device=device, truncation=True, max_length=512)

そもそもTokenizerの__init__時にmodel_max_lengthで設定できるっぽいが、truncationなどの設定の仕方がわからない。

Pipelineの出力にtokenized textとattention weightを加えたい

自作記事:huggingface/transformersでpipelineの出力にtokenized textとattention weightを加える
既存のPipelineを継承し、forwardpostprocessに変更を加えた自作Pipelineを作成する。詳細は上記記事を参照。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1