Skip to content

autograd sample code contains wrong logic and wrong answer #1

Open
@chhotii-alex

Description

@chhotii-alex

Code sample in this section: https://github.com/Devinterview-io/pytorch-interview-questions?tab=readme-ov-file#code-example-autograd-in-action

x.grad is the partial derivative of w with respect to x, which is $\frac{\partial{w}}{\partial{z}}$ $\frac{\partial{z}}{\partial{x}}$
$\frac{\partial{w}}{\partial{z}}$ = $2x$
$\frac{\partial{z}}{\partial{x}}$ = $y$
Thus $\frac{\partial{w}}{\partial{z}}$ $\frac{\partial{z}}{\partial{x}}$ = $2zy$. $y$ was initialized to 4, and $z$ = $xy$ = 12. So $2zy$ = 2 times 12 times 4 = 96.

Likewise you can work out the value of y.grad using the chain rule as well, and it's 72.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions