Thanks. I see.
So let me layout the code, and you might be able to tell me if there is something wrong:
class Grad_finder:
def fun1(x1, x2, x3): 

    x1.requires_grad_(True)
    x1.retain_grad()
    x2.requires_grad_(True)
    x2.retain_grad()
    

    
    '''
    we minimize the loss
    '''
    for i in range(epochs):
        L1 = f(x1, x3) # f is a neural network model
        L2 = g(L1, x2) # g is a defined function
        L3 = h(x1, x3) # h is a defined function
    
        loss = L2 - L3
        optimizer.zero_grad()
        loss.backward(retain_graph=True)
        optimzer.step()
    
    # now f is trained 
    L1 = f(x1, x3)
    L2 = g(L1, x2)
    
    return L2
    
def fun2(x1, x2, x3): 

    x1.requires_grad_(True)
    x1.retain_grad()
    x2.requires_grad_(True)
    x2.retain_grad()
    
    L2 = fun1(x1, x2, x3)
    
    '''
    Here i want to find :
    dL2/dx2
    '''
    
    return dL2dx2

dL2dx2 = Grad_finder.fun2(x1, x2, x3)
Capture539×851 76.1 KB