コード例 #1
1
ファイル: subprocess.py プロジェクト: vishalbelsare/pybo
def main():
    """Run the demo."""
    # grab a test function
    f = SubprocessQuery("bc <<< 'scale=8; x={}; -((x-3)^2)'")
    bounds = [0, 8]
    x = np.linspace(bounds[0], bounds[1], 500)

    # solve the model
    xbest, model, info = solve_bayesopt(f, bounds, niter=30, verbose=True)
    mu, s2 = model.predict(x[:, None])

    # plot the final model
    ax = figure().gca()
    ax.plot_banded(x, mu, 2 * np.sqrt(s2))
    ax.axvline(xbest)
    ax.scatter(info.x.ravel(), info.y)
    ax.figure.canvas.draw()
    show()
コード例 #2
0
ファイル: fourier.py プロジェクト: appcoreopc/reggie
def main():
    """Run the demo."""
    # grab some kernels
    kernels = [SE(1, 1),
               Matern(1, 1, d=5)]

    # the points we'll test at
    x = np.linspace(-5, 5, 500)
    n = 1000

    # seed the rng and create a figure
    rng = np.random.RandomState(0)
    fig = figure()

    for i, kernel in enumerate(kernels):
        # the true kernel
        k1 = kernel.get_kernel([[0]], x[:, None]).ravel()

        # the approximation
        W, a = kernel.sample_spectrum(n, rng)
        b = rng.rand(n) * 2 * np.pi
        k2 = np.dot(np.cos(np.dot(x[:, None], W.T) + b), np.cos(b)) * 2 * a / n

        # plot it
        ax = fig.add_subplot(len(kernels), 1, 1+i)
        ax.plot(x, k1, label='true kernel')
        ax.plot(x, k2, label='feature approximation')
        ax.set_title(kernel.__class__.__name__)

        if i == 0:
            ax.legend(loc=0)

    # draw everything
    fig.canvas.draw()
    show()
コード例 #3
0
ファイル: basic.py プロジェクト: psederberg/reggie
def main():
    """Run the demo."""
    # generate random data from a gp prior
    rng = np.random.RandomState(0)
    gp = make_gp(0.1, 1.0, 0.1, kernel='matern1')
    X = rng.uniform(-2, 2, size=(20, 1))
    Y = gp.sample(X, latent=False, rng=rng)

    # create a new GP and optimize its hyperparameters
    gp = make_gp(1, 1, 1, kernel='se')
    gp.add_data(X, Y)
    gp.optimize()

    # get the posterior moments
    x = np.linspace(X.min(), X.max(), 500)
    mu, s2 = gp.predict(x[:, None])

    # plot the posterior
    ax = figure().gca()
    ax.plot_banded(x, mu, 2 * np.sqrt(s2), label='posterior mean')
    ax.scatter(X.ravel(), Y, label='observed data')
    ax.legend(loc=0)
    ax.set_title('Basic GP')
    ax.set_xlabel('inputs, X')
    ax.set_ylabel('outputs, Y')

    # draw/show it
    ax.figure.canvas.draw()
    show()
コード例 #4
0
ファイル: fourier.py プロジェクト: psederberg/reggie
def main():
    """Run the demo."""
    # grab some kernels
    kernels = [SE(1, 1), Matern(1, 1, d=5)]

    # the points we'll test at
    x = np.linspace(-5, 5, 500)
    n = 1000

    # seed the rng and create a figure
    rng = np.random.RandomState(0)
    fig = figure()

    for i, kernel in enumerate(kernels):
        # the true kernel
        k1 = kernel.get_kernel([[0]], x[:, None]).ravel()

        # the approximation
        W, a = kernel.sample_spectrum(n, rng)
        b = rng.rand(n) * 2 * np.pi
        k2 = np.dot(np.cos(np.dot(x[:, None], W.T) + b), np.cos(b)) * 2 * a / n

        # plot it
        ax = fig.add_subplot(len(kernels), 1, 1 + i)
        ax.plot(x, k1, label='true kernel')
        ax.plot(x, k2, label='feature approximation')
        ax.set_title(kernel.__class__.__name__)

        if i == 0:
            ax.legend(loc=0)

    # draw everything
    fig.canvas.draw()
    show()
