(discretization, gpfun, number=1, return_function=True)
return functions
Sample a function from a gp with corresponding kernel within its bounds.

    Parameters
    ----------
    discretization : ndarray
        The discretization on which to draw a sample from the GP. Can be
        obtained, for example, from GridWorld.all_points.
    gpfun : instance of safe_learning.GaussianProcess
        The GP from which to draw a sample.
    number : int
        The number of functions to sample.
    return_function : bool, optional
        Whether to return a function or the sampled data only.

    Returns
    -------
    function : list of functions or ndarray
        function(x, noise=True)
        A function that takes as inputs new locations x to be evaluated and
        returns the corresponding noisy function values as a tensor. If
        noise=False is set the true function values are returned (useful for
        plotting).
Sample a function from a gp with corresponding kernel within its bounds.
Sample a function from a gp with corresponding kernel within its bounds .
def sample_gp_function(discretization, gpfun, number=1, return_function=True):
    """
    Sample a function from a gp with corresponding kernel within its bounds.

    Parameters
    ----------
    discretization : ndarray
        The discretization on which to draw a sample from the GP. Can be
        obtained, for example, from GridWorld.all_points.
    gpfun : instance of safe_learning.GaussianProcess
        The GP from which to draw a sample.
    number : int
        The number of functions to sample.
    return_function : bool, optional
        Whether to return a function or the sampled data only.

    Returns
    -------
    function : list of functions or ndarray
        function(x, noise=True)
        A function that takes as inputs new locations x to be evaluated and
        returns the corresponding noisy function values as a tensor. If
        noise=False is set the true function values are returned (useful for
        plotting).

    """
    if isinstance(discretization, GridWorld):
        discretization = discretization.all_points

    gp = gpfun.gaussian_process

    with gp.tf_mode():
        mean, cov = gp.build_predict(discretization, full_cov=True)

    # Evaluate
    sess = tf.get_default_session()
    mean, cov = sess.run([mean, cov], feed_dict=gpfun.feed_dict)

    # Turn mean and covariance into 1D and 2D arrays
    mean = mean.squeeze(-1)
    cov = cov.squeeze(-1)

    # Make sure the covariance is positive definite
    cov += np.eye(len(cov)) * 1E-8

    # Draw a sample
    output = np.random.multivariate_normal(mean, cov, size=number)

    if not return_function:
        return output

    # cholesky
    cho_factor = linalg.cho_factor(cov, lower=True)

    @concatenate_inputs(start=1)
    def gp_sample(alpha, x, noise=True):
        with gp.tf_mode():
            k = gp.kern.K(x, discretization)
            y = gp.mean_function(x) + tf.matmul(k, alpha)
            if noise:
                y += (tf.sqrt(gp.likelihood.variance)
                      * tf.random_normal(tf.shape(y), dtype=tf.float64))
        return y

    # Now let's plug in the alpha to generate samples
    functions = []
    for i in range(number):
        alpha = linalg.cho_solve(cho_factor, output[[i], :].T)
        fun = partial(gp_sample, alpha)

        # Attach the feed_dict for ease of use
        fun.feed_dict = gpfun.feed_dict

        functions.append(fun)

    return functions
def sample_gp_function ( discretization , gpfun , number = 1 , return_function = True ) : if isinstance ( discretization , GridWorld ) : discretization = discretization . all_points gp = gpfun . gaussian_process with gp . tf_mode ( ) : mean , cov = gp . build_predict ( discretization , full_cov = True ) # Evaluate sess = tf . get_default_session ( ) mean , cov = sess . run ( [ mean , cov ] , feed_dict = gpfun . feed_dict ) # Turn mean and covariance into 1D and 2D arrays mean = mean . squeeze ( - 1 ) cov = cov . squeeze ( - 1 ) # Make sure the covariance is positive definite cov += np . eye ( len ( cov ) ) * 1E-8 # Draw a sample output = np . random . multivariate_normal ( mean , cov , size = number ) if not return_function : return output # cholesky cho_factor = linalg . cho_factor ( cov , lower = True ) @ concatenate_inputs ( start = 1 ) def gp_sample ( alpha , x , noise = True ) : with gp . tf_mode ( ) : k = gp . kern . K ( x , discretization ) y = gp . mean_function ( x ) + tf . matmul ( k , alpha ) if noise : y += ( tf . sqrt ( gp . likelihood . variance ) * tf . random_normal ( tf . shape ( y ) , dtype = tf . float64 ) ) return y # Now let's plug in the alpha to generate samples functions = [ ] for i in range ( number ) : alpha = linalg . cho_solve ( cho_factor , output [ [ i ] , : ] . T ) fun = partial ( gp_sample , alpha ) # Attach the feed_dict for ease of use fun . feed_dict = gpfun . feed_dict functions . append ( fun ) return functions