Last active
May 12, 2016 11:00
-
-
Save kudkudak/15d00fa0ea1cce4f3cde705faa08372b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Probability model | |
Posterior: (2-dimensional) Normal | |
Variational model | |
Likelihood: Mean-field Normal | |
""" | |
import edward as ed | |
import tensorflow as tf | |
from edward.stats import multivariate_normal | |
from edward.variationals import Variational, Normal | |
from edward.util import get_dims | |
class NormalPosterior: | |
""" | |
p(x, z) = p(z) = p(z | x) = Normal(z; mu, Sigma) | |
""" | |
def __init__(self, mu, Sigma): | |
self.mu = mu | |
self.Sigma = Sigma | |
self.num_vars = get_dims(mu)[0] | |
def log_prob(self, xs, zs): | |
return tf.pack([multivariate_normal.logpdf(z, self.mu, self.Sigma) | |
for z in tf.unpack(zs)]) | |
for _ in range(2): | |
ed.set_seed(42) | |
mu = tf.constant([1.0, 1.0]) | |
Sigma = tf.constant( | |
[[1.0, 0.1], | |
[0.1, 1.0]]) | |
model = NormalPosterior(mu, Sigma) | |
variational = Variational() | |
variational.add(Normal(model.num_vars)) | |
inference = ed.MFVI(model, variational) | |
inference.run(n_iter=100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment