一、错误问题
前两天帮忙看一个错误,是pytorch训练算法,这个错误我也是第一次发现,报错信息如下:
RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.
运行错误:第二次尝试向后遍历图形(或在已释放的张量后直接访问已保存的张量)。调用时,图形的已保存中间值将被释放。向后()或自动标记。梯度()。如果需要再次向后浏览图形,或者如果需要在向后调用后访问保存的张量,请指定retain_graph=True。
错误的原因是:俗话就是,修改代码后,在使用torch训练中反向传播梯度的时候,中间的梯度值不存在。
二、方法总结
我在网上也查了相关的问题,各个博主也给了出现这个错误的理由以及要解决这个错误的方法,
总结了一下,
①:是在调用bachward()或者autograd.grad()函数时,指定retain_graph =