# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

import torch

a = torch.tensor([2.,3])
print(a)
a = torch.nn.Parameter(a)  # allow gradient calculation
a3 = torch.tensor([4,6,7.,2])
a3 = torch.nn.Parameter(a3)  # allow gradient calculation
print("a3: ", a3)
print(a)
a1 = torch.FloatTensor([2,3])
print("a1: ", a1)
a1.requires_grad_()
print("a1 with grad: ", a1)
a1.requires_grad_(True)
print(a1.requires_grad)  # Bool
b = a+a * a
print(b.requires_grad)
a, b
b = a * a
#a = a.detach()
print("a= ", a, ", a= ", a1, ", b= ", b)
b = b + 3
#a = a.detach()
print("a= ", a, ", a= ", a1, ", b= ", b)

c = torch.sum(b)
print(c)

c = torch.sum(b)
# Cannot run c.backward() two times in a row
if a.grad != None:
    a.grad.zero_()
c.backward(retain_graph=True)

a.grad, b.grad

# * Can only take derivatives with respect to leaves. 
# * Since $b$ depends on $a$, I can only compute the gradient with respect to $a$. 
# * To compute the gradient with respect to $b$, I have to cut off the gradient propagation at $b$. HOW TO DO THIS? 

# +

a.grad, a
# -

a.grad

c



StarcoderdataJupyterScriptsDedupFiltered