Skip to content

Commit a59f688

Browse files
committed
Use extended help for loadmodel! docstring
1 parent cba299b commit a59f688

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

src/loading.jl

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
loadleaf!(dst, src, err) = dst
2+
loadleaf!(dst::AbstractArray, src, err) =
3+
error("Tried to copy $src into an array destination; this is not allowed.")
4+
loadleaf!(dst, src::AbstractArray, err) =
5+
error("Tried to copy an array to $dst; this is not allowed.")
26
function loadleaf!(dst::AbstractArray, src::Bool, err)
37
if iszero(src)
48
dst .= src
@@ -30,20 +34,12 @@ _bool_tie_check(dst, src) = true
3034
Copy all the parameters (trainable and non-trainable) from `src` into `dst`.
3135
3236
Recursively walks `dst` and `src` together using [`Functors.children`](@ref),
33-
and calling `copyto!` on parameter arrays.
34-
Non-array elements (such as activation functions) are not copied
35-
and do not need to match between `dst` and `src`.
36-
Inactive parameters can be encoded by using the boolean value `false` instead of an array.
37-
If `dst == false` and `src` is an all-zero array, no error will be raised (and no values copied);
38-
however, attempting to copy a non-zero array to an inactive parameter will throw an error.
39-
Likewise, copying `src == false` to any `dst` array is valid, but copying `src == true` will error.
40-
41-
Throws an error when:
42-
- `dst` and `src` do not share the same fields (at any level)
43-
- the sizes of leaf nodes are mismatched between `dst` and `src`
44-
- `dst` is a "tied" parameter (i.e. refers to another parameter) and
45-
loaded into multiple times with mismatched source values
37+
and calling `copyto!` on parameter arrays or throwing an error when there is a mismatch.
38+
Non-array elements (such as activation functions) are not copied and need not match.
39+
Zero bias vectors and `bias=false` are considered equivalent
40+
(see extended help for more details).
4641
42+
# Examples
4743
```julia
4844
julia> using Flux: loadmodel!
4945
@@ -69,6 +65,22 @@ true
6965
julia> dst[2].bias == src[2].bias
7066
true
7167
```
68+
69+
# Extended help
70+
71+
Throws an error when:
72+
- `dst` and `src` do not share the same fields (at any level)
73+
- the sizes of leaf nodes are mismatched between `dst` and `src`
74+
- copying non-array values to/from an array parameter
75+
(except inactive parameters described below)
76+
- `dst` is a "tied" parameter (i.e. refers to another parameter) and
77+
loaded into multiple times with mismatched source values
78+
79+
Inactive parameters can be encoded by using the boolean value `false` instead of an array.
80+
If `dst == false` and `src` is an all-zero array, no error will be raised (and no values copied);
81+
however, attempting to copy a non-zero array to an inactive parameter will throw an error.
82+
Likewise, copying a `src` value of `false` to any `dst` array is valid,
83+
but copying a `src` value of `true` will error.
7284
"""
7385
function loadmodel!(dst, src; cache = Base.IdSet())
7486
ldsts, _ = functor(dst)

0 commit comments

Comments
 (0)