Feature
Better error logging for inplace operations that throw errors in automatic differentiation.
Motivation
In complex computational graphs, locating the operation causing the error is nontrivial. A feature for isolating the offending operation would save a lot of developer time.
Pitch
See
here
for a more concrete suggestion. To quote:
I wonder whether it might be worth adding a "debug" modus that records the stack of the op in the forward pass and spits it out on error in the backward. That way, it would point to the right line of code directly.
Alternatives
Just being able to log all inplace operations would be useful.
Question: can't you use the
anomaly_detection
for that?
Here is an example where it shows up the exact line where the error occurs:
import torch
with torch.autograd.set_detect_anomaly(True):
a = torch.rand(1, requires_grad=True)
c = torch.rand(1, requires_grad=True)
b = a ** 2 * c ** 2
b += 1
b *= c + a
d = b.exp_()
d *= 5
b.backward()
And the stack trace:
sys:1: RuntimeWarning: Traceback of forward call that caused the error:
File "tst.py", line 13, in <module>
d = b.exp_()
Traceback (most recent call last):
File "tst.py", line 16, in <module>
b.backward()
File "/Users/fmassa/anaconda3/lib/python3.6/site-packages/torch/tensor.py", line 102, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/Users/fmassa/anaconda3/lib/python3.6/site-packages/torch/autograd/__init__.py", line 93, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation
which points directly to b.exp_(), and indeed, if you change that line to be b.exp(), it all works fine.
Closing as this can be obtained with
anomaly_detection
.
Great, thanks. This might be a good thing to add to the
section on in-place operations in the autograd mechanics tutorial
. I'm happy to add a sentence or two, but the
contributing doc
doesn't mention how to modify tutorials or notes.
which points directly to
b.exp_()
, and indeed, if you change that line to be
b.exp()
, it all works fine.
To clarify for other readers, the anomaly detection will
not
necessarily point you at the inplace operation that caused the failure. Instead, it will point you at the operation that could not compute its gradient in the backward pass. The inplace operation to blame may occur anywhere after that, modifying one of the tensors that participated in the line found by the anomaly detection.
Example:
x = torch.rand(10, 20, requires_grad=True)
y = torch.rand(10)
z = (x / y[:, np.newaxis]) # anomaly detection will point here
c = y.abs_() # but the problem is here
z.sum().backward()
The last line will cause a RuntimeError. With anomaly detection enabled, it will point at the line performing the division, but the inplace operation came later.
FeatureBetter error logging for inplace operations that throw errors in automatic differentiation.MotivationIn complex computational graphs, locating the operation causing the error is nontrivial. A feature for isolating the offending operation would
1......torch/autograd/__init__.py", line 90, in back
war
d
allow
_un
reac
hable
=
True
) #
allow
_un
reac
hable
f
lag
Runtime
Error
: one of the variables needed for g...
Runtime
Error
: Trying to back
war
d through the graph a second time, but the buffers have already been freed. Specify retain_graph=
True
when calling back
war
d the first time.
在pytorch神经网络迁移的官方教程中有这样一个损失层函数(具体看这里提供0.3.0版中文链接:https://oldpan.me/archives/pyto
1.Variable是神经网络中特有的一个概念,提供了自动求导的功能,在numpy中没有该功能,存在与torch.autograd.Variable().
通过data可以取出Variable中的tensor数值,grad_fn表示的是得到这个Variable的操作,比如是通过加减还是乘除得来的,最后grad是这个Variable的反向传播梯度。
对于标量的求导
x = Variable(...
前言:当我们再训练网络的时候可能希望保持一部分的网络参数不变,只对其中一部分的参数进行调整;或者值训练部分分支网络,并不让其梯度对主网络的梯度造成影响,这时候我们就需要使用detach()函数来切断一些分支的反向传播
一、tensor.detach()
返回一个新的tensor,从当前计算图中分离下来的,但是仍指向原变量的存放位置,不同之处只是requires_grad为false,得...
创建一个Tensor时,使用requires_grad参数指定是否记录对其的操作,以便之后利用back
war
d()方法进行梯度求解。
一个Tensor的requires_grad成员保存该Tensor是否记录操作用于计算梯度。
可利用requires_grad_()方法修改Tenso...
1......torch/autograd/__init__.py", line 90, in back
war
d
allow
_un
reac
hable
=
True
) #
allow
_un
reac
hable
f
lag
Runtime
Error
: one of the variables needed for gradient computation has been
modified
by an i...
转载自:https://www.cnblogs.com/wanghui-garcia/p/10677071.html,本文只做个人记录学习使用,版权归原作者所有。
当我们在训练网络的时候可能希望保持一部分的网络参数不变,只对其中一部分的参数进行调整;或者只训练部分分支网络,并不让其梯度对主网络的梯度造成影响,这时候我们就需要使用detach()函数来切断一些分支的反向传播。
1 detach(...
调试工具是计算机程序员必需的工具之一,特别是用于Windows系统的程序开发。在Windows系统上,调试工具的选择非常广泛,但是本文主要介绍针对Windows 10 x64体系结构的调试工具。
1. WinDbg
WinDbg是微软开发的多功能调试器,支持应用程序和系统级别的调试。它还具有高级调试功能,包括源代码级级调试和远程调试。WinDbg支持使用命令行和图形界面进行操作。它还可以使用扩展插件进行扩展。
2. Visual Studio
Visual Studio是一款强大的开发工具集,包括开发环境、调试器和编译器等。Visual Studio支持多语言和多平台开发,并具备高级调试功能。它还具有集成式开发环境,提供丰富的工具和插件。Visual Studio还支持Windows应用商店的应用程序开发。
3. Process Explorer
Process Explorer是一款用于显示系统进程、服务和其它系统对象的实时进程监视工具。Process Explorer可以显示进程的详细信息,并提供高级的进程监视功能,例如堆栈跟踪和线程列表。它是Microsoft官方支持的工具。
4.
Debugging
Tools for Windows
Debugging
Tools for Windows(Windbg,kd、cdb、ntsd)是一组开发工具,提供了多种调试选项,用于系统级别调试。
Debugging
Tools for Windows还支持使用命令行和图形界面进行操作,并具备远程调试功能。
5. WinCDEmu
WinCDEmu是一款免费的虚拟光驱软件,支持Windows 10 x64。WinCDEmu可以加载和卸载光盘映像文件,并提供许多高级选项以最大程度地满足用户需求。WinCDEmu被广泛用于软件开发和测试中。
总之,对于Windows 10 x64体系结构,无论开发或者测试,以上几款工具都是程序员必不可少的调试工具。这些工具提供了一系列的功能,用于改善代码质量和提高调试效率。需要注意的是,正确使用调试工具需要一定的技术和经验,并且不能仅仅依靠这些工具来找出所有的程序问题。只有在使用正确的工具来辅助分析和确定问题后,才能更快更有效地编写和测试程序。