上述代码报错:Using TensorFlow backend. 2023-06-02 17:07:57.266774: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2023-06-02 17:07:57.267572: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. Loading data... Traceback (most recent call last): File "C:/Users/85448/PycharmProjects/LightGBM_text/deep2.py", line 12, in <module> (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features) File "C:\Users\85448\anaconda3\envs\tensorflow37\lib\site-packages\keras\datasets\imdb.py", line 54, in load_data x_train, labels_train = f['x_train'], f['y_train'] File "C:\Users\85448\anaconda3\envs\tensorflow37\lib\site-packages\numpy\lib\npyio.py", line 256, in __getitem__ pickle_kwargs=self.pickle_kwargs) File "C:\Users\85448\anaconda3\envs\tensorflow37\lib\site-packages\numpy\lib\format.py", line 743, in read_array raise ValueError("Object arrays cannot be loaded when " ValueError: Object arrays cannot be loaded when allow_pickle=False
时间: 2023-08-28 13:05:17 浏览: 183
这个错误看起来像是 numpy 版本不兼容导致的问题。您可以试着升级 numpy 版本,或者添加一行代码来解决这个问题:
```
import numpy as np
np_load_old = np.load
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
```
然后在加载IMDB数据集之前添加以上代码即可,例如:
```python
import numpy as np
np_load_old = np.load
np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)
from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Embedding, LSTM
# 加载数据集
max_features = 20000
maxlen = 80
batch_size = 32
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=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)
# 构建模型
model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
# 训练模型
print('Train...')
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=15,
validation_data=(x_test, y_test))
# 评估模型
score, acc = model.evaluate(x_test, y_test,
batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)
np.load = np_load_old # 恢复原来的 numpy.load 方法
```
如果还有问题,请随时告诉我。
阅读全文