Skip to content

Commit a2c1b7d

Browse files
add an example to update! (#83)
* add an example to update! * StaticArrays example Co-authored-by: Michael Abbott <32575566+mcabbott@users.noreply.github.com>
1 parent 9b75dbb commit a2c1b7d

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/Optimisers.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,34 @@ The initial tree of states comes from [`setup`](@ref).
129129
130130
This is used in exactly the same manner as [`update`](@ref), but because it may mutate
131131
arrays within the old model (and the old state), it will be faster for models of ordinary
132-
`Array`s or `CuArray`s. However, you should not rely on the old model being fully updated.
132+
`Array`s or `CuArray`s. However, you should not rely on the old model being fully updated
133+
but rather use the returned model.
134+
135+
# Example
136+
137+
```jldoctest
138+
julia> using StaticArrays, Zygote, Optimisers
139+
140+
julia> m = (x = [1f0, 2f0], y = SA[4f0, 5f0]); # partly mutable model
141+
142+
julia> t = Optimisers.setup(Momentum(1/30, 0.9), m);
143+
144+
julia> g = gradient(m -> sum(abs2.(m.x .+ m.y)), m)[1]
145+
(x = Float32[10.0, 14.0], y = Float32[10.0, 14.0])
146+
147+
julia> t2, m2 = Optimisers.update!(t, m, g);
148+
149+
julia> m2 # after update or update!, this is the new model
150+
(x = Float32[0.6666666, 1.5333333], y = Float32[3.6666667, 4.5333333])
151+
152+
julia> m2.x === m.x # update! has re-used this array, for efficiency
153+
true
154+
155+
julia> m # original should be discarded, may be mutated but no guarantee
156+
(x = Float32[0.6666666, 1.5333333], y = Float32[4.0, 5.0])
157+
158+
julia> t # original state should likewise be discarded
159+
(x = Leaf(Momentum{Float64}(0.0333333, 0.9), Float32[0.333333, 0.466667]), y = Leaf(Momentum{Float64}(0.0333333, 0.9), Float32[0.0, 0.0]))
133160
"""
134161
update!
135162

0 commit comments

Comments
 (0)