python - 如何修复 imdb.load_data() 函数的 'Object arrays cannot be loaded when allow_pickle=False'?

标签 python numpy keras

我正在尝试使用 Google Colab 中的 IMDb 数据集来实现二进制分类示例。我以前实现过这个模型。但是当我几天后再次尝试执行此操作时,它返回了 value 错误:'Object arrays cannot be load when allow_pickle=False' for the load_data() function。

我已经尝试过解决这个问题,引用了类似问题的现有答案:How to fix 'Object arrays cannot be loaded when allow_pickle=False' in the sketch_rnn algorithm . 但事实证明,仅仅添加一个 allow_pickle 参数是不够的。

我的代码:

from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-1-2ab3902db485> in <module>()
      1 from keras.datasets import imdb
----> 2 (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

2 frames
/usr/local/lib/python3.6/dist-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 

/usr/local/lib/python3.6/dist-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)

/usr/local/lib/python3.6/dist-packages/numpy/lib/format.py in read_array(fp, allow_pickle, pickle_kwargs)
    690         # The array contained Python objects. We need to unpickle the data.
    691         if not allow_pickle:
--> 692             raise ValueError("Object arrays cannot be loaded when "
    693                              "allow_pickle=False")
    694         if pickle_kwargs is None:

ValueError: Object arrays cannot be loaded when allow_pickle=False

最佳答案

这是强制 imdb.load_data 允许 pickle 的技巧,方法是在您的笔记本中替换此行:

(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

通过这个:

import numpy as np
# save np.load
np_load_old = np.load

# modify the default parameters of np.load
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)

# call load_data with allow_pickle implicitly set to true
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)

# restore np.load for future normal usage
np.load = np_load_old

关于python - 如何修复 imdb.load_data() 函数的 'Object arrays cannot be loaded when allow_pickle=False'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55890813/

相关文章:

python - 使用千位分隔符格式化多个数据框列

python - 字典中的 NumPy 切片表示法

Keras 使用掩码卡住特定权重

python - 在文件中查找和替换字符串(MAC 地址)不起作用

Python:如何将 unixtimestamp 和 timezone 转换为 datetime 对象?

python - 在Python中标准化点积等于1的向量?

python - 每个张量组的 Keras 自定义损失函数

python - 如何在交叉验证中获得 Keras scikit-learn 包装器的训练和验证损失?

python - 如何使用 virtualenv 设置环境变量?

python - 根据 Pandas 数据框中2列的数据计算值