0
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 3 years have passed since last update.

Keras の imdb.load_data で ValueError が起きた際の解決法

Posted at

発端

以下の様なコードで読み込みを行おうとした。

from keras.datasets import imdb

(X_train,y_train),(X_test,y_test) = imdb.load_data(num_words=10000)

すると、以下のエラーが発生

ValueError                                Traceback (most recent call last)
<ipython-input-203-2fc6b409cd07> in <module>
      5 #np_load_old = np.load
      6 #np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
----> 7 (X_train,y_train),(X_test,y_test) = imdb.load_data(num_words=10000)
      8 #np.load = np_load_old
      9 
/opt/anaconda3/lib/python3.7/site-packages/keras/datasets/imdb.py in load_data(path, num_words, skip_top, maxlen, seed, start_char, oov_char, index_from, **kwargs)
     57                     file_hash='599dadb1135973df5b59232a0e9a887c')
     58     with np.load(path) as f:
---> 59         x_train, labels_train = f['x_train'], f['y_train']
     60         x_test, labels_test = f['x_test'], f['y_test']
     61 
/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/npyio.py in __getitem__(self, key)
    260                 return format.read_array(bytes,
    261                                          allow_pickle=self.allow_pickle,
--> 262                                          pickle_kwargs=self.pickle_kwargs)
    263             else:
    264                 return self.zip.read(key)
/opt/anaconda3/lib/python3.7/site-packages/numpy/lib/format.py in read_array(fp, allow_pickle, pickle_kwargs)
    720         # The array contained Python objects. We need to unpickle the data.
    721         if not allow_pickle:
--> 722             raise ValueError("Object arrays cannot be loaded when "
    723                              "allow_pickle=False")
    724         if pickle_kwargs is None:
ValueError: Object arrays cannot be loaded when allow_pickle=False

原因

numpyバージョンアップ の際に allow_pickle=False がデフォルトになってしまったことが原因の模様。

解消法

numpyのバージョンを下げることでも対応可能だが、面倒なのでコード上で解決を図る。

具体的には、以下のコードに置き換えることで解消。

# 頻度順位10000語までを指定
from keras.datasets import imdb

np_load_old = np.load
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
(X_train,y_train),(X_test,y_test) = imdb.load_data(num_words=10000)
np.load = np_load_old

np.load にて、 allow_pickle=True を指定する様に一時的に上書きしている。 imdb.load_data が完了したら、 np.loadの定義を元に戻すことで、元通り。

参考

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