Skip to content

Update FAQ to make more sense after tensor/variable merge#6017

Merged
soumith merged 1 commit intopytorch:masterfrom
goldsborough:total_loss
Mar 27, 2018
Merged

Update FAQ to make more sense after tensor/variable merge#6017
soumith merged 1 commit intopytorch:masterfrom
goldsborough:total_loss

Conversation

@goldsborough
Copy link
Copy Markdown
Contributor

@goldsborough goldsborough commented Mar 26, 2018

Looks like there's already a note about the total_loss += loss trouble. I've updated it to make more sense after the tensor/variable merge.

Also, GitHub doesn't allow pull requests for the wiki. Can someone please edit https://github.com/pytorch/pytorch/wiki/Breaking-Changes-from-Variable-and-Tensor-merge with something like

## Possible Issues

* If you have code that accumulates losses across iterations (e.g. to compute an
average at the end of an epoch), such as `total_loss += loss` where `loss` is
your per-iteration loss, you may find increased memory usage in your program.
This is because `loss` probably used to be a Python float (such as when it is
the result of `.sum()`), while it is now a zero-dim Tensor. `total_loss` is
thus accumulating `Tensor`s and their gradient history, which may keep around
large autograd graphs for much longer than necessary. To fix this, be sure to
convert the per-iteration loss to a Python number as soon as possible, for
example with `total_loss += float(loss)`.

@ezyang @apaszke @soumith

@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Mar 26, 2018

wiki applied

Copy link
Copy Markdown
Contributor

@apaszke apaszke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also loss.item(), but I think float(loss) is ok too

@soumith soumith merged commit 47f31cb into pytorch:master Mar 27, 2018
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
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

Successfully merging this pull request may close these issues.

4 participants