/
langevin_test.py
310 lines (252 loc) · 12 KB
/
langevin_test.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Copyright 2018 The TensorFlow Probability Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Tests for MetropolisAdjustedLangevinAlgorithm."""
# Dependency imports
import numpy as np
import tensorflow.compat.v2 as tf
from tensorflow_probability.python.distributions import mvn_tril
from tensorflow_probability.python.distributions import normal
from tensorflow_probability.python.internal import distribute_lib
from tensorflow_probability.python.internal import distribute_test_lib
from tensorflow_probability.python.internal import samplers
from tensorflow_probability.python.internal import test_util
from tensorflow_probability.python.mcmc import langevin
from tensorflow_probability.python.mcmc import sample
JAX_MODE = False
@test_util.test_graph_and_eager_modes
class LangevinTest(test_util.TestCase):
def testLangevin1DNormal(self):
"""Sampling from the Standard Normal Distribution."""
dtype = np.float32
nchains = 32
target = normal.Normal(loc=dtype(0), scale=dtype(1))
samples = sample.sample_chain(
num_results=500,
current_state=np.ones([nchains], dtype=dtype),
kernel=langevin.MetropolisAdjustedLangevinAlgorithm(
target_log_prob_fn=target.log_prob,
step_size=0.75,
volatility_fn=lambda *args: .5),
num_burnin_steps=200,
trace_fn=None,
seed=test_util.test_seed())
sample_mean = tf.reduce_mean(samples, axis=(0, 1))
sample_std = tf.math.reduce_std(samples, axis=(0, 1))
sample_mean_, sample_std_ = self.evaluate([sample_mean, sample_std])
self.assertAllClose(sample_mean_, 0., atol=0.12)
self.assertAllClose(sample_std_, 1., atol=0.1)
def testLangevin3DNormal(self):
"""Sampling from a 3-D Multivariate Normal distribution."""
dtype = np.float32
true_mean = dtype([1, 2, 7])
true_cov = dtype([[1, 0.25, 0.25], [0.25, 1, 0.25], [0.25, 0.25, 1]])
num_results = 500
num_chains = 500
# Target distribution is defined through the Cholesky decomposition
chol = tf.linalg.cholesky(true_cov)
target = mvn_tril.MultivariateNormalTriL(loc=true_mean, scale_tril=chol)
# Assume that the state is passed as a list of tensors `x` and `y`.
# Then the target log-density is defined as follows:
def target_log_prob(x, y):
# Stack the input tensors together
z = tf.concat([x, y], axis=-1)
return target.log_prob(z)
# Initial state of the chain
init_state = [np.ones([num_chains, 2], dtype=dtype),
np.ones([num_chains, 1], dtype=dtype)]
# Run MALA with normal proposal for `num_results` iterations for
# `num_chains` independent chains:
states = sample.sample_chain(
num_results=num_results,
current_state=init_state,
kernel=langevin.MetropolisAdjustedLangevinAlgorithm(
target_log_prob_fn=target_log_prob, step_size=.1),
num_burnin_steps=200,
num_steps_between_results=1,
trace_fn=None,
seed=test_util.test_seed())
states = tf.concat(states, axis=-1)
sample_mean = tf.reduce_mean(states, axis=[0, 1])
x = (states - sample_mean)[..., tf.newaxis]
sample_cov = tf.reduce_mean(
tf.matmul(x, tf.transpose(a=x, perm=[0, 1, 3, 2])),
axis=[0, 1])
sample_mean_, sample_cov_ = self.evaluate([sample_mean, sample_cov])
self.assertAllClose(true_mean, np.squeeze(sample_mean_), atol=0.1, rtol=0.1)
self.assertAllClose(true_cov, np.squeeze(sample_cov_), atol=0.1, rtol=0.1)
def testLangevin3DNormalDynamicVolatility(self):
"""Sampling from a 3-D Multivariate Normal distribution."""
dtype = np.float32
true_mean = dtype([1, 2, 7])
true_cov = dtype([[1, 0.25, 0.25], [0.25, 1, 0.25], [0.25, 0.25, 1]])
num_results = 500
num_chains = 500
# Targeg distribution is defined through the Cholesky decomposition
chol = tf.linalg.cholesky(true_cov)
target = mvn_tril.MultivariateNormalTriL(loc=true_mean, scale_tril=chol)
# Assume that the state is passed as a list of 1-d tensors `x` and `y`.
# Then the target log-density is defined as follows:
def target_log_prob(x, y):
# Stack the input tensors together
z = tf.concat([x, y], axis=-1)
return target.log_prob(z)
# Here we define the volatility function to be non-caonstant
def volatility_fn(x, y):
# Stack the input tensors together
return [1. / (0.5 + 0.1 * tf.abs(x + y)),
1. / (0.5 + 0.1 * tf.abs(y))]
# Initial state of the chain
init_state = [np.ones([num_chains, 2], dtype=dtype),
np.ones([num_chains, 1], dtype=dtype)]
# Run Random Walk Metropolis with normal proposal for `num_results`
# iterations for `num_chains` independent chains:
states = sample.sample_chain(
num_results=num_results,
current_state=init_state,
kernel=langevin.MetropolisAdjustedLangevinAlgorithm(
target_log_prob_fn=target_log_prob,
volatility_fn=volatility_fn,
step_size=.1),
num_burnin_steps=200,
num_steps_between_results=1,
trace_fn=None,
seed=test_util.test_seed())
states = tf.concat(states, axis=-1)
sample_mean = tf.reduce_mean(states, axis=[0, 1])
x = (states - sample_mean)[..., tf.newaxis]
sample_cov = tf.reduce_mean(tf.matmul(x, x, transpose_b=True), axis=[0, 1])
sample_mean_, sample_cov_ = self.evaluate([sample_mean, sample_cov])
self.assertAllClose(true_mean, np.squeeze(sample_mean_), atol=0.1, rtol=0.1)
self.assertAllClose(true_cov, np.squeeze(sample_cov_), atol=0.1, rtol=0.1)
def testLangevinCorrectVolatilityGradient(self):
"""Check that the gradient of the volatility is computed correctly."""
# Consider the example target distribution as in `testLangevin3DNormal`
dtype = np.float32
true_mean = dtype([1, 2, 7])
true_cov = dtype([[1, 0.25, 0.25], [0.25, 1, 0.25], [0.25, 0.25, 1]])
num_chains = 100
chol = tf.linalg.cholesky(true_cov)
target = mvn_tril.MultivariateNormalTriL(loc=true_mean, scale_tril=chol)
def target_log_prob(x, y):
# Stack the input tensors together
z = tf.concat([x, y], axis=-1)
return target.log_prob(z)
def volatility_fn(x, y):
# Stack the input tensors together
return [1. / (0.5 + 0.1 * tf.abs(x + y)),
1. / (0.5 + 0.1 * tf.abs(y))]
# Initial state of the chain
init_state = [np.ones([num_chains, 2], dtype=dtype),
np.ones([num_chains, 1], dtype=dtype)]
# Define MALA with constant volatility
langevin_unit = langevin.MetropolisAdjustedLangevinAlgorithm(
target_log_prob_fn=target_log_prob, step_size=0.1)
# Define MALA with volatility being `volatility_fn`
langevin_general = langevin.MetropolisAdjustedLangevinAlgorithm(
target_log_prob_fn=target_log_prob,
step_size=0.1,
volatility_fn=volatility_fn)
# Initialize the samplers
kernel_unit_volatility = langevin_unit.bootstrap_results(init_state)
kernel_general = langevin_general.bootstrap_results(init_state)
# For `langevin_general` volatility gradient should be zero.
grad_1, grad_2 = kernel_unit_volatility.accepted_results.grads_volatility
self.assertAllEqual(self.evaluate(grad_1),
np.zeros(shape=init_state[0].shape, dtype=dtype))
self.assertAllEqual(self.evaluate(grad_2),
np.zeros(shape=init_state[1].shape, dtype=dtype))
# For `langevin_unit` volatility gradient should be around -0.926 for
# each direction.
grad_1, grad_2 = kernel_general.accepted_results.grads_volatility
self.assertAllClose(self.evaluate(grad_1),
-0.583 * np.ones(shape=init_state[0].shape,
dtype=dtype),
atol=0.01, rtol=0.01)
self.assertAllClose(self.evaluate(grad_2),
-0.926 * np.ones(shape=init_state[1].shape,
dtype=dtype),
atol=0.01, rtol=0.01)
def testMALAIsCalibrated(self):
mala = langevin.MetropolisAdjustedLangevinAlgorithm(
target_log_prob_fn=lambda x: -tf.square(x) / 2.,
step_size=0.1,
)
self.assertTrue(mala.is_calibrated)
def testUncalibratedLangevinIsNotCalibrated(self):
uncal_langevin = langevin.UncalibratedLangevin(
target_log_prob_fn=lambda x: -tf.square(x) / 2.,
step_size=0.1,
)
self.assertFalse(uncal_langevin.is_calibrated)
@test_util.test_all_tf_execution_regimes
class DistributedLangevinTest(distribute_test_lib.DistributedTest):
def test_langevin_kernel_tracks_axis_names(self):
kernel = langevin.MetropolisAdjustedLangevinAlgorithm(
normal.Normal(0, 1).log_prob, step_size=1.9)
self.assertIsNone(kernel.experimental_shard_axis_names)
kernel = langevin.MetropolisAdjustedLangevinAlgorithm(
normal.Normal(0, 1).log_prob,
step_size=1.9,
experimental_shard_axis_names=['a'])
self.assertListEqual(kernel.experimental_shard_axis_names, ['a'])
kernel = langevin.MetropolisAdjustedLangevinAlgorithm(
normal.Normal(0, 1).log_prob,
step_size=1.9).experimental_with_shard_axes(['a'])
self.assertListEqual(kernel.experimental_shard_axis_names, ['a'])
def test_computes_same_log_acceptance_correction_with_sharded_state(self):
if not JAX_MODE:
self.skipTest('Test in TF runs into `merge_call` error: see b/178944108')
def target_log_prob(a, b):
return (normal.Normal(0., 1.).log_prob(a) + distribute_lib.psum(
normal.Normal(distribute_lib.pbroadcast(a, 'foo'), 1.).log_prob(b),
'foo'))
kernel = langevin.MetropolisAdjustedLangevinAlgorithm(
target_log_prob, step_size=1.9)
sharded_kernel = kernel.experimental_with_shard_axes([None, ['foo']])
def run(seed):
state = [tf.convert_to_tensor(0.), tf.convert_to_tensor(0.)]
kr = sharded_kernel.bootstrap_results(state)
_, kr = sharded_kernel.one_step(state, kr, seed=seed)
return kr.proposed_results.log_acceptance_correction
log_acceptance_correction = self.evaluate(self.per_replica_to_tensor(
self.strategy_run(run, args=(samplers.zeros_seed(),),
in_axes=None, axis_name='foo'), 0))
for i in range(distribute_test_lib.NUM_DEVICES):
self.assertAllClose(log_acceptance_correction[i],
log_acceptance_correction[0])
def test_unsharded_state_remains_synchronized_across_devices(self):
if not JAX_MODE:
self.skipTest('Test in TF runs into `merge_call` error: see b/178944108')
def target_log_prob(a, b):
return (normal.Normal(0., 1.).log_prob(a) + distribute_lib.psum(
normal.Normal(distribute_lib.pbroadcast(a, 'foo'), 1.).log_prob(b),
'foo'))
kernel = langevin.MetropolisAdjustedLangevinAlgorithm(
target_log_prob, step_size=1e-1)
sharded_kernel = kernel.experimental_with_shard_axes([None, ['foo']])
def run(seed):
state = [tf.convert_to_tensor(-10.),
tf.convert_to_tensor(-10.)]
kr = sharded_kernel.bootstrap_results(state)
state, _ = sharded_kernel.one_step(state, kr, seed=seed)
return state
state = self.evaluate(self.per_replica_to_tensor(
self.strategy_run(run, args=(samplers.zeros_seed(),),
in_axes=None, axis_name='foo'), 0))
for i in range(distribute_test_lib.NUM_DEVICES):
self.assertAllClose(state[0][i],
state[0][0])
if __name__ == '__main__':
test_util.main()