pytorch版本:1.1.0
函数介绍:
gradcheck(func, inputs, eps=1e-06, atol=1e-05, rtol=0.001, raise_exception=True, check_sparse_nnz=False)
运行下面的程序时报错
# sigmoid
class Sigmoid(Function):
@staticmethod
def forward(ctx , x):
output=1/(1+t.exp(-x))
ctx.save_for_backward(output)
return output
@staticmethod
def backward(ctx,grad_output):
output, = ctx.saved_tensors
grad_x= grad_output* output * (1-output)
return grad_x
input=t.randn(3,4,requires_grad=True)
t.autograd.gradcheck(Sigmoid.apply,(input,))
错误截图:
问题出在最后一行上,查Google后解决:
最后一行代码改为
t.autograd.gradcheck(Sigmoid.apply,(input,),eps=1e-3)
问题原因
从函数介绍中可以发现 eps 参数值默认为1e-6,这是对应双精度的默认值,与输入数据不匹配,我们把这个数值上调到1e-4 1e-3 或更大都行