-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add reset_grad() function (#42754) #44423
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,16 +164,32 @@ def update_group(group, new_group): | |
| update_group(g, ng) for g, ng in zip(groups, saved_groups)] | ||
| self.__setstate__({'state': state, 'param_groups': param_groups}) | ||
|
|
||
| def zero_grad(self): | ||
| r"""Clears the gradients of all optimized :class:`torch.Tensor` s.""" | ||
| def zero_grad(self, set_to_none: bool = False): | ||
| r"""Set the gradients of all optimized :class:`torch.Tensor` s to zero. | ||
|
|
||
| Arguments: | ||
| set_to_none (bool): instead of setting to zero, set the grad to None. | ||
| This is will in general have lower memory footprint, and can modestly improve performance. | ||
| However, it changes certain behaviors. For example: | ||
| 1. When user tries to access the gradient value and perform manual ops on it. | ||
| A None attribute or a Tensor full of 0s will be different. | ||
| 2. If the user requests `zero_grad(set_to_none=True)` followed by a backward pass, `.grad` s | ||
|
||
| are guaranteed to be None for params that did not receive a gradient. | ||
| 3. `torch.optim` optimizers have a different behavior if the gradient is 0 or None | ||
|
||
| (in one case it does the step with a gradient of 0 and in the other it skip | ||
| the step altogether). | ||
| """ | ||
| for group in self.param_groups: | ||
| for p in group['params']: | ||
| if p.grad is not None: | ||
| if p.grad.grad_fn is not None: | ||
| p.grad.detach_() | ||
| if set_to_none: | ||
| p.grad = None | ||
| else: | ||
| p.grad.requires_grad_(False) | ||
| p.grad.zero_() | ||
| if p.grad.grad_fn is not None: | ||
| p.grad.detach_() | ||
| else: | ||
| p.grad.requires_grad_(False) | ||
| p.grad.zero_() | ||
|
|
||
| def step(self, closure): | ||
| r"""Performs a single optimization step (parameter update). | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.