forked from openai/ebm_code_release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hmc.py
129 lines (99 loc) · 3.5 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
import tensorflow as tf
import numpy as np
from tensorflow.python.platform import flags
flags.DEFINE_bool('proposal_debug', False, 'Print hmc acceptance raes')
FLAGS = flags.FLAGS
def kinetic_energy(velocity):
"""Kinetic energy of the current velocity (assuming a standard Gaussian)
(x dot x) / 2
Parameters
----------
velocity : tf.Variable
Vector of current velocity
Returns
-------
kinetic_energy : float
"""
return 0.5 * tf.square(velocity)
def hamiltonian(position, velocity, energy_function):
"""Computes the Hamiltonian of the current position, velocity pair
H = U(x) + K(v)
U is the potential energy and is = -log_posterior(x)
Parameters
----------
position : tf.Variable
Position or state vector x (sample from the target distribution)
velocity : tf.Variable
Auxiliary velocity variable
energy_function
Function from state to position to 'energy'
= -log_posterior
Returns
-------
hamitonian : float
"""
batch_size = tf.shape(velocity)[0]
kinetic_energy_flat = tf.reshape(kinetic_energy(velocity), (batch_size, -1))
return tf.squeeze(energy_function(position)) + tf.reduce_sum(kinetic_energy_flat, axis=[1])
def leapfrog_step(x0,
v0,
neg_log_posterior,
step_size,
num_steps):
# Start by updating the velocity a half-step
v = v0 - 0.5 * step_size * tf.gradients(neg_log_posterior(x0), x0)[0]
# Initalize x to be the first step
x = x0 + step_size * v
for i in range(num_steps):
# Compute gradient of the log-posterior with respect to x
gradient = tf.gradients(neg_log_posterior(x), x)[0]
# Update velocity
v = v - step_size * gradient
# x_clip = tf.clip_by_value(x, 0.0, 1.0)
# x = x_clip
# v_mask = 1 - 2 * tf.abs(tf.sign(x - x_clip))
# v = v * v_mask
# Update x
x = x + step_size * v
# x = tf.clip_by_value(x, -0.01, 1.01)
# x = tf.Print(x, [tf.reduce_min(x), tf.reduce_max(x), tf.reduce_mean(x)])
# Do a final update of the velocity for a half step
v = v - 0.5 * step_size * tf.gradients(neg_log_posterior(x), x)[0]
# return new proposal state
return x, v
def hmc(initial_x,
step_size,
num_steps,
neg_log_posterior):
"""Summary
Parameters
----------
initial_x : tf.Variable
Initial sample x ~ p
step_size : float
Step-size in Hamiltonian simulation
num_steps : int
Number of steps to take in Hamiltonian simulation
neg_log_posterior : str
Negative log posterior (unnormalized) for the target distribution
Returns
-------
sample :
Sample ~ target distribution
"""
v0 = tf.random_normal(tf.shape(initial_x))
x, v = leapfrog_step(initial_x,
v0,
step_size=step_size,
num_steps=num_steps,
neg_log_posterior=neg_log_posterior)
orig = hamiltonian(initial_x, v0, neg_log_posterior)
current = hamiltonian(x, v, neg_log_posterior)
prob_accept = tf.exp(orig - current)
if FLAGS.proposal_debug:
prob_accept = tf.Print(prob_accept, [tf.reduce_mean(tf.clip_by_value(prob_accept, 0, 1))])
uniform = tf.random_uniform(tf.shape(prob_accept))
keep_mask = (prob_accept > uniform)
# print(keep_mask.get_shape())
x_new = tf.where(keep_mask, x, initial_x)
return x_new