added the Get_gradient helper function