def _test(alpha, beta, n): rv = InverseGamma(alpha=alpha, beta=beta) rv_sample = rv.sample(n) x = rv_sample.eval() x_tf = tf.constant(x, dtype=tf.float32) alpha = alpha.eval() beta = beta.eval() assert np.allclose( rv.log_prob(x_tf).eval(), stats.invgamma.logpdf(x, alpha, scale=beta))
def _test(alpha, beta, n): rv = InverseGamma(alpha=alpha, beta=beta) rv_sample = rv.sample(n) x = rv_sample.eval() x_tf = tf.constant(x, dtype=tf.float32) alpha = alpha.eval() beta = beta.eval() assert np.allclose(rv.log_prob(x_tf).eval(), stats.invgamma.logpdf(x, alpha, scale=beta))
init = tf.initialize_all_variables() init.run() for _ in range(inference.n_iter): info_dict = inference.update() inference.print_progress(info_dict) t = info_dict['t'] if t % inference.n_print == 0: print("Inferred cluster means:") print(sess.run(qmu.value())) # Average per-cluster and per-data point likelihood over many posterior samples. log_liks = [] for _ in range(100): mu_sample = qmu.sample() sigma_sample = qsigma.sample() # Take per-cluster and per-data point likelihood. log_lik = [] for k in range(K): x_post = Normal(mu=tf.ones([N, 1]) * tf.gather(mu_sample, k), sigma=tf.ones([N, 1]) * tf.gather(sigma_sample, k)) log_lik.append(tf.reduce_sum(x_post.log_prob(x_train), 1)) log_lik = tf.pack(log_lik) # has shape (K, N) log_liks.append(log_lik) log_liks = tf.reduce_mean(log_liks, 0) # Choose the cluster with the highest likelihood for each data point. clusters = tf.argmax(log_liks, 0).eval() plt.scatter(x_train[:, 0], x_train[:, 1], c=clusters, cmap=cm.bwr)
def _test(alpha, beta, n): x = InverseGamma(alpha=alpha, beta=beta) val_est = get_dims(x.sample(n)) val_true = n + get_dims(alpha) assert val_est == val_true
sess = ed.get_session() init = tf.global_variables_initializer() init.run() for _ in range(inference.n_iter): info_dict = inference.update() inference.print_progress(info_dict) t = info_dict['t'] if t % inference.n_print == 0: print("Inferred cluster means:") print(sess.run(qmu.mean())) # Calculate likelihood for each data point and cluster assignment, # averaged over many posterior samples. ``x_post`` has shape (N, 100, K, D). mu_sample = qmu.sample(100) sigma_sample = qsigma.sample(100) x_post = Normal(mu=tf.ones([N, 1, 1, 1]) * mu_sample, sigma=tf.ones([N, 1, 1, 1]) * sigma_sample) x_broadcasted = tf.tile(tf.reshape(x_train, [N, 1, 1, D]), [1, 100, K, 1]) # Sum over latent dimension, then average over posterior samples. # ``log_liks`` ends up with shape (N, K). log_liks = x_post.log_prob(x_broadcasted) log_liks = tf.reduce_sum(log_liks, 3) log_liks = tf.reduce_mean(log_liks, 1) # Choose the cluster with the highest likelihood for each data point. clusters = tf.argmax(log_liks, 1).eval() plt.scatter(x_train[:, 0], x_train[:, 1], c=clusters, cmap=cm.bwr) plt.axis([-3, 3, -3, 3]) plt.title("Predicted cluster assignments")
qmu_sigma = tf.nn.softplus(tf.Variable(tf.random_normal([K * D]))) qsigma_alpha = tf.nn.softplus(tf.Variable(tf.random_normal([K * D]))) qsigma_beta = tf.nn.softplus(tf.Variable(tf.random_normal([K * D]))) qpi = Dirichlet(alpha=qpi_alpha) qmu = Normal(mu=qmu_mu, sigma=qmu_sigma) qsigma = InverseGamma(alpha=qsigma_alpha, beta=qsigma_beta) data = {'x': x_train} inference = ed.KLqp({'pi': qpi, 'mu': qmu, 'sigma': qsigma}, data, model) inference.run(n_iter=2500, n_samples=10, n_minibatch=20) # Average per-cluster and per-data point likelihood over many posterior samples. log_liks = [] for s in range(100): zrep = { 'pi': qpi.sample(()), 'mu': qmu.sample(()), 'sigma': qsigma.sample(()) } log_liks += [model.predict(data, zrep)] log_liks = tf.reduce_mean(log_liks, 0) # Choose the cluster with the highest likelihood for each data point. clusters = tf.argmax(log_liks, 0).eval() plt.scatter(x_train[:, 0], x_train[:, 1], c=clusters, cmap=cm.bwr) plt.axis([-3, 3, -3, 3]) plt.title("Predicted cluster assignments") plt.show()
qpi_alpha = tf.nn.softplus(tf.Variable(tf.random_normal([K]))) qmu_mu = tf.Variable(tf.random_normal([K * D])) qmu_sigma = tf.nn.softplus(tf.Variable(tf.random_normal([K * D]))) qsigma_alpha = tf.nn.softplus(tf.Variable(tf.random_normal([K * D]))) qsigma_beta = tf.nn.softplus(tf.Variable(tf.random_normal([K * D]))) qpi = Dirichlet(alpha=qpi_alpha) qmu = Normal(mu=qmu_mu, sigma=qmu_sigma) qsigma = InverseGamma(alpha=qsigma_alpha, beta=qsigma_beta) data = {'x': x_train} inference = ed.KLqp({'pi': qpi, 'mu': qmu, 'sigma': qsigma}, data, model) inference.run(n_iter=2500, n_samples=10, n_minibatch=20) # Average per-cluster and per-data point likelihood over many posterior samples. log_liks = [] for s in range(100): zrep = {'pi': qpi.sample(()), 'mu': qmu.sample(()), 'sigma': qsigma.sample(())} log_liks += [model.predict(data, zrep)] log_liks = tf.reduce_mean(log_liks, 0) # Choose the cluster with the highest likelihood for each data point. clusters = tf.argmax(log_liks, 0).eval() plt.scatter(x_train[:, 0], x_train[:, 1], c=clusters, cmap=cm.bwr) plt.axis([-3, 3, -3, 3]) plt.title("Predicted cluster assignments") plt.show()