theta_0 = torch.tensor(1.0, requires_grad=True)
theta_1 = torch.tensor(1.0, requires_grad=True)
theta_2 = torch.tensor(2.0, requires_grad=True)
x1 = torch.tensor(1.0)
x2 = torch.tensor(2.0)
f1 = theta_1*x1
f2 = theta_2*x2
f3 = f1 + f2
f4 = f3 + theta_0
f5 = f4*-1
f6 = torch.exp(f5)
f7 = 1 + f6
f8 = 1/f7
f9 = torch.log(f8)
L = f9*-1
all_nodes = {"theta_0": theta_0, "theta_1": theta_1, "theta_2": theta_2,
"f1": f1, "f2": f2, "f3": f3, "f4": f4, "f5": f5, "f6": f6, "f7": f7, "f8": f8, "f9": f9, "L": L}
# Retain grad for all nodes
for node in all_nodes.values():
node.retain_grad()