Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A What binds together the loss function, the optimizer and the model in PyTorch?

Your suspicion is right that PyTorch relies heavily on (hidden) global states. In your specific example, loss.backward() computes (all) gradients and accumulates them directly in the grad attribut...

posted 1y ago by mr Tsjolder‭

Answer
#1: Initial revision by user avatar mr Tsjolder‭ · 2025-06-09T11:23:10Z (over 1 year ago)
Your suspicion is right that PyTorch relies heavily on (hidden) global states.

In your specific example, `loss.backward()` computes (all) gradients and **accumulates** them directly in the `grad` attribute of each parameter. You can verify this by printing the grad attributes of the parameters before and after the `backward` call using the following list comprehension `[par.grad for par in model.parameters()]` (note that these can be big matrices and it might be useful to select only a few elements to reduce the amount of generated output).

Once the gradients have been stored in the `grad` attributes, the optimiser, which has access to (a subset of) the parameters by means of the references you pass upon construction, can use these gradients to perform the desired updates.

Also, note my emphasis on **accumulate**: the computed gradients are added to whatever was stored in the `grad` attribute previously. This is why it is so important to call `optimizer.zero_grad()` (or `model.zero_grad()`) before starting gradient computations.

I hope this gives you the desired insights in how this works.

PS: there is also an official [PyTorch tutorial](https://docs.pytorch.org/tutorials/beginner/basics/autogradqs_tutorial.html) on the autograd system.

PPS: there is more global state in how the autograd works, but I think that would be something for a different question.