Machine Learning and Pattern Recognition Minimal Stochastic Variational Inference Demo
Machine Learning and Pattern Recognition Minimal Stochastic Variational Inference Demo
/usr/bin/env python
"""
This is a demo (not production code!) of black-box stochastic variational
inference applied to logistic regression.
import numpy as np
import matplotlib.pyplot as plt
Inputs:
mm D, mean of variational posterior
LL D,D lower-triangular Cholesky decomposition of
variational posterior, with diagonal log-transformed
Lsigma_w scalar: log of prior standard deviation over weights
neg_log_like_grad fn -ve log-likelihood of model and gradients wrt weights
Could be an unbiased estimate based on a mini-batch,
we only get unbiased estimates of cost and gradients
anyway.
Outputs:
J scalar: estimate of variational cost function = -ELBO
mm_bar D, with derivatives wrt mm, ...
LL_bar D,D ...LL, ...
Lsigma_w_bar ...and Lsigma_w
"""
# The derivatives
mm_bar = mm/sigma2_w + ww_bar
L_bar = L/sigma2_w + np.tril(np.dot(ww_bar[:,None], nu[None,:]))
LL_bar = L_bar
LL_bar[diag] = (L_bar[diag] * L[diag]) - 1
Lsigma_w_bar = D - tmp
Inputs:
ww D,
X N,D
yy N,
Outputs:
negLlike scalar
ww_bar D,
"""
if __name__ == "__main__":
# Generate synthetic dataset and note corresponding likelihood
np.random.seed(0)
D = 3
ww = 5*np.random.randn(D)
N = 20
X = np.hstack((np.random.randn(N, D-1), np.ones((N,1))))
yy = np.random.rand(N) < (1.0/(1.0 + np.exp(-np.dot(X,ww))))
neg_log_like_grad = lambda w: logreg_negLlike(w, X, yy)
# If you rerun the fitting with different seeds you'll see we don't
# get quite the same answer each time. I'd need to set the learning rate
# schedule better, and/or run for longer, to get better convergence.
np.random.seed(2)
# Plot data:
plt.clf()
plt.plot(X[yy==1, 0], X[yy==1, 1], 'bx')
plt.plot(X[yy==0, 0], X[yy==0, 1], 'ro')
plt.legend(('y=1', 'y=0'))