pytorch / pytorch/tutorials

💡 [REQUEST] - What is purpose of `out.backward(torch.randn(1, 10))` in neural_networks_tutorial

Open
#3,017 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

core intro question
Dominant language
Python
Stars
9.3k
Forks
4.4k
Avg merge
1d 21h
Merged PRs (30d)
4

Description

🚀 Describe the improvement or the new tutorial

In neural networks tutorial for beginners, we have the following:

Zero the gradient buffers of all parameters and backprops with random gradients:

net.zero_grad()
out.backward(torch.randn(1, 10))

What is the purpose of this? It is not part of standard ML workflows and can be confusing to beginners. (As evidence,I am helping some people learn basics of ML and I got questions about this line. This is how I found out about it!)

If there is no good reason for it, then I suggest:

  • dropping these few lines
  • changing wording of other parts of the page if needed. E.g. 'at this point we covered... calling backward'
Existing tutorials on this topic

No response

Additional context

No response

cc @subramen @albanD

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the linked neural networks tutorial and read the section containing net.zero_grad() and out.backward(torch.randn(1, 10)), including the surrounding explanation. Decide whether the example needs those lines for its teaching goal, then update the snippet and any wording that depends on it. Done means the beginner-facing explanation is consistent with the revised example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation, machine-learning
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.