Skip to content

Commit

Permalink
make miscellaneous revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinvtran committed Apr 16, 2017
1 parent 0dc4e5a commit ab363d5
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 56 deletions.
9 changes: 9 additions & 0 deletions docs/tex/api/inference-classes.tex
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ \subsubsection{Exact Inference}
.. autoclass:: edward.inferences.GANInference
:members:

.. autoclass:: edward.inferences.BiGANInference
:members:

.. autoclass:: edward.inferences.WGANInference
:members:

Expand All @@ -179,6 +182,9 @@ \subsubsection{Exact Inference}
.. autoclass:: edward.inferences.MetropolisHastings
:members:

.. autoclass:: edward.inferences.Gibbs
:members:

.. autoclass:: edward.inferences.HMC
:members:

Expand All @@ -188,6 +194,9 @@ \subsubsection{Exact Inference}
.. autoclass:: edward.inferences.SGHMC
:members:

.. automodule:: edward.inferences.conjugacy
:members: complete_conditional

%}

\subsubsection{References}\label{references}
9 changes: 5 additions & 4 deletions docs/tex/api/model-compositionality.tex
Original file line number Diff line number Diff line change
Expand Up @@ -163,26 +163,27 @@ \subsubsection{Bayesian Nonparametrics}
tutorial. This approach is also useful for Poisson process models.

