我有以下Python代码:
for i in range(0, len(oscillations) - sequence_length):
a = patterns[i:i + sequence_length]
b = oscillations[i:i + sequence_length]
sequence_in = [a+b for a,b in zip(a,b)]
sequence_out = oscillations[i + sequence_length]
network_input.append(sequence_in)
network_output.append(sequence_out)
振荡的长度是212922。每个振荡元素的长度是25。模式的长度是完全相同的。两个列表的结构相同,但数据不同
上面的代码失败了,它给了我一个内存错误。有时在遍历循环时,有时在返回两个列表时
如果我把列表缩短到大约100000个元素,那么它就可以工作了
我知道这可能是我试图分配太多的内存,但我的问题是,是否有一种更聪明的方法来浏览我的列表,这样它就不需要分配那么多内存了
正如一些评论者指出的,您可能不需要构建整个列表
network_input
和network_output
。内存消耗的最大改进是yield
:另一方面,通过注意多次对两个集合中的相同元素进行切片和求和,可以获得较小的改进
a
和b
在迭代之间只相差一个元素。您可以使用一个简单的移动和算法:相关问题 更多 >
编程相关推荐