コード例 #5
0
ファイル: basic.py プロジェクト: appcoreopc/reggie
def main():
    """Run the demo."""
    # generate random data from a gp prior
    rng = np.random.RandomState(0)
    gp = make_gp(0.1, 1.0, 0.1, kernel='matern1')
    X = rng.uniform(-2, 2, size=(20, 1))
    Y = gp.sample(X, latent=False, rng=rng)

    # create a new GP and optimize its hyperparameters
    gp = make_gp(1, 1, 1, kernel='se')
    gp.add_data(X, Y)
    gp.optimize()

    # get the posterior moments
    x = np.linspace(X.min(), X.max(), 500)
    mu, s2 = gp.predict(x[:, None])

    # plot the posterior
    ax = figure().gca()
    ax.plot_banded(x, mu, 2*np.sqrt(s2), label='posterior mean')
    ax.scatter(X.ravel(), Y, label='observed data')
    ax.legend(loc=0)
    ax.set_title('Basic GP')
    ax.set_xlabel('inputs, X')
    ax.set_ylabel('outputs, Y')

    # draw/show it
    ax.figure.canvas.draw()
    show()
コード例 #6
0
ファイル: sparse.py プロジェクト: psederberg/reggie
def main():
    """Run the demo."""
    # generate random data from a gp prior
    rng = np.random.RandomState(0)
    gp = make_gp(0.1, 1.0, 0.1, kernel='matern1')
    X = rng.uniform(-2, 2, size=(20, 1))
    Y = gp.sample(X, latent=False, rng=rng)
    U = np.linspace(X.min(), X.max(), 10)[:, None]

    # create a new (sparse) GP and optimize its hyperparameters
    gp = make_gp(1, 1, 1, inf='fitc', U=U)
    gp.add_data(X, Y)
    gp.optimize()

    # get the posterior moments
    x = np.linspace(X.min(), X.max(), 500)
    mu, s2 = gp.predict(x[:, None])

    # plot the posterior
    ax = figure().gca()
    ax.plot_banded(x, mu, 2*np.sqrt(s2), label='posterior mean')
    ax.scatter(X, Y, label='observed data')
    ax.scatter(U, np.full_like(U, -1), marker='x', label='inducing points')
    ax.legend(loc=0)
    ax.set_xlabel('inputs, X')
    ax.set_ylabel('outputs, Y')
    ax.set_title('Sparse GP (FITC)')

    # show the figure
    ax.figure.canvas.draw()
    show()
コード例 #7
0
def main():
    """Run the demo."""
    # define the model and sample an instance from it
    n = 10
    rng = np.random.RandomState(3)
    model = BetaBernoulli(np.ones(n))
    f = model.sample(rng=rng)

    # grab the optimal latent value and make lists of observations
    xopt = f.argmax()
    fopt = f.max()

    # create a new figure
    fig = figure(figsize=(10, 6))

    while True:
        # evaluate the posterior before updating the model for plotting
        mu = model.get_quantile(0.5)
        lo = model.get_quantile(0.05)
        hi = model.get_quantile(0.95)

        # get our index
        target = mu.max()
        index = model.get_improvement(target)

        # query
        x = index.argmax()
        y = int(rng.uniform() < f[x])

        # add the data
        model.add_data(x, y)

        # PLOT EVERYTHING
        fig.clear()
        ax1 = fig.add_subplotspec((2, 1), (0, 0), hidex=True)
        ax2 = fig.add_subplotspec((2, 1), (1, 0), hidey=True, sharex=ax1)

        ax1.errorbar(np.arange(n),
                     mu, (mu - lo, hi - mu),
                     ls='',
                     marker='s',
                     markersize=20,
                     capsize=30,
                     capthick=2)
        ax1.axvline(xopt, zorder=-1)
        ax1.axhline(fopt, zorder=-1)
        ax1.set_ylim(0, 1)
        ax1.set_xlim(-0.3, n - 1 + 0.3)

        ax2.bar(np.arange(n) - 0.25, index, 0.5)

        # draw
        fig.canvas.draw()
        show()
コード例 #8
0
ファイル: bandit.py プロジェクト: Mehrian/pybo
def main():
    """Run the demo."""
    # define the model and sample an instance from it
    n = 10
    rng = np.random.RandomState(3)
    model = BetaBernoulli(np.ones(n))
    f = model.sample(rng=rng)

    # grab the optimal latent value and make lists of observations
    xopt = f.argmax()
    fopt = f.max()

    # create a new figure
    fig = figure(figsize=(10, 6))

    while True:
        # evaluate the posterior before updating the model for plotting
        mu = model.get_quantile(0.5)
        lo = model.get_quantile(0.05)
        hi = model.get_quantile(0.95)

        # get our index
        target = mu.max()
        index = model.get_improvement(target)

        # query
        x = index.argmax()
        y = int(rng.uniform() < f[x])

        # add the data
        model.add_data(x, y)

        # PLOT EVERYTHING
        fig.clear()
        ax1 = fig.add_subplotspec((2, 1), (0, 0), hidex=True)
        ax2 = fig.add_subplotspec((2, 1), (1, 0), hidey=True, sharex=ax1)

        ax1.errorbar(np.arange(n), mu, (mu-lo, hi-mu), ls='', marker='s',
                     markersize=20, capsize=30, capthick=2)
        ax1.axvline(xopt, zorder=-1)
        ax1.axhline(fopt, zorder=-1)
        ax1.set_ylim(0, 1)
        ax1.set_xlim(-0.3, n-1+0.3)

        ax2.bar(np.arange(n)-0.25, index, 0.5)

        # draw
        fig.canvas.draw()
        show()
コード例 #9
0
ファイル: subprocess.py プロジェクト: Mehrian/pybo
def main():
    """Run the demo."""
    # grab a test function
    f = SubprocessQuery("bc <<< 'scale=8; x={}; -((x-3)^2)'")
    bounds = [0, 8]
    x = np.linspace(bounds[0], bounds[1], 500)

    # solve the model
    xbest, model, info = solve_bayesopt(f, bounds, niter=30, verbose=True)
    mu, s2 = model.predict(x[:, None])

    # plot the final model
    ax = figure().gca()
    ax.plot_banded(x, mu, 2*np.sqrt(s2))
    ax.axvline(xbest)
    ax.scatter(info.x.ravel(), info.y)
    ax.figure.canvas.draw()
    show()
コード例 #10
0
def main():
    """Run the demo."""
    # grab a test function
    bounds = [0, 2*np.pi]
    x = np.linspace(bounds[0], bounds[1], 500)

    # solve the model
    xbest, model, info = solve_bayesopt(f, bounds, niter=30, verbose=True)

    # make some predictions
    mu, s2 = model.predict(x[:, None])

    # plot the final model
    ax = figure().gca()
    ax.plot_banded(x, mu, 2*np.sqrt(s2))
    ax.axvline(xbest)
    ax.scatter(info.x.ravel(), info.y)
    ax.figure.canvas.draw()
    show()
コード例 #11
0
ファイル: interactive.py プロジェクト: Mehrian/pybo
def main():
    """Run the demo."""
    # initialize interactive function and 1d bounds
    f = InteractiveQuery()
    bounds = [0, 1]
    x = np.linspace(bounds[0], bounds[1], 100)

    # optimize the model and get final predictions
    xbest, model, info = solve_bayesopt(f, bounds, niter=10)
    mu, s2 = model.predict(x[:, None])

    # plot the final model
    fig = figure()
    axs = fig.gca()
    axs.plot_banded(x, mu, 2*np.sqrt(s2))
    axs.axvline(xbest)
    axs.scatter(info.x.ravel(), info.y)
    fig.canvas.draw()
    show()
コード例 #12
0
ファイル: posterior.py プロジェクト: appcoreopc/reggie
def main():
    """Run the demo."""
    # generate random data from a gp prior
    rng = np.random.RandomState(1)
    N = 5
    X = rng.uniform(-2, 2, size=(N, 1))
    Y = rng.uniform(-2, 2, size=N)
    x = np.linspace(X.min(), X.max(), 500)

    # create a GP and sample its prior
    gp = make_gp(0.01, 1, 0.3, kernel='se')
    pr_fs = gp.sample(x[:, None], 3, rng=rng)
    pr_mu, pr_s2 = gp.predict(x[:, None])

    # add data and sample the posterior
    gp.add_data(X, Y)
    po_fs = gp.sample(x[:, None], 3, rng=rng)
    po_mu, po_s2 = gp.predict(x[:, None])

    # plot the posterior
    fig = figure(w_pad=3)
    ax1 = fig.add_subplotspec((1, 2), (0, 0), hidexy=True)
    ax2 = fig.add_subplotspec((1, 2), (0, 1), hidexy=True, sharey=ax1)

    ax1.plot_banded(x, pr_mu, 3*np.sqrt(pr_s2))
    ax1.plot(x, pr_fs.T, ls='--')
    ax1.set_title('prior')
    ax1.set_ylim(-3.5, 3.5)

    ax2.plot_banded(x, po_mu, 3*np.sqrt(po_s2))
    ax2.plot(x, po_fs.T, ls='--')
    ax2.scatter(X.ravel(), Y, s=80, marker='+', zorder=3, lw=3)
    ax2.set_title('posterior')

    # draw/show it
    fig.canvas.draw()
    show()
コード例 #13
0
ファイル: posterior.py プロジェクト: psederberg/reggie
def main():
    """Run the demo."""
    # generate random data from a gp prior
    rng = np.random.RandomState(1)
    N = 5
    X = rng.uniform(-2, 2, size=(N, 1))
    Y = rng.uniform(-2, 2, size=N)
    x = np.linspace(X.min(), X.max(), 500)

    # create a GP and sample its prior
    gp = make_gp(0.01, 1, 0.3, kernel='se')
    pr_fs = gp.sample(x[:, None], 3, rng=rng)
    pr_mu, pr_s2 = gp.predict(x[:, None])

    # add data and sample the posterior
    gp.add_data(X, Y)
    po_fs = gp.sample(x[:, None], 3, rng=rng)
    po_mu, po_s2 = gp.predict(x[:, None])

    # plot the posterior
    fig = figure(w_pad=3)
    ax1 = fig.add_subplotspec((1, 2), (0, 0), hidexy=True)
    ax2 = fig.add_subplotspec((1, 2), (0, 1), hidexy=True, sharey=ax1)

    ax1.plot_banded(x, pr_mu, 3 * np.sqrt(pr_s2))
    ax1.plot(x, pr_fs.T, ls='--')
    ax1.set_title('prior')
    ax1.set_ylim(-3.5, 3.5)

    ax2.plot_banded(x, po_mu, 3 * np.sqrt(po_s2))
    ax2.plot(x, po_fs.T, ls='--')
    ax2.scatter(X.ravel(), Y, s=80, marker='+', zorder=3, lw=3)
    ax2.set_title('posterior')

    # draw/show it
    fig.canvas.draw()
    show()
コード例 #14
0
ファイル: animated2.py プロジェクト: Mehrian/pybo
def main():
    """Run the demo."""
    # define the bounds over which we'll optimize, the optimal x for comparison,
    # and a sequence of test points
    bounds = np.array([[-5, 10.], [0, 15]])
    xopt = np.array([np.pi, 2.275])
    x1, x2 = np.meshgrid(np.linspace(*bounds[0], num=100),
                         np.linspace(*bounds[1], num=100))
    xx = np.c_[x1.flatten(), x2.flatten()]

    # get initial data and some test points.
    X = list(inits.init_latin(bounds, 6))
    Y = [f(x_) for x_ in X]
    F = list()

    # initialize the model
    model = make_gp(0.01, 10, [1., 1.], 0)
    model.add_data(X, Y)

    # set a prior on the parameters
    model.params['like.sn2'].set_prior('uniform', 0.005, 0.015)
    model.params['kern.rho'].set_prior('lognormal', 0, 3)
    model.params['kern.ell'].set_prior('lognormal', 0, 3)
    model.params['mean.bias'].set_prior('normal', 0, 20)

    # make a model which samples parameters
    model = MCMC(model, n=10, rng=None)

    # create a new figure
    fig = figure(figsize=(10, 6))

    while True:
        # get index to solve it and plot it
        index = policies.EI(model, bounds, X, xi=0.1)

        # get the recommendation and the next query
        xbest = recommenders.best_incumbent(model, bounds, X)
        xnext, _ = solvers.solve_lbfgs(index, bounds)

        # observe and update model
        ynext = f(xnext)
        model.add_data(xnext, ynext)

        # evaluate the posterior and the acquisition function
        mu, s2 = model.predict(xx)

        # record our data and update the model
        X.append(xnext)
        Y.append(ynext)
        F.append(f(xbest))

        fig.clear()
        ax1 = fig.add_subplotspec((2, 2), (0, 0), hidex=True)
        ax2 = fig.add_subplotspec((2, 2), (1, 0), hidey=True, sharex=ax1)
        ax3 = fig.add_subplotspec((2, 2), (0, 1), rowspan=2)

        # plot the posterior and data
        ax1.contourf(x1, x2, mu.reshape(x1.shape), alpha=0.4)
        X_ = np.array(X)
        ax1.scatter(X_[:-1, 0], X_[:-1, 1], marker='.')
        ax1.scatter(xbest[0], xbest[1], linewidths=3, marker='o', color='r')
        ax1.scatter(xnext[0], xnext[1], linewidths=3, marker='o', color='g')
        ax1.set_xlim(*bounds[0])
        ax1.set_ylim(*bounds[1])
        ax1.set_title('current model (xbest and xnext)')

        # plot the acquisition function
        ax2.contourf(x1, x2, index(xx).reshape(x1.shape), alpha=0.5)
        ax2.scatter(xbest[0], xbest[1], linewidths=3, marker='o', color='r')
        ax2.scatter(xnext[0], xnext[1], linewidths=3, marker='o', color='g')
        ax2.set_xlim(*bounds[0])
        ax2.set_ylim(*bounds[1])
        ax2.set_title('current policy (xnext)')

        # plot the latent function at recomended points
        ax3.axhline(f(xopt))
        ax3.plot(F)
        ax3.set_ylim(-1., 0.)
        ax3.set_title('value of recommendation')

        # draw
        fig.canvas.draw()
        show(block=False)
コード例 #15
0
ファイル: pybodemo.py プロジェクト: hustpython/todoBOA
def main():
    """Run the demo."""
    # define the bounds over which we'll optimize, the optimal x for comparison,
    # and a sequence of test points
    bounds = np.array([[-5, 10.], [0, 15]])
    xopt = np.array([np.pi, 2.275])
    x1, x2 = np.meshgrid(np.linspace(*bounds[0], num=100),
                         np.linspace(*bounds[1], num=100))
    xx = np.c_[x1.flatten(), x2.flatten()]

    # get initial data and some test points.
    X = list(inits.init_latin(bounds, 6))
    Y = [f(x_) for x_ in X]
    F = list()

    # initialize the model
    model = make_gp(0.01, 10, [1., 1.], 0)
    model.add_data(X, Y)

    # set a prior on the parameters
    model.params['like.sn2'].set_prior('uniform', 0.005, 0.015)
    model.params['kern.rho'].set_prior('lognormal', 0, 3)
    model.params['kern.ell'].set_prior('lognormal', 0, 3)
    model.params['mean.bias'].set_prior('normal', 0, 20)

    # make a model which samples parameters
    model = MCMC(model, n=10, rng=None)

    # create a new figure
    fig = figure(figsize=(10, 6))

    while True:
        # get index to solve it and plot it
        index = policies.EI(model, bounds, X, xi=0.1)

        # get the recommendation and the next query
        xbest = recommenders.best_incumbent(model, bounds, X)
        xnext, _ = solvers.solve_lbfgs(index, bounds)

        # observe and update model
        ynext = f(xnext)
        model.add_data(xnext, ynext)

        # evaluate the posterior and the acquisition function
        mu, s2 = model.predict(xx)

        # record our data and update the model
        X.append(xnext)
        Y.append(ynext)
        F.append(f(xbest))

        fig.clear()
        ax1 = fig.add_subplotspec((2, 2), (0, 0), hidex=True)
        ax2 = fig.add_subplotspec((2, 2), (1, 0), hidey=True, sharex=ax1)
        ax3 = fig.add_subplotspec((2, 2), (0, 1), rowspan=2)

        # plot the posterior and data
        ax1.contourf(x1, x2, mu.reshape(x1.shape), alpha=0.4)
        X_ = np.array(X)
        ax1.scatter(X_[:-1, 0], X_[:-1, 1], marker='.')
        ax1.scatter(xbest[0], xbest[1], linewidths=3, marker='o', color='r')
        ax1.scatter(xnext[0], xnext[1], linewidths=3, marker='o', color='g')
        ax1.set_xlim(*bounds[0])
        ax1.set_ylim(*bounds[1])
        ax1.set_title('current model (xbest and xnext)')

        # plot the acquisition function
        ax2.contourf(x1, x2, index(xx).reshape(x1.shape), alpha=0.5)
        ax2.scatter(xbest[0], xbest[1], linewidths=3, marker='o', color='r')
        ax2.scatter(xnext[0], xnext[1], linewidths=3, marker='o', color='g')
        ax2.set_xlim(*bounds[0])
        ax2.set_ylim(*bounds[1])
        ax2.set_title('current policy (xnext)')

        # plot the latent function at recomended points
        ax3.axhline(f(xopt))
        ax3.plot(F)
        ax3.set_ylim(-1., 0.)
        ax3.set_title('value of recommendation')

        # draw
        fig.canvas.draw()
        show(block=False)
