Пример #1
0
def main():

    # Load data
    X = pickle.load(open(data_fn, "rb"))
    N, D = X.shape

    # Model parameters
    alpha = 1.
    K = 4  # number of components
    mu_scale = 3.0
    covar_scale = 1.0

    # Sampling parameters
    n_runs = 2
    n_iter = 12

    # Intialize prior
    m_0 = np.zeros(D)
    k_0 = covar_scale**2 / mu_scale**2
    v_0 = D + 3
    S_0 = covar_scale**2 * v_0 * np.eye(D)
    prior = NIW(m_0, k_0, v_0, S_0)

    #  Initialize component assignment: this is not random for testing purposes
    z = np.array([i * np.ones(N / K) for i in range(K)],
                 dtype=np.int).flatten()

    # Setup FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, assignments=z)
    print("Initial log marginal prob:", fbgmm.log_marg())

    # Perform several Gibbs sampling runs and average the log marginals
    log_margs = np.zeros(n_iter)
    for j in range(n_runs):
        # Perform Gibbs sampling
        record = fbgmm.gibbs_sample(n_iter)
        log_margs += record["log_marg"]
    log_margs /= n_runs

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, fbgmm)
    for k in range(fbgmm.components.K):
        mu, sigma = fbgmm.components.rand_k(k)
        plot_ellipse(ax, mu, sigma)

    # Plot log probability
    plt.figure()
    plt.plot(list(range(n_iter)), log_margs)
    plt.xlabel("Iterations")
    plt.ylabel("Log prob")

    plt.show()
Пример #2
0
def main():

    # Load data
    X = pickle.load(open(data_fn, "rb"))
    N, D = X.shape

    # Model parameters
    alpha = 1.
    K = 4           # number of components
    mu_scale = 3.0
    covar_scale = 1.0

    # Sampling parameters
    n_runs = 2
    n_iter = 12

    # Intialize prior
    m_0 = np.zeros(D)
    k_0 = covar_scale**2/mu_scale**2
    v_0 = D + 3
    S_0 = covar_scale**2*v_0*np.eye(D)
    prior = NIW(m_0, k_0, v_0, S_0)

    #  Initialize component assignment: this is not random for testing purposes
    z = np.array([i*np.ones(N/K) for i in range(K)], dtype=np.int).flatten()

    # Setup FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, assignments=z)
    print("Initial log marginal prob:", fbgmm.log_marg())

    # Perform several Gibbs sampling runs and average the log marginals
    log_margs = np.zeros(n_iter)
    for j in range(n_runs):
        # Perform Gibbs sampling
        record = fbgmm.gibbs_sample(n_iter)
        log_margs += record["log_marg"]
    log_margs /= n_runs

    # Plot results
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plot_mixture_model(ax, fbgmm)
    for k in range(fbgmm.components.K):
        mu, sigma = fbgmm.components.rand_k(k)
        plot_ellipse(ax, mu, sigma)

    # Plot log probability
    plt.figure()
    plt.plot(list(range(n_iter)), log_margs)
    plt.xlabel("Iterations")
    plt.ylabel("Log prob")

    plt.show()
Пример #3
0
def test_sampling_2d_log_marg_deleted_components():

    random.seed(1)
    np.random.seed(1)

    # Data parameters
    D = 2           # dimensions
    N = 10          # number of points to generate
    K_true = 4      # the true number of components

    # Model parameters
    alpha = 1.
    K = 6           # number of components
    n_iter = 1

    # Generate data
    mu_scale = 4.0
    covar_scale = 0.7
    z_true = np.random.randint(0, K_true, N)
    mu = np.random.randn(D, K_true)*mu_scale
    X = mu[:, z_true] + np.random.randn(D, N)*covar_scale
    X = X.T

    # Intialize prior
    m_0 = np.zeros(D)
    k_0 = covar_scale**2/mu_scale**2
    v_0 = D + 3
    S_0 = covar_scale**2*v_0*np.eye(D)
    prior = NIW(m_0, k_0, v_0, S_0)

    # Setup FBGMM
    fbgmm = FBGMM(X, prior, alpha, K, "rand")

    # Perform Gibbs sampling
    record = fbgmm.gibbs_sample(n_iter)

    expected_log_marg = -60.1448630929
    log_marg = fbgmm.log_marg()

    print(fbgmm.components.assignments)

    npt.assert_almost_equal(log_marg, expected_log_marg)