如何更改此功能以使其更有效?我不断收到MemoryError
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)
x_test = vectorize_sequences(test_data)
训练和测试数据是用于情感分析的IMDB数据集,即
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
编辑:我正在具有4 GB RAM的64位Ubuntu系统上运行此程序。
这是回溯:
Traceback (most recent call last):
File "/home/uttam/PycharmProjects/IMDB/imdb.py", line 29, in <module>
x_test = vectorize_sequences(test_data)
File "/home/uttam/PycharmProjects/IMDB/imdb.py", line 20, in vectorize_sequences
results = np.zeros((len(sequences), dimension))
MemoryError
答案 0 :(得分:2)
您的数组似乎是10k x 10k,这是1亿个元素,每个元素64位(因为默认dtype为float64)。这就是8亿字节,也就是763兆字节。
如果使用float32,它将把内存使用量减少一半:
np.zeros((len(sequences), dimension), dtype=np.float32)
或者,如果您只关心0和1,则会将其减少88%:
np.zeros((len(sequences), dimension), dtype=np.int8)