|
| 1 | +using Optimisers |
| 2 | +using ChainRulesCore, Functors, StaticArrays, Zygote |
| 3 | +using LinearAlgebra, Statistics, Test, Random |
| 4 | + |
| 5 | +Random.seed!(1) |
| 6 | + |
| 7 | +RULES = [ |
| 8 | + # All the rules at default settings |
| 9 | + Descent(), ADAM(), Momentum(), Nesterov(), RMSProp(), |
| 10 | + ADAGrad(), AdaMax(), ADADelta(), AMSGrad(), NADAM(), |
| 11 | + ADAMW(), RADAM(), OADAM(), AdaBelief(), |
| 12 | + # A few chained combinations: |
| 13 | + OptimiserChain(WeightDecay(), ADAM(0.001)), |
| 14 | + OptimiserChain(ClipNorm(), ADAM(0.001)), |
| 15 | + OptimiserChain(ClipGrad(0.5), Momentum()), |
| 16 | +] |
| 17 | + |
| 18 | +name(o) = typeof(o).name.name |
| 19 | +name(o::OptimiserChain) = join(name.(o.opts), " → ") |
| 20 | + |
| 21 | +@testset "independence" begin |
| 22 | + @testset "$(name(o))" for o in RULES |
| 23 | + w = randn(10, 10) |
| 24 | + w′ = randn(10, 10) |
| 25 | + iloss(x, w, w′) = mean((w*x .- w′*x) .^ 2) |
| 26 | + @test iloss(rand(10, 10), w, w′) > 1 |
| 27 | + st = Optimisers.setup(o, w) |
| 28 | + for t = 1:10^5 |
| 29 | + x = rand(10) |
| 30 | + gs = gradient(w -> iloss(x, w, w′), w) |
| 31 | + st, w = Optimisers.update!(st, w, gs...) |
| 32 | + end |
| 33 | + @test iloss(rand(10, 10), w, w′) < 0.01 |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +@testset verbose=true "simple sum" begin |
| 38 | + @testset "$(name(o))" for o in RULES |
| 39 | + m = shuffle!(reshape(1:64, 8, 8) .+ 0.0) |
| 40 | + s = Optimisers.setup(o, m) |
| 41 | + for _ in 1:10^5 |
| 42 | + g = gradient(x -> sum(abs2, x + x'), m)[1] |
| 43 | + s, m = Optimisers.update!(s, m, g) |
| 44 | + end |
| 45 | + # @test sum(m) < sum(1:64) |
| 46 | + if sum(m) < 1 |
| 47 | + @test sum(m) < 1 |
| 48 | + else |
| 49 | + @show name(o) sum(m)/sum(1:64) |
| 50 | + @test_broken sum(m) < 1 |
| 51 | + end |
| 52 | + end |
| 53 | +end |
| 54 | + |
| 55 | +@testset "original" begin |
| 56 | + @testset "$(name(o))" for o in RULES |
| 57 | + w′ = (α = rand(3, 3), β = rand(3, 3)) |
| 58 | + w = (α = 5rand(3, 3), β = rand(3, 3)) |
| 59 | + st = Optimisers.setup(o, w) |
| 60 | + loss(x, y) = mean((x.α .* x.β .- y.α .* y.β) .^ 2) |
| 61 | + @test loss(w, w′) > 1 |
| 62 | + for i = 1:10^4 |
| 63 | + gs = gradient(x -> loss(x, w′), w) |
| 64 | + st, w = Optimisers.update(st, w, gs...) |
| 65 | + end |
| 66 | + lw = loss(w, w′) |
| 67 | + if o isa ADADelta |
| 68 | + @show name(o) loss(w, w′) |
| 69 | + @test_broken lw < 0.001 |
| 70 | + else |
| 71 | + @test lw < 0.001 |
| 72 | + end |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +@testset verbose=true "StaticArrays" begin |
| 77 | + @testset "$(name(o))" for o in RULES |
| 78 | + W1 = @SMatrix randn(10, 10) |
| 79 | + b1 = @SVector randn(10) |
| 80 | + W2 = @SMatrix randn(10, 10) |
| 81 | + model = (; W1, b1, W2, tanh) |
| 82 | + s_loss(m, x, y) = sum(abs2, m.W2 * (m.tanh).(m.W1*x .+ m.b1) .- y) |
| 83 | + # x = @SMatrix randn(10, 10) |
| 84 | + # y = @SMatrix randn(10, 10) # gives an error from sum(; dims=()) |
| 85 | + x = @SVector randn(10) |
| 86 | + y = @SVector randn(10) |
| 87 | + @test s_loss(model, x, y) > 10 |
| 88 | + state = Optimisers.setup(o, model) |
| 89 | + for t = 1:10^3 |
| 90 | + g = gradient(m -> s_loss(m, x, y), model)[1] |
| 91 | + state, model = Optimisers.update!(state, model, g) |
| 92 | + end |
| 93 | + if o isa Union{Descent, RMSProp, ADAGrad, ADADelta, NADAM} |
| 94 | + @show name(o) s_loss(model, x, y) |
| 95 | + @test_broken s_loss(model, x, y) < 1 |
| 96 | + else |
| 97 | + @test s_loss(model, x, y) < 1 |
| 98 | + end |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | +@testset verbose=true "element types" begin |
| 103 | + @testset "$(name(o))" for o in RULES |
| 104 | + marray = (Float32[1,2], Float64[3,4], Float16[5,6]) |
| 105 | + types = map(eltype, marray) |
| 106 | + |
| 107 | + # This is a weak test, as it copies & then does `update!` |
| 108 | + uparray = Optimisers.update(Optimisers.setup(o, marray), marray, marray)[2] |
| 109 | + @test map(eltype, uparray) == types |
| 110 | + |
| 111 | + # Static version is truly out-of-place: |
| 112 | + mstatic = (SA{Float32}[1,2], SA{Float64}[3,4]) # , SA{Float16}[5,6]) with Float16, all fail |
| 113 | + upstatic = Optimisers.update(Optimisers.setup(o, mstatic), mstatic, mstatic)[2] |
| 114 | + if o isa OptimiserChain && o.opts[2] isa ADAM # These promote to Float64 |
| 115 | + @test_broken map(eltype, upstatic) == types[1:2] |
| 116 | + else |
| 117 | + @test map(eltype, upstatic) == types[1:2] |
| 118 | + end |
| 119 | + @test upstatic[1] isa SVector |
| 120 | + |
| 121 | + # With ordinary Array gradient, what happens? |
| 122 | + upstatic2 = Optimisers.update(Optimisers.setup(o, mstatic), mstatic, marray[1:2])[2] |
| 123 | + # @test map(eltype, upstatic2) == types[1:2] # same information |
| 124 | + if upstatic2[1] isa SVector |
| 125 | + @test upstatic2[1] isa SVector |
| 126 | + else |
| 127 | + @test_broken upstatic2[1] isa SVector |
| 128 | + end |
| 129 | + end |
| 130 | +end |
| 131 | + |
| 132 | +@testset "gradient types" begin |
| 133 | + @testset "$(name(o))" for o in RULES |
| 134 | + x = (a = ones(2,2), b = transpose(ones(2,2))) |
| 135 | + s = Optimisers.setup(o, x) |
| 136 | + |
| 137 | + _, x1 = Optimisers.update(s, x, (a = [1 2; 3 4], b = nothing)) |
| 138 | + @test x1.a != ones(2,2) |
| 139 | + @test x1.b == ones(2,2) |
| 140 | + |
| 141 | + _, xfill = Optimisers.update(s, x, (a = Zygote.Fill(2.0,2,2), b = Zygote.Fill(true,2,2))) |
| 142 | + @test xfill.a != ones(2,2) |
| 143 | + @test xfill.b != ones(2,2) |
| 144 | + |
| 145 | + bc = Optimisers.@.. 1 + log([2 3; 4 5]) / 6 |
| 146 | + _, xbc = Optimisers.update(s, x, (a = bc, b = bc)) |
| 147 | + @test xbc.a != ones(2,2) |
| 148 | + @test xbc.b != ones(2,2) |
| 149 | + |
| 150 | + th = ChainRulesCore.@thunk @. 1 + log([2 3; 4 5]) / 6 |
| 151 | + _, xth = Optimisers.update(s, x, (a = bc, b = bc)) |
| 152 | + @test xth.a != ones(2,2) |
| 153 | + @test xth.b != ones(2,2) |
| 154 | + end |
| 155 | +end |
| 156 | + |
0 commit comments