TypeError 'Tensor' object is not callable

Hello,

I am new to PyTorch and I was trying to use the autograd package. I keep getting TypeError: 'Tensor' object is not callable. I am not sure what I am doing wrong. I have attached a screenshot of the error and my code below. Can someone please help?

def lagrangian(q, q_dot, m1, m2, l1, l2, g):
    
    t1, t2 = q     # theta 1 and theta 2
    w1, w2 = q_dot # omega 1 and omega 2
    T1 = 0.5 * m1 * (l1 * w1)**2
    T2 = 0.5 * m2 * ((l1 * w1)**2 + (l2 * w2)**2 + 2 * l1 * l2 * w1 * w2 * torch.cos(t1 - t2))
    T = T1 + T2
    y1 = -l1 * torch.cos(t1)
    y2 = y1 - l2 * torch.cos(t2)
    V = m1 * g * y1 + m2 * g * y2
    
    return T - V

def equation_of_motion(lagrangian, state, t=None):
    n = state.shape[0]//2 # Get number of generalised coords
    state_tensor = torch.autograd.Variable(state, requires_grad=True)
    q, q_t = torch.split(state_tensor, 2)

    A = torch.inverse(hessian(lagrangian, state_tensor, create_graph=True))[n:, n:]
    B = jacobian(lagrangian, state_tensor, create_graph=True)[:n]
    C = hessian(lagrangian, state_tensor, create_graph=True)[n:, :n]
    
    q_tt = A @ (B - C @ q_t)
    
    res = torch.cat([q_t,torch.squeeze(q_tt)])

    return res

state = torch.tensor([3*np.pi/7, 3*np.pi/4, 0, 0])
q, q_t = torch.split(state, 2)
L_test = partial(lagrangian, m1=1., m2=1., l1=1., l2=1., g=9.8)(q, q_t)
equation_of_motion(L_test, state)

Thank you.

Could you check your imports and variable names, as based on the error message it seems you might have overridden the hessian method with a tensor using the same name.

1 Like