How to fix 'Object arrays cannot be loaded when allow_pickle=False' for imdb.load_data() function?

后端 未结 20 2061
花落未央
花落未央 2020-12-04 11:28

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

相关标签:
20条回答
  • 2020-12-04 11:41

    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.

    !pip install numpy==1.16.1
    import numpy as np
    

    This version of numpy has the default value of allow_pickle as True.

    0 讨论(0)
  • 2020-12-04 11:42

    Following this issue on 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:

    -  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).

    0 讨论(0)
  • 2020-12-04 11:46

    You can try changing the flag's value

    np.load(training_image_names_array,allow_pickle=True)
    
    0 讨论(0)
  • 2020-12-04 11:46

    I landed up here, tried your ways and could not figure out.

    I was actually working on a pregiven code where

    pickle.load(path)
    

    was used so i replaced it with

    np.load(path, allow_pickle=True)
    
    0 讨论(0)
  • 2020-12-04 11:46

    Its work for me

            np_load_old = np.load
            np.load = lambda *a: np_load_old(*a, allow_pickle=True)
            (x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=None, test_split=0.2)
            np.load = np_load_old
    
    0 讨论(0)
  • 2020-12-04 11:46

    The answer of @cheez sometime doesn't work and recursively call the function again and again. To solve this problem you should copy the function deeply. You can do this by using the function partial, so the final code is:

    import numpy as np
    from functools import partial
    
    # save np.load
    np_load_old = partial(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
    
    0 讨论(0)
提交回复
热议问题