def fit_gp(X, Y, noise_var=None, train_noise_var=True, min_var=0.0001, max_var=4.0, kernel_type='matern52'):
    with tf.Graph().as_default() as graph:
        with tf.Session(graph=graph).as_default():
            xdim = X.shape[1]
            if (kernel_type == 'se'):
                kernel = gpflow.kernels.RBF(xdim, ARD=True)
            elif (kernel_type == 'matern52'):
                kernel = gpflow.kernels.Matern52(xdim, ARD=True)
            else:
                raise Exception('Unknown kernel:', kernel_type)
            meanf = gpflow.mean_functions.Constant()
            with gpflow.defer_build():
                m = gpflow.models.GPR(X, Y, kern=kernel, mean_function=meanf)
            if train_noise_var:
                gamma_shape = 1.1
                gamma_scale = 0.5
                m.likelihood.variance.prior = gpflow.priors.Gamma(gamma_shape, gamma_scale)
                m.likelihood.variance.transform = gpflow.transforms.Logistic(min_var, max_var)
                prior_mode = ((gamma_shape - 1) * gamma_scale)
                m.likelihood.variance.assign(prior_mode)
            elif (noise_var is not None):
                m.likelihood.variance = noise_var
                m.likelihood.variance.trainable = False
            else:
                raise Exception('Require noise variance!')
            m.compile()
            opt = gpflow.train.ScipyOptimizer()
            has_error = False
            try:
                opt.minimize(m)
            except:
                has_error = True
            if has_error:
                return (has_error, None)
            else:
                gpf_lscale = m.kern.lengthscales.value
                gpf_signal_var = m.kern.variance.value
                lscale = (1.0 / (gpf_lscale * gpf_lscale))
                meanf_const = m.mean_function.c.value
                noise_var = m.likelihood.variance.value
    return (has_error, {'meanf': meanf_const, 'signal_var': gpf_signal_var, 'lengthscale': lscale, 'noise_var': noise_var})
def fit_gp(X, Y, noise_var=None, train_noise_var=True, min_var=0.0001, max_var=4.0, kernel_type='matern52'):
    with tf.Graph().as_default() as graph:
        with tf.Session(graph=graph).as_default():
            xdim = X.shape[1]
            if (kernel_type == 'se'):
                kernel = gpflow.kernels.RBF(xdim, ARD=True)
            elif (kernel_type == 'matern52'):
                kernel = gpflow.kernels.Matern52(xdim, ARD=True)
            else:
                raise Exception('Unknown kernel:', kernel_type)
            meanf = gpflow.mean_functions.Constant()
            with gpflow.defer_build():
                m = gpflow.models.GPR(X, Y, kern=kernel, mean_function=meanf)
            if train_noise_var:
                gamma_shape = 1.1
                gamma_scale = 0.5
                m.likelihood.variance.prior = gpflow.priors.Gamma(gamma_shape, gamma_scale)
                m.likelihood.variance.transform = gpflow.transforms.Logistic(min_var, max_var)
                prior_mode = ((gamma_shape - 1) * gamma_scale)
                m.likelihood.variance.assign(prior_mode)
            elif (noise_var is not None):
                m.likelihood.variance = noise_var
                m.likelihood.variance.trainable = False
            else:
                raise Exception('Require noise variance!')
            m.compile()
            opt = gpflow.train.ScipyOptimizer()
            has_error = False
            try:
                opt.minimize(m)
            except:
                has_error = True
            if has_error:
                return (has_error, None)
            else:
                gpf_lscale = m.kern.lengthscales.value
                gpf_signal_var = m.kern.variance.value
                lscale = (1.0 / (gpf_lscale * gpf_lscale))
                meanf_const = m.mean_function.c.value
                noise_var = m.likelihood.variance.value
    return (has_error, {'meanf': meanf_const, 'signal_var': gpf_signal_var, 'lengthscale': lscale, 'noise_var': noise_var})