2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Hugging Face】Transformers 3.2.0から4.26.0にバージョンアップした際のコード変更

Last updated at Posted at 2023-02-02

論文のコードを参考にモデルの実装を行っているのですが、Transformersのバージョンが古く(v.3.2.0)、現在のSTABLEバージョンv.4.26.0と所々API仕様が異なるところがありました(メジャーバージョンが変わったので当たり前ですが...)。

公式docやライブラリのコードを読む際に不便なので、このたびバージョンをあげてみました。

案の定エラーが何個か出たので、解消した方法をこちらにメモします。

DataCollatorForLanguageModelingの_tensorize_batchが使用できない

torch.tensorで書き換えました。

batch = self._tensorize_batch(examples)

batch = torch.tensor([e.tolist() for e in examples],dtype=torch.long)

ACT2FN, BertLayerNorm, BertForMaskedLM, BertConfigのインポート失敗

インポート先を変更しました。
BertLayerNormは、torch.nn.LayerNormで書けるようになりました。

from transformers.modeling_bert import ACT2FN, BertLayerNorm
from transformers.modeling_bert import BertForMaskedLM
from transformers.configuration_bert import BertConfig

from transformers.activations import ACT2FN
from transformers import BertForMaskedLM
from transformers import BertConfig

Trainerクラスの諸々

trainer.train()の引数にmodel_pathが使えなくなり、resume_from_checkpointが追加されました。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?