Skip to content

Commit

Permalink
remove need for rvs for internal random_ops (blei-lab#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinvtran authored Jan 22, 2018
1 parent daf086a commit 052731a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 47 deletions.
12 changes: 2 additions & 10 deletions edward/inferences/hmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
from edward.models import RandomVariable
from edward.util import copy

try:
from edward.models import Normal, Uniform
except Exception as e:
raise ImportError("{0}. Your TensorFlow version is not supported.".format(e))


class HMC(MonteCarlo):
"""Hamiltonian Monte Carlo, also known as hybrid Monte Carlo
Expand Down Expand Up @@ -93,9 +88,7 @@ def build_update(self):
old_r_sample = OrderedDict()
for z, qz in six.iteritems(self.latent_vars_unconstrained):
event_shape = qz.event_shape
normal = Normal(loc=tf.zeros(event_shape, dtype=qz.dtype),
scale=tf.ones(event_shape, dtype=qz.dtype))
old_r_sample[z] = normal.sample()
old_r_sample[z] = tf.random_normal(event_shape, dtype=qz.dtype)

# Simulate Hamiltonian dynamics.
new_sample, new_r_sample = leapfrog(old_sample, old_r_sample,
Expand All @@ -112,8 +105,7 @@ def build_update(self):
ratio -= self._log_joint_unconstrained(old_sample)

# Accept or reject sample.
u = Uniform(low=tf.constant(0.0, dtype=ratio.dtype),
high=tf.constant(1.0, dtype=ratio.dtype)).sample()
u = tf.random_uniform([], dtype=ratio.dtype)
accept = tf.log(u) < ratio
sample_values = tf.cond(accept, lambda: list(six.itervalues(new_sample)),
lambda: list(six.itervalues(old_sample)))
Expand Down
8 changes: 1 addition & 7 deletions edward/inferences/metropolis_hastings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
from edward.models import RandomVariable
from edward.util import check_latent_vars, copy

try:
from edward.models import Uniform
except Exception as e:
raise ImportError("{0}. Your TensorFlow version is not supported.".format(e))


class MetropolisHastings(MonteCarlo):
"""Metropolis-Hastings [@metropolis1953equation; @hastings1970monte].
Expand Down Expand Up @@ -139,8 +134,7 @@ def build_update(self):
ratio -= tf.reduce_sum(x_zold.log_prob(dict_swap[x]))

# Accept or reject sample.
u = Uniform(low=tf.constant(0.0, dtype=ratio.dtype),
high=tf.constant(1.0, dtype=ratio.dtype)).sample()
u = tf.random_uniform([], dtype=ratio.dtype)
accept = tf.log(u) < ratio
sample_values = tf.cond(accept, lambda: list(six.itervalues(new_sample)),
lambda: list(six.itervalues(old_sample)))
Expand Down
13 changes: 3 additions & 10 deletions edward/inferences/sghmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
from edward.models import RandomVariable, Empirical
from edward.util import copy

try:
from edward.models import Normal
except Exception as e:
raise ImportError("{0}. Your TensorFlow version is not supported.".format(e))


class SGHMC(MonteCarlo):
"""Stochastic gradient Hamiltonian Monte Carlo [@chen2014stochastic].
Expand Down Expand Up @@ -82,14 +77,12 @@ def build_update(self):
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, dtype=qz.dtype),
scale=(tf.sqrt(tf.cast(learning_rate * self.friction, qz.dtype)) *
tf.ones(event_shape, dtype=qz.dtype)))
stddev = tf.sqrt(tf.cast(learning_rate * self.friction, qz.dtype))
normal = tf.random_normal(event_shape, dtype=qz.dtype)
sample[z] = old_sample[z] + old_v_sample[z]
v_sample[z] = ((1.0 - 0.5 * self.friction) * old_v_sample[z] +
learning_rate * tf.convert_to_tensor(grad_log_p) +
normal.sample())
stddev * normal)

# Update Empirical random variables.
assign_ops = []
Expand Down
17 changes: 5 additions & 12 deletions edward/inferences/sgld.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
from edward.models import RandomVariable
from edward.util import copy

try:
from edward.models import Normal
except Exception as e:
raise ImportError("{0}. Your TensorFlow version is not supported.".format(e))


class SGLD(MonteCarlo):
"""Stochastic gradient Langevin dynamics [@welling2011bayesian].
Expand Down Expand Up @@ -75,13 +70,11 @@ def build_update(self):
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, dtype=qz.dtype),
scale=(tf.sqrt(tf.cast(learning_rate, qz.dtype)) *
tf.ones(event_shape, dtype=qz.dtype)))
sample[z] = old_sample[z] + \
0.5 * learning_rate * tf.convert_to_tensor(grad_log_p) + \
normal.sample()
stddev = tf.sqrt(tf.cast(learning_rate, qz.dtype))
normal = tf.random_normal(event_shape, dtype=qz.dtype)
sample[z] = (old_sample[z] +
0.5 * learning_rate * tf.convert_to_tensor(grad_log_p) +
stddev * normal)

# Update Empirical random variables.
assign_ops = []
Expand Down
9 changes: 1 addition & 8 deletions edward/inferences/wgan_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
from edward.inferences.gan_inference import GANInference
from edward.util import get_session

try:
from edward.models import Uniform
except Exception as e:
raise ImportError("{0}. Your TensorFlow version is not supported.".format(e))


class WGANInference(GANInference):
"""Parameter estimation with GAN-style training
Expand Down Expand Up @@ -81,9 +76,7 @@ def build_loss_and_gradients(self, var_list):
if self.penalty is None or self.penalty == 0:
penalty = 0.0
else:
eps = Uniform().sample(x_true.shape[0])
while eps.shape.ndims < x_true.shape.ndims:
eps = tf.expand_dims(eps, -1)
eps = tf.random_uniform(tf.shape(x_true))
x_interpolated = eps * x_true + (1.0 - eps) * x_fake
with tf.variable_scope("Disc", reuse=True):
d_interpolated = self.discriminator(x_interpolated)
Expand Down

0 comments on commit 052731a

Please sign in to comment.