Skip to content

Commit a2313c8

Browse files
committed
Enable doctests
1 parent 7b804eb commit a2313c8

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[deps]
22
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
3+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
34
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
45
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
56
MLDatasets = "eb30cadb-4394-5ae3-aed4-317e484a6458"

docs/make.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using Documenter, Flux, NNlib, Functors, MLUtils, BSON, Optimisers, Plots, MLDatasets, Statistics
1+
using Documenter, Flux, NNlib, Functors, MLUtils, BSON, Optimisers, Plots, MLDatasets, Statistics, DataFrames
22

33

44
DocMeta.setdocmeta!(Flux, :DocTestSetup, :(using Flux); recursive = true)
55

66
makedocs(
7-
modules = [Flux, NNlib, Functors, MLUtils, BSON, Optimisers, Plots, MLDatasets, Statistics],
7+
modules = [Flux, NNlib, Functors, MLUtils, BSON, Optimisers, Plots, MLDatasets, Statistics, DataFrames],
88
doctest = false,
99
sitename = "Flux",
1010
pages = [

docs/src/getting_started/linear_regression.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,22 @@ julia> using MLDatasets: BostonHousing
270270
### Data
271271
Let's start by initializing our dataset. We will be using the [`BostonHousing`](https://juliaml.github.io/MLDatasets.jl/stable/datasets/misc/#MLDatasets.BostonHousing) dataset consisting of `506` data points. Each of these data points has `13` features and a corresponding label, the house's price. The `x`s are still mapped to a single `y`, but now, a single `x` data point has 13 features.
272272

273-
```julia linear_regression_complex
273+
```jldoctest linear_regression_complex
274+
julia> using DataFrames
275+
274276
julia> dataset = BostonHousing()
275277
dataset BostonHousing:
276278
metadata => Dict{String, Any} with 5 entries
277279
features => 506×13 DataFrame
278280
targets => 506×1 DataFrame
279281
dataframe => 506×14 DataFrame
280282
281-
julia> x, y = BostonHousing(as_df=false)[:]
283+
julia> x, y = BostonHousing(as_df=false)[:];
282284
```
283285

284286
We can now split the obtained data into training and testing data -
285287

286-
```julia linear_regression_complex
288+
```jldoctest linear_regression_complex
287289
julia> x_train, x_test, y_train, y_test = x[:, 1:400], x[:, 401:end], y[:, 1:400], y[:, 401:end];
288290
289291
julia> x_train |> size, x_test |> size, y_train |> size, y_test |> size
@@ -292,14 +294,14 @@ julia> x_train |> size, x_test |> size, y_train |> size, y_test |> size
292294

293295
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.
294296

295-
```julia linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
297+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
296298
julia> std(x_train)
297299
134.06784844377117
298300
```
299301

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

302-
```julia linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
304+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
303305
julia> x_train_n = Flux.normalise(x_train);
304306
305307
julia> std(x_train_n)
@@ -308,7 +310,7 @@ julia> std(x_train_n)
308310

309311
The standard deviation is now close to one! The last step for this section would be to wrap the `x`s and `y`s together to create the training data.
310312

311-
```julia linear_regression_complex
313+
```jldoctest linear_regression_complex
312314
julia> train_data = [(x_train_n, y_train)];
313315
```
314316

@@ -317,14 +319,14 @@ Our data is ready!
317319
### Model
318320
We can now directly use `Flux` and let it do all the work internally! Let's define a model that takes in 13 inputs (13 features) and gives us a single output (the label). We will then pass our entire data through this model in one go, and `Flux` will handle everything for us! Remember, we could have declared a model in plain `Julia` as well. The model will have 14 parameters, 13 weights, and one bias.
319321

320-
```julia linear_regression_complex
322+
```jldoctest linear_regression_complex
321323
julia> model = Dense(13 => 1)
322324
Dense(13 => 1) # 14 parameters
323325
```
324326

325327
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!
326328

327-
```julia linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
329+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
328330
julia> function loss(x, y)
329331
ŷ = model(x)
330332
Flux.mse(ŷ, y)
@@ -339,7 +341,7 @@ We can now proceed to the training phase!
339341
### Training
340342
Before training the model, let's initialize the optimiser and let `Flux` know that we want all the derivatives of all the parameters of our `model`.
341343

342-
```julia linear_regression_complex
344+
```jldoctest linear_regression_complex
343345
julia> opt = Descent(0.05);
344346
345347
julia> params = Flux.params(model);
@@ -348,16 +350,15 @@ julia> params = Flux.params(model);
348350
Contrary to our last training procedure, let's say that this time we don't want to hardcode the number of epochs. We want the training procedure to stop when the loss converges, that is, when `change in loss < δ`. The quantity `δ` can be altered according to a user's need, but let's fix it to `10⁻³` for this tutorial.
349351

350352
We can write such custom training loops effortlessly using Flux and plain Julia!
351-
```julia linear_regression_complex
353+
```jldoctest linear_regression_complex
352354
julia> loss_init = Inf;
353355
354356
julia> while true
355-
Flux.train!(loss, params, data, opt)
357+
Flux.train!(loss, params, train_data, opt)
356358
if loss_init == Inf
357359
loss_init = loss(x_train_n, y_train)
358360
continue
359361
end
360-
361362
if abs(loss_init - loss(x_train_n, y_train)) < 1e-3
362363
break
363364
else
@@ -372,7 +373,7 @@ This custom loop works! This shows how easily a user can write down any custom t
372373

373374
Let's have a look at the loss -
374375

375-
```julia linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
376+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
376377
julia> loss(x_train_n, y_train)
377378
27.127200028562164
378379
```
@@ -382,7 +383,7 @@ The loss went down significantly! It can be minimized further by choosing an eve
382383
### Testing
383384
The last step of this tutorial would be to test our model using the testing data. We will first normalise the testing data and then calculate the corresponding loss.
384385

385-
```julia linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
386+
```jldoctest linear_regression_complex; filter = r"[+-]?([0-9]*[.])?[0-9]+"
386387
julia> x_test_n = Flux.normalise(x_test);
387388
388389
julia> loss(x_test_n, y_test)

0 commit comments

Comments
 (0)