def test_hess_finite_diff_1(): # Quadratic function. A = np.eye(3) b = np.array([1, 2, 3]) quadratic = oracles.QuadraticOracle(A, b) H = oracles.hess_finite_diff(quadratic.func, np.zeros(3)) ok_(isinstance(H, np.ndarray)) ok_(np.allclose(H, A))
def test_hess_finite_diff_2(): # f(x, y) = x^3 + y^2 func = lambda x: x[0]**3 + x[1]**2 x = np.array([2.0, 3.0]) eps = 1e-5 H = oracles.hess_finite_diff(func, x, eps) ok_(isinstance(H, np.ndarray)) ok_(np.allclose(H, [[12.0, 0.], [0., 2.0]], atol=1e-3))
def main(): A = np.random.uniform(0, 10, (5, 5)) b = np.random.uniform(0, 10, 5) regcoef = np.random.uniform(0, 10, 1) oracle = oracles.create_log_reg_oracle(A, b, regcoef) print(A) print(b) print(regcoef) for i in range(10): x = np.random.uniform(0, 10, 5) grad_oracle = oracle.grad(x) hess_oracle = oracle.hess(x) grad_finite = oracles.grad_finite_diff(oracle.func, x) hess_finite = oracles.hess_finite_diff(oracle.func, x) diff_grad = np.abs(grad_finite - grad_oracle) diff_hess = np.abs(hess_finite - hess_oracle) #print(i) #print(grad_oracle) #print(grad_finite) print(np.max(diff_hess))