In the lazy approach, we work directly on the infinite-dimensional space via
\href{https://www.tensorflow.org/versions/master/api_docs/python/control_flow_ops.html}{control flow operations}
\href{https://www.tensorflow.org/api_guides/python/control_flow_ops}{control flow operations}
in TensorFlow. At runtime, the control flow will execute only the
necessary computation in order to terminate. As an example, Edward
provides a \texttt{DirichletProcess} random variable.

\begin{lstlisting}[language=Python]
import matplotlib.pyplot as plt
from edward.models import DirichletProcess, Normal

def plot_dirichlet_process(alpha):
with tf.Session() as sess:
dp = DirichletProcess(alpha, Normal, mu=0.0, sigma=1.0)
dp = DirichletProcess(alpha, Normal(0.0, 1.0))
samples = sess.run(dp.sample(1000))
plt.hist(samples, bins=100, range=(-3.0, 3.0))
plt.title("DP({0}, N(0, 1))".format(alpha))
plt.show()

# Dirichlet process with high concentration
plot_dirichlet_process(alpha=1.0)
plot_dirichlet_process(1.0)
# Dirichlet process with low concentration (more spread out)
plot_dirichlet_process(alpha=50.0)
plot_dirichlet_process(50.0)
\end{lstlisting}

\includegraphics[width=350px]{/images/dirichlet-process-fig0.png}
Expand Down
2 changes: 1 addition & 1 deletion docs/tex/api/model-development.tex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ \subsubsection{Developing Custom Random Variables}
in the Github repository.
For more details and more methods one can implement, see the API
documentation in TensorFlow's
\href{https://www.tensorflow.org/versions/master/api_docs/python/contrib.distributions/base_classes#Distribution}{\texttt{Distribution} class}.
\href{https://www.tensorflow.org/api_docs/python/tf/contrib/distributions/Distribution}{\texttt{Distribution} class}.

\subsubsection{Advanced settings}

Expand Down
6 changes: 3 additions & 3 deletions docs/tex/contributing.tex
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ \subsubsection{Pull requests}

\textbf{Style guide.}
Follow
\href{https://www.tensorflow.org/versions/master/how_tos/style_guide.html}{TensorFlow's
\href{https://www.tensorflow.org/community/style_guide}{TensorFlow's
style guide}
for writing code, which more or less follows PEP 8.
Follow
\href{https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt}
{NumPy's documentation guide}
for writing documention,
and follow
\href{https://www.tensorflow.org/versions/master/how_tos/documentation/index.html}{TensorFlow's documentation guide}
\href{https://www.tensorflow.org/community/documentation}{TensorFlow's documentation guide}
for describing TensorFlow tensors in the documentation.

\textbf{Unit testing.}
Expand All @@ -71,7 +71,7 @@ \subsubsection{Pull requests}
as you are developing it.
Most pull requests require unit tests.
Follow
\href{https://www.tensorflow.org/versions/master/api_docs/python/test.html}
\href{https://www.tensorflow.org/api_guides/python/test}
{TensorFlow's testing guide}.
To run the unit tests, we use
\href{http://doc.pytest.org/}{pytest}:
Expand Down
2 changes: 1 addition & 1 deletion edward/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
HMC, MetropolisHastings, SGLD, SGHMC, \
KLpq, KLqp, ReparameterizationKLqp, ReparameterizationKLKLqp, \
ReparameterizationEntropyKLqp, ScoreKLqp, ScoreKLKLqp, ScoreEntropyKLqp, \
GANInference, BiGANInference, WGANInference, ImplicitKLqp, MAP, Laplace, \
GANInference, BiGANInference, WGANInference, ImplicitKLqp, MAP, Laplace, \
complete_conditional, Gibbs
from edward.models import RandomVariable
from edward.util import check_data, check_latent_vars, copy, dot, \
Expand Down
3 changes: 2 additions & 1 deletion edward/inferences/metropolis_hastings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections import OrderedDict
from edward.inferences.monte_carlo import MonteCarlo
from edward.models import RandomVariable
from edward.util import copy
from edward.util import check_latent_vars, copy

try:
from edward.models import Uniform
Expand Down Expand Up @@ -54,6 +54,7 @@ def __init__(self, latent_vars, proposal_vars, data=None):
>>> data = {x: np.array([0.0] * 10, dtype=np.float32)}
>>> inference = ed.MetropolisHastings({z: qz}, {z: proposal_z}, data)
"""
check_latent_vars(proposal_vars)
self.proposal_vars = proposal_vars
super(MetropolisHastings, self).__init__(latent_vars, data)

Expand Down
16 changes: 8 additions & 8 deletions examples/beta_bernoulli_conjugate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import edward as ed
import numpy as np
import six
import tensorflow as tf

from edward.models import Bernoulli, Beta
Expand All @@ -24,14 +25,13 @@
# COMPLETE CONDITIONAL
pi_cond = ed.complete_conditional(pi)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
sess = ed.get_session()
tf.global_variables_initializer().run()

print('p(pi | x) type:', pi_cond.parameters['name'])
relevant_params = {key: val for key, val in pi_cond.parameters.iteritems()
if isinstance(val, tf.Tensor)}
param_vals = sess.run(relevant_params.values(), {x: x_data})
param_vals = sess.run({key: val for
key, val in six.iteritems(pi_cond.parameters)
if isinstance(val, tf.Tensor)}, {x: x_data})
print('parameters:')
for i, j in enumerate(relevant_params.keys()):
print('%s:\t%.3f' % (j, param_vals[i]))
for key, val in six.iteritems(param_vals):
print('%s:\t%.3f' % (key, val))
18 changes: 9 additions & 9 deletions examples/mixture_gaussian_gibbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
plt.style.use('ggplot')

# Generate data
true_mu = np.array([-1., 0., 1.], np.float32) * 10
true_sigmasq = np.array([1.**2, 2.**2, 3.**2], np.float32)
true_mu = np.array([-1.0, 0.0, 1.0], np.float32) * 10
true_sigmasq = np.array([1.0**2, 2.0**2, 3.0**2], np.float32)
true_pi = np.array([0.2, 0.3, 0.5], np.float32)
N = 10000
K = len(true_mu)
true_z = np.random.choice(np.arange(K), size=N, p=true_pi)
x_val = true_mu[true_z] + np.random.randn(N) * np.sqrt(true_sigmasq[true_z])
x_data = true_mu[true_z] + np.random.randn(N) * np.sqrt(true_sigmasq[true_z])

# Prior hyperparameters
pi_alpha = 1. + np.zeros(K, dtype=np.float32)
mu_sigma = np.std(true_mu)
sigmasq_alpha = 1.
sigmasq_beta = 2.
sigmasq_alpha = 1.0
sigmasq_beta = 2.0

# Model
pi = Dirichlet(pi_alpha)
mu = Normal(0., mu_sigma, sample_shape=[K])
sigmasq = InverseGamma(sigmasq_alpha, sigmasq_beta, sample_shape=[K])
mu = Normal(0.0, mu_sigma, sample_shape=K)
sigmasq = InverseGamma(sigmasq_alpha, sigmasq_beta, sample_shape=K)
x = ParamMixture(pi, {'mu': mu, 'sigma': tf.sqrt(sigmasq)}, Normal,
sample_shape=N)
z = x.cat
Expand All @@ -59,7 +59,7 @@
print()

# Gibbs sampler
cond_dict = {pi: pi_est, mu: mu_est, sigmasq: sigmasq_est, z: z_est, x: x_val}
cond_dict = {pi: pi_est, mu: mu_est, sigmasq: sigmasq_est, z: z_est, x: x_data}
t0 = time()
T = 500
for t in range(T):
Expand Down Expand Up @@ -88,7 +88,7 @@

plt.figure(figsize=[10, 10])
plt.subplot(2, 1, 1)
plt.hist(x_val, 50)
plt.hist(x_data, 50)
plt.title('Empirical Distribution of $x$')
plt.xlabel('$x$')
plt.ylabel('frequency')
Expand Down
54 changes: 28 additions & 26 deletions tests/test-inferences/test_conjugacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def test_basic_bernoulli(self):
z_cond = ed.complete_conditional(z, [z])
self.assertIsInstance(z_cond, rvs.Bernoulli)

sess = tf.InteractiveSession()
p_val = sess.run(z_cond.p)
with self.test_session() as sess:
p_val = sess.run(z_cond.p)

self.assertAllClose(p_val, 0.75 + np.zeros(N, np.float32))

Expand All @@ -28,8 +28,8 @@ def test_incomplete_blanket(self):
z_cond = ed.complete_conditional(z, [])
self.assertIsInstance(z_cond, rvs.Bernoulli)

sess = tf.InteractiveSession()
p_val = sess.run(z_cond.p)
with self.test_session() as sess:
p_val = sess.run(z_cond.p)

self.assertAllClose(p_val, 0.75 + np.zeros(N, np.float32))

Expand All @@ -39,8 +39,8 @@ def test_missing_blanket(self):
z_cond = ed.complete_conditional(z)
self.assertIsInstance(z_cond, rvs.Bernoulli)

sess = tf.InteractiveSession()
p_val = sess.run(z_cond.p)
with self.test_session() as sess:
p_val = sess.run(z_cond.p)

self.assertAllClose(p_val, 0.75 + np.zeros(N, np.float32))

Expand All @@ -56,8 +56,8 @@ def test_beta_bernoulli(self):

self.assertIsInstance(pi_cond, rvs.Beta)

sess = tf.InteractiveSession()
a_val, b_val = sess.run([pi_cond.a, pi_cond.b], {x: x_data})
with self.test_session() as sess:
a_val, b_val = sess.run([pi_cond.a, pi_cond.b], {x: x_data})

self.assertAllClose(a_val, a0 + x_data.sum())
self.assertAllClose(b_val, b0 + (1 - x_data).sum())
Expand All @@ -74,8 +74,10 @@ def test_gamma_poisson(self):

self.assertIsInstance(lam_cond, rvs.Gamma)

sess = tf.InteractiveSession()
alpha_val, beta_val = sess.run([lam_cond.alpha, lam_cond.beta], {x: x_data})
with self.test_session() as sess:
alpha_val, beta_val = sess.run(
[lam_cond.alpha, lam_cond.beta], {x: x_data})

self.assertAllClose(alpha_val, alpha0 + x_data.sum())
self.assertAllClose(beta_val, beta0 + len(x_data))

Expand All @@ -93,9 +95,9 @@ def test_gamma_gamma(self):

self.assertIsInstance(beta_cond, rvs.Gamma)

sess = tf.InteractiveSession()
alpha_val, beta_val = sess.run([beta_cond.alpha, beta_cond.beta],
{x: x_data})
with self.test_session() as sess:
alpha_val, beta_val = sess.run([beta_cond.alpha, beta_cond.beta],
{x: x_data})
self.assertAllClose(alpha_val, alpha0 + alpha_likelihood * len(x_data))
self.assertAllClose(beta_val, beta0 + x_data.sum())

Expand All @@ -113,9 +115,9 @@ def test_mul_rate_gamma(self):

self.assertIsInstance(beta_cond, rvs.Gamma)

sess = tf.InteractiveSession()
alpha_val, beta_val = sess.run([beta_cond.alpha, beta_cond.beta],
{x: x_data})
with self.test_session() as sess:
alpha_val, beta_val = sess.run([beta_cond.alpha, beta_cond.beta],
{x: x_data})
self.assertAllClose(alpha_val, alpha0 + alpha_likelihood * len(x_data))
self.assertAllClose(beta_val, beta0 + alpha_likelihood * x_data.sum())

Expand All @@ -132,8 +134,8 @@ def test_normal_normal(self):
mu_cond = ed.complete_conditional(mu, [mu, x])
self.assertIsInstance(mu_cond, rvs.Normal)

sess = tf.InteractiveSession()
mu_val, sigma_val = sess.run([mu_cond.mu, mu_cond.sigma], {x: x_data})
with self.test_session() as sess:
mu_val, sigma_val = sess.run([mu_cond.mu, mu_cond.sigma], {x: x_data})

self.assertAllClose(sigma_val, (1.0 / sigma0**2 +
len(x_data) / sigma_likelihood**2) ** -0.5)
Expand All @@ -156,8 +158,8 @@ def test_normal_normal_scaled(self):
mu_cond = ed.complete_conditional(mu, [mu, x])
self.assertIsInstance(mu_cond, rvs.Normal)

sess = tf.InteractiveSession()
mu_val, sigma_val = sess.run([mu_cond.mu, mu_cond.sigma], {x: x_data})
with self.test_session() as sess:
mu_val, sigma_val = sess.run([mu_cond.mu, mu_cond.sigma], {x: x_data})

self.assertAllClose(sigma_val,
(1.0 / sigma0**2 +
Expand All @@ -181,8 +183,8 @@ def test_dirichlet_categorical(self):
blanket = [theta, x]
theta_cond = ed.complete_conditional(theta, blanket)

sess = tf.InteractiveSession()
alpha_val = sess.run(theta_cond.alpha, {x: x_data})
with self.test_session() as sess:
alpha_val = sess.run(theta_cond.alpha, {x: x_data})

self.assertAllClose(alpha_val, np.array([6.0, 5.0, 4.0, 3.0], np.float32))

Expand Down Expand Up @@ -211,10 +213,10 @@ def test_mog(self):
pi_cond = ed.complete_conditional(pi, blanket)
z_cond = ed.complete_conditional(z, blanket)

sess = tf.InteractiveSession()
pi_cond_alpha, mu_cond_mu, mu_cond_sigma, z_cond_p = (
sess.run([pi_cond.alpha, mu_cond.mu, mu_cond.sigma, z_cond.p],
{z: z_val, x: x_val, pi: pi_val, mu: mu_val}))
with self.test_session() as sess:
pi_cond_alpha, mu_cond_mu, mu_cond_sigma, z_cond_p = (
sess.run([pi_cond.alpha, mu_cond.mu, mu_cond.sigma, z_cond.p],
{z: z_val, x: x_val, pi: pi_val, mu: mu_val}))

true_pi = pi_alpha + np.unique(z_val, return_counts=True)[1]
self.assertAllClose(pi_cond_alpha, true_pi)
Expand Down
6 changes: 3 additions & 3 deletions tests/test-inferences/test_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def test_mul_n(self):
ab = simplify._mul_n([a, b])
abc = simplify._mul_n([a, b, c])

sess = tf.InteractiveSession()
self.assertEqual(sess.run(ab), 2.0)
self.assertEqual(sess.run(abc), 6.0)
with self.test_session() as sess:
self.assertEqual(sess.run(ab), 2.0)
self.assertEqual(sess.run(abc), 6.0)

def test_is_number(self):
self.assertTrue(simplify.is_number(1))
Expand Down

0 comments on commit ab363d5

Please sign in to comment.