Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Second order gradient wrt inputs, expected behaviour. #14991

Description

@larroy

What would be the expected behaviour of this code?

It tries to calculate the gradient of a function using the gradient wrt of the inputs of the first gradient.

def test_ag_grad():
    x = mx.nd.ones((3,3))
    y = mx.nd.ones((3,3))
    x.attach_grad()
    y.attach_grad()
    with mx.autograd.record():
        z = x + y
        x_grad_y_grad = mx.autograd.grad(z, [x,y], create_graph=True, retain_graph=True)
        print(x_grad_y_grad)
        first_grad = nd.concat(*[x.reshape(-1) for x in x_grad_y_grad], dim=0)
        fg_f = 2 * first_grad
        second_grad = mx.autograd.grad(fg_f, [x,y], retain_graph=True)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions