-
Notifications
You must be signed in to change notification settings - Fork 0
/
bayesian_linear_regression.py
76 lines (63 loc) · 2.24 KB
/
bayesian_linear_regression.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
# coding: utf-8
import tensorflow as tf
import edward as ed
from tensorflow.python import debug as tf_debug
import matplotlib.pyplot as plt
import seaborn as sns
from edward.models import Normal, Empirical
def build_toy_dataset(N, noise_std=0.5):
X = np.concatenate([np.linspace(0, 2, num=N/2),
np.linspace(6, 8, num=N/2)])
Y = 2.0*X + 10 * np.random.normal(0, noise_std, size=N)
X = X.reshape((N, 1))
return X, Y
ed.set_seed(42)
N = 40 # number of data points
D = 1 # number of features
# data
X_train, y_train = build_toy_dataset(N)
X_test, y_test = build_toy_dataset(N)
# model
X = tf.placeholder(tf.float32, [N, D])
w = Normal(loc=tf.zeros(D), scale=tf.ones(D))
b = Normal(loc=tf.zeros(1), scale=tf.ones(1))
y = Normal(loc=ed.dot(X, w), scale=tf.ones(N))
# inference
T=5000 # number of samples
nburn = 100 # number of burn-in samples
stride = 10 # frequency with which to plot samples
qw = Empirical(params=tf.Variable(tf.random_normal([T, D])))
qb = Empirical(params=tf.Variable(tf.random_normal([T, 1])))
inference = ed.SGHMC({w:qw, b:qb}, data={X:X_train, y: y_train})
inference.run(step_size=1e-3)
# criticism
# plot posterior samples
sns.jointplot(qb.params.eval()[nburn:T:stride],
qw.params.eval()[nburn:T:stride])
plt.show()
# posterior predictive check
# this is equivalent to
# y_post = Normal(loc=ed.dot(X, qw)+qb, scale=tf.ones(N))
y_post = ed.copy(y, {w:qw, b:qb})
print("mean squared error on test data:")
print(ed.evaluate("mean_squared_error", data={X:X_test, y_post:y_test}))
print("Displaying prior predictive samples")
n_prior_samples=10
w_prior = w.sample(n_prior_samples).eval()
b_prior = b.sample(n_prior_samples).eval()
plt.scatter(X_train, y_train)
inputs = np.linspace(-1, 10, num=400)
for ns in range(n_prior_samples):
output = inputs*w_prior[ns] + b_prior[ns]
plt.plot(inputs, output)
plt.show()
print("Displaying posterior predictive samples")
n_posterior_samples=20
w_prior = qw.sample(n_posterior_samples).eval()
b_prior = qb.sample(n_posterior_samples).eval()
plt.scatter(X_train, y_train)
inputs = np.linspace(-1, 10, num=400)
for ns in range(n_posterior_samples):
output = inputs*w_prior[ns] + b_prior[ns]
plt.plot(inputs, output)
plt.show()