from silence_tensorflow import silence_tensorflow
silence_tensorflow()
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import functools
import seaborn as sns
import tensorflow_probability as tfp
import pandas as pd
= tfp.distributions
tfd = tfp.layers
tfl = tfp.bijectors
tfb
sns.reset_defaults()="talk", font_scale=1)
sns.set_context(context%matplotlib inline
%config InlineBackend.figure_format='retina'
0)
np.random.seed(0) tf.random.set_seed(
Basic Imports
def lr(x, stddv_datapoints):
= x.shape
num_datapoints, data_dim = yield tfd.Normal(
b =0.0,
loc=2.0,
scale="b",
name
)= yield tfd.Normal(
w =tf.zeros([data_dim]), scale=2.0 * tf.ones([data_dim]), name="w"
loc
)
= yield tfd.Normal(
y =tf.linalg.matvec(x, w) + b, scale=stddv_datapoints, name="y"
loc )
= tf.linspace(-5.0, 5.0, 100)
x = tf.expand_dims(x, 1) x
= 1
stddv_datapoints
= functools.partial(lr, x=x, stddv_datapoints=stddv_datapoints)
concrete_lr_model
= tfd.JointDistributionCoroutineAutoBatched(concrete_lr_model) model
model
<tfp.distributions.JointDistributionCoroutineAutoBatched 'JointDistributionCoroutineAutoBatched' batch_shape=[] event_shape=StructTuple(
b=[],
w=[1],
y=[100]
) dtype=StructTuple(
b=float32,
w=float32,
y=float32
)>
= model.sample() actual_b, actual_w, y_train
=30, alpha=0.6)
plt.scatter(x, y_train, s+ actual_b, color="k")
plt.plot(x, tf.linalg.matvec(x, actual_w) sns.despine()
= lambda traceable_quantities: {
trace_fn "loss": traceable_quantities.loss,
"w": w,
"b": b,
}
= 1
data_dim = tf.Variable(tf.zeros_like(actual_w))
w
= tf.Variable(tf.zeros_like(actual_b))
b
= lambda w, b: model.log_prob((b, w, y_train))
target_log_prob_fn target_log_prob_fn
<function __main__.<lambda>(w, b)>
= tfp.math.minimize(
trace lambda: -target_log_prob_fn(w, b),
=tf.optimizers.Adam(learning_rate=0.05),
optimizer=trace_fn,
trace_fn=200,
num_steps )
w, b, actual_w, actual_b
(<tf.Variable 'Variable:0' shape=(1,) dtype=float32, numpy=array([2.1811483], dtype=float32)>,
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=3.0149531>,
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([2.1337605], dtype=float32)>,
<tf.Tensor: shape=(), dtype=float32, numpy=3.0221252>)
"w"], label="w")
plt.plot(trace["b"], label="b")
plt.plot(trace[
plt.legend() sns.despine()
= tf.Variable(tf.random.normal([data_dim]))
qw_mean = tf.Variable(tf.random.normal([1]))
qb_mean = tfp.util.TransformedVariable(
qw_stddv 1e-4 * tf.ones([data_dim]), bijector=tfb.Softplus()
)= tfp.util.TransformedVariable(1e-4 * tf.ones([1]), bijector=tfb.Softplus()) qb_stddv
def factored_normal_variational_model():
= yield tfd.Normal(loc=qw_mean, scale=qw_stddv, name="qw")
qw = yield tfd.Normal(loc=qb_mean, scale=qb_stddv, name="qb")
qb
= tfd.JointDistributionCoroutineAutoBatched(
surrogate_posterior
factored_normal_variational_model
)
= tfp.vi.fit_surrogate_posterior(
losses
target_log_prob_fn,=surrogate_posterior,
surrogate_posterior=tf.optimizers.Adam(learning_rate=0.05),
optimizer=200,
num_steps )
/Users/nipun/miniforge3/lib/python3.9/site-packages/tensorflow_probability/python/internal/vectorization_util.py:87: UserWarning: Saw Tensor seed Tensor("seed:0", shape=(2,), dtype=int32), implying stateless sampling. Autovectorized functions that use stateless sampling may be quite slow because the current implementation falls back to an explicit loop. This will be fixed in the future. For now, you will likely see better performance from stateful sampling, which you can invoke by passing a Python `int` seed.
warnings.warn(
/Users/nipun/miniforge3/lib/python3.9/site-packages/tensorflow_probability/python/internal/vectorization_util.py:87: UserWarning: Saw Tensor seed Tensor("seed:0", shape=(2,), dtype=int32), implying stateless sampling. Autovectorized functions that use stateless sampling may be quite slow because the current implementation falls back to an explicit loop. This will be fixed in the future. For now, you will likely see better performance from stateful sampling, which you can invoke by passing a Python `int` seed.
warnings.warn(
qw_mean, qw_stddv, qb_mean, qb_stddv
(<tf.Variable 'Variable:0' shape=(1,) dtype=float32, numpy=array([2.1905935], dtype=float32)>,
<TransformedVariable: name=softplus, dtype=float32, shape=[1], fn="softplus", numpy=array([0.04352505], dtype=float32)>,
<tf.Variable 'Variable:0' shape=(1,) dtype=float32, numpy=array([3.0260112], dtype=float32)>,
<TransformedVariable: name=softplus, dtype=float32, shape=[1], fn="softplus", numpy=array([0.09258726], dtype=float32)>)
= surrogate_posterior.sample(500) s_qw, s_qb
= tf.linalg.matvec(x, s_qw) + s_qb ys
x.shape, ys.shape
(TensorShape([100, 1]), TensorShape([500, 100]))
='k', alpha=0.05);
plt.plot(x, ys.numpy().T, color=30, alpha=0.6)
plt.scatter(x, y_train, s sns.despine()
TODO
- How to replace x in
lr
function from x_train to x_test?
References
- https://www.tensorflow.org/probability/examples/Probabilistic_PCA
- https://www.youtube.com/watch?v=l2f6Ic6SeqE&list=PLISXH-iEM4JlFsAp7trKCWyxeO3M70QyJ&index=4
- https://jeffpollock9.github.io/almost-always-auto-batched/
- https://jeffpollock9.github.io/bayesian-workflow-with-tfp-and-arviz/