Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chain rule will lead to Nan, sqrt() example #6394

Closed
zchky opened this issue Apr 8, 2018 · 3 comments
Closed

chain rule will lead to Nan, sqrt() example #6394

zchky opened this issue Apr 8, 2018 · 3 comments

Comments

@zchky
Copy link

zchky commented Apr 8, 2018

i find a interesting fact.

for example sqrt( ( x-1 )^2 ) =y and |x-1| = y , their grad of the two x should be same . but their grad of x different when x close to 1

>> x=Variable(torch.Tensor([1]),requires_grad=True)
>> y=torch.sqrt(torch.pow(x - 1,0))
>> y.backward()
>> x.grad
Variable containing:
nan
[torch.FloatTensor of size 1]

but in the fact, the grad of x for x-1 =y is just

>> x=Variable(torch.Tensor([1]),requires_grad=True)
>> y=x-1
>> y.backward()
>> x.grad
Variable containing:
 1
[torch.FloatTensor of size 1]

the formula is reasonable in mathematics, but it is different for autograd to compute.
i think the point is when one of sub chain's grad is Nan, the final grad will be Nan.

@vishwakftw
Copy link
Contributor

sqrt((x-1)^2) = |x - 1| by definition. The derivative of |x - 1| at x = 1, is supposed to be nan, unless a subgradient is specified.

@ruotianluo
Copy link
Contributor

Both (x-1).abs() and (x-1).norm(2) gives subgradient 0.

@fmassa
Copy link
Member

fmassa commented Apr 11, 2018

The subgradient for norm at zero was added in #2775 and required some special casing.

I don't think we can do much when the user specifies a set of operations by hand, because the derivative of sqrt at zero is inf.

This is something we need to live with in numeric computing.
For example, log(x ** x) == x * log(x), but when x is zero, the limit (which is 0) gets undefined using the second expression:

print(np.log(0 ** 0))  # 0.0
print(0 * np.log(0))  # nan

Closing as a wontfix. Please let us know if you disagree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants