Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a few inference tests #487

Merged
merged 21 commits into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
68f7c16
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Oct 1, 2016
094c2f4
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Oct 4, 2016
f6ff5e6
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Oct 11, 2016
5b84ae8
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Oct 18, 2016
45ec7f7
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Oct 25, 2016
18212c8
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Nov 8, 2016
4268e20
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Nov 12, 2016
9b0ee96
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Nov 13, 2016
99abf4d
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Nov 14, 2016
fa4e9b3
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Dec 14, 2016
f1f766c
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Feb 13, 2017
e642d6c
Merge branch 'master' of github.com:blei-lab/edward
akucukelbir Feb 16, 2017
4814c40
basic test file structure
akucukelbir Feb 16, 2017
a55a3a8
klpq and map tests
akucukelbir Feb 16, 2017
473b32a
cleanup vi tests, add mh test
akucukelbir Feb 16, 2017
842b71c
more MCMC tests
akucukelbir Feb 16, 2017
5d4f290
found magic combination for SGLD to converge
akucukelbir Feb 16, 2017
77523fd
Merge branch 'master' of github.com:blei-lab/edward into tests/infere…
akucukelbir Feb 28, 2017
b9d3495
fixed map, disabled sgld and klpq tests
akucukelbir Feb 28, 2017
9b12d9b
fix test_sgld.py
dustinvtran Feb 28, 2017
5ba1214
fix test_klpq.py
dustinvtran Feb 28, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tests/test-inferences/test_hmc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

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

from edward.models import Normal, Empirical


class test_hmc_class(tf.test.TestCase):

def test_normalnormal_run(self):
with self.test_session() as sess:
x_data = np.array([0.0] * 50, dtype=np.float32)

mu = Normal(mu=0.0, sigma=1.0)
x = Normal(mu=tf.ones(50) * mu, sigma=1.0)

qmu = Empirical(params=tf.Variable(tf.ones(2000)))

# analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140)
inference = ed.HMC({mu: qmu}, data={x: x_data})
inference.run()

self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-2, atol=1e-2)
self.assertAllClose(qmu.std().eval(), np.sqrt(1 / 51),
rtol=1e-2, atol=1e-2)

if __name__ == '__main__':
ed.set_seed(42)
tf.test.main()
35 changes: 35 additions & 0 deletions tests/test-inferences/test_klpq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

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

from edward.models import Normal


class test_klpq_class(tf.test.TestCase):

def test_normalnormal_run(self):
with self.test_session() as sess:
x_data = np.array([0.0] * 50, dtype=np.float32)

mu = Normal(mu=0.0, sigma=1.0)
x = Normal(mu=tf.ones(50) * mu, sigma=1.0)

qmu_mu = tf.Variable(tf.random_normal([]))
qmu_sigma = tf.nn.softplus(tf.Variable(tf.random_normal([])))
qmu = Normal(mu=qmu_mu, sigma=qmu_sigma)

# analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140)
inference = ed.KLpq({mu: qmu}, data={x: x_data})
inference.run(n_samples=25, n_iter=100)

self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-1, atol=1e-1)
self.assertAllClose(qmu.std().eval(), np.sqrt(1 / 51),
rtol=1e-1, atol=1e-1)

if __name__ == '__main__':
ed.set_seed(42)
tf.test.main()
35 changes: 35 additions & 0 deletions tests/test-inferences/test_klqp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

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

from edward.models import Normal


class test_klqp_class(tf.test.TestCase):

def test_normalnormal_run(self):
with self.test_session() as sess:
x_data = np.array([0.0] * 50, dtype=np.float32)

mu = Normal(mu=0.0, sigma=1.0)
x = Normal(mu=tf.ones(50) * mu, sigma=1.0)

qmu_mu = tf.Variable(tf.random_normal([]))
qmu_sigma = tf.nn.softplus(tf.Variable(tf.random_normal([])))
qmu = Normal(mu=qmu_mu, sigma=qmu_sigma)

# analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140)
inference = ed.KLqp({mu: qmu}, data={x: x_data})
inference.run(n_iter=5000)

self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-2, atol=1e-2)
self.assertAllClose(qmu.std().eval(), np.sqrt(1 / 51),
rtol=1e-2, atol=1e-2)

if __name__ == '__main__':
ed.set_seed(42)
tf.test.main()
31 changes: 31 additions & 0 deletions tests/test-inferences/test_map.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

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

from edward.models import Normal, PointMass


class test_map_class(tf.test.TestCase):

def test_normalnormal_run(self):
with self.test_session() as sess:
x_data = np.array([0.0] * 50, dtype=np.float32)

mu = Normal(mu=0.0, sigma=1.0)
x = Normal(mu=tf.ones(50) * mu, sigma=1.0)

qmu = PointMass(params=tf.Variable(tf.ones([])))

# analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140)
inference = ed.MAP({mu: qmu}, data={x: x_data})
inference.run(n_iter=1000)

self.assertAllClose(qmu.mean().eval(), 0)

if __name__ == '__main__':
ed.set_seed(42)
tf.test.main()
36 changes: 36 additions & 0 deletions tests/test-inferences/test_metropolishastings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

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

from edward.models import Normal, Empirical


class test_metropolishastings_class(tf.test.TestCase):

def test_normalnormal_run(self):
with self.test_session() as sess:
x_data = np.array([0.0] * 50, dtype=np.float32)

mu = Normal(mu=0.0, sigma=1.0)
x = Normal(mu=tf.ones(50) * mu, sigma=1.0)

qmu = Empirical(params=tf.Variable(tf.ones(2000)))
proposal_mu = Normal(mu=0.0, sigma=1.0)

# analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140)
inference = ed.MetropolisHastings({mu: qmu},
{mu: proposal_mu},
data={x: x_data})
inference.run()

self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-2, atol=1e-2)
self.assertAllClose(qmu.std().eval(), np.sqrt(1 / 51),
rtol=1e-2, atol=1e-2)

if __name__ == '__main__':
ed.set_seed(42)
tf.test.main()
33 changes: 33 additions & 0 deletions tests/test-inferences/test_sgld.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

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

from edward.models import Normal, Empirical


class test_sgld_class(tf.test.TestCase):

def test_normalnormal_run(self):
with self.test_session() as sess:
x_data = np.array([0.0] * 50, dtype=np.float32)

mu = Normal(mu=0.0, sigma=1.0)
x = Normal(mu=tf.ones(50) * mu, sigma=1.0)

qmu = Empirical(params=tf.Variable(tf.ones(5000)))

# analytic solution: N(mu=0.0, sigma=\sqrt{1/51}=0.140)
inference = ed.SGLD({mu: qmu}, data={x: x_data})
inference.run(step_size=0.2)

self.assertAllClose(qmu.mean().eval(), 0, rtol=1e-2, atol=1e-2)
self.assertAllClose(qmu.std().eval(), np.sqrt(1 / 51),
rtol=5e-2, atol=5e-2)

if __name__ == '__main__':
ed.set_seed(42)
tf.test.main()