【发布时间】:2020-11-13 14:51:13
【问题描述】:
以下两个循环将使用内存,直到我用完为止,但我不知道为什么。我在每次迭代结束时删除所有创建的变量,但它仍然泄漏。
!pip3 install cupy-cuda101
import cupy as cp
import numpy as np
from sklearn.preprocessing import PolynomialFeatures
xtrain = cp.asnumpy(cp.random.uniform(-1,1,size = (150000,50)))
for i in range(0,1000):
weights = cp.random.uniform(-1,1,size = (1275,1000))
for chunk in range(0,xtrain.shape[0],5000):
xchunk = xtrain[chunk:chunk+5000,:]
poly=PolynomialFeatures(interaction_only = True, include_bias = False)
xchunk = cp.array(poly.fit_transform(xchunk))
ranks = cp.matmul(xchunk,weights)
del ranks, xchunk, poly
del weights
xtrain 也是浮点数据,介于 -1 和 1 之间。
【问题讨论】:
-
你所有的
del语句都是毫无意义的,因为这些变量在每次迭代时都会重新分配。 -
没错,那么每次迭代后它如何使用越来越多的内存?
-
不知道。您必须提供 minimal reproducible example 才能真正期望任何人都能够诊断出类似 taht 的问题
-
好的,现在可以重现了
-
您能否详细说明您认为存在“泄漏”的原因?另请注意,CuPy 默认使用内存池:docs.cupy.dev/en/latest/reference/memory.html
标签: python for-loop memory-leaks cupy