Python 如何修复 imdb.load_data() 函数的“当 allow_pickle=False”时无法加载对象数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55890813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix 'Object arrays cannot be loaded when allow_pickle=False' for imdb.load_data() function?
提问by Kanad
I'm trying to implement the binary classification example using the IMDb dataset in Google Colab. I have implemented this model before. But when I tried to do it again after a few days, it returned a value error:'Object arrays cannot be loaded when allow_pickle=False' for the load_data() function.
我正在尝试使用Google Colab 中的 IMDb 数据集实现二进制分类示例。我之前已经实现了这个模型。但是,当我几天后再次尝试执行此操作时,它返回了一个值错误:load_data() 函数的“当 allow_pickle=False 时无法加载对象数组”。
I have already tried solving this, referring to an existing answer for a similar problem: How to fix 'Object arrays cannot be loaded when allow_pickle=False' in the sketch_rnn algorithmBut turns out that just adding an allow_pickle argument isn't sufficient.
我已经尝试解决这个问题,参考了一个类似问题的现有答案:How to fix 'Object arrays cannot be loaded when allow_pickle=False' in the sketch_rnn algorithm但结果证明只添加一个allow_pickle参数是不够的。
My code:
我的代码:
from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
The error:
错误:
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
回答by cheez
Here's a trick to force imdb.load_data
to allow pickle by, in your notebook, replacing this line:
这是强制imdb.load_data
允许泡菜的技巧,在您的笔记本中,替换此行:
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
by this:
这样:
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
回答by Tirth Patel
This issue is still up on keras git. I hope it gets solved as soon as possible. Until then, try downgrading your numpy version to 1.16.2. It seems to solve the problem.
这个问题仍然存在于 keras git 上。我希望它尽快得到解决。在此之前,尝试将您的 numpy 版本降级到 1.16.2。它似乎解决了问题。
!pip install numpy==1.16.1
import numpy as np
This version of numpy has the default value of allow_pickle
as True
.
此版本的 numpy 具有默认值allow_pickle
as True
。
回答by MappaGnosis
Following this issueon GitHub, the official solution is to edit the imdb.py file. This fix worked well for me without the need to downgrade numpy. Find the imdb.py file at tensorflow/python/keras/datasets/imdb.py
(full path for me was: C:\Anaconda\Lib\site-packages\tensorflow\python\keras\datasets\imdb.py
- other installs will be different) and change line 85 as per the diff:
按照GitHub上的这个问题,官方的解决方法是编辑imdb.py文件。此修复程序对我来说效果很好,无需降级 numpy。找到 imdb.py 文件tensorflow/python/keras/datasets/imdb.py
(我的完整路径是:C:\Anaconda\Lib\site-packages\tensorflow\python\keras\datasets\imdb.py
- 其他安装会有所不同)并根据差异更改第 85 行:
- with np.load(path) as f:
+ with np.load(path, allow_pickle=True) as f:
The reason for the change is security to prevent the Python equivalent of an SQL injection in a pickled file. The change above will ONLY effect the imdb data and you therefore retain the security elsewhere (by not downgrading numpy).
更改的原因是为了防止在腌制文件中进行 SQL 注入的 Python 等价物的安全性。上面的更改只会影响 imdb 数据,因此您可以在其他地方保留安全性(通过不降级 numpy)。
回答by Madhuparna Bhowmik
I just used allow_pickle = True as an argument to np.load() and it worked for me.
我只是使用 allow_pickle = True 作为 np.load() 的参数,它对我有用。
回答by travs15
In my case worked with:
在我的情况下与:
np.load(path, allow_pickle=True)
回答by Gustavo Mirapalheta
I think the answer from cheez (https://stackoverflow.com/users/122933/cheez) is the easiest and most effective one. I'd elaborate a little bit over it so it would not modify a numpy function for the whole session period.
我认为 cheez ( https://stackoverflow.com/users/122933/cheez)的答案是最简单、最有效的答案。我会详细说明一下,这样它就不会在整个会话期间修改 numpy 函数。
My suggestion is below. I′m using it to download the reuters dataset from keras which is showing the same kind of error:
我的建议如下。我正在使用它从 keras 下载 reuters 数据集,该数据集显示相同类型的错误:
old = np.load
np.load = lambda *a,**k: old(*a,**k,allow_pickle=True)
from keras.datasets import reuters
(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)
np.load = old
del(old)
回答by Brayan Armando Yaquian Gonzale
You can try changing the flag's value
您可以尝试更改标志的值
np.load(training_image_names_array,allow_pickle=True)
回答by farid khafizov
none of the above listed solutions worked for me: i run anaconda with python 3.7.3. What worked for me was
上面列出的解决方案都不适合我:我使用 python 3.7.3 运行 anaconda。对我有用的是
run "conda install numpy==1.16.1" from Anaconda powershell
close and reopen the notebook
从 Anaconda powershell 运行“conda install numpy==1.16.1”
关闭并重新打开笔记本
回答by Yasser Albarbay
on jupyter notebook using
在 jupyter 笔记本上使用
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)
worked fine, but the problem appears when you use this method in spyder(you have to restart the kernel every time or you will get an error like:
工作正常,但是在 spyder 中使用此方法时会出现问题(每次都必须重新启动内核,否则会出现如下错误:
TypeError : () got multiple values for keyword argument 'allow_pickle'
TypeError : () 为关键字参数“allow_pickle”获得了多个值
I solved this issue using the solution here:
我使用这里的解决方案解决了这个问题:
回答by Leonhard Rathnak
find the path to imdb.py then just add the flag to np.load(path,...flag...)
找到 imdb.py 的路径,然后将标志添加到 np.load(path,...flag...)
def load_data(.......):
.......................................
.......................................
- with np.load(path) as f:
+ with np.load(path,allow_pickle=True) as f: