import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import seaborn as sns
import tensorflow_probability as tfp
import pandas as pd
= tfp.distributions
tfd = tfp.layers
tfl from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.callbacks import Callback
sns.reset_defaults()='talk',font_scale=1)
sns.set_context(context%matplotlib inline
%config InlineBackend.figure_format='retina'
42)
np.random.seed(= np.linspace(-0.5, 1, 100)
x = 5*x + 4 + 2*np.multiply(x, np.random.randn(100)) y
=20, alpha=0.6)
plt.scatter(x, y, s
sns.despine()"x")
plt.xlabel("y") plt.ylabel(
Text(0, 0.5, 'y')
Model 1: Vanilla Linear Regression
= Sequential([
model =(1,), units=1, name='D1')]) Dense(input_shape
2022-02-01 09:37:25.292936: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
D1 (Dense) (None, 1) 2
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________
compile(loss='mse', optimizer='adam')
model.=4000, verbose=0) model.fit(x, y, epochs
<keras.callbacks.History at 0x1a36573a0>
'D1').weights model.get_layer(
[<tf.Variable 'D1/kernel:0' shape=(1, 1) dtype=float32, numpy=array([[4.929521]], dtype=float32)>,
<tf.Variable 'D1/bias:0' shape=(1,) dtype=float32, numpy=array([3.997371], dtype=float32)>]
=20, alpha=0.6)
plt.scatter(x, y, s
sns.despine()"x")
plt.xlabel("y")
plt.ylabel(= model.predict(x)
pred_m1 plt.plot(x, pred_m1)
Model 2
= Sequential([
model_2 =(1,), units=1, name='M2_D1'),
Dense(input_shapelambda loc: tfd.Normal(loc=loc, scale=1.), name='M2_Likelihood')]) tfl.DistributionLambda(
2022-02-01 09:37:33.529583: W tensorflow/python/util/util.cc:368] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
model_2.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
M2_D1 (Dense) (None, 1) 2
M2_Likelihood (Distribution ((None, 1), 0
Lambda) (None, 1))
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________
= model_2.get_layer('M2_D1').weights m2_untrained_weight
=20, alpha=0.6)
plt.scatter(x, y, s
sns.despine()"x")
plt.xlabel("y")
plt.ylabel(= model_2(x)
m_2 40).numpy()[:, :, 0].T, color='k', alpha=0.05);
plt.plot(x, m_2.sample(='k') plt.plot(x, m_2.mean().numpy().flatten(), color
def plot(model):
=20, alpha=0.6)
plt.scatter(x, y, s
sns.despine()"x")
plt.xlabel("y")
plt.ylabel(= model(x)
m = m.stddev().numpy().flatten()
m_s = m.mean().numpy().flatten()
m_m
='k')
plt.plot(x, m_m , color-m_s, m_m+m_s, color='k', alpha=0.4) plt.fill_between(x, m_m
plot(model_2)
def nll(y_true, y_pred):
# y_pred is distribution
return -y_pred.log_prob(y_true)
compile(loss=nll, optimizer='adam')
model_2.=4000, verbose=0) model_2.fit(x, y, epochs
<keras.callbacks.History at 0x1a3b96880>
plot(model_2)
Model 3
= Sequential([
model_3 =(1,), units=2, name='M3_D1'),
Dense(input_shapelambda t: tfd.Normal(loc=t[..., 0], scale=tf.exp(t[..., 1])), name='M3_Likelihood')]) tfl.DistributionLambda(
'M3_D1').weights model_3.get_layer(
[<tf.Variable 'M3_D1/kernel:0' shape=(1, 2) dtype=float32, numpy=array([[0.04476571, 0.55212975]], dtype=float32)>,
<tf.Variable 'M3_D1/bias:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>]
compile(loss=nll, optimizer='adam')
model_3.=4000, verbose=0) model_3.fit(x, y, epochs
<keras.callbacks.History at 0x1a3d20190>
plot(model_3)
Good reference https://tensorchiefs.github.io/bbs/files/21052019-bbs-Beate-uncertainty.pdf
At this point, we see that scale or sigma is a linear function of input x.
#### Model 4
= Sequential([
model_4 =(1,), units=2, name='M4_D1', activation='relu'),
Dense(input_shape=2, name='M4_D2', activation='relu'),
Dense(units=2, name='M4_D3'),
Dense(unitslambda t: tfd.Normal(loc=t[..., 0], scale=tf.math.softplus(t[..., 1])), name='M4_Likelihood')]) tfl.DistributionLambda(
compile(loss=nll, optimizer='adam')
model_4.=4000, verbose=0) model_4.fit(x, y, epochs
<keras.callbacks.History at 0x1a3cd0a60>
plot(model_4)
Model 5
Follow: https://juanitorduz.github.io/tfp_lm/
Model 6
Dense Variational
def prior(kernel_size, bias_size, dtype=None):
= kernel_size + bias_size
n # Independent Normal Distribution
return lambda t: tfd.Independent(tfd.Normal(loc=tf.zeros(n, dtype=dtype),
=1),
scale=1)
reinterpreted_batch_ndims
def posterior(kernel_size, bias_size, dtype=None):
= kernel_size + bias_size
n return Sequential([
=dtype),
tfl.VariableLayer(tfl.IndependentNormal.params_size(n), dtype
tfl.IndependentNormal(n) ])
= len(x)
N = Sequential([
model_6 # Requires posterior and prior distribution
# Add kl_weight for weight regularization
#tfl.DenseVariational(16, posterior, prior, kl_weight=1/N, activation='relu', input_shape=(1, )),
2, posterior, prior, kl_weight=1/N, input_shape=(1,)),
tfl.DenseVariational(1)
tfl.IndependentNormal( ])
compile(loss=nll, optimizer='adam') model_6.
=5000, verbose=0) model_6.fit(x, y, epochs
<keras.callbacks.History at 0x1a61a9ee0>
plot(model_6)
= len(x)
N = Sequential([
model_7 # Requires posterior and prior distribution
# Add kl_weight for weight regularization
16, posterior, prior, kl_weight=1/N, activation='relu', input_shape=(1, )),
tfl.DenseVariational(2, posterior, prior, kl_weight=1/N),
tfl.DenseVariational(1)
tfl.IndependentNormal(
])
compile(loss=nll, optimizer='adam') model_7.
=5000, verbose=0) model_7.fit(x, y, epochs
<keras.callbacks.History at 0x1a669da90>
plot(model_7)
https://livebook.manning.com/book/probabilistic-deep-learning-with-python/chapter-8/123