Skip to content

Commit 8936859

Browse files
committed
Resolve more conflicts
1 parent a9f5c63 commit 8936859

File tree

5 files changed

+59
-36
lines changed

5 files changed

+59
-36
lines changed

docs/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
[deps]
2+
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
3+
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
24
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
35
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
46
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
7+
<<<<<<< HEAD
58
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
69
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
10+
=======
11+
>>>>>>> f437aabe (Add doctests for `recurrence.md`, `saving.md`, and `training.md`)
712

813
[compat]
914
Documenter = "0.26"

docs/make.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using Documenter, Flux, NNlib, Functors, MLUtils, CUDA
1+
using Documenter, Flux, NNlib, Functors, MLUtils, CUDA, BSON
22

33
DocMeta.setdocmeta!(Flux, :DocTestSetup, :(using Flux); recursive = true)
4-
makedocs(modules = [Flux, NNlib, Functors, MLUtils, CUDA],
4+
makedocs(modules = [Flux, NNlib, Functors, MLUtils, CUDA, BSON],
55
doctest = VERSION == v"1.5",
66
sitename = "Flux",
77
pages = ["Home" => "index.md",

docs/src/models/recurrence.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ The `Recur` wrapper stores the state between runs in the `m.state` field.
6464

6565
If we use the `RNN(2, 5)` constructor – as opposed to `RNNCell` – you'll see that it's simply a wrapped cell.
6666

67-
```julia
67+
```jldoctest recurrence; setup = :(using Random; Random.seed!(0))
68+
julia> using Flux
69+
6870
julia> RNN(2, 5) # or equivalently RNN(2 => 5)
6971
Recur(
7072
RNNCell(2 => 5, tanh), # 45 parameters
@@ -76,21 +78,28 @@ Equivalent to the `RNN` stateful constructor, `LSTM` and `GRU` are also availabl
7678

7779
Using these tools, we can now build the model shown in the above diagram with:
7880

79-
```julia
80-
m = Chain(RNN(2 => 5), Dense(5 => 1))
81+
```jldoctest recurrence
82+
julia> m = Chain(RNN(2 => 5), Dense(5 => 1))
83+
Chain(
84+
Recur(
85+
RNNCell(2 => 5, tanh), # 45 parameters
86+
),
87+
Dense(5 => 1), # 6 parameters
88+
) # Total: 6 trainable arrays, 51 parameters,
89+
# plus 1 non-trainable, 5 parameters, summarysize 580 bytes.
8190
```
8291
In this example, each output has only one component.
8392

8493
## Working with sequences
8594

8695
Using the previously defined `m` recurrent model, we can now apply it to a single step from our sequence:
8796

88-
```julia
97+
```jldoctest recurrence
8998
julia> x = rand(Float32, 2);
9099
91100
julia> m(x)
92101
1-element Vector{Float32}:
93-
0.31759313
102+
0.45860028
94103
```
95104

96105
The `m(x)` operation would be represented by `x1 -> A -> y1` in our diagram.
@@ -102,14 +111,14 @@ iterating the model on a sequence of data.
102111

103112
To do so, we'll need to structure the input data as a `Vector` of observations at each time step. This `Vector` will therefore be of `length = seq_length` and each of its elements will represent the input features for a given step. In our example, this translates into a `Vector` of length 3, where each element is a `Matrix` of size `(features, batch_size)`, or just a `Vector` of length `features` if dealing with a single observation.
104113

105-
```julia
114+
```jldoctest recurrence
106115
julia> x = [rand(Float32, 2) for i = 1:3];
107116
108117
julia> [m(xi) for xi in x]
109118
3-element Vector{Vector{Float32}}:
110-
[-0.033448644]
111-
[0.5259023]
112-
[-0.11183384]
119+
[0.36080405]
120+
[-0.1391441]
121+
[0.9310162]
113122
```
114123

115124
!!! warning "Use of map and broadcast"

docs/src/saving.md

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ session. The easiest way to do this is via
66

77
Save a model:
88

9-
```julia
9+
```jldoctest saving; setup = :(using Random; Random.seed!(0))
1010
julia> using Flux
1111
1212
julia> model = Chain(Dense(10, 5, NNlib.relu), Dense(5, 2), NNlib.softmax)
@@ -23,9 +23,7 @@ julia> @save "mymodel.bson" model
2323

2424
Load it again:
2525

26-
```julia
27-
julia> using Flux
28-
26+
```jldoctest saving
2927
julia> using BSON: @load
3028
3129
julia> @load "mymodel.bson" model
@@ -56,11 +54,13 @@ In some cases it may be useful to save only the model parameters themselves, and
5654
rebuild the model architecture in your code. You can use `params(model)` to get
5755
model parameters.
5856

59-
```Julia
60-
julia> using Flux
61-
57+
```jldoctest saving
6258
julia> model = Chain(Dense(10 => 5,relu),Dense(5 => 2),softmax)
63-
Chain(Dense(10, 5, NNlib.relu), Dense(5, 2), NNlib.softmax)
59+
Chain(
60+
Dense(10 => 5, relu), # 55 parameters
61+
Dense(5 => 2), # 12 parameters
62+
NNlib.softmax,
63+
) # Total: 4 arrays, 67 parameters, 524 bytes.
6464
6565
julia> weights = Flux.params(model);
6666
@@ -72,10 +72,12 @@ julia> @save "mymodel.bson" weights
7272
You can easily load parameters back into a model with `Flux.loadparams!`.
7373

7474
```julia
75-
julia> using Flux
76-
7775
julia> model = Chain(Dense(10 => 5,relu),Dense(5 => 2),softmax)
78-
Chain(Dense(10, 5, NNlib.relu), Dense(5, 2), NNlib.softmax)
76+
Chain(
77+
Dense(10 => 5, relu), # 55 parameters
78+
Dense(5 => 2), # 12 parameters
79+
NNlib.softmax,
80+
) # Total: 4 arrays, 67 parameters, 524 bytes.
7981

8082
julia> using BSON: @load
8183

@@ -90,32 +92,39 @@ The new `model` we created will now be identical to the one we saved parameters
9092

9193
In longer training runs it's a good idea to periodically save your model, so that you can resume if training is interrupted (for example, if there's a power cut). You can do this by saving the model in the [callback provided to `train!`](training/training.md).
9294

93-
```julia
94-
using Flux: throttle
95-
using BSON: @save
95+
```jldoctest saving
96+
julia> using Flux: throttle
9697
97-
m = Chain(Dense(10 => 5, relu), Dense(5 => 2), softmax)
98+
julia> using BSON: @save
99+
100+
julia> m = Chain(Dense(10 => 5, relu), Dense(5 => 2), softmax)
101+
Chain(
102+
Dense(10 => 5, relu), # 55 parameters
103+
Dense(5 => 2), # 12 parameters
104+
NNlib.softmax,
105+
) # Total: 4 arrays, 67 parameters, 524 bytes.
98106
99-
evalcb = throttle(30) do
100-
# Show loss
101-
@save "model-checkpoint.bson" model
102-
end
107+
julia> evalcb = throttle(30) do
108+
# Show loss
109+
@save "model-checkpoint.bson" model
110+
end
111+
(::Flux.var"#throttled#70"{Flux.var"#throttled#66#71"{Bool, Bool, var"#1#2", Int64}}) (generic function with 1 method)
103112
```
104113

105114
This will update the `"model-checkpoint.bson"` file every thirty seconds.
106115

107116
You can get more advanced by saving a series of models throughout training, for example
108117

109118
```julia
110-
@save "model-$(now()).bson" model
119+
julia> @save "model-$(now()).bson" model
111120
```
112121

113122
will produce a series of models like `"model-2018-03-06T02:57:10.41.bson"`. You
114123
could also store the current test set loss, so that it's easy to (for example)
115124
revert to an older copy of the model if it starts to overfit.
116125

117126
```julia
118-
@save "model-$(now()).bson" model loss = testloss()
127+
julia> @save "model-$(now()).bson" model loss = testloss()
119128
```
120129

121130
Note that to resume a model's training, you might need to restore other stateful parts of your training loop. Possible examples are stateful optimizers (which usually utilize an `IdDict` to store their state), and the randomness used to partition the original data into the training and validation sets.
@@ -124,7 +133,7 @@ You can store the optimiser state alongside the model, to resume training
124133
exactly where you left off. BSON is smart enough to [cache values](https://github.com/JuliaIO/BSON.jl/blob/v0.3.4/src/write.jl#L71) and insert links when saving, but only if it knows everything to be saved up front. Thus models and optimizers must be saved together to have the latter work after restoring.
125134

126135
```julia
127-
opt = ADAM()
128-
@save "model-$(now()).bson" model opt
136+
julia> opt = ADAM()
137+
julia> @save "model-$(now()).bson" model opt
129138
```
130139

docs/src/training/training.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ A convenient way to run multiple epochs from the REPL is provided by `@epochs`.
116116
julia> using Flux: @epochs
117117

118118
julia> @epochs 2 println("hello")
119-
INFO: Epoch 1
119+
[ Info: Epoch 1
120120
hello
121-
INFO: Epoch 2
121+
[ Info: Epoch 2
122122
hello
123123

124124
julia> @epochs 2 Flux.train!(...)

0 commit comments

Comments
 (0)