作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
该文档不包含 gradcheck 的任何示例用例,它在哪里有用?
最佳答案
这里的文档中提供了一个示例用例:
https://pytorch.org/docs/master/notes/extending.html
You probably want to check if the backward method you implemented actually computes the derivatives of your function. It is possible by comparing with numerical approximations using small finite differences:
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 功能负责计算绝大多数操作的梯度。
关于pytorch - 如何在 PyTorch 中使用 autograd.gradcheck?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57627406/
我是一名优秀的程序员,十分优秀!