-
-
Notifications
You must be signed in to change notification settings - Fork 23
add trainables
#171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add trainables
#171
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
43e51a2
trainables
CarloLucibello a6e40a0
trainables
CarloLucibello 53e0678
cl/trainables
CarloLucibello 9131192
trainables
CarloLucibello 06f786c
test second order derivatives
CarloLucibello faea10f
add doc section
CarloLucibello 292a82d
fix test
CarloLucibello 2694e9e
Update src/trainables.jl
CarloLucibello c42823d
Update docs/src/index.md
CarloLucibello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
""" | ||
trainables(x) | ||
|
||
Return an iterable over all the trainable parameters in `x`, that is all the numerical | ||
CarloLucibello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
arrays (see [`isnumeric`](@ref Optimisers.isnumeric)) which are reachable through [`trainable`](@ref Optimisers.trainable). | ||
|
||
Parameters appearing multiple times in the model (tied weights) will be present only once in the output. | ||
|
||
See also [`destructure`](@ref) for a similar operation that returns a single flat vector instead. | ||
|
||
# Examples | ||
|
||
```jldoctest | ||
julia> struct MyLayer | ||
w | ||
b | ||
end | ||
|
||
julia> Functors.@functor MyLayer | ||
|
||
julia> Optimisers.trainable(x::MyLayer) = (; w = x.w,) # only w is trainable in this example | ||
|
||
julia> x = MyLayer([1.0,2.0,3.0], [4.0,5.0,6.0]); | ||
|
||
julia> trainables(x) | ||
1-element Vector{AbstractArray}: | ||
[1.0, 2.0, 3.0] | ||
|
||
julia> x = MyLayer((a=[1.0,2.0], b=[3.0]), [4.0,5.0,6.0]); | ||
|
||
julia> trainables(x) # collects nested parameters | ||
2-element Vector{AbstractArray}: | ||
[1.0, 2.0] | ||
[3.0] | ||
""" | ||
function trainables(x) | ||
arrays = AbstractArray[] | ||
exclude(x) = Optimisers.isnumeric(x) | ||
fmap(x; exclude, walk = Optimisers.TrainableStructWalk()) do y | ||
push!(arrays, y) | ||
return y | ||
end | ||
return arrays | ||
end | ||
|
||
function ∇trainables(x, Δ) | ||
exclude(x) = Optimisers.isnumeric(x) | ||
i = 0 | ||
return fmapstructure(x; exclude, walk = TrainableStructWalk()) do _ | ||
return Δ[i+=1] | ||
end | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(trainables), x) | ||
y = trainables(x) | ||
trainables_back(Δ) = (NoTangent(), ∇trainables(x, unthunk(Δ))) | ||
return y, trainables_back | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
|
||
m1 = collect(1:3.0) | ||
m2 = (collect(1:3.0), collect(4:6.0)) | ||
m3 = (x = m1, y = sin, z = collect(4:6.0)) | ||
|
||
m4 = (x = m1, y = m1, z = collect(4:6.0)) # tied | ||
m5 = (a = (m3, true), b = (m1, false), c = (m4, true)) | ||
m6 = (a = m1, b = [4.0 + im], c = m1) | ||
|
||
m7 = TwoThirds((sin, collect(1:3.0)), (cos, collect(4:6.0)), (tan, collect(7:9.0))) | ||
m8 = [Foo(m1, m1), (a = true, b = Foo([4.0], false), c = ()), [[5.0]]] | ||
|
||
mat = Float32[4 6; 5 7] | ||
m9 = (a = m1, b = mat, c = [mat, m1]) | ||
|
||
@testset "trainables" begin | ||
ps = trainables(m1) | ||
@test ps isa Vector | ||
@test length(ps) == 1 | ||
@test ps[1] == m1 | ||
|
||
ps = trainables(m2) | ||
@test ps isa Vector | ||
@test length(ps) == 2 | ||
@test ps[1] == m2[1] | ||
@test ps[2] == m2[2] | ||
|
||
ps = trainables(m3) | ||
@test length(ps) == 2 | ||
@test ps[1] == 1:3 | ||
@test ps[2] == 4:6 | ||
|
||
ps = trainables(m4) | ||
@test length(ps) == 2 | ||
@test ps[1] == 1:3 | ||
@test ps[2] == 4:6 | ||
|
||
ps = trainables(m5) | ||
@test length(ps) == 3 | ||
@test ps[1] == 1:3 | ||
@test ps[2] == 4:6 | ||
@test ps[3] == 4:6 | ||
|
||
ps = trainables(m6) | ||
@test length(ps) == 2 | ||
@test ps[1] == 1:3 | ||
@test ps[2] == ComplexF64[4.0 + 1.0im] | ||
|
||
ps = trainables(m7) | ||
@test length(ps) == 1 | ||
@test ps[1] == [1.0, 2.0, 3.0] | ||
|
||
ps = trainables(m8) | ||
@test length(ps) == 3 | ||
@test ps[1] == 1:3 | ||
@test ps[2] == [4.0] | ||
@test ps[3] == [5.0] | ||
|
||
ps = trainables(m9) | ||
@test length(ps) == 2 | ||
@test ps[1] == 1:3 | ||
@test ps[2] == mat | ||
end | ||
|
||
@testset "gradient" begin | ||
loss(m) = sum([sum(abs2, p) for p in trainables(m)]) | ||
g = gradient(loss, m1)[1] | ||
@test g == [2.0, 4.0, 6.0] | ||
|
||
g = gradient(loss, m2)[1] | ||
@test g == ([2.0, 4.0, 6.0], [8.0, 10.0, 12.0]) | ||
|
||
g = gradient(loss, m3)[1] | ||
@test g.x == [2.0, 4.0, 6.0] | ||
@test g.y === nothing | ||
@test g.z == [8.0, 10.0, 12.0] | ||
|
||
g = gradient(loss, m4)[1] | ||
@test g == (x = [2.0, 4.0, 6.0], y = [2.0, 4.0, 6.0], z = [8.0, 10.0, 12.0]) | ||
g.x === g.y # shared gradient for shared weights | ||
|
||
g = gradient(loss, m5)[1] | ||
@test g == (a = ((x = [2.0, 4.0, 6.0], y = nothing, z = [8.0, 10.0, 12.0]), nothing), b = ([2.0, 4.0, 6.0], nothing), c = ((x = [2.0, 4.0, 6.0], y = [2.0, 4.0, 6.0], z = [8.0, 10.0, 12.0]), nothing)) | ||
|
||
g = gradient(loss, m6)[1] | ||
@test g == (a = [2.0, 4.0, 6.0], b = ComplexF64[8.0 + 2.0im], c = [2.0, 4.0, 6.0]) | ||
|
||
g = gradient(loss, m7)[1] | ||
@test g == (a = (nothing, [2.0, 4.0, 6.0]), b = nothing, c = nothing) | ||
|
||
g = gradient(loss, m8)[1] | ||
@test g[1] == (x = [2.0, 4.0, 6.0], y = [2.0, 4.0, 6.0]) | ||
@test g[2] == (a = nothing, b = (x = [8.0], y = nothing), c = nothing) | ||
@test g[3] == [[10.0]] | ||
|
||
g = gradient(loss, m9)[1] | ||
@test g == (a = [2.0, 4.0, 6.0], b = Float32[8.0 12.0; 10.0 14.0], c = Array[Float32[8.0 12.0; 10.0 14.0], [2.0, 4.0, 6.0]]) | ||
end | ||
|
||
@testset "second order derivatives" begin | ||
struct DenseLayer | ||
w | ||
b | ||
end | ||
|
||
Functors.@functor DenseLayer | ||
|
||
loss(m) = sum([sum(abs2, p) for p in trainables(m)]) | ||
|
||
model = DenseLayer([1. 2.; 3. 4.], [0., 0.]) | ||
|
||
g = gradient(m -> loss(gradient(loss, m)), model)[1] | ||
@test g.w == [8.0 16.0; 24.0 32.0] | ||
@test g.b == [0.0, 0.0] | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.