def test_dl_dG():
nz, neq, nineq = 10, 0, 3
[p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
nz=nz, neq=neq, nineq=nineq)
def f(G):
G = G.reshape(nineq, nz)
_, zhat, nu, lam, slacks = qp_cvxpy.forward_single_np(Q, p, G, h, A, b)
return 0.5 * np.sum(np.square(zhat - truez))
df = nd.Gradient(f)
dG_fd = df(G.ravel()).reshape(nineq, nz)
if verbose:
# print('dG_fd[1,:]: ', dG_fd[1,:])
# print('dG[1,:]: ', dG[1,:])
print('dG_fd: ', dG_fd)
print('dG: ', dG)
npt.assert_allclose(dG_fd, dG, rtol=RTOL, atol=ATOL)
评论列表
文章目录