【发布时间】:2018-03-28 05:31:17
【问题描述】:
我正在浏览neural transfer pytorch tutorial,对retain_variable(已弃用,现在称为retain_graph)的使用感到困惑。代码示例显示:
class ContentLoss(nn.Module):
def __init__(self, target, weight):
super(ContentLoss, self).__init__()
self.target = target.detach() * weight
self.weight = weight
self.criterion = nn.MSELoss()
def forward(self, input):
self.loss = self.criterion(input * self.weight, self.target)
self.output = input
return self.output
def backward(self, retain_variables=True):
#Why is retain_variables True??
self.loss.backward(retain_variables=retain_variables)
return self.loss
retain_graph (bool, optional) – 如果为 False,则用于计算的图 毕业生将被释放。请注意,几乎在所有情况下都设置此 不需要 True 选项,通常可以在很长时间内解决 更有效的方式。默认为 create_graph 的值。
因此,通过设置retain_graph= True,我们不会释放为向后传递的图形分配的内存。保留这个内存有什么好处,我们为什么需要它?
【问题讨论】:
标签: neural-network conv-neural-network backpropagation pytorch automatic-differentiation