LoginSignup
0
1

More than 3 years have passed since last update.

transformersからTFGPT2LMHeadModelをインポートできない(cannot import name 'TFGPT2LMHeadModel' from 'transformers')件について

Posted at

はじめに

最近技術調査でHuggingFaceのサンプルコードを動かしているのですが、なかなか動かないことが多く(公式のものでも)、今回は動くようにするところまでできたので、まとめたいと思います。

問題

こちらの記事を参考にハンズオンをGoogle Colabratory`で行っていたところ、

import tensorflow as tf
from transformers import TFGPT2LMHeadModel, GPT2Tokenizer


tokenizer = GPT2Tokenizer.from_pretrained("gpt2")

# 警告を避けるために、EOS トークンを PAD トークンとして追加
model = TFGPT2LMHeadModel.from_pretrained("gpt2", pad_token_id=tokenizer.eos_token_id)

この最初の箇所で以下のようなエラーが発生しました。

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-23-4d671ac52c7c> in <module>()
      1 import tensorflow as tf
----> 2 from transformers import TFGPT2LMHeadModel, GPT2Tokenizer
      3 
      4 
      5 tokenizer = GPT2Tokenizer.from_pretrained("gpt2")

ImportError: cannot import name 'TFGPT2LMHeadModel' from 'transformers' (unknown location)

また、いくつかのサイトをみたところtensorflow==2.0.1を入れると使えるよというissueがあったが動きませんでした。

原因

同様の報告がバグとして報告され、installationの記事が追加されたようです。
https://github.com/huggingface/transformers/issues/3396

This repository is tested on Python 3.6+, PyTorch 1.0.0+ (PyTorch 1.3.1+ for examples) and TensorFlow 2.0.

You should install 🤗 Transformers in a virtual environment. If you're unfamiliar with Python virtual environments, check out the user guide.

他のライブラリが影響しているようでColabratoryのようなライブラリがたくさんある環境では、ほかのライブラリの影響のせいでimportできないらしいです。

詳しい原因はまだわかっていないが、自分で作成した環境で実行したところ記事通りのコードで動作した。

おわりに

FuggingFaceを理解するためにとりあえずColabで動かすという流れをしていますが、Colabで動かないことがあることがよくわかりました。引き続きいろいろ試してHuggingFaceを理解したいと思います。

参考サイト

https://note.com/npaka/n/n5d296d8ae26d

https://github.com/huggingface/transformers/issues/3396

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