CPU Dynet 内存分配失败
CPU memory allocation failed in Dynet
我不确定为什么 运行 内存不足。走parser of Goldberg, all I do is to change this行:
scores, exprs = self.__evaluate(conll_sentence, True)
并在其周围添加一个 for 循环以重复它 K 次:
for k in xrange(K):
scores, exprs = self.__evaluate(conll_sentence, True)
# do something
然后在 getExpr 中,我执行以下操作:
samples_out = np.random.normal(0,0.001, (1, self.hidden_units))
samples_FOH = np.random.normal(0,0.001,(self.hidden_units, self.ldims * 2))
samples_FOM = np.random.normal(0,0.001,(self.hidden_units, self.ldims * 2))
samples_Bias = np.random.normal(0,0.001, (self.hidden_units))
XoutLayer = self.outLayer.expr()+inputTensor(samples_out)
XhidLayerFOH = self.hidLayerFOH.expr()+inputTensor(samples_FOH)
XhidLayerFOM = self.hidLayerFOM.expr()+inputTensor(samples_FOM)
XhidBias = self.hidBias.expr()+inputTensor(samples_Bias)
if sentence[i].headfov is None:
sentence[i].headfov = XhidLayerFOH * concatenate([sentence[i].lstms[0], sentence[i].lstms[1]])
if sentence[j].modfov is None:
sentence[j].modfov = XhidLayerFOM * concatenate([sentence[j].lstms[0], sentence[j].lstms[1]])
output = XoutLayer * self.activation(sentence[i].headfov + sentence[j].modfov + XhidBias)
return output
本质上,上述块中发生的事情是首先生成正态分布的噪声,然后将其添加到训练值中。但似乎在某个地方,所有生成的值都保留在内存中,只是内存不足。有人知道为什么吗?
Dynet 表达式保留在内存中,直到下一次调用 renew_cg()
。
因此解决方法是在循环的每次迭代后调用它,前提是您已从计算图中检索到所需的所有信息。
旁注:当你做一个简单的加法时,例如:
XoutLayer = self.outLayer.expr()+inputTensor(samples_out)
实际上没有执行任何添加。您只需创建一个新表达式并指定如何从其他表达式对其求值。当在 XoutLayer
或计算依赖于 XoutLayer
的表达式上调用 .forward()
(或 .value()
等)时,将执行实际计算。
所以,dynet需要为当前计算图中的所有表达式分配内存。
我不确定为什么 运行 内存不足。走parser of Goldberg, all I do is to change this行:
scores, exprs = self.__evaluate(conll_sentence, True)
并在其周围添加一个 for 循环以重复它 K 次:
for k in xrange(K):
scores, exprs = self.__evaluate(conll_sentence, True)
# do something
然后在 getExpr 中,我执行以下操作:
samples_out = np.random.normal(0,0.001, (1, self.hidden_units))
samples_FOH = np.random.normal(0,0.001,(self.hidden_units, self.ldims * 2))
samples_FOM = np.random.normal(0,0.001,(self.hidden_units, self.ldims * 2))
samples_Bias = np.random.normal(0,0.001, (self.hidden_units))
XoutLayer = self.outLayer.expr()+inputTensor(samples_out)
XhidLayerFOH = self.hidLayerFOH.expr()+inputTensor(samples_FOH)
XhidLayerFOM = self.hidLayerFOM.expr()+inputTensor(samples_FOM)
XhidBias = self.hidBias.expr()+inputTensor(samples_Bias)
if sentence[i].headfov is None:
sentence[i].headfov = XhidLayerFOH * concatenate([sentence[i].lstms[0], sentence[i].lstms[1]])
if sentence[j].modfov is None:
sentence[j].modfov = XhidLayerFOM * concatenate([sentence[j].lstms[0], sentence[j].lstms[1]])
output = XoutLayer * self.activation(sentence[i].headfov + sentence[j].modfov + XhidBias)
return output
本质上,上述块中发生的事情是首先生成正态分布的噪声,然后将其添加到训练值中。但似乎在某个地方,所有生成的值都保留在内存中,只是内存不足。有人知道为什么吗?
Dynet 表达式保留在内存中,直到下一次调用 renew_cg()
。
因此解决方法是在循环的每次迭代后调用它,前提是您已从计算图中检索到所需的所有信息。
旁注:当你做一个简单的加法时,例如:
XoutLayer = self.outLayer.expr()+inputTensor(samples_out)
实际上没有执行任何添加。您只需创建一个新表达式并指定如何从其他表达式对其求值。当在 XoutLayer
或计算依赖于 XoutLayer
的表达式上调用 .forward()
(或 .value()
等)时,将执行实际计算。
所以,dynet需要为当前计算图中的所有表达式分配内存。