jod*_*dag 10
此处的文档中提供了一个示例用例:
https://pytorch.org/docs/master/notes/extending.html
您可能想检查您实现的向后方法是否实际计算了您的函数的导数。通过与使用小的有限差分的数值近似值进行比较是可能的:
Run Code Online (Sandbox Code Playgroud)from torch.autograd import gradcheck # gradcheck takes a tuple of tensors as input, check if your gradient # evaluated with these tensors are close enough to numerical # approximations and returns True if they all verify this condition. input = (torch.randn(20,20,dtype=torch.double,requires_grad=True), torch.randn(30,20,dtype=torch.double,requires_grad=True)) test = gradcheck(linear, input, eps=1e-6, atol=1e-4) print(test)
正如上面的引用所暗示的那样,该gradcheck
函数的目的是验证自定义向后函数是否与梯度的数值近似一致。主要用例是当您实现自定义向后操作时。在极少数情况下,您应该在 PyTorch 中实现自己的向后功能。这是因为 PyTorch 的 autograd 功能负责计算绝大多数操作的梯度。
最明显的例外是
您有一个不能表示为其他可微函数的有限组合的函数(例如,如果您需要不完整的 gamma 函数,您可能想要编写自己的使用 numpy 和/或查找表的前向和后向函数)。
您希望加快一个特别复杂的表达式的计算速度,在应用链式法则后,该表达式的梯度可以大大简化。
归档时间: |
|
查看次数: |
4198 次 |
最近记录: |