-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-reticulate-gp.Rmd
More file actions
396 lines (285 loc) · 10.4 KB
/
python-reticulate-gp.Rmd
File metadata and controls
396 lines (285 loc) · 10.4 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
---
output: github_document
---
This directly maps the python code provided in the early examples of the TensorFlow official documentation into R using `reticulate`, see: https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/GaussianProcess
```{r message=FALSE}
library(tidyverse)
library(reticulate)
library(tensorflow)
np <- import("numpy")
tf <- import("tensorflow")
tfp <- import("tensorflow_probability")
sess <- tf$Session()
knitr::opts_chunk$set(error=FALSE)
```
```{r}
tfd <- tfp$distributions
psd_kernels <- tfp$positive_semidefinite_kernels
```
## Draws from Prior
```{r}
num_points = 100L
# Index points should be a collection (100, here) of feature vectors. In this
# example, we're using 1-d vectors, so we just need to reshape the output from
# np.linspace, to give a shape of (100, 1).
index_points = np$expand_dims(np$linspace(-1., 1., num_points), -1L)
var <- tf$constant(1.0, dtype = tf$float64)
len <- tf$constant(0.1, dtype = tf$float64)
obs <- tf$constant(0.05, dtype = tf$float64)
# Define a kernel with default parameters.
kernel = psd_kernels$ExponentiatedQuadratic(var, len)## Radial basis function
#psd_kernels$ExponentiatedQuadratic()
```
```{r}
gp = tfd$GaussianProcess(kernel, index_points)
samples = gp$sample(10L)
# ==> 10 independently drawn, joint samples at `index_points`
```
Or with observation error:
```{r}
noisy_gp = tfd$GaussianProcess(
kernel=kernel,
index_points=index_points,
observation_noise_variance=obs)
noisy_samples = noisy_gp$sample(10L)
```
Evaluate and extract results
```{r}
out <- sess$run(samples)
```
```{r}
out %>%
t() %>%
as_tibble %>%
mutate(x = seq(-1, 1, length.out = 100) ) %>%
pivot_longer(-x) %>%
ggplot(aes(x, value, group = name)) + geom_line(col = "purple", alpha=0.4)
```
## Gaussian Process Regression
This follows the examples in the official Tensorflow documentation for Gaussian Process Regression: https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/GaussianProcessRegressionModel
```{r}
# Generate noisy observations from a known function at some random points.
observation_noise_variance <- 0 #<- tf$constant(obs, dtype = tf$float64)
len <- tf$constant(0.1, dtype = tf$float64)
var <- tf$constant(1, dtype = tf$float64)
f = function(x) sin(10 * x) * exp(-x ^ 2)
## Index points and observation_index_points is an array, with 1 col per feature! ## (N-dimensional X)
observation_index_points = runif(5L, -1, 1) %>% as.matrix(ncol = 1)
## Observations must be a numeric, not an array. (vector of values Y, not matrix of features)
observations = f(as.numeric(observation_index_points)) +
runif(2L, 0., sqrt(observation_noise_variance))
index_points = seq(-1, 1, len=100L) %>% as.matrix(ncol = 1)
## This works too but no need to it in python, the above is fine!!
#index_points = np$linspace(-1., 1., 100L) %>% np$expand_dims(-1L)
#x = np$random$uniform(-1., 1., 5L)
#observations = (np$sin(10*x) * np$exp(-x**2) +
# np$random$normal(0., np$sqrt(observation_noise_variance)))
#observation_index_points <- x %>% np$expand_dims(-1L)
kernel = psd_kernels$ExponentiatedQuadratic(var, len) # MaternFiveHalves()
gprm = tfd$GaussianProcessRegressionModel(
kernel = kernel,
index_points = index_points,
observation_index_points=observation_index_points,
observations = observations,
observation_noise_variance = observation_noise_variance)
samples = gprm$sample(100L)
```
```{r}
out <- sess$run(samples)
```
```{r}
ob <- data.frame(x = observation_index_points, y = observations, name = "data")
out %>%
t() %>%
as_tibble %>%
mutate(x = index_points) %>%
pivot_longer(-x) %>%
ggplot(aes(x, value, group = name)) +
geom_line(col = "purple", alpha=0.2) +
geom_point(data = ob, aes(x,y))
```
## With additive noise
```{r}
# Generate noisy observations from a known function at some random points.
observation_noise_variance <- 0.1 #<- tf$constant(obs, dtype = tf$float64)
len <- tf$constant(0.1, dtype = tf$float64)
var <- tf$constant(1, dtype = tf$float64)
f = function(x) sin(10 * x) * exp(-x ^ 2)
## Index points and observation_index_points is an array, with 1 col per feature! ## (N-dimensional X)
observation_index_points = runif(5L, -1, 1) %>% as.matrix(ncol = 1)
## Observations must be a numeric, not an array. (vector of values Y, not matrix of features)
observations = f(as.numeric(observation_index_points)) +
runif(2L, 0., sqrt(observation_noise_variance))
index_points = seq(-1, 1, len=100L) %>% as.matrix(ncol = 1)
## This works too but no need to it in python, the above is fine!!
#index_points = np$linspace(-1., 1., 100L) %>% np$expand_dims(-1L)
#x = np$random$uniform(-1., 1., 5L)
#observations = (np$sin(10*x) * np$exp(-x**2) +
# np$random$normal(0., np$sqrt(observation_noise_variance)))
#observation_index_points <- x %>% np$expand_dims(-1L)
kernel = psd_kernels$ExponentiatedQuadratic(var, len) # MaternFiveHalves()
gprm = tfd$GaussianProcessRegressionModel(
kernel = kernel,
index_points = index_points,
observation_index_points=observation_index_points,
observations = observations,
observation_noise_variance = observation_noise_variance)
samples = gprm$sample(100L)
```
```{r}
out <- sess$run(samples)
```
```{r}
ob <- data.frame(x = observation_index_points, y = observations, name = "data")
out %>%
t() %>%
as_tibble %>%
mutate(x = index_points) %>%
pivot_longer(-x) %>%
ggplot(aes(x, value, group = name)) +
geom_line(col = "purple", alpha=0.2) +
geom_point(data = ob, aes(x,y))
```
## ML
From:
```{r}
# Define a kernel with trainable parameters. Note we transform the trainable
# variables to apply a positivity constraint.
amplitude = tf$exp(tf$Variable(1, dtype=np$float64), name='amplitude')
length_scale = tf$exp(tf$Variable(2, dtype=np$float64), name='length_scale')
kernel = psd_kernels$ExponentiatedQuadratic(amplitude, length_scale)
observation_noise_variance = tf$exp(
tf$Variable(0.5, dtype=np$float64), name='observation_noise_variance')
# We'll use an unconditioned GP to train the kernel parameters.
gp = tfd$GaussianProcess(
kernel=kernel,
index_points= observation_index_points,
observation_noise_variance=observation_noise_variance)
neg_log_likelihood = -gp$log_prob(observations)
optimizer = tf$train$AdamOptimizer(learning_rate=.05, beta1=.5, beta2=.99)
optimize = optimizer$minimize(neg_log_likelihood)
gprm = tfd$GaussianProcessRegressionModel(
kernel=kernel,
index_points=index_points,
observation_index_points=observation_index_points,
observations=observations,
observation_noise_variance=observation_noise_variance)
ml_samples = gprm$sample(10L)
```
Note that we need to initialize variables!
```{r}
init_op <- tf$global_variables_initializer()
with(tf$Session() %as% sess, {
# Run the 'init' op
sess$run(init_op)
out <- sess$run(ml_samples)
param_est <- sess$run(c(amp = amplitude,
len = length_scale,
obs = observation_noise_variance))
})
```
```{r}
ob <- data.frame(x = observation_index_points, y = observations, name = "data")
out %>%
t() %>%
as_tibble %>%
mutate(x = index_points) %>%
pivot_longer(-x) %>%
ggplot(aes(x, value, group = name)) +
geom_line(col = "purple", alpha=0.4) +
geom_point(data = ob, aes(x,y))
```
## MCMC
**Work in progress, this mapping from the python into R is not working yet**
```{r}
joint_log_prob <- function(
index_points, observations, amplitude, length_scale, noise_variance){
# Hyperparameter Distributions
rv_amplitude = tfd$LogNormal(tf$constant(0, dtype=np$float64),
tf$constant(1, dtype=np$float64))
rv_length_scale = tfd$LogNormal(tf$constant(0, dtype=np$float64),
tf$constant(1, dtype=np$float64))
rv_noise_variance = tfd$LogNormal(tf$constant(0, dtype=np$float64),
tf$constant(1, dtype=np$float64))
gp = tfd$GaussianProcess(
kernel=psd_kernels$ExponentiatedQuadratic(amplitude, length_scale),
index_points=index_points,
observation_noise_variance=noise_variance)
return (
rv_amplitude$log_prob(amplitude) +
rv_length_scale$log_prob(length_scale) +
rv_noise_variance$log_prob(noise_variance) +
gp$log_prob(observations)
)
}
```
```{r}
scale <- tf$constant(1e-1, dtype=np$float64)
initial_chain_states = list(
scale * tf$ones(1, dtype=np$float64, name='init_amplitude'),
scale * tf$ones(1, dtype=np$float64, name='init_length_scale'),
scale * tf$ones(1, dtype=np$float64, name='init_obs_noise_variance')
)
# Since HMC operates over unconstrained space, we need to transform the
# samples so they live in real-space.
unconstraining_bijectors = list(
tfp$bijectors$Softplus(),
tfp$bijectors$Softplus(),
tfp$bijectors$Softplus()
)
unnormalized_log_posterior <- function(amplitude, length_scale, noise_variance){
joint_log_prob(observation_index_points,
observations,
amplitude,
length_scale,
noise_variance)
}
num_results = 200L
# list(
# list(
# amplitudes,
# length_scales,
# observation_noise_variances
# ),
# kernel_results
# )
```
```{r}
out = tfp$mcmc$sample_chain(
num_results = num_results,
num_burnin_steps = 500L,
num_steps_between_results = 3L,
current_state = initial_chain_states,
kernel = tfp$mcmc$TransformedTransitionKernel(
inner_kernel = tfp$mcmc$HamiltonianMonteCarlo(
target_log_prob_fn = unnormalized_log_posterior,
step_size= tf$constant(.15, dtype = np$float64),
num_leapfrog_steps = 3L
),
bijector = unconstraining_bijectors
)
)
# Now we can sample from the posterior predictive distribution at a new set
# of index points.
```
```{r}
#with(tf$Session() %as% sess){
# out <- sess$run(out)
#}
```
```{r}
gprm = tfd$GaussianProcessRegressionModel(
# Batch of `num_results` kernels parameterized by the MCMC samples.
kernel=psd_kernels$ExponentiatedQuadratic(out[[0]][[1]], out[[0]][[2]]),
index_points=index_points,
observation_index_points=observation_index_points,
observations=observations,
observation_noise_variance=out[[0]][[3]])
## sample!
samples = gprm$sample()
```
```{r eval=FALSE}
## NOT RUN YET
samples_ = sess$run(samples)
res = sess$run(c(kernel_results = out, samples = samples))
```