-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_machine_learning.jl
312 lines (258 loc) · 9.62 KB
/
01_machine_learning.jl
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
using DeepPumas
using CairoMakie
using Distributions
using Random
set_theme!(deep_light())
set_mlp_backend(:simplechains)
#
# TABLE OF CONTENTS
#
#
# 0. BASIC SAMPLE PARAMETERS
#
# 1. A SIMPLE MACHINE LEARNING (ML) MODEL
#
# 1.1. Sample subjects with an obvious `true_function`
# 1.2. Model `true_function` with a linear regression
#
# 2. CAPTURING COMPLEX RELATIONSHIPS
#
# 2.1. Sample subjects with a more complex `true_function`
# 2.2. Exercise: Reason about using a linear regression to model the current `true_function`
# 2.3. Use a neural network (NN) to model `true_function`
#
# 3. BASIC UNDERFITTING AND OVERFITTING
#
# 3.1. Exercise: Investigate the impact of the number of fitting iterations in NNs
# (Hint: Train again the NN for few and for many iterations.)
# 3.2. Exercise: Reason about Exercise 2.2 again (that is, using a linear regression
# to model a quadratic relationship). Is the number of iterations relevant there?
# 3.3. The impact of the NN size
#
# 4. INSPECTION OF THE VALIDATION LOSS AND REGULARIZATION
#
# 4.1. Validation loss as a proxy for generalization performance
# 4.2. Regularization to prevent overfitting
# 4.3. Hyperparameter tuning
#
#
# 0. BASIC SAMPLE PARAMETERS
#
num_samples = 100
uniform = Uniform(-1, 1)
normal = Normal(0, 1)
σ = 0.25
#
# 1. A SIMPLE MACHINE LEARNING (ML) MODEL
#
# 1.1. Sample from an obvious `true_function`
# 1.2. Model `true_function` with a DeepPumas linear regression
#
# 1.1. Sample from an obvious `true_function`
true_function = x -> x
x = rand(uniform, 1, num_samples) # samples stored columnwise
ϵ = rand(normal, 1, num_samples) # samples stored columnwise
y = true_function.(x) + σ * ϵ
fig = scatter(vec(x), vec(y); axis = (xlabel = "x", ylabel = "y"), label = "data");
lines!(-1..1, true_function; color = :gray, label = "true");
axislegend(; position=:rb);
fig
# 1.2. Model `true_function` with a linear regression
target = preprocess(x, y) # DeepPumas `target`
linreg = MLPDomain(1, (1, identity); bias = true) # DeepPumas multilayer perceptron
# y = a * x + b
fitted_linreg = fit(linreg, target; optim_alg = DeepPumas.BFGS())
coef(fitted_linreg) # `true_function` is y = x + noise (that is, a = 1 b = 0)
ŷ = fitted_linreg(x)
fig = scatter(vec(x), vec(y); axis = (xlabel = "x", ylabel = "y"), label = "data");
scatter!(vec(x), vec(ŷ); label = "prediction");
lines!(-1..1, true_function; color = :gray, label = "true");
axislegend(; position=:rb);
fig
#
# 2. CAPTURING COMPLEX RELATIONSHIPS
#
# 2.1. Sample from a more complex `true_function`
# 2.2. Exercise: Reason about using a linear regression to model the current `true_function`
# 2.3. Use a neural network (NN) to model `true_function`
#
# 2.1. Sample from a more complex `true_function`
true_function = x -> x^2
x = rand(uniform, 1, num_samples)
ϵ = rand(normal, 1, num_samples)
y = true_function.(x) + σ * ϵ
fig = scatter(vec(x), vec(y); axis = (xlabel = "x", ylabel = "y"), label = "data");
lines!(-1..1, true_function; color = :gray, label = "true");
axislegend();
fig
# 2.2. Exercise: Reason about using a linear regression to model `true_function`
target = preprocess(x, y)
fitted_linreg =
fit(linreg, target; optim_alg = DeepPumas.BFGS(), optim_options = (; iterations = 50))
coef(fitted_linreg)
ŷ_ex22_50iter = fitted_linreg(x)
fig = scatter(vec(x), vec(y); axis = (xlabel = "x", ylabel = "y"), label = "data");
scatter!(vec(x), vec(ŷ_ex22_50iter); label = "prediction");
lines!(-1..1, true_function; color = :gray, label = "true");
axislegend();
fig
# 2.3. Use a neural network (NN) to model `true_function`
nn = MLPDomain(1, (8, tanh), (1, identity); bias = true)
fitted_nn =
fit(nn, target; optim_alg = DeepPumas.BFGS(), optim_options = (; iterations = 50))
coef(fitted_nn) # try to make sense of the parameters in the NN
ŷ = fitted_nn(x)
fig = scatter(vec(x), vec(y); axis = (xlabel = "x", ylabel = "y"), label = "data");
scatter!(vec(x), vec(ŷ), label = "prediction");
lines!(-1..1, true_function; color = :gray, label = "true");
axislegend();
fig
#
# 3. BASIC UNDERFITTING AND OVERFITTING
#
# 3.1. Exercise: Investigate the impact of the number of fitting iterations in NNs
# (Hint: Train again the NN for few and for many iterations.)
# 3.2. Exercise: Reason about Exercise 2.2 again (that is, using a linear regression
# to model a quadratic relationship). Is the number of iterations relevant there?
# 3.3. The impact of the NN size
#
# 3.1. Exercise: Investigate the impact of the number of fitting iterations in NNs
# (Hint: Train again the NN for few and for many iterations.)
underfit_nn =
fit(nn, target; optim_alg = DeepPumas.BFGS(), optim_options = (; iterations = 2))
ŷ_underfit = underfit_nn(x)
overfit_nn =
fit(nn, target; optim_alg = DeepPumas.BFGS(), optim_options = (; iterations = 1_000))
ŷ_overfit = overfit_nn(x) # clarification on the term "overfitting"
fig = scatter(vec(x), vec(y); axis = (xlabel = "x", ylabel = "y"), label = "data");
scatter!(vec(x), vec(ŷ_underfit), label = "prediction (5 iterations)");
scatter!(vec(x), vec(ŷ), label = "prediction (50 iterations)");
scatter!(vec(x), vec(ŷ_overfit), label = "prediction (1000 iterations)");
lines!(-1..1, true_function; color = :gray, label = "true");
axislegend();
fig
# 3.2. Exercise: Reason about Exercise 2.2 again (that is, using a linear regression
# to model a quadratic relationship). Is the number of iterations relevant there?
# Investigate the effect of `max_iterations`.
max_iterations = 2
fitted_linreg = fit(
linreg,
target;
optim_alg = DeepPumas.BFGS(),
optim_options = (; iterations = max_iterations),
)
ŷ_linreg = fitted_linreg(x)
fig = scatter(vec(x), vec(y); axis = (xlabel = "x", ylabel = "y"), label = "data");
scatter!(vec(x), vec(ŷ_linreg), label = "$max_iterations iterations");
scatter!(vec(x), vec(ŷ_ex22_50iter), label = "50 iterations");
lines!(-1..1, true_function; color = :gray, label = "true");
axislegend();
fig
# 3.3. The impact of the NN size
nn = MLPDomain(1, (32, tanh), (32, tanh), (1, identity); bias = true)
fitted_nn = fit(
nn,
target;
optim_alg = DeepPumas.BFGS(),
optim_options = (; iterations = 1_000)
)
ŷ = fitted_nn(x)
fig = scatter(vec(x), vec(y); axis = (xlabel = "x", ylabel = "y"), label = "data");
scatter!(vec(x), vec(ŷ), label = "prediction MLP(1, 32, 32, 1)");
lines!(-1:0.1:1, true_function.(-1:0.1:1); color = :gray, label = "true");
axislegend();
fig
#
# 4. INSPECTION OF THE VALIDATION LOSS, REGULARIZATION AND HYPERPARAMETER TUNING
#
# 4.1. Validation loss as a proxy for generalization performance
# 4.2. Regularization to prevent overfitting
# 4.3. Hyperparameter tuning
#
# 4.1. Validation loss as a proxy for generalization performance
x_train, y_train = x, y
target_train = target
ϵ = rand(normal, 1, num_samples)
x_valid = rand(uniform, 1, num_samples)
y_valid = true_function.(x_valid) + σ * ϵ
target_valid = preprocess(x_valid, y_valid)
fig = scatter(vec(x_train), vec(y_train); axis = (xlabel = "x", ylabel = "y"), label = "training data");
scatter!(vec(x_valid), vec(y_valid); label = "validation data");
lines!(-1..1, true_function; color = :gray, label = "true");
axislegend();
fig
loss_train_l, loss_valid_l = Float64[], Float64[]
fitted_nn = fit(
nn,
target_train;
optim_alg = DeepPumas.BFGS(),
optim_options = (; iterations = 10),
)
push!(loss_train_l, mae(fitted_nn(x_train), y_train))
push!(loss_valid_l, mae(fitted_nn(x_valid), y_valid))
iteration_blocks = 100
for _ = 2:iteration_blocks
global fitted_nn = fit(
nn,
target_train,
coef(fitted_nn);
optim_alg = DeepPumas.BFGS(),
optim_options = (; iterations = 10),
)
push!(loss_train_l, mae(fitted_nn(x_train), y_train))
push!(loss_valid_l, mae(fitted_nn(x_valid), y_valid))
end
iteration = 10 .* (1:iteration_blocks)
fig, ax = scatterlines(
iteration,
Float32.(loss_train_l);
label = "training",
axis = (; xlabel = "Iteration", ylabel = "Mean absolute loss"),
);
scatterlines!(iteration, Float32.(loss_valid_l); label = "validation");
axislegend();
fig
# 4.2. Regularization to prevent overfitting
reg_nn = MLPDomain(1, (32, tanh), (32, tanh), (1, identity); bias = true, reg = L2(0.1))
reg_loss_train_l, reg_loss_valid_l = Float64[], Float64[]
fitted_reg_nn = fit(
reg_nn,
target_train;
optim_alg = DeepPumas.BFGS(),
optim_options = (; iterations = 10),
)
push!(reg_loss_train_l, mae(fitted_reg_nn(x_train), y_train))
push!(reg_loss_valid_l, mae(fitted_reg_nn(x_valid), y_valid))
iteration_blocks = 100
for _ = 2:iteration_blocks
global fitted_reg_nn = fit(
reg_nn,
target_train,
coef(fitted_reg_nn);
optim_alg = DeepPumas.BFGS(),
optim_options = (; iterations = 10),
)
push!(reg_loss_train_l, mae(fitted_reg_nn(x_train), y_train))
push!(reg_loss_valid_l, mae(fitted_reg_nn(x_valid), y_valid))
end
iteration = 10 .* (1:iteration_blocks)
fig, ax = scatterlines(
iteration,
Float32.(loss_train_l);
label = "training",
axis = (; xlabel = "Blocks of 10 iterations", ylabel = "Mean absolute loss"),
);
scatterlines!(iteration, Float32.(loss_valid_l); label = "validation");
scatterlines!(iteration, Float32.(reg_loss_train_l); label = "training (L2)");
scatterlines!(iteration, Float32.(reg_loss_valid_l); label = "validation (L2)");
axislegend();
fig
# 4.3. Programmatic hyperparameter tuning
nn_ho = hyperopt(reg_nn, target_train)
nn_ho.best_hyperparameters
ŷ_ho = nn_ho(x_valid)
fig = scatter(vec(x_valid), vec(y_valid); label = "validation data");
scatter!(vec(x_valid), vec(ŷ_ho), label = "prediction (hyperparam opt.)");
lines!(-1..1, true_function; color = :gray, label = "true");
axislegend(; position=:ct);
fig