1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Understanding backpropagation in PyTorch

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am exploring PyTorch, and I do not understand the output of the following example:

    # Initialize x, y and z to values 4, -3 and 5
    x = torch.tensor(4., requires_grad = True)
    y = torch.tensor(-3., requires_grad = True)
    z = torch.tensor(5., requires_grad = True)

    # Set q to sum of x and y, set f to product of q with z
    q = x + y
    f = q * z

    # Compute the derivatives
    f.backward()

    # Print the gradients
    print("Gradient of x is: " + str(x.grad))
    print("Gradient of y is: " + str(y.grad))
    print("Gradient of z is: " + str(z.grad))


    Output

    Gradient of x is: tensor(5.)
    Gradient of y is: tensor(5.)
    Gradient of z is: tensor(1.)


    I have little doubt that my confusion originates with a minor misunderstanding. Can someone explain in a stepwise manner?

    Continue reading...

Compartilhe esta Página