Skip to content

Commit

Permalink
update unit tests for ed.{evaluate,ppc}
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinvtran committed Mar 19, 2017
1 parent cae7670 commit edc9d32
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 65 deletions.
86 changes: 47 additions & 39 deletions tests/test-criticisms/test_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,59 @@
class test_evaluate_class(tf.test.TestCase):

def test_metrics(self):
tf.InteractiveSession()
x = Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
ed.evaluate('mean_squared_error', {x: x_data}, n_samples=1)
ed.evaluate(['mean_squared_error'], {x: x_data}, n_samples=1)
ed.evaluate(['mean_squared_error', 'mean_absolute_error'],
{x: x_data}, n_samples=1)
self.assertRaises(NotImplementedError, ed.evaluate, 'hello world',
{x: x_data}, n_samples=1)
with self.test_session():
x = Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
ed.evaluate('mean_squared_error', {x: x_data}, n_samples=1)
ed.evaluate(['mean_squared_error'], {x: x_data}, n_samples=1)
ed.evaluate(['mean_squared_error', 'mean_absolute_error'],
{x: x_data}, n_samples=1)
self.assertRaises(TypeError, ed.evaluate, x, {x: x_data}, n_samples=1)
self.assertRaises(NotImplementedError, ed.evaluate, 'hello world',
{x: x_data}, n_samples=1)

def test_data(self):
tf.InteractiveSession()
x_ph = tf.placeholder(tf.float32, [])
x = Normal(mu=x_ph, sigma=1.0)
y = 2.0 * Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
x_ph_data = np.array(0.0)
y_data = tf.constant(20.0)
ed.evaluate('mean_squared_error', {x: x_data, x_ph: x_ph_data},
n_samples=1)
ed.evaluate('mean_squared_error', {y: y_data}, n_samples=1)
with self.test_session():
x_ph = tf.placeholder(tf.float32, [])
x = Normal(mu=x_ph, sigma=1.0)
y = 2.0 * Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
x_ph_data = np.array(0.0)
y_data = tf.constant(20.0)
ed.evaluate('mean_squared_error', {x: x_data, x_ph: x_ph_data},
n_samples=1)
ed.evaluate('mean_squared_error', {y: y_data}, n_samples=1)
self.assertRaises(TypeError, ed.evaluate, 'mean_squared_error',
{'y': y_data}, n_samples=1)

def test_n_samples(self):
tf.InteractiveSession()
x = Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
ed.evaluate('mean_squared_error', {x: x_data}, n_samples=1)
ed.evaluate('mean_squared_error', {x: x_data}, n_samples=50)
with self.test_session():
x = Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
ed.evaluate('mean_squared_error', {x: x_data}, n_samples=1)
ed.evaluate('mean_squared_error', {x: x_data}, n_samples=5)
self.assertRaises(TypeError, ed.evaluate, 'mean_squared_error',
{x: x_data}, n_samples='1')

def test_output_key(self):
tf.InteractiveSession()
x_ph = tf.placeholder(tf.float32, [])
x = Normal(mu=x_ph, sigma=1.0)
y = 2.0 * x
x_data = tf.constant(0.0)
x_ph_data = np.array(0.0)
y_data = tf.constant(20.0)
ed.evaluate('mean_squared_error', {x: x_data, x_ph: x_ph_data},
n_samples=1)
ed.evaluate('mean_squared_error', {y: y_data, x_ph: x_ph_data},
n_samples=1)
ed.evaluate('mean_squared_error', {x: x_data, y: y_data, x_ph: x_ph_data},
n_samples=1, output_key=x)
self.assertRaises(KeyError, ed.evaluate, 'mean_squared_error',
{x: x_data, y: y_data, x_ph: x_ph_data}, n_samples=1)
with self.test_session():
x_ph = tf.placeholder(tf.float32, [])
x = Normal(mu=x_ph, sigma=1.0)
y = 2.0 * x
x_data = tf.constant(0.0)
x_ph_data = np.array(0.0)
y_data = tf.constant(20.0)
ed.evaluate('mean_squared_error', {x: x_data, x_ph: x_ph_data},
n_samples=1)
ed.evaluate('mean_squared_error', {y: y_data, x_ph: x_ph_data},
n_samples=1)
ed.evaluate('mean_squared_error', {x: x_data, y: y_data, x_ph: x_ph_data},
n_samples=1, output_key=x)
self.assertRaises(KeyError, ed.evaluate, 'mean_squared_error',
{x: x_data, y: y_data, x_ph: x_ph_data}, n_samples=1)
self.assertRaises(TypeError, ed.evaluate, 'mean_squared_error',
{x: x_data, y: y_data, x_ph: x_ph_data}, n_samples=1,
output_key='x')

if __name__ == '__main__':
tf.test.main()
58 changes: 32 additions & 26 deletions tests/test-criticisms/test_ppc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,42 @@
class test_ppc_class(tf.test.TestCase):

def test_data(self):
tf.InteractiveSession()
x = Normal(mu=0.0, sigma=1.0)
y = 2.0 * x
x_data = tf.constant(0.0)
y_data = tf.constant(0.0)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]), {x: x_data}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[y]), {y: y_data}, n_samples=1)
with self.test_session():
x = Normal(mu=0.0, sigma=1.0)
y = 2.0 * x
x_data = tf.constant(0.0)
y_data = tf.constant(0.0)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]), {x: x_data}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[y]), {y: y_data}, n_samples=1)
self.assertRaises(TypeError, ed.ppc, lambda xs, zs: tf.reduce_mean(xs[y]),
{'y': y_data}, n_samples=1)

def test_latent_vars(self):
tf.InteractiveSession()
x = Normal(mu=0.0, sigma=1.0)
y = 2.0 * x
z = Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
y_data = tf.constant(0.0)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]) + tf.reduce_mean(zs[z]),
{x: x_data}, {z: z}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]) + tf.reduce_mean(zs[z]),
{x: x_data}, {z: y}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]) + tf.reduce_mean(zs[y]),
{x: x_data}, {y: y}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]) + tf.reduce_mean(zs[y]),
{x: x_data}, {y: z}, n_samples=1)
with self.test_session():
x = Normal(mu=0.0, sigma=1.0)
y = 2.0 * x
z = Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
y_data = tf.constant(0.0)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]) + tf.reduce_mean(zs[z]),
{x: x_data}, {z: z}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]) + tf.reduce_mean(zs[z]),
{x: x_data}, {z: y}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]) + tf.reduce_mean(zs[y]),
{x: x_data}, {y: y}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]) + tf.reduce_mean(zs[y]),
{x: x_data}, {y: z}, n_samples=1)
self.assertRaises(TypeError, ed.ppc, lambda xs, zs: tf.reduce_mean(xs[x]),
{x: x_data}, {'y': z}, n_samples=1)

def test_n_samples(self):
tf.InteractiveSession()
x = Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]), {x: x_data}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]), {x: x_data}, n_samples=10)
with self.test_session():
x = Normal(mu=0.0, sigma=1.0)
x_data = tf.constant(0.0)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]), {x: x_data}, n_samples=1)
ed.ppc(lambda xs, zs: tf.reduce_mean(xs[x]), {x: x_data}, n_samples=5)
self.assertRaises(TypeError, ed.ppc, lambda xs, zs: tf.reduce_mean(xs[x]),
{x: x_data}, n_samples='1')

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

0 comments on commit edc9d32

Please sign in to comment.