6
2

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.

【Transformer】tokenizerでNoneType エラーが出た場合

Last updated at Posted at 2022-04-17

環境

  • Gradient
  • PyTorch 1.11

エラー内容

以下のようなコードを実行していると、4ブロック目の2行目 tokenizer = T5Tokenizer.from_pretrained("rinna/japanese-gpt2-medium") の部分で TypeError が出た。

ipynb
!pip install transformers
ipynb
from transformers import pipeline, set_seed
generator = pipeline('text-generation', model='gpt2')
set_seed(42)
generator("Hello, I'm a language model,", max_length=30, num_return_sequences=5)
ipynb
!pip install sentencepiece
ipynb
from transformers import T5Tokenizer, AutoModelForCausalLM
tokenizer = T5Tokenizer.from_pretrained("rinna/japanese-gpt2-medium")
tokenizer.do_lower_case = True  # due to some bug of tokenizer config loading
model = AutoModelForCausalLM.from_pretrained("rinna/japanese-gpt2-medium")
エラー
TypeError: 'NoneType' object is not callable

原因

Stack Overflow で似たような質問があった。それに対する回答を一部引用する。

So I open a brand new colab session, and install everything including the SentencePiece before creating tokenizer, and this time it worked. The Nonetype error simply means it doesn't know what is 'albert-base-v2'. However if you install the packages in right order colab will recognize better the relationship between AlbertTokenizer and SentencePiece. In short for this to work in colab 0. Open a new Colab session 1. Install Transformers and SentencePiece 2. import AlbertTokenizer 3.create tokenizer.(MeiNan Zhu)

MeiNan Zhu's answer is correct.
Installing or importing SentencePiece before transformers works.(Mayank Soni)

コードの順番に問題があったようだ。

解決

transformes が機能する前に sentencepiece をインストールすればよい。
つまり、最初に transformes, sentencepiece をインストールすることで解決する。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?