-
Notifications
You must be signed in to change notification settings - Fork 4
/
examples.py
149 lines (124 loc) · 5.64 KB
/
examples.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
from utils import Dataset, PointFactory
import matplotlib.pyplot as plt
import numpy as np
class DummyRandomPoints(PointFactory):
def __init__(self,
points_per_blob: int = 100,
loc: float = 0.0,
scale: float = 0.6,
):
self._points_per_blob = points_per_blob
self._loc = loc
self._scale = scale
def get_points(self):
result = np.random.normal(loc=self._loc, scale=self._scale, size=self._points_per_blob)
result.sort()
return result
class DummyRandomLeftPoints(DummyRandomPoints):
def __init__(self,
points_per_blob: int = 100,
scale: float = 0.6,
):
super().__init__(loc=-3.0, scale=scale, points_per_blob=points_per_blob)
class DummyRandomCenterPoints(DummyRandomPoints):
def __init__(self,
points_per_blob: int = 100,
scale: float = 0.6,
):
super().__init__(loc=0.0, scale=scale, points_per_blob=points_per_blob)
class DummyRandomRightPoints(DummyRandomPoints):
def __init__(self,
points_per_blob: int = 100,
scale: float = 0.6,
):
super().__init__(loc=3.0, scale=scale, points_per_blob=points_per_blob)
class DummyDatasetForRegression(Dataset):
@classmethod
def get_point_factories(cls, points_per_blob: int = 100, scale: float = 0.6):
return (
DummyRandomLeftPoints(points_per_blob=points_per_blob, scale=scale),
DummyRandomCenterPoints(points_per_blob=points_per_blob, scale=scale),
DummyRandomRightPoints(points_per_blob=points_per_blob, scale=scale),
)
@classmethod
def get_functions(cls):
return (
lambda x: 2 * x + 3,
lambda x: -2 * x - 1,
lambda x: x ** 2 - 6 * x + 5,
)
def __init__(self, points_per_blob: int = 100, scale: float = 0.6):
super().__init__(
point_factories=self.get_point_factories(points_per_blob=points_per_blob, scale=scale),
functions=self.get_functions(),
)
def visualize_graphs(self, graph_alpha=0.1):
left_dataset, center_dataset, right_dataset = self.dataset_collection
plt.title('Function graphs')
plt.ylabel('y')
plt.scatter(left_dataset.x, left_dataset.y, alpha=graph_alpha)
plt.scatter(center_dataset.x, center_dataset.y, alpha=graph_alpha)
plt.scatter(right_dataset.x, right_dataset.y, alpha=graph_alpha)
def visualize_histograms(self, hist_alpha=0.5, bins=50):
left_dataset, center_dataset, right_dataset = self.dataset_collection
plt.title('Values histograms')
plt.xlabel('x')
plt.ylabel('Density value')
plt.hist(left_dataset.x, alpha=hist_alpha, density=True, bins=bins)
plt.hist(center_dataset.x, alpha=hist_alpha, density=True, bins=bins)
plt.hist(right_dataset.x, alpha=hist_alpha, density=True, bins=bins)
def visualize_dataset(self, graph_alpha=0.1, hist_alpha=0.5, bins=50):
plt.figure(figsize=(7, 6))
plt.subplot(2, 1, 1)
self.visualize_graphs(graph_alpha=graph_alpha)
plt.subplot(2, 1, 2)
self.visualize_histograms(hist_alpha=hist_alpha, bins=bins)
def visualize_model(self, model, start=-7, stop=7, steps=1000, graph_alpha=0.1, prediction_alpha=0.5):
self.visualize_graphs(graph_alpha=graph_alpha)
full_x = np.linspace(start=start, stop=stop, num=steps)
predictions = model.predict(full_x, batch_size=steps)
plt.plot(full_x, predictions, alpha=prediction_alpha, c='r', label='Prediction')
plt.legend()
def visualize_variational_model(
self,
model,
nb_of_samples,
start=-7,
stop=7,
steps=1000,
graph_alpha=0.1,
prediction_alpha=0.5,
):
full_x = np.linspace(start=start, stop=stop, num=steps)
samples = np.hstack([model.predict(full_x, batch_size=steps) for _ in range(nb_of_samples)])
means = samples.mean(axis=1)
stds = samples.std(axis=1)
low_confidence_itervals = means - stds
high_confidence_itervals = means + stds
plt.plot(full_x, means, alpha=prediction_alpha, c='r', label='Prediction')
plt.plot(full_x, low_confidence_itervals, alpha=prediction_alpha, c='c', label='Low confidence interval')
plt.plot(full_x, high_confidence_itervals, alpha=prediction_alpha, c='c', label='High confidence interval')
plt.fill_between(full_x, low_confidence_itervals, high_confidence_itervals, color='c')
self.visualize_graphs(graph_alpha=graph_alpha)
plt.legend()
def visualize_prior_ensemble_model(
self,
model,
start=-7,
stop=7,
steps=1000,
graph_alpha=0.1,
prediction_alpha=0.5,
):
full_x = np.linspace(start=start, stop=stop, num=steps)
samples = model.predict(full_x)
means = samples.mean(axis=0)
stds = samples.std(axis=0)
low_confidence_itervals = means - stds
high_confidence_itervals = means + stds
plt.plot(full_x, means, alpha=prediction_alpha, c='r', label='Prediction')
plt.plot(full_x, low_confidence_itervals, alpha=prediction_alpha, c='c', label='Low confidence interval')
plt.plot(full_x, high_confidence_itervals, alpha=prediction_alpha, c='c', label='High confidence interval')
plt.fill_between(full_x, low_confidence_itervals, high_confidence_itervals, color='c')
self.visualize_graphs(graph_alpha=graph_alpha)
plt.legend()