Skip to content

Commit 8f89bd7

Browse files
committed
More general regex
1 parent 055f6a4 commit 8f89bd7

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

docs/src/getting_started/linear_regression.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The `@.` macro allows you to perform the calculations by broadcasting the scalar
7070

7171
The next step would be to initialize the model parameters, which are the weight and the bias. There are a lot of initialization techniques available for different machine learning models, but for the sake of this example, let's pull out the weight from a uniform distribution and initialize the bias as `0`.
7272

73-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
73+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
7474
julia> W = rand(Float32, 1, 1)
7575
1×1 Matrix{Float32}:
7676
0.99285793
@@ -82,7 +82,7 @@ julia> b = [0.0f0]
8282

8383
Time to test if our model works!
8484

85-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
85+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
8686
julia> custom_model(W, b, x) |> size
8787
(1, 61)
8888
@@ -92,7 +92,7 @@ julia> custom_model(W, b, x)[1], y[1]
9292

9393
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.
9494

95-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
95+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
9696
julia> function custom_loss(W, b, x, y)
9797
ŷ = custom_model(W, b, x)
9898
sum((y .- ŷ).^2) / length(x)
@@ -113,14 +113,14 @@ Dense(1 => 1) # 2 parameters
113113

114114
A [`Dense(1 => 1)`](@ref Dense) layer denotes a layer of one neuron with one input (one feature) and one output. This layer is exactly same as the mathematical model defined by us above! Under the hood, `Flux` too calculates the output using the same expression! But, we don't have to initialize the parameters ourselves this time, instead `Flux` does it for us.
115115

116-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
116+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
117117
julia> flux_model.weight, flux_model.bias
118118
(Float32[1.1412252], Float32[0.0])
119119
```
120120

121121
Now we can check if our model is acting right. We can pass the complete data in one go, with each `x` having exactly one feature (one input) -
122122

123-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
123+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
124124
julia> flux_model(x) |> size
125125
(1, 61)
126126
@@ -130,7 +130,7 @@ julia> flux_model(x)[1], y[1]
130130

131131
It is! The next step would be defining the loss function using `Flux`'s functions -
132132

133-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
133+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
134134
julia> function flux_loss(flux_model, x, y)
135135
ŷ = flux_model(x)
136136
Flux.mse(ŷ, y)
@@ -143,15 +143,15 @@ julia> flux_loss(flux_model, x, y)
143143
Everything works as before! It almost feels like `Flux` provides us with smart wrappers for the functions we could have written on our own. Now, as the last step of this section, let's see how different the `flux_model` is from our custom `model`. A good way to go about this would be to fix the parameters of both models to be the same. Let's change the parameters of our custom `model` to match that of the `flux_model` -
144144

145145

146-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
146+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
147147
julia> W = Float32[1.1412252]
148148
1-element Vector{Float32}:
149149
1.1412252
150150
```
151151

152152
To check how both the models are performing on the data, let's find out the losses using the `loss` and `flux_loss` functions -
153153

154-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
154+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
155155
julia> custom_loss(W, b, x, y), flux_loss(flux_model, x, y)
156156
(22.74856f0, 22.74856f0)
157157
```
@@ -179,7 +179,7 @@ julia> dLdW, dLdb, _, _ = gradient(custom_loss, W, b, x, y);
179179

180180
We can now update the parameters, following the gradient descent algorithm -
181181

182-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
182+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
183183
julia> W .= W .- 0.1 .* dLdW
184184
1-element Vector{Float32}:
185185
1.8144473
@@ -191,7 +191,7 @@ julia> b .= b .- 0.1 .* dLdb
191191

192192
The parameters have been updated! We can now check the value of the loss function -
193193

194-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
194+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
195195
julia> custom_loss(W, b, x, y)
196196
17.157953f0
197197
```
@@ -200,7 +200,7 @@ The loss went down! This means that we successfully trained our model for one ep
200200

201201
Let's plug our super training logic inside a function and test it again -
202202

203-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
203+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
204204
julia> function train_custom_model()
205205
dLdW, dLdb, _, _ = gradient(custom_loss, W, b, x, y)
206206
@. W = W - 0.1 * dLdW
@@ -215,7 +215,7 @@ julia> W, b, custom_loss(W, b, x, y)
215215

216216
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.
217217

218-
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+"
218+
```jldoctest linear_regression_simple; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
219219
julia> for i = 1:40
220220
train_custom_model()
221221
end
@@ -281,14 +281,14 @@ julia> x_train |> size, x_test |> size, y_train |> size, y_test |> size
281281

282282
This data contains a diverse number of features, which means that the features have different scales. A wise option here would be to `normalise` the data, making the training process more efficient and fast. Let's check the standard deviation of the training data before normalising it.
283283

284-
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
284+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
285285
julia> std(x_train)
286286
134.06784844377117
287287
```
288288

289289
The data is indeed not normalised. We can use the [`Flux.normalise`](@ref) function to normalise the training data.
290290

291-
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
291+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
292292
julia> x_train_n = Flux.normalise(x_train);
293293
294294
julia> std(x_train_n)
@@ -307,7 +307,7 @@ Dense(13 => 1) # 14 parameters
307307

308308
Same as before, our next step would be to define a loss function to quantify our accuracy somehow. The lower the loss, the better the model!
309309

310-
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
310+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
311311
julia> function loss(model, x, y)
312312
ŷ = model(x)
313313
Flux.mse(ŷ, y)
@@ -356,7 +356,7 @@ This custom loop works! This shows how easily a user can write down any custom t
356356

357357
Let's have a look at the loss -
358358

359-
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
359+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
360360
julia> loss(model, x_train_n, y_train)
361361
27.127200028562164
362362
```
@@ -366,7 +366,7 @@ The loss went down significantly! It can be minimized further by choosing an eve
366366
### Testing
367367
The last step of this guide would be to test our model using the testing data. We will first normalise the testing data and then calculate the corresponding loss.
368368

369-
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
369+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+(f[+-]*[0-9])?"
370370
julia> x_test_n = Flux.normalise(x_test);
371371
372372
julia> loss(model, x_test_n, y_test)

xy.jld2

-1.31 KB
Binary file not shown.

0 commit comments

Comments
 (0)