Skip to content

Instantly share code, notes, and snippets.

@sbos
Created October 20, 2015 19:36
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sbos/65ea08d5885660625fd3 to your computer and use it in GitHub Desktop.
import cgt
import cgt.nn as nn
import numpy as np
from scipy.stats import norm
def gaussian_density(x, mu, sigma):
return cgt.exp(-cgt.square(x - mu) / 2 / cgt.square(sigma)) \
/ cgt.sqrt(2 * np.pi) / sigma
var_mu = nn.parameter(np.array(0.5))
var_log_sigma = nn.parameter(np.array(0.5))
x = cgt.scalar('x')
mu = np.array(0.3)
sigma = np.array(1.)
q_density = gaussian_density(x, var_mu, cgt.exp(var_log_sigma))
KL = cgt.log(q_density) \
- cgt.log(gaussian_density(x, mu, sigma))
params = nn.get_parameters(KL)
#this is the parameter causing problems
baseline = nn.parameter(np.array(0.))
#note that baseline acts only in computing the gradients,
#not the KL itself
KL_grad = [cgt.grad(q_density, [p])[0] * (KL - baseline) for p in params]
KL = cgt.function(inputs=[x], outputs=[KL] + KL_grad)
N = 10000
def eval(N):
x = norm(loc=var_mu.op.get_value(),
scale=np.exp(var_log_sigma.op.get_value())).rvs(size=N)
l = 0.
for i in xrange(N):
l += KL(x[i])[0] / N
return l
l = eval(N)
print l
#this should not affect the result!
baseline.op.set_value(np.array(l))
print eval(N)
baseline.op.set_value(np.array(0.))
print eval(N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment