def test_hess_vec(self):
        # f(x, y) = x^3 + y^2
        func = lambda x: x[0] ** 3 + x[1] ** 2
        x = np.array([2.0, 3.0])
        v = np.array([1.0, 0.1])
        hess_vec_test = hess_vec_finite_diff(func, x, v, eps=1e-5)
        hess_vec_real = np.array([12, 0.2])
        assert_array_almost_equal(hess_vec_real, hess_vec_test, decimal=3)

        v = np.array([1.0, -0.1])
        hess_vec_test = hess_vec_finite_diff(func, x, v, eps=1e-5)
        hess_vec_real = np.array([12, -0.2])
        assert_array_almost_equal(hess_vec_real, hess_vec_test, decimal=3)
Example #2
0
def main():
    danger = []
    for _ in range(10):
        A = np.random.uniform(0, 1000, (5, 5))
        b = np.random.uniform(0, 1000, 5)
        regcoef = np.random.uniform(0, 100, 1)
        oracle = oracles.create_log_reg_oracle(A, b, regcoef)
        diffs = []
        for i in range(100):
            x = np.random.uniform(0, 100, 5)
            v = np.random.uniform(0, 100, 5)
            hess_vec_finite = oracles.hess_vec_finite_diff(oracle.func, x, v)
            hess_vec_oracle = oracle.hess_vec(x, v)
            diff = np.abs(hess_vec_finite - hess_vec_oracle)
            if max(diff) > 1:
                danger.append((A, b, regcoef, x, v))
            diffs.append(max(diff))
        print(max(diffs))
    print(len(danger))
Example #3
0
def check_hess_vec():
    m, n = 1000, 500
    A = np.random.randn(m, n)
    b = np.sign(np.random.randn(m))
    regcoef = 1 / m

    x = np.random.randn(n)
    v = np.random.randn(n)

    logreg_oracle = create_log_reg_oracle(A, b, regcoef, oracle_type='optimized')

    v1 = logreg_oracle.hess_vec(x, v)
    v2 = hess_vec_finite_diff(logreg_oracle.func, x, v, eps=1e-6)
    res = np.allclose(v1, v2, atol=1e-2, rtol=1e-1)
    print(v1[:10])
    print(v2[:10])
    if res:
        print("Logreg hess_vec is OK!")
    else:
        print("Something wrong.")
    return res