예제 #1
0
def callback_sigma_lmbda(model, bounds, info, x, index, ftrue):
    global D
    fig = pl.figure(1)
    fig.clf()

    sigma = 2**info[-1]['xbest'][0]
    lmbda = 2**info[-1]['xbest'][1]
    pl.title("sigma=%.2f, lmbda=%.6f" % (sigma, lmbda))
    gamma = 0.5 * (sigma**2)
    omega, u = sample_basis(D, m, gamma)
    theta = score_matching_sym(Z, lmbda, omega, u)
    logq_est = lambda x: np.dot(theta, feature_map_single(x, omega, u))
    dlogq_est = lambda x: np.dot(theta, feature_map_grad_single(x, omega, u))
    Xs = np.linspace(-3, 3)
    Ys = np.linspace(-3, 3)
    Q = evaluate_density_grid(Xs, Ys, logq_est)
    plot_array(Xs, Ys, Q, pl.gca(), plot_contour=True)
    pl.plot(Z[:, 0], Z[:, 1], 'bx')

    for ax in fig.axes:  # remove tick labels
        ax.set_xticklabels([])
        ax.set_yticklabels([])

    pl.draw()
    pl.show(block=False)
예제 #2
0
def test_feature_map():
    x = 3.
    u = 2.
    omega = 2.
    phi = feature_map_single(x, omega, u)
    phi_manual = np.cos(omega * x + u) * np.sqrt(2.)
    assert_close(phi, phi_manual)
예제 #3
0
def update_plot(val=None):
    global omega, u
    print("Updating plot")

    lmbda = 2**s_lmbda.val
    sigma = 2**s_sigma.val

    b = compute_b(Z, omega, u)
    C = compute_C(Z, omega, u)
    theta = score_matching_sym(Z, lmbda, omega, u, b, C)
    J = objective(Z, theta, lmbda, omega, u, b, C)
    J_xval = np.mean(
        xvalidate(Z, lmbda, omega, u, n_folds=5, num_repetitions=3))

    logq_est = lambda x: np.dot(theta, feature_map_single(x, omega, u))
    dlogq_est = lambda x: np.dot(theta, feature_map_grad_single(x, omega, u))

    description = "N=%d, sigma: %.2f, lambda: %.2f, m=%.d, J=%.2f, J_xval=%.2f" % \
        (N, sigma, lmbda, m, J, J_xval)

    if plot_pdf:
        D = evaluate_density_grid(Xs, Ys, logq_est)
        description = "log-pdf: " + description
    else:
        D = evaluate_density_grad_grid(Xs, Ys, dlogq_est)
        description = "norm-grad-log-pdf: " + description

    ax.clear()
    ax.plot(Z[:, 0], Z[:, 1], 'bx')
    plot_array(Xs, Ys, D, ax, plot_contour=True)

    ax.set_title(description)

    fig.canvas.draw_idle()
예제 #4
0
def test_feature_map_single_equals_feature_map():
    N = 10
    D = 20
    m = 3
    X = np.random.randn(N, D)
    omega = np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)

    phis = feature_map(X, omega, u)

    for i, x in enumerate(X):
        phi = feature_map_single(x, omega, u)
        assert_allclose(phis[i], phi)
예제 #5
0
m = N
# cma_opts = {'tolfun':0.3, 'maxiter':10, 'verb_disp':1}
# sigma, lmbda = select_sigma_lambda_cma(Z, m,
#                                        sigma0=sigma, lmbda0=lmbda,
#                                        cma_opts=cma_opts)

# cma_opts = {'tolfun':0.3, 'maxiter':10, 'verb_disp':1}
# sigma, lmbda = select_sigma_lambda_cma(Z, m,
#                                        sigma0=sigma, lmbda0=lmbda,
#                                        cma_opts=cma_opts)
# print sigma,lmbda

gamma = 0.5 * (sigma ** 2)
omega, u = sample_basis(D, m, gamma)
theta = score_matching_sym(Z, lmbda, omega, u)
logq_est = lambda x: log_pdf_estimate(feature_map_single(x, omega, u), theta)
dlogq_est = lambda x: log_pdf_estimate_grad(feature_map_grad_single(x, omega, u),
                                            theta)

# plot density estimate
plt.figure(figsize=(4, 8))
Xs = np.linspace(-15, 15)
Ys = np.linspace(-7, 3)
Xs_grad = np.linspace(-40, 40, 40)
Ys_grad = np.linspace(-15, 25, 40)
G = evaluate_density_grid(Xs, Ys, logq_est)
G_norm, quiver_U, quiver_V, _, _ = evaluate_gradient_grid(Xs_grad, Ys_grad, dlogq_est)
plt.subplot(211)
plt.plot(Z[:, 0], Z[:, 1], 'bx')
plot_array(Xs, Ys, np.exp(G), plot_contour=True)
plt.subplot(212)
예제 #6
0
def callback_lmbda(model, bounds, info, x, index, ftrue):
    global D
    """
    Plot the current posterior, the index, and the value of the current
    recommendation.
    """
    xmin, xmax = bounds[0]
    xx_ = np.linspace(xmin, xmax, 500)  # define grid
    xx = xx_[:, None]

    #     ff = ftrue(xx)                                      # compute true function
    acq = index(xx)  # compute acquisition

    mu, s2 = model.posterior(xx)  # compute posterior and
    lo = mu - 2 * np.sqrt(s2)  # quantiles
    hi = mu + 2 * np.sqrt(s2)

    #     ymin, ymax = ff.min(), ff.max()                     # get plotting ranges
    #     ymin -= 0.2 * (ymax - ymin)
    #     ymax += 0.2 * (ymax - ymin)

    kwplot = {'lw': 2, 'alpha': 0.5}  # common plotting kwargs

    fig = pl.figure(1)
    fig.clf()

    pl.subplot(221)
    #     pl.plot(xx, ff, 'k:', **kwplot)                     # plot true function
    pl.plot(xx, mu, 'b-', **kwplot)  # plot the posterior and
    pl.fill_between(xx_, lo, hi, color='b', alpha=0.1)  # uncertainty bands
    pl.scatter(
        info['x'],
        info['y'],  # plot data
        marker='o',
        facecolor='none',
        zorder=3)
    pl.axvline(x, color='r', **kwplot)  # latest selection
    pl.axvline(info[-1]['xbest'], color='g',
               **kwplot)  # current recommendation
    #     pl.axis((xmin, xmax, ymin, ymax))
    pl.ylabel('posterior')

    pl.subplot(223)
    pl.fill_between(
        xx_,
        acq.min(),
        acq,  # plot acquisition
        color='r',
        alpha=0.1)
    pl.axis('tight')
    pl.axvline(x, color='r', **kwplot)  # plot latest selection
    pl.xlabel('input')
    pl.ylabel('acquisition')

    pl.subplot(224)
    pl.plot(info['x'], 'g')

    pl.subplot(222)
    lmbda = 2**info[-1]['xbest']
    gamma = 0.5 * (sigma**2)
    omega, u = sample_basis(D, m, gamma)
    theta = score_matching_sym(Z, lmbda, omega, u)
    logq_est = lambda x: np.dot(theta, feature_map_single(x, omega, u))
    dlogq_est = lambda x: np.dot(theta, feature_map_grad_single(x, omega, u))
    Xs = np.linspace(-3, 3)
    Ys = np.linspace(-3, 3)
    Q = evaluate_density_grid(Xs, Ys, logq_est)
    plot_array(Xs, Ys, Q, pl.gca(), plot_contour=True)
    pl.plot(Z[:, 0], Z[:, 1], 'bx')

    for ax in fig.axes:  # remove tick labels
        ax.set_xticklabels([])
        ax.set_yticklabels([])

    pl.draw()
    pl.show(block=False)
예제 #7
0
 def log_pdf(self, x):
     phi = feature_map_single(x, self.omega, self.u)
     return np.dot(phi, self.theta)
예제 #8
0
    D = 2
    Z = np.random.randn(N, D)
    m = 200
    omega = gamma * np.random.randn(D, m)
    u = np.random.uniform(0, 2 * np.pi, m)
    theta = score_matching_sym(Z, lmbda, omega, u)

    # compute log-density and true log density
    Xs = np.linspace(-3, 3)
    Ys = np.linspace(-3, 3)
    D = np.zeros((len(Xs), len(Ys)))
    G = np.zeros(D.shape)
    for i in range(len(Xs)):
        for j in range(len(Ys)):
            x = np.array([Xs[i], Ys[j]])
            phi = feature_map_single(x, omega, u)
            D[j, i] = theta.dot(phi)
            G[j, i] = -np.linalg.norm(x) ** 2
    
    plt.figure(figsize=(16,4))
    plt.subplot(131)
    plt.imshow(G)
    plt.colorbar()
    plt.title("Truth")
    plt.subplot(132)
    plt.imshow(D)
    plt.title("Random features")
    plt.colorbar()
    plt.subplot(133)
    plt.plot(theta)
    plt.title("theta")