我正在尝试遵循this教程,以学习有关使用keras进行深度学习的知识,但是我不断遇到MemoryError。您能指出是什么原因造成的,以及如何护理它吗?
代码如下:
import numpy as np
from keras import models, regularizers, layers
from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
def vectorize_sequences(sequences, dimension=10000):
results = np.zeros((len(sequences), dimension))
for i, sequence in enumerate(sequences):
results[i, sequence] = 1.
return results
x_train = vectorize_sequences(train_data)
这是回溯(行号与上述代码中的行号不匹配)
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/uttam/pycharm-2018.2.4/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/home/uttam/pycharm-2018.2.4/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/uttam/PycharmProjects/IMDB/imdb.py", line 33, in <module>
x_train = vectorize_sequences(train_data)
File "/home/uttam/PycharmProjects/IMDB/imdb.py", line 27, in vectorize_sequences
results = np.zeros((len(sequences), dimension))
MemoryError
答案 0 :(得分:1)
是的,您是正确的。问题确实来自vectorize_sequences
。
您应该分批执行此逻辑(使用像partial_x_train
这样的切片数据)或使用生成器(here是一个很好的解释和示例)。
我希望这会有所帮助:)