|
| 1 | +@testset "NewRecur RNN" begin |
| 2 | + @testset "Forward Pass" begin |
| 3 | + # tanh is needed for forward check to determine ordering of inputs. |
| 4 | + cell = Flux.RNNCell(1, 1, tanh) |
| 5 | + layer = Fluxperimental.NewRecur(cell; return_sequence=true) |
| 6 | + layer.cell.Wi .= 5.0 |
| 7 | + layer.cell.Wh .= 4.0 |
| 8 | + layer.cell.b .= 0.0f0 |
| 9 | + layer.cell.state0 .= 7.0 |
| 10 | + x = reshape([2.0f0, 3.0f0], 1, 1, 2) |
| 11 | + |
| 12 | + # Lets make sure th output is correct |
| 13 | + h = cell.state0 |
| 14 | + h, out = cell(h, [2.0f0]) |
| 15 | + h, out = cell(h, [3.0f0]) |
| 16 | + |
| 17 | + @test eltype(layer(x)) <: Float32 |
| 18 | + @test size(layer(x)) == (1, 1, 2) |
| 19 | + @test layer(x)[1, 1, 2] ≈ out[1,1] |
| 20 | + |
| 21 | + @test length(layer(cell.state0, x)) == 2 # should return a tuple. Maybe better test is needed. |
| 22 | + @test layer(cell.state0, x)[2][1,1,2] ≈ out[1,1] |
| 23 | + |
| 24 | + @test_throws MethodError layer([2.0f0]) |
| 25 | + @test_throws MethodError layer([2.0f0;; 3.0f0]) |
| 26 | + end |
| 27 | + |
| 28 | + @testset "gradients-implicit" begin |
| 29 | + cell = Flux.RNNCell(1, 1, identity) |
| 30 | + layer = Flux.Recur(cell) |
| 31 | + layer.cell.Wi .= 5.0 |
| 32 | + layer.cell.Wh .= 4.0 |
| 33 | + layer.cell.b .= 0.0f0 |
| 34 | + layer.cell.state0 .= 7.0 |
| 35 | + x = [[2.0f0], [3.0f0]] |
| 36 | + |
| 37 | + # theoretical primal gradients |
| 38 | + primal = |
| 39 | + layer.cell.Wh .* (layer.cell.Wh * layer.cell.state0 .+ x[1] .* layer.cell.Wi) .+ |
| 40 | + x[2] .* layer.cell.Wi |
| 41 | + ∇Wi = x[1] .* layer.cell.Wh .+ x[2] |
| 42 | + ∇Wh = 2 .* layer.cell.Wh .* layer.cell.state0 .+ x[1] .* layer.cell.Wi |
| 43 | + ∇b = layer.cell.Wh .+ 1 |
| 44 | + ∇state0 = layer.cell.Wh .^ 2 |
| 45 | + |
| 46 | + nm_layer = Fluxperimental.NewRecur(cell; return_sequence = true) |
| 47 | + ps = Flux.params(nm_layer) |
| 48 | + x_block = reshape(vcat(x...), 1, 1, length(x)) |
| 49 | + e, g = Flux.withgradient(ps) do |
| 50 | + out = nm_layer(x_block) |
| 51 | + sum(out[1, 1, 2]) |
| 52 | + end |
| 53 | + |
| 54 | + @test primal[1] ≈ e |
| 55 | + @test ∇Wi ≈ g[ps[1]] |
| 56 | + @test ∇Wh ≈ g[ps[2]] |
| 57 | + @test ∇b ≈ g[ps[3]] |
| 58 | + @test ∇state0 ≈ g[ps[4]] |
| 59 | + end |
| 60 | + |
| 61 | + @testset "gradients-explicit" begin |
| 62 | + |
| 63 | + cell = Flux.RNNCell(1, 1, identity) |
| 64 | + layer = Flux.Recur(cell) |
| 65 | + layer.cell.Wi .= 5.0 |
| 66 | + layer.cell.Wh .= 4.0 |
| 67 | + layer.cell.b .= 0.0f0 |
| 68 | + layer.cell.state0 .= 7.0 |
| 69 | + x = [[2.0f0], [3.0f0]] |
| 70 | + |
| 71 | + # theoretical primal gradients |
| 72 | + primal = |
| 73 | + layer.cell.Wh .* (layer.cell.Wh * layer.cell.state0 .+ x[1] .* layer.cell.Wi) .+ |
| 74 | + x[2] .* layer.cell.Wi |
| 75 | + ∇Wi = x[1] .* layer.cell.Wh .+ x[2] |
| 76 | + ∇Wh = 2 .* layer.cell.Wh .* layer.cell.state0 .+ x[1] .* layer.cell.Wi |
| 77 | + ∇b = layer.cell.Wh .+ 1 |
| 78 | + ∇state0 = layer.cell.Wh .^ 2 |
| 79 | + |
| 80 | + |
| 81 | + x_block = reshape(vcat(x...), 1, 1, length(x)) |
| 82 | + nm_layer = Fluxperimental.NewRecur(cell; return_sequence = true) |
| 83 | + e, g = Flux.withgradient(nm_layer) do layer |
| 84 | + out = layer(x_block) |
| 85 | + sum(out[1, 1, 2]) |
| 86 | + end |
| 87 | + grads = g[1][:cell] |
| 88 | + |
| 89 | + @test primal[1] ≈ e |
| 90 | + @test ∇Wi ≈ grads[:Wi] |
| 91 | + @test ∇Wh ≈ grads[:Wh] |
| 92 | + @test ∇b ≈ grads[:b] |
| 93 | + @test ∇state0 ≈ grads[:state0] |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +@testset "New Recur RNN Partial Sequence" begin |
| 98 | + @testset "Forward Pass" begin |
| 99 | + cell = Flux.RNNCell(1, 1, identity) |
| 100 | + layer = Fluxperimental.NewRecur(cell) |
| 101 | + layer.cell.Wi .= 5.0 |
| 102 | + layer.cell.Wh .= 4.0 |
| 103 | + layer.cell.b .= 0.0f0 |
| 104 | + layer.cell.state0 .= 7.0 |
| 105 | + x = reshape([2.0f0, 3.0f0], 1, 1, 2) |
| 106 | + |
| 107 | + h = cell.state0 |
| 108 | + h, out = cell(h, [2.0f0]) |
| 109 | + h, out = cell(h, [3.0f0]) |
| 110 | + |
| 111 | + @test eltype(layer(x)) <: Float32 |
| 112 | + @test size(layer(x)) == (1, 1) |
| 113 | + @test layer(x)[1, 1] ≈ out[1,1] |
| 114 | + |
| 115 | + @test length(layer(cell.state0, x)) == 2 |
| 116 | + @test layer(cell.state0, x)[2][1,1] ≈ out[1,1] |
| 117 | + |
| 118 | + @test_throws MethodError layer([2.0f0]) |
| 119 | + @test_throws MethodError layer([2.0f0;; 3.0f0]) |
| 120 | + end |
| 121 | + |
| 122 | + @testset "gradients-implicit" begin |
| 123 | + cell = Flux.RNNCell(1, 1, identity) |
| 124 | + layer = Flux.Recur(cell) |
| 125 | + layer.cell.Wi .= 5.0 |
| 126 | + layer.cell.Wh .= 4.0 |
| 127 | + layer.cell.b .= 0.0f0 |
| 128 | + layer.cell.state0 .= 7.0 |
| 129 | + x = [[2.0f0], [3.0f0]] |
| 130 | + |
| 131 | + # theoretical primal gradients |
| 132 | + primal = |
| 133 | + layer.cell.Wh .* (layer.cell.Wh * layer.cell.state0 .+ x[1] .* layer.cell.Wi) .+ |
| 134 | + x[2] .* layer.cell.Wi |
| 135 | + ∇Wi = x[1] .* layer.cell.Wh .+ x[2] |
| 136 | + ∇Wh = 2 .* layer.cell.Wh .* layer.cell.state0 .+ x[1] .* layer.cell.Wi |
| 137 | + ∇b = layer.cell.Wh .+ 1 |
| 138 | + ∇state0 = layer.cell.Wh .^ 2 |
| 139 | + |
| 140 | + nm_layer = Fluxperimental.NewRecur(cell; return_sequence = false) |
| 141 | + ps = Flux.params(nm_layer) |
| 142 | + x_block = reshape(vcat(x...), 1, 1, length(x)) |
| 143 | + e, g = Flux.withgradient(ps) do |
| 144 | + out = (nm_layer)(x_block) |
| 145 | + sum(out) |
| 146 | + end |
| 147 | + |
| 148 | + @test primal[1] ≈ e |
| 149 | + @test ∇Wi ≈ g[ps[1]] |
| 150 | + @test ∇Wh ≈ g[ps[2]] |
| 151 | + @test ∇b ≈ g[ps[3]] |
| 152 | + @test ∇state0 ≈ g[ps[4]] |
| 153 | + end |
| 154 | + |
| 155 | + @testset "gradients-explicit" begin |
| 156 | + cell = Flux.RNNCell(1, 1, identity) |
| 157 | + layer = Flux.Recur(cell) |
| 158 | + layer.cell.Wi .= 5.0 |
| 159 | + layer.cell.Wh .= 4.0 |
| 160 | + layer.cell.b .= 0.0f0 |
| 161 | + layer.cell.state0 .= 7.0 |
| 162 | + x = [[2.0f0], [3.0f0]] |
| 163 | + |
| 164 | + # theoretical primal gradients |
| 165 | + primal = |
| 166 | + layer.cell.Wh .* (layer.cell.Wh * layer.cell.state0 .+ x[1] .* layer.cell.Wi) .+ |
| 167 | + x[2] .* layer.cell.Wi |
| 168 | + ∇Wi = x[1] .* layer.cell.Wh .+ x[2] |
| 169 | + ∇Wh = 2 .* layer.cell.Wh .* layer.cell.state0 .+ x[1] .* layer.cell.Wi |
| 170 | + ∇b = layer.cell.Wh .+ 1 |
| 171 | + ∇state0 = layer.cell.Wh .^ 2 |
| 172 | + |
| 173 | + x_block = reshape(vcat(x...), 1, 1, length(x)) |
| 174 | + nm_layer = Fluxperimental.NewRecur(cell; return_sequence = false) |
| 175 | + e, g = Flux.withgradient(nm_layer) do layer |
| 176 | + out = layer(x_block) |
| 177 | + sum(out) |
| 178 | + end |
| 179 | + grads = g[1][:cell] |
| 180 | + |
| 181 | + @test primal[1] ≈ e |
| 182 | + @test ∇Wi ≈ grads[:Wi] |
| 183 | + @test ∇Wh ≈ grads[:Wh] |
| 184 | + @test ∇b ≈ grads[:b] |
| 185 | + @test ∇state0 ≈ grads[:state0] |
| 186 | + |
| 187 | + end |
| 188 | +end |
0 commit comments