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

F.normalize NaN gradient #3264

Closed
elbaro opened this issue Oct 24, 2017 · 3 comments
Closed

F.normalize NaN gradient #3264

elbaro opened this issue Oct 24, 2017 · 3 comments

Comments

@elbaro
Copy link

elbaro commented Oct 24, 2017

0.2.0+dc6510f

F.normalize(p=1) gives NaN gradients.
Manually dividing by the sum works.

Code

import torch
from torch.autograd.variable import Variable
import torch.nn.functional as F

a = Variable(torch.FloatTensor([[0,0,0,0,0.1996]]),requires_grad=True)
# b = F.normalize(a, p=1, dim=1)  # nan nan nan nan 0
# b = a / a.sum().clamp(min=1e-12)  # 0 0 0 0 0
b.sum().backward()

print(a.grad.data)

Output

nan nan nan nan 0

or

0 0 0 0 0
@albanD
Copy link
Collaborator

albanD commented Oct 25, 2017

Hi,
The problem is that you use the norm 1, not the sum in this case (there is an absolute value difference).
The problem is that the current implementation of the norms with p < 1 has a nan gradient at 0 (I guess you were expecting it to return the subgradient of 0).
@soumith should we give this a similar treatment as #2421 ?

@soumith
Copy link
Member

soumith commented Oct 25, 2017

@albanD yes we should

@albanD
Copy link
Collaborator

albanD commented Oct 25, 2017

@colesbury should I do this now or do you prefer to include this in the refactor for #3275 ?

colesbury added a commit to colesbury/pytorch that referenced this issue Nov 3, 2017
colesbury added a commit that referenced this issue Nov 6, 2017
colesbury added a commit that referenced this issue Nov 6, 2017
wuhuikx pushed a commit to wuhuikx/pytorch that referenced this issue Jan 30, 2020
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

3 participants