【问题标题】:Numpy/Scipy: Memory consumption increases linearly per iterationNumpy/Scipy:每次迭代的内存消耗线性增加
【发布时间】:2019-10-03 10:28:56
【问题描述】:

运行以下代码时:

import numpy as np
import scipy.sparse
import time

def test():
    m = 10000
    n = 10000 
    for i in range(5):
        A = scipy.sparse.random(m, n, density=0.1, format='csr')
        x = np.random.randn(n)
        Ax = A.dot(x)
        time.sleep(2)

if __name__ == "__main__":
    test()

我观察到内存消耗线性增加到 >4.8Gb!

我用以下函数再次测试:

def test2():
    m = 10000
    n = 10000
    for i in range(5):
        print(i)
        A = np.random.rand(m, n)
        x = np.random.randn(A.shape[1])
        Ax = A.dot(x)
        time.sleep(2)

内存消耗线性增加至>800Mb。

我有两个问题:

  1. 为什么在每种情况下内存消耗都会线性增加?每次迭代都没有声明新变量...

  2. 考虑到矩阵是稀疏的(只有 0.1 密度),为什么在第一个测试中内存消耗比第二个高得多?

提前感谢您的回答!

【问题讨论】:

    标签: python numpy scipy


    【解决方案1】:

    1. 因为你所有的变量声明都在一个un-nested for...loop中,所以它们在每次迭代中重新运行,每一行都会对总内存使用量贡献不同的线性增量(O(n))。此外,由于在迭代后没有像delete operation 这样的释放内存的操作,因此内存使用量从先前的迭代中增加。 下面是def test()的内存配置文件的结果

    |Line|  |Mem usage|    |Increment|   |Line Contents|
    -----------------------------------------------------
         5   1844.9 MiB   1844.9 MiB   def test():
         6   1844.9 MiB      0.0 MiB       m = 10000
         7   1844.9 MiB      0.0 MiB       n = 10000 
         8   4518.7 MiB      0.0 MiB       for i in range(5):
         9   4518.7 MiB    763.7 MiB           A = scipy.sparse.random(m, n, density=0.1, format='csr')
        10   4518.7 MiB      0.0 MiB           x = np.random.randn(n)
        11   4518.7 MiB      0.0 MiB           Ax = A.dot(x)
        12   4518.7 MiB      0.0 MiB           time.sleep(2)
    
    1. 我将这两个函数占用的内存差异归因于 numpy 数据结构比 scipy 更优化的事实。即它们占用的空间更少。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2021-03-10
      • 1970-01-01
      相关资源
      最近更新 更多