コード例 #16
0
ファイル: animated.py プロジェクト: vishalbelsare/pybo
def main():
    """Run the demo."""
    # define the bounds over which we'll optimize, the optimal x for
    # comparison, and a sequence of test points
    bounds = np.array([[0.5, 2.5]])
    xopt = 0.54856343
    fopt = f(xopt)
    x = np.linspace(bounds[0][0], bounds[0][1], 500)

    # get initial data and some test points.
    X = list(inits.init_latin(bounds, 3))
    Y = [f(x_) for x_ in X]
    F = []

    # initialize the model
    model = make_gp(0.01, 1.9, 0.1, 0)
    model.add_data(X, Y)

    # set a prior on the parameters
    model.params['like.sn2'].set_prior('uniform', 0.005, 0.015)
    model.params['kern.rho'].set_prior('lognormal', 0, 100)
    model.params['kern.ell'].set_prior('lognormal', 0, 10)
    model.params['mean.bias'].set_prior('normal', 0, 20)

    # make a model which samples parameters
    model = MCMC(model, n=20, rng=None)

    # create a new figure
    fig = figure(figsize=(10, 6))

    while True:
        # get acquisition function (or index)
        index = policies.EI(model, bounds, X, xi=0.1)

        # get the recommendation and the next query
        xbest = recommenders.best_incumbent(model, bounds, X)
        xnext, _ = solvers.solve_lbfgs(index, bounds)
        ynext = f(xnext)

        # evaluate the posterior before updating the model for plotting
        mu, s2 = model.predict(x[:, None])

        # record our data and update the model
        X.append(xnext)
        Y.append(ynext)
        F.append(f(xbest))
        model.add_data(xnext, ynext)

        # PLOT EVERYTHING
        fig.clear()
        ax1 = fig.add_subplotspec((2, 2), (0, 0), hidex=True)
        ax2 = fig.add_subplotspec((2, 2), (1, 0), hidey=True, sharex=ax1)
        ax3 = fig.add_subplotspec((2, 2), (0, 1), rowspan=2)

        # plot the posterior and data
        ax1.plot_banded(x, mu, 2*np.sqrt(s2))
        ax1.scatter(np.ravel(X), Y)
        ax1.axvline(xbest)
        ax1.axvline(xnext, color='g')
        ax1.set_ylim(-6, 3)
        ax1.set_title('current model (xbest and xnext)')

        # plot the acquisition function
        ax2.plot_banded(x, index(x[:, None]))
        ax2.axvline(xnext, color='g')
        ax2.set_xlim(*bounds)
        ax2.set_title('current policy (xnext)')

        # plot the latent function at recomended points
        ax3.plot(F)
        ax3.axhline(fopt)
        ax3.set_ylim(0.4, 0.9)
        ax3.set_title('value of recommendation')

        # draw
        fig.canvas.draw()
        show(block=False)