def get_kernel_cholesky(X_train, hyps, str_cov, is_fixed_noise=constants.IS_FIXED_GP_NOISE, is_gradient=False, debug=False): """ This function computes a kernel inverse with Cholesky decomposition. :param X_train: inputs. Shape: (n, d) or (n, m, d). :type X_train: numpy.ndarray :param hyps: dictionary of hyperparameters for Gaussian process. :type hyps: dict. :param str_cov: the name of covariance function. :type str_cov: str. :param is_fixed_noise: flag for fixing a noise. :type is_fixed_noise: bool., optional :param is_gradient: flag for computing and returning gradients of negative log marginal likelihood. :type is_gradient: bool., optional :param debug: flag for printing log messages. :type debug: bool., optional :returns: a tuple of kernel matrix over `X_train`, lower matrix computed by Cholesky decomposition, and gradients of kernel matrix. If `is_gradient` is False, gradients of kernel matrix would be None. :rtype: tuple of (numpy.ndarray, numpy.ndarray, numpy.ndarray) :raises: AssertionError """ assert isinstance(X_train, np.ndarray) assert isinstance(hyps, dict) assert isinstance(str_cov, str) assert isinstance(is_fixed_noise, bool) assert isinstance(is_gradient, bool) assert isinstance(debug, bool) utils_gp.check_str_cov('get_kernel_cholesky', str_cov, X_train.shape) cov_X_X = covariance.cov_main( str_cov, X_train, X_train, hyps, True) + hyps['noise']**2 * np.eye(X_train.shape[0]) cov_X_X = (cov_X_X + cov_X_X.T) / 2.0 try: lower = scipy.linalg.cholesky(cov_X_X, lower=True) except np.linalg.LinAlgError: # pragma: no cover cov_X_X += 1e-2 * np.eye(X_train.shape[0]) lower = scipy.linalg.cholesky(cov_X_X, lower=True) if is_gradient: grad_cov_X_X = covariance.grad_cov_main(str_cov, X_train, X_train, hyps, is_fixed_noise, same_X_Xs=True) else: grad_cov_X_X = None return cov_X_X, lower, grad_cov_X_X
def test_grad_cov_main(): cur_hyps = utils_covariance.get_hyps('se', 2) with pytest.raises(AssertionError) as error: covariance.grad_cov_main(123, np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True) with pytest.raises(AssertionError) as error: covariance.grad_cov_main('se', 123, np.zeros((10, 2)), cur_hyps, True) with pytest.raises(AssertionError) as error: covariance.grad_cov_main('se', np.zeros((10, 2)), 123, cur_hyps, True) with pytest.raises(AssertionError) as error: covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), 123, True) with pytest.raises(AssertionError) as error: covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, 'abc') with pytest.raises(AssertionError) as error: covariance.grad_cov_main('abc', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True) with pytest.raises(AssertionError) as error: covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True, same_X_Xs='abc') with pytest.raises(AssertionError) as error: covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True, same_X_Xs=False) with pytest.raises(AssertionError) as error: covariance.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True, jitter='abc') grad_cov_ = covariance.grad_cov_main('se', np.ones((1, 2)), np.ones( (1, 2)), cur_hyps, True) print(grad_cov_) truth_grad_cov_ = np.array([[[2.00002, 0.0, 0.0]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) grad_cov_ = covariance.grad_cov_main('se', np.ones((1, 2)), np.ones( (1, 2)), cur_hyps, False) print(grad_cov_) truth_grad_cov_ = np.array([[[0.02, 2.00002, 0.0, 0.0]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) cur_hyps['lengthscales'] = 1.0 grad_cov_ = covariance.grad_cov_main('se', np.ones((1, 2)), np.zeros( (1, 2)), cur_hyps, False) print(grad_cov_) truth_grad_cov_ = np.array([[[0.02, 0.73577888, 0.73577888]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) grad_cov_ = covariance.grad_cov_main('matern32', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False) print(grad_cov_) truth_grad_cov_ = np.array([[[0.02, 0.59566154, 0.51802578]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) grad_cov_ = covariance.grad_cov_main('matern32', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, True) print(grad_cov_) truth_grad_cov_ = np.array([[[0.59566154, 0.51802578]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) grad_cov_ = covariance.grad_cov_main('matern52', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False) print(grad_cov_) truth_grad_cov_ = np.array([[[0.02, 0.63458673, 0.8305486]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)
def test_grad_cov_main(): cur_hyps = utils_covariance.get_hyps('se', 2) with pytest.raises(AssertionError) as error: package_target.grad_cov_main(123, np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True) with pytest.raises(AssertionError) as error: package_target.grad_cov_main('se', 123, np.zeros((10, 2)), cur_hyps, True) with pytest.raises(AssertionError) as error: package_target.grad_cov_main('se', np.zeros((10, 2)), 123, cur_hyps, True) with pytest.raises(AssertionError) as error: package_target.grad_cov_main('se', np.zeros((10, 2)), np.zeros( (10, 2)), 123, True) with pytest.raises(AssertionError) as error: package_target.grad_cov_main('se', np.zeros((10, 2)), np.zeros( (10, 2)), cur_hyps, 'abc') with pytest.raises(AssertionError) as error: package_target.grad_cov_main('abc', np.zeros((10, 2)), np.zeros( (10, 2)), cur_hyps, True) with pytest.raises(AssertionError) as error: package_target.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True, same_X_Xp='abc') with pytest.raises(AssertionError) as error: package_target.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True, same_X_Xp=False) with pytest.raises(AssertionError) as error: package_target.grad_cov_main('se', np.zeros((10, 2)), np.zeros((10, 2)), cur_hyps, True, jitter='abc') grad_cov_ = package_target.grad_cov_main('se', np.ones((1, 2)), np.ones((1, 2)), cur_hyps, True) print(grad_cov_) truth_grad_cov_ = np.array([[[2.00002, 0.0, 0.0]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) grad_cov_ = package_target.grad_cov_main('se', np.ones((1, 2)), np.ones((1, 2)), cur_hyps, False) print(grad_cov_) truth_grad_cov_ = np.array([[[0.02, 2.00002, 0.0, 0.0]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) cur_hyps['lengthscales'] = 1.0 grad_cov_ = package_target.grad_cov_main('se', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False) print(grad_cov_) truth_grad_cov_ = np.array([[[0.02, 0.73577888, 0.73577888]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) grad_cov_ = package_target.grad_cov_main('matern32', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False) print(grad_cov_) truth_grad_cov_ = np.array([[[0.02, 0.59566154, 0.51802578]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) grad_cov_ = package_target.grad_cov_main('matern32', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, True) print(grad_cov_) truth_grad_cov_ = np.array([[[0.59566154, 0.51802578]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON) grad_cov_ = package_target.grad_cov_main('matern52', np.ones((1, 2)), np.zeros((1, 2)), cur_hyps, False) print(grad_cov_) print(np.squeeze(grad_cov_)[0]) print(np.squeeze(grad_cov_)[1]) print(np.squeeze(grad_cov_)[2]) truth_grad_cov_ = np.array( [[[0.02, 0.6345867279080876, 0.5872865507000906]]]) assert np.all(np.abs(grad_cov_ - truth_grad_cov_) < TEST_EPSILON)