pytorch / pytorch/tutorials

💡 [REQUEST] - Clarification of requires_grad about beginner/nn_tutorial.html

Open
#3,716 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

It was challenging for me to initially grasp why requires_grad was done after weights, but in the same line as bias under https://docs.pytorch.org/tutorials/beginner/nn_tutorial.html#neural-net-from-scratch-without-torch-nn

At first glance, the code looks inconsistent:

  1. weights initialization is split into two lines.
  2. bias initialization is done in one line.

The Logic Gap
The tutorial currently explains that we do it, but not exactly why the distinction exists between these two specific variables.

  • The Bias is created using a factory function (torch.zeros) with no subsequent mathematical operations. It is born as a "Leaf Node" (a source parameter).
  • The Weights involve a mathematical operation (/ math.sqrt(...)). If we set requires_grad=True inside torch.randn(), PyTorch records the division as a computational step. The resulting weights variable becomes a non-leaf node (a calculated outcome), which the optimizer cannot update.

Proposed Improvement
I propose modifying the comment block to explicitly mention that requires_grad must be deferred until after the initialization math is complete to preserve the tensor as a trainable parameter (Leaf Node).

Existing tutorials on this topic
Additional context
Image

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 source for beginner/nn_tutorial.html and locate the “Neural Net from Scratch without torch.nn” section. Update the existing comment around weights and bias to explain why requires_grad is deferred for weights after initialization math; done when the leaf versus non-leaf distinction is clear in the rendered tutorial.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.