def test_objective_matches_sym(): sigma = 1. lmbda = 1. Z = np.random.randn(100, 2) alpha = np.random.randn(len(Z)) J_sym = develop_gaussian.objective_sym(Z, sigma, lmbda, alpha) J = gaussian.objective(Z, Z, sigma, lmbda, alpha) print type(J) print type(J_sym) assert_equal(J, J_sym)
def test_objective_matches_sym_precomputed_KbC(): sigma = 1. lmbda = 1. Z = np.random.randn(100, 2) K = gaussian_kernel(Z, sigma=sigma) alpha = np.random.randn(len(Z)) C = develop_gaussian.compute_C_sym(Z, K, sigma) b = develop_gaussian.compute_b_sym(Z, K, sigma) K = gaussian_kernel(Z, sigma=sigma) J_sym = develop_gaussian.objective_sym(Z, sigma, lmbda, alpha, K, b, C) J = gaussian.objective(Z, Z, sigma, lmbda, alpha, K_XY=K, b=b, C=C) assert_equal(J, J_sym)
def test_objective_matches_full(): sigma = 1. lmbda = 1. X = np.random.randn(100, 2) Y = np.random.randn(10, 2) low_rank_dim = int(len(X) * 0.9) kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma) alpha = np.random.randn(len(X)) K_XY = kernel(X, Y) C = gaussian.compute_C(X, Y, K_XY, sigma) b = gaussian.compute_b(X, Y, K_XY, sigma) J_full = gaussian.objective(X, Y, sigma, lmbda, alpha, K_XY=K_XY, b=b, C=C) temp = incomplete_cholesky(X, kernel, eta=low_rank_dim) I, R, nu = (temp["I"], temp["R"], temp["nu"]) R_test = incomplete_cholesky_new_points(X, Y, kernel, I, R, nu) b = gaussian_low_rank.compute_b(X, Y, R.T, R_test.T, sigma) J = gaussian_low_rank.objective(X, Y, sigma, lmbda, alpha, R.T, R_test.T, b) assert_close(J, J_full, decimal=1)
def test_objective_against_naive(): sigma = 1. D = 2 NX = 10 NY = 20 X = np.random.randn(NX, D) Y = np.random.randn(NY, D) K_XY = gaussian_kernel(X, Y, sigma=sigma) num_trials = 10 for _ in range(num_trials): alpha = np.random.randn(NX) J_naive_a = 0 for d in range(D): for i in range(NX): for j in range(NY): J_naive_a += alpha[i] * K_XY[i, j] * \ (-1 + 2. / sigma * ((X[i][d] - Y[j][d]) ** 2)) J_naive_a *= (2. / (NX * sigma)) J_naive_b = 0 for d in range(D): for i in range(NY): temp = 0 for j in range(NX): temp += alpha[j] * (X[j, d] - Y[i, d]) * K_XY[j, i] J_naive_b += (temp**2) J_naive_b *= (2. / (NX * (sigma**2))) J_naive = J_naive_a + J_naive_b # compare to unregularised objective lmbda = 0. J = gaussian.objective(X, Y, sigma, lmbda, alpha, K_XY=K_XY) assert_close(J_naive, J)
def test_objective_against_naive(): sigma = 1. D = 2 NX = 10 NY = 20 X = np.random.randn(NX, D) Y = np.random.randn(NY, D) K_XY = gaussian_kernel(X, Y, sigma=sigma) num_trials = 10 for _ in range(num_trials): alpha = np.random.randn(NX) J_naive_a = 0 for d in range(D): for i in range(NX): for j in range(NY): J_naive_a += alpha[i] * K_XY[i, j] * \ (-1 + 2. / sigma * ((X[i][d] - Y[j][d]) ** 2)) J_naive_a *= (2. / (NX * sigma)) J_naive_b = 0 for d in range(D): for i in range(NY): temp = 0 for j in range(NX): temp += alpha[j] * (X[j, d] - Y[i, d]) * K_XY[j, i] J_naive_b += (temp ** 2) J_naive_b *= (2. / (NX * (sigma ** 2))) J_naive = J_naive_a + J_naive_b # compare to unregularised objective lmbda = 0. J = gaussian.objective(X, Y, sigma, lmbda, alpha, K_XY=K_XY) assert_close(J_naive, J)