-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhmc.py
163 lines (131 loc) · 4.17 KB
/
hmc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import numpy as np
import anglepy.ndict as ndict
import scipy.stats
import scipy.linalg
import time, sys
import random
log = {'acceptRate': [1],}
# Hybrid Monte Carlo sampler
# HMC move where x is a batch (rows=variables, columns=samples)
# NOTE: _stepsize can be a scalar OR a column vector with individual stepsizes
def hmc_step(fgrad, x0, _stepsize=1e-2, n_steps=20):
# === INITIALIZE
n_batch = x0.itervalues().next().shape[1]
stepsize = (np.random.uniform(size=(1, n_batch)) < 0.5).astype(float)*2-1
stepsize *= _stepsize
if np.random.uniform() < 0.5:
stepsize *= -1
# Sample velocity
vnew = {}
for i in x0:
vnew[i] = np.random.normal(size=x0[i].shape)
# copy initial state
xnew = ndict.clone(x0)
v0 = ndict.clone(vnew)
# === LEAPFROG STEPS
# Compute velocity at time (t + stepsize/2)
# Compute position at time (t + stepsize)
logpxz0, g = fgrad(xnew)
for i in xnew:
vnew[i] += 0.5 * stepsize * g[i]
xnew[i] += stepsize * vnew[i]
# Perform leapfrog steps
for step in xrange(n_steps):
#print 'hmc_step:', step
logpxz, g = fgrad(xnew)
for i in xnew:
vnew[i] += stepsize * g[i]
xnew[i] += stepsize * vnew[i]
# Perform final half-step for velocity
logpxz1, g = fgrad(xnew)
for i in xnew:
vnew[i] += 0.5 * stepsize * g[i]
# === METROPOLIS-HASTINGS ACCEPT/REJECT
# Compute old and new Hamiltonians
k0 = 0
k1 = 0
for i in vnew:
k0 += (v0[i]**2).sum(axis=0, keepdims=True)
k1 += (vnew[i]**2).sum(axis=0, keepdims=True)
h0 = -logpxz0 + 0.5 * k0
h1 = -logpxz1 + 0.5 * k1
#print logpxz0, k0, logpxz1, k1
#print h0-h1
# Perform Metropolis-Hasting step
accept = np.exp(h0 - h1) >= np.random.uniform(size=h1.shape)
accept = accept.astype(float)
for i in xnew:
accept2 = np.dot(np.ones((xnew[i].shape[0], 1)), accept)
x0[i] = (1-accept2)*x0[i] + accept2 * xnew[i]
# result: updated 'x0'
logpxz = (1-accept)*logpxz0 + accept * logpxz1
return logpxz, accept
# Auto-tuning HMC step with global stepsize
def hmc_step_autotune(n_steps=20, init_stepsize=1e-2, target=0.9):
alpha = 1.02 #1.02
max_factor = 1.25
stepsize = [init_stepsize]
steps = [0]
def dostep(fgrad, x):
logpxz, accept = hmc_step(fgrad, x, stepsize[0], n_steps)
factor = min(max_factor, alpha**float(min(100, accept.size)))
if accept.mean() < target:
stepsize[0] /= factor
else:
stepsize[0] *= factor
steps[0] += 1
#print steps[0], 'stepsize:', accept.mean(), stepsize[0], accept.size
return logpxz, accept.mean(), stepsize[0]
return dostep
# Auto-tuning with individual stepsizes
def hmc_step_autotune_indiv(n_steps=20, init_stepsize=1e-2, target=0.9):
alpha = 1.02 #1.02
stepsize = [None]
init = [False]
steps = [0]
def dostep(fgrad, x):
if init[0] == False:
n_batch = x.itervalues().next().shape[1]
stepsize[0] = init_stepsize*np.ones((1, n_batch))
init[0] = True
logpxz, accept = hmc_step(fgrad, x, stepsize[0], n_steps)
stepsize[0] *= np.exp((alpha * (accept*2-1)))
steps[0] += 1
#print steps[0], 'stepsize:', accept.mean(), stepsize[0], accept.size
return logpxz, accept.mean(), stepsize[0]
return dostep
# Merge lists of z's and corresponding logpxz's into single matrices
def combine_samples(z_list, logpxz_list):
n_list = len(logpxz_list)
n_batch = logpxz_list[0].shape[1]
# Stack logpxz values into one matrix
for i in range(n_list):
logpxz_list[i] = logpxz_list[i].reshape((n_batch, 1))
logpxz = np.hstack(logpxz_list).reshape((1, n_batch*n_list))
# Stack x samples into one ndict
z = {}
for i in z_list[0].keys():
list = []
for _z in z_list:
list.append(_z[i].reshape((-1, 1)))
z[i] = np.hstack(list).reshape((-1, n_batch*n_list))
return z, logpxz
# Convenience function: run HMC automatically
def sample_standard_auto(z, fgrad, n_burnin, n_samples):
hmc_dostep = hmc_step_autotune(n_steps=10, init_stepsize=1e-2, target=0.9)
def dostep(_z):
logpxz, _, _ = hmc_dostep(fgrad, _z)
return logpxz
# Burn-in phase
for i in range(n_burnin):
dostep(z)
# Sample
z_list = []
logpxz_list = []
for _ in range(n_samples):
logpxz = dostep(z)
#print 'logpxz:', logpxz
logpxz_list.append(logpxz.copy())
z_list.append(ndict.clone(z))
z, logpxz = combine_samples(z_list, logpxz_list)
return z, logpxz