Memory growth with broadcast operations in NumPy

后端 未结 2 977
独厮守ぢ
独厮守ぢ 2020-12-29 11:47

I am using NumPy to handle some large data matrices (of around ~50GB in size). The machine where I am running this code has 128GB of RAM so doing simple linear operations of

2条回答
  •  臣服心动
    2020-12-29 12:39

    @rth's suggestion to do the operation in smaller batches is a good one. You could also try using the function np.subtract and give it the destination array to avoid creating an addtional temporary array. I also think you don't need to index c as c[np.newaxis, :, :], because it is already a 3-d array.

    So instead of

    a[:] = b[:, :, np.newaxis] - c[np.newaxis, :, :] # memory explodes here
    

    try

    np.subtract(b[:, :, np.newaxis], c, a)
    

    The third argument of np.subtract is the destination array.

提交回复
热议问题