from keras.datasets import imdb
from keras.preprocessing import sequence
max_features = 10000
# Number of words to consider as features(作为特征的单词个数)
max_len = 500
# cut texts after this number of words (among top max_features most common words)
# 在这么多单词之后截断文本(这些单词都属于前 max_features 个最常见的单词)
print('Loading data...')
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')
print('Pad sequences (samples x time)')
x_train = sequence.pad_sequences(x_train, maxlen=max_len)
x_test = sequence.pad_sequences(x_test, maxlen=max_len)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)
2. 解决方法
参考链接:Object arrays cannot be loaded when allow_pickle=False
① 首先找到该运行环境envs下的目录→Lib→site-packages→keras→datasets→imdb.py
比如说我的这个python37运行环境,最后就在:
D:AnacondaDonenvspython37Libsite-packageskerasdatasetsimdb.py
②打开imdb.py,找到with np.load这行,将np.load(path)改成np.load(path,allow_pickle=True),保存!
③ 重启Jupyter内核!一定要重启!然后重新运行该段代码!就没问题了!
最后运行的结果:
Using TensorFlow backend. Loading data... 25000 train sequences 25000 test sequences Pad sequences (samples x time) x_train shape: (25000, 500) x_test shape: (25000, 500)



