Skip to content

Commit

Permalink
Bugfix/issue 722 (#731)
Browse files Browse the repository at this point in the history
* fix noise std issue in SGLD

* use stepsizes annealing schedule of SGLD paper

* make SGLD tests pass
  • Loading branch information
emilemathieu authored and dustinvtran committed Aug 10, 2017
1 parent de5d0ac commit 79f4193
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions edward/inferences/sgld.py
Expand Up @@ -67,15 +67,16 @@ def build_update(self):
for z, qz in six.iteritems(self.latent_vars)}

# Simulate Langevin dynamics.
learning_rate = self.step_size / tf.cast(self.t + 1, tf.float32)
learning_rate = self.step_size / tf.pow(tf.cast(self.t + 1, tf.float32),
0.55)
grad_log_joint = tf.gradients(self._log_joint(old_sample),
list(six.itervalues(old_sample)))
sample = {}
for z, grad_log_p in zip(six.iterkeys(old_sample), grad_log_joint):
qz = self.latent_vars[z]
event_shape = qz.event_shape
normal = Normal(loc=tf.zeros(event_shape),
scale=learning_rate * tf.ones(event_shape))
scale=tf.sqrt(learning_rate) * tf.ones(event_shape))
sample[z] = old_sample[z] + \
0.5 * learning_rate * tf.convert_to_tensor(grad_log_p) + \
normal.sample()
Expand Down
4 changes: 2 additions & 2 deletions tests/test-inferences/test_sgld.py
Expand Up @@ -22,9 +22,9 @@ def test_normalnormal_run(self):

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

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

Expand Down

0 comments on commit 79f4193

Please sign in to comment.