Skip to content

Commit a2128ba

Browse files
committed
Better variable names and cleaner print statements
1 parent 23a3a68 commit a2128ba

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

docs/src/getting_started/linear_regression.md

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ model(W, b, x) = Wx + b
7171
where `W` is the weight matrix and `b` is the bias. For our case, the weight matrix (`W`) would constitute only a single element, as we have only a single feature. We can define our model in `Julia` using the exact same notation!
7272

7373
```jldoctest linear_regression_simple
74-
julia> model(W, b, x) = @. W*x + b
75-
model (generic function with 1 method)
74+
julia> custom_model(W, b, x) = @. W*x + b
75+
custom_model (generic function with 1 method)
7676
```
7777

7878
The `@.` macro allows you to perform the calculations by broadcasting the scalar quantities (for example - the bias).
@@ -92,22 +92,22 @@ julia> b = [0.0f0]
9292
Time to test if our model works!
9393

9494
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
95-
julia> model(W, b, x) |> size
95+
julia> custom_model(W, b, x) |> size
9696
(1, 61)
9797
98-
julia> model(W, b, x)[1], y[1]
98+
julia> custom_model(W, b, x)[1], y[1]
9999
(-1.6116865f0, -7.0f0)
100100
```
101101

102102
It does! But the predictions are way off. We need to train the model to improve the predictions, but before training the model we need to define the loss function. The loss function would ideally output a quantity that we will try to minimize during the entire training process. Here we will use the mean sum squared error loss function.
103103

104104
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
105-
julia> function loss(W, b, x, y)
106-
ŷ = model(W, b, x)
105+
julia> function custom_loss(W, b, x, y)
106+
ŷ = custom_model(W, b, x)
107107
sum((y .- ŷ).^2) / length(x)
108108
end;
109109
110-
julia> loss(W, b, x, y)
110+
julia> custom_loss(W, b, x, y)
111111
23.772217f0
112112
```
113113

@@ -140,12 +140,12 @@ julia> flux_model(x)[1], y[1]
140140
It is! The next step would be defining the loss function using `Flux`'s functions -
141141

142142
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
143-
julia> function flux_loss(x, y)
143+
julia> function flux_loss(flux_model, x, y)
144144
ŷ = flux_model(x)
145145
Flux.mse(ŷ, y)
146146
end;
147147
148-
julia> flux_loss(x, y)
148+
julia> flux_loss(flux_model, x, y)
149149
22.74856f0
150150
```
151151

@@ -161,7 +161,7 @@ julia> W = Float32[1.1412252]
161161
To check how both the models are performing on the data, let's find out the losses using the `loss` and `flux_loss` functions -
162162

163163
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
164-
julia> loss(W, b, x, y), flux_loss(x, y)
164+
julia> custom_loss(W, b, x, y), flux_loss(flux_model, x, y)
165165
(22.74856f0, 22.74856f0)
166166
```
167167

@@ -182,9 +182,8 @@ The derivatives are calculated using an Automatic Differentiation tool, and `Flu
182182

183183
Our first step would be to obtain the gradient of the loss function with respect to the weights and the biases. `Flux` re-exports `Zygote`'s `gradient` function; hence, we don't need to import `Zygote` explicitly to use the functionality.
184184

185-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
186-
julia> dLdW, dLdb, _, _ = gradient(loss, W, b, x, y)
187-
(Float32[-6.7322206], Float32[-4.132563], Float32[0.1926041 0.14162663 … -0.39782608 -0.29997927], Float32[-0.16876957 -0.12410051 … 0.3485956 0.2628572])
185+
```jldoctest linear_regression_simple
186+
julia> dLdW, dLdb, _, _ = gradient(custom_loss, W, b, x, y);
188187
```
189188

190189
We can now update the parameters, following the gradient descent algorithm -
@@ -202,7 +201,7 @@ julia> b .= b .- 0.1 .* dLdb
202201
The parameters have been updated! We can now check the value of the loss function -
203202

204203
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
205-
julia> loss(W, b, x, y)
204+
julia> custom_loss(W, b, x, y)
206205
17.157953f0
207206
```
208207

@@ -211,26 +210,26 @@ The loss went down! This means that we successfully trained our model for one ep
211210
Let's plug our super training logic inside a function and test it again -
212211

213212
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
214-
julia> function train_model()
215-
dLdW, dLdb, _, _ = gradient(loss, W, b, x, y)
213+
julia> function train_custom_model()
214+
dLdW, dLdb, _, _ = gradient(custom_loss, W, b, x, y)
216215
@. W = W - 0.1 * dLdW
217216
@. b = b - 0.1 * dLdb
218217
end;
219218
220-
julia> train_model();
219+
julia> train_custom_model();
221220
222-
julia> W, b, loss(W, b, x, y)
221+
julia> W, b, custom_loss(W, b, x, y)
223222
(Float32[2.340657], Float32[0.7516814], 13.64972f0)
224223
```
225224

226225
It works, and the loss went down again! This was the second epoch of our training procedure. Let's plug this in a for loop and train the model for 30 epochs.
227226

228227
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
229228
julia> for i = 1:30
230-
train_model()
229+
train_custom_model()
231230
end
232231
233-
julia> W, b, loss(W, b, x, y)
232+
julia> W, b, custom_loss(W, b, x, y)
234233
(Float32[4.2408285], Float32[2.243728], 7.668049f0)
235234
```
236235

@@ -239,7 +238,7 @@ There was a significant reduction in loss, and the parameters were updated!
239238
`Flux` provides yet another convenience functionality, the [`Flux.@epochs`](@ref) macro, which can be used to train a model for a specific number of epochs.
240239

241240
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
242-
julia> Flux.@epochs 10 train_model()
241+
julia> Flux.@epochs 10 train_custom_model()
243242
[ Info: Epoch 1
244243
[ Info: Epoch 2
245244
[ Info: Epoch 3
@@ -251,7 +250,7 @@ julia> Flux.@epochs 10 train_model()
251250
[ Info: Epoch 9
252251
[ Info: Epoch 10
253252
254-
julia> W, b, loss(W, b, x, y)
253+
julia> W, b, custom_loss(W, b, x, y)
255254
(Float32[4.2422233], Float32[2.2460847], 7.6680417f0)
256255
```
257256

@@ -428,30 +427,30 @@ x = x .* reshape(rand(Float32, 61), (1, 61))
428427
plot(reshape(x, (61, 1)), reshape(y, (61, 1)), lw = 3, seriestype = :scatter, label = "", title = "Generated data", xlabel = "x", ylabel= "y")
429428

430429
# custom model and parameters
431-
model(W, b, x) = @. W*x + b
430+
custom_model(W, b, x) = @. W*x + b
432431
W = rand(Float32, 1, 1)
433432
b = [0.0f0]
434433

435434
# loss function
436-
function loss(model, x, y)
437-
ŷ = model(x)
435+
function custom_loss(W, b, x, y)
436+
ŷ = custom_model(W, b, x)
438437
sum((y .- ŷ).^2) / length(x)
439438
end;
440439

441-
print("Initial loss", loss(model, x, y), "\n")
440+
print("Initial loss: ", custom_loss(W, b, x, y), "\n")
442441

443442
# train
444-
function train_model()
445-
dLdW, dLdb, _, _ = gradient(loss, W, b, x, y)
443+
function train_custom_model()
444+
dLdW, dLdb, _, _ = gradient(custom_loss, W, b, x, y)
446445
@. W = W - 0.1 * dLdW
447446
@. b = b - 0.1 * dLdb
448447
end
449448

450449
for i = 1:40
451-
train_model()
450+
train_custom_model()
452451
end
453452

454-
print("Final loss", loss(model, x, y), "\n")
453+
print("Final loss: ", custom_loss(W, b, x, y), "\n")
455454

456455
# plot data and results
457456
plot(reshape(x, (61, 1)), reshape(y, (61, 1)), lw = 3, seriestype = :scatter, label = "", title = "Simple Linear Regression", xlabel = "x", ylabel= "y")
@@ -477,18 +476,18 @@ function loss(model, x, y)
477476
Flux.mse(ŷ, y)
478477
end;
479478

480-
print("Initial loss", loss(model, x_train_n, y_train), "\n")
479+
print("Initial loss: ", loss(model, x_train_n, y_train), "\n")
481480

482481
# train
483-
function train_model()
482+
function train_custom_model()
484483
dLdm, _, _ = gradient(loss, model, x, y)
485484
@. model.weight = model.weight - 0.000001 * dLdm.weight
486485
@. model.bias = model.bias - 0.000001 * dLdm.bias
487486
end
488487

489488
loss_init = Inf;
490489
while true
491-
train_model()
490+
train_custom_model()
492491
if loss_init == Inf
493492
loss_init = loss(model, x_train_n, y_train)
494493
continue
@@ -500,9 +499,9 @@ while true
500499
end
501500
end
502501

503-
print("Final loss", loss(model, x_train_n, y_train), "\n")
502+
print("Final loss: ", loss(model, x_train_n, y_train), "\n")
504503

505504
# test
506505
x_test_n = Flux.normalise(x_test);
507-
print("Test loss", loss(model, x_test_n, y_test), "\n")
506+
print("Test loss: ", loss(model, x_test_n, y_test), "\n")
508507
```

0 commit comments

Comments
 (0)