def maml_simulation() -> None:
    x_inner = torch.tensor(2.0, requires_grad=True)
    x_outer = torch.tensor(3.0, requires_grad=True)
    theta_outer = torch.randn(1, requires_grad=True)
    print(f"before training: x inner: {x_inner} theta outer: {theta_outer}")

    loss_func = lambda x: (x - 10) ** 2

    loss = torch.tensor(0.0, requires_grad=True)
    for i in range(5):
        theta_outer = zero_grad(theta_outer)

        print(f"outer loop, theta before: {theta_outer}")
        theta_inner = torch.tensor(theta_outer.item(), requires_grad=True)
        for j in range(5):
            theta_inner = zero_grad(theta_inner)

            prediction = theta_inner * x_inner
            inner_loss = loss_func(prediction)

            grad = torch.autograd.grad(inner_loss, theta_inner)[0]
            with torch.no_grad():
                theta_inner = theta_inner - 0.01 * grad
            print(f"inner prediction: {prediction}, loss: {inner_loss}, grad: {grad}")

        prediction = theta_outer * x_outer
        loss = loss_func(prediction)
        print(loss.requires_grad, theta_outer.requires_grad)
        grad = torch.autograd.grad(loss, theta_outer)[0]
        print("outer grad: ", grad)

        with torch.no_grad():
            theta_outer = theta_outer - 0.01 * grad
        print(f"\n\nouter prediction: {prediction}, loss: {loss}, grad: {grad}")
        print(f"theta after: {theta_outer}\n\n")

I am going through some meta learning stuff and I want to try and follow the second derivatives of this loop to see what it looks likes and if it is doing what I think it is doing.


Does this code calulate a second derivative as is stated in this paper? https://arxiv.org/abs/1703.03400 4


How can I print out the graph or verify somehow that it is doing what I think it is doing?


Thanks