Esempio n. 1
0
def test_fit_returns_min_1d_grid():
    N = 100
    D = 3
    m = 1
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)
    X = np.random.randn(N, D)

    C = compute_C_memory(X, omega, u)
    b = compute_b_memory(X, omega, u)
    theta = fit(X, omega, u)
    J = objective(X, theta, omega, u, b, C)

    thetas_test = np.linspace(theta - 3, theta + 3)
    Js = np.zeros(len(thetas_test))

    for i, theta_test in enumerate(thetas_test):
        Js[i] = objective(X, np.array([theta_test]), omega, u, b, C)


#     plt.plot(thetas_test, Js)
#     plt.plot([theta, theta], [Js.min(), Js.max()])
#     plt.title(str(theta))
#     plt.show()

    assert_almost_equal(Js.min(), J, delta=thetas_test[1] - thetas_test[0])
    assert_almost_equal(thetas_test[Js.argmin()],
                        theta[0],
                        delta=thetas_test[1] - thetas_test[0])
Esempio n. 2
0
def test_compute_C_1d2n():
    X = np.array([[1.], [2.]])
    u = np.array([2.])
    omega = np.array([[2.]])
    d = 0
    C_manual = np.mean(rff_feature_map_grad_d(X, omega, u, d)**2)
    C = compute_C_memory(X, omega, u)
    assert_allclose(C_manual, C)
Esempio n. 3
0
def test_compute_C_1d1n():
    X = np.array([[1.]])
    u = np.array([2.])
    omega = np.array([[2.]])
    d = 0
    phi = rff_feature_map_grad_d(X, omega, u, d).flatten()
    C_manual = np.outer(phi, phi)
    C = compute_C_memory(X, omega, u)
    assert_allclose(C_manual, C)
Esempio n. 4
0
def test_compute_C_equals_compute_C_memory():
    N = 100
    D = 3
    m = 10
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)
    X = np.random.randn(N, D)

    C = compute_C(X, omega, u)
    C_storage = compute_C_memory(X, omega, u)
    assert_allclose(C, C_storage, rtol=1e-4)
Esempio n. 5
0
def test_fit():
    N = 100
    D = 3
    m = 10
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)
    X = np.random.randn(N, D)

    C = compute_C_memory(X, omega, u)
    b = compute_b_memory(X, omega, u)
    theta = fit(X, omega, u)
    theta_manual = np.linalg.solve(C, b)
    assert_allclose(theta, theta_manual)
Esempio n. 6
0
def test_objective_sym_equals_completely_manual_manually():
    N = 100
    D = 3
    m = 3
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)
    X = np.random.randn(N, D)
    theta = np.random.randn(m)

    J_manual = 0.
    for n in range(N):
        b_manual = np.zeros(m)
        C_manual = np.zeros((m, m))
        J_n_manual = 0.
        for d in range(D):
            b_term_manual = -np.sqrt(2. / m) * np.cos(np.dot(X[n], omega) +
                                                      u) * (omega[d, :]**2)
            b_term = rff_feature_map_grad2_d(X[n], omega, u, d)
            assert_allclose(b_term_manual, b_term)
            b_manual -= b_term_manual
            J_manual += np.dot(b_term_manual, theta)
            J_n_manual += np.dot(b_term_manual, theta)

            c_vec_manual = -np.sqrt(2. / m) * np.sin(np.dot(X[n], omega) +
                                                     u) * omega[d, :]
            c_vec = rff_feature_map_grad_d(X[n], omega, u, d)
            assert_allclose(c_vec_manual, c_vec)
            C_term = np.outer(c_vec_manual, c_vec_manual)
            C_manual += C_term

            # not regularised here, done afterwards
            J_manual += 0.5 * np.dot(theta, np.dot(C_term, theta))
            J_n_manual += 0.5 * np.dot(theta, np.dot(C_term, theta))

        b = compute_b_memory(X[n].reshape(1, m), omega, u)
        C = compute_C_memory(X[n].reshape(1, m), omega, u)
        assert_allclose(b_manual, b)
        assert_allclose(C_manual, C)

        # discard regularisation for these internal checks
        J_n = objective(X[n].reshape(1, m), theta, omega, u)
        J_n_2 = 0.5 * np.dot(theta, np.dot(C, theta)) - np.dot(theta, b)
        assert_allclose(J_n_2, J_n, rtol=1e-4)
        assert_allclose(J_n_manual, J_n, rtol=1e-4)

    J_manual /= N
    J = objective(X, theta, omega, u)

    assert_close(J, J_manual, decimal=5)
Esempio n. 7
0
def test_objective_sym_given_b_C_equals_given_nothing():
    N = 100
    D = 3
    m = 10
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)
    X = np.random.randn(N, D)

    C = compute_C_memory(X, omega, u)
    b = compute_b_memory(X, omega, u)
    theta = np.random.randn(m)

    J = objective(X, theta, omega, u, b, C)
    J2 = objective(X, theta, omega, u)

    assert_close(J, J2)
Esempio n. 8
0
def test_objective_sym_given_b_C():
    N = 100
    D = 3
    m = 10
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)
    X = np.random.randn(N, D)

    C = compute_C_memory(X, omega, u)
    b = compute_b_memory(X, omega, u)
    theta = np.random.randn(m)

    J = objective(X, theta, omega, u, b, C)
    J_manual = 0.5 * np.dot(theta.T, np.dot(C, theta)) - np.dot(theta, b)

    assert_close(J, J_manual)
Esempio n. 9
0
def test_fit_returns_min_random_search():
    N = 100
    D = 3
    m = 10
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)
    X = np.random.randn(N, D)

    C = compute_C_memory(X, omega, u)
    b = compute_b_memory(X, omega, u)
    theta = fit(X, omega, u)
    J = objective(X, theta, omega, u, b, C)

    for noise in [0.0001, 0.001, 0.1, 1, 10, 100]:
        for _ in range(10):
            theta_test = np.random.randn(m) * noise + theta
            J_test = objective(X, theta_test, omega, u, b, C)

            assert_less_equal(J, J_test)