@@ -129,7 +129,34 @@ The initial tree of states comes from [`setup`](@ref).
129
129
130
130
This is used in exactly the same manner as [`update`](@ref), but because it may mutate
131
131
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]))
133
160
"""
134
161
update!
135
162
0 commit comments