def test_diag_fit_kmeans(): model1, _ = create_diag_pdf() model1.initialize() x = model1.sample(num_samples_train) x_val = model1.sample(num_samples) model2 = GMM(num_comp=4, x_dim=x_dim) model2.initialize(x) elbo = model2.fit(x, x_val=x_val) model2_diag = GMMDiagCov(num_comp=4, x_dim=x_dim) model2_diag.initialize(x) elbo_diag = model2_diag.fit(x, x_val=x_val) model2.plot2D(feat_idx=[0, 1], num_sigmas=1) plt.savefig(output_dir + '/plot_fit_kmeans_init_D01.pdf') plt.close() model2.plot2D(feat_idx=[0, 2], num_sigmas=1) plt.savefig(output_dir + '/plot_fit_kmeans_init_D02.pdf') plt.close() plt.figure() plt.plot(np.repeat(model1.elbo(x) / x.shape[0], len(elbo[1])), 'b') plt.plot(np.repeat(model1.elbo(x_val) / x.shape[0], len(elbo[1])), 'b--') plt.plot(elbo[1], 'r') plt.plot(elbo[3], 'r--') plt.plot(elbo_diag[1], 'g') plt.plot(elbo_diag[3], 'g--') plt.savefig(output_dir + '/fit_kmeans_init_elbo.pdf') plt.close()
def test_initialize_kmeans(): model1 = create_pdf() x = model1.sample(num_samples=num_samples_init) model2 = GMMDiagCov(num_comp=4, x_dim=x_dim) model2.initialize(x)
def test_initialize_stdnormal(): model = GMMDiagCov(num_comp=1, x_dim=x_dim) model.initialize() assert (model.pi == 1) assert_allclose(model.mu, np.zeros((1, x_dim))) assert_allclose(model.Lambda, np.ones((1, x_dim)))
def test_initialize(): model1 = create_pdf() model1.initialize() model2 = GMMDiagCov(num_comp=model1.num_comp, pi=model1.pi, eta=model1.eta, x_dim=model1.x_dim) model2.initialize() model3 = GMMDiagCov(num_comp=model2.num_comp, pi=model2.pi, mu=model2.mu, Lambda=model2.Lambda, x_dim=model1.x_dim) model3.initialize() assert_allclose(model1.eta, model2.eta) assert_allclose(model1.eta, model3.eta) assert_allclose(model1.A, model2.A) assert_allclose(model1.A, model3.A) assert_allclose(model1.mu, model2.mu) assert_allclose(model1.mu, model3.mu) assert_allclose(model1.Lambda, model2.Lambda) assert_allclose(model1.Lambda, model3.Lambda)
def create_pdf(): model = GMMDiagCov(num_comp=len(pi1), pi=pi1, mu=mu1, Lambda=1 / S1, x_dim=x_dim) return model
def test_fit_kmeans_split4(): model1 = create_pdf() model1.initialize() x = model1.sample(num_samples_train) x_val = model1.sample(num_samples) model2 = GMMDiagCov(num_comp=1, x_dim=x_dim) model2.initialize() elbo = model2.fit(x, x_val=x_val, epochs=1) model2 = model2.split_comp(4) elbo = model2.fit(x, x_val=x_val) model1.plot2D(feat_idx=[0, 1], num_sigmas=1) plt.savefig(output_dir + '/plot_fit_split4_init_D01.pdf') plt.close() model1.plot2D(feat_idx=[0, 2], num_sigmas=1) plt.savefig(output_dir + '/plot_fit_split4_init_D02.pdf') plt.close() plt.figure() plt.plot(np.repeat(model1.elbo(x) / x.shape[0], len(elbo[1])), 'b') plt.plot(np.repeat(model1.elbo(x_val) / x.shape[0], len(elbo[1])), 'b--') plt.plot(elbo[1], 'r') plt.plot(elbo[3], 'r--') plt.savefig(output_dir + '/fit_split4_init_elbo.pdf') plt.close()