|
1 | 1 |
|
2 | 2 | using SliceMap
|
3 | 3 | using Test
|
| 4 | +using ForwardDiff, Tracker, Zygote, TensorCast |
4 | 5 |
|
5 |
| -@testset "nothing" begin |
| 6 | +Zygote.refresh() |
6 | 7 |
|
7 |
| - @test true |
| 8 | +@testset "columns" begin |
| 9 | + |
| 10 | + mat = rand(1:9, 3,10) |
| 11 | + fun(x) = 2 .+ x.^2 |
| 12 | + res = mapslices(fun, mat, dims=1) |
| 13 | + |
| 14 | + @test res ≈ mapcols(fun, mat) |
| 15 | + @test res ≈ MapCols{3}(fun, mat) |
| 16 | + |
| 17 | + grad = ForwardDiff.gradient(m -> sum(sin, mapslices(fun, m, dims=1)), mat) |
| 18 | + |
| 19 | + @test grad ≈ Tracker.gradient(m -> sum(sin, mapcols(fun, m)), mat)[1] |
| 20 | + @test grad ≈ Tracker.gradient(m -> sum(sin, MapCols{3}(fun, m)), mat)[1] |
| 21 | + |
| 22 | + @test grad ≈ Zygote.gradient(m -> sum(sin, mapcols(fun, m)), mat)[1] |
| 23 | + @test grad ≈ Zygote.gradient(m -> sum(sin, MapCols{3}(fun, m)), mat)[1] |
| 24 | + |
| 25 | + tcm(mat) = @cast out[i,j] := fun(mat[:,j])[i] |
| 26 | + @test res ≈ tcm(mat) |
| 27 | + @test grad ≈ Zygote.gradient(m -> sum(sin, tcm(m)), mat)[1] |
| 28 | + |
| 29 | +end |
| 30 | +@testset "columns, scalar" begin |
| 31 | + |
| 32 | + mat = rand(1:9, 3,10) |
| 33 | + fun(x) = sum(x) # different function! |
| 34 | + res = mapslices(fun, mat, dims=1) |
| 35 | + |
| 36 | + @test res ≈ mapcols(fun, mat) |
| 37 | + @test res ≈ MapCols{3}(fun, mat) |
| 38 | + |
| 39 | + grad = ForwardDiff.gradient(m -> sum(sin, mapslices(fun, m, dims=1)), mat) |
| 40 | + |
| 41 | + @test grad ≈ Tracker.gradient(m -> sum(sin, mapcols(fun, m)), mat)[1] |
| 42 | + @test grad ≈ Tracker.gradient(m -> sum(sin, MapCols{3}(fun, m)), mat)[1] |
| 43 | + |
| 44 | + @test grad ≈ Zygote.gradient(m -> sum(sin, mapcols(fun, m)), mat)[1] |
| 45 | + @test grad ≈ Zygote.gradient(m -> sum(sin, MapCols{3}(fun, m)), mat)[1] |
| 46 | + |
| 47 | + tcm3(mat) = @cast out[_,j] := fun(mat[:,j]) # changed here too |
| 48 | + @test res ≈ tcm3(mat) |
| 49 | + @test grad ≈ Zygote.gradient(m -> sum(sin, tcm3(m)), mat)[1] |
| 50 | + |
| 51 | +end |
| 52 | +@testset "columns, matrix" begin |
| 53 | + |
| 54 | + mat = rand(1:9, 3,10) |
| 55 | + fun(x) = x .* x' # different function! vector -> matrix |
| 56 | + res = mapslices(vec∘fun, mat, dims=1) |
| 57 | + |
| 58 | + @test res ≈ mapcols(fun, mat) |
| 59 | + @test res ≈ MapCols{3}(fun, mat) |
| 60 | + |
| 61 | + grad = ForwardDiff.gradient(m -> sum(sin, mapslices(vec∘fun, m, dims=1)), mat) |
| 62 | + |
| 63 | + @test grad ≈ Tracker.gradient(m -> sum(sin, mapcols(fun, m)), mat)[1] |
| 64 | + @test grad ≈ Tracker.gradient(m -> sum(sin, MapCols{3}(fun, m)), mat)[1] |
| 65 | + |
| 66 | + @test grad ≈ Zygote.gradient(m -> sum(sin, mapcols(fun, m)), mat)[1] |
| 67 | + @test grad ≈ Zygote.gradient(m -> sum(sin, MapCols{3}(fun, m)), mat)[1] |
| 68 | + |
| 69 | + tcm4(mat) = @cast out[i⊗i′,j] := fun(mat[:,j])[i,i′] i:3, i′:3 # changed here too |
| 70 | + @test res ≈ tcm4(mat) |
| 71 | + @test grad ≈ Zygote.gradient(m -> sum(sin, tcm4(m)), mat)[1] |
| 72 | + |
| 73 | +end |
| 74 | +@testset "rows" begin |
| 75 | + |
| 76 | + mat = randn(4,5) |
| 77 | + fun(x) = 2 .+ x.^2 ./ sum(x) |
| 78 | + |
| 79 | + res = mapslices(fun, mat, dims=2) |
| 80 | + @test res ≈ maprows(fun, mat) |
| 81 | + |
| 82 | + grad = ForwardDiff.gradient(m -> sum(sin, mapslices(fun, m, dims=2)), mat) |
| 83 | + @test grad ≈ Tracker.gradient(m -> sum(sin, maprows(fun, m)), mat)[1] |
| 84 | + @test grad ≈ Zygote.gradient(m -> sum(sin, maprows(fun, m)), mat)[1] |
| 85 | + |
| 86 | + tcm2(mat) = @cast out[i,j] := fun(mat[i,:])[j] |
| 87 | + @test res ≈ tcm2(mat) |
| 88 | + @test grad ≈ Zygote.gradient(m -> sum(sin, tcm2(m)), mat)[1] |
| 89 | + |
| 90 | +end |
| 91 | +@testset "slices" begin |
| 92 | + |
| 93 | + ten = randn(3,4,5,2) |
| 94 | + fun(x) = sqrt(3) .+ x.^3 ./ (sum(x)^2) |
| 95 | + res = mapslices(fun, ten, dims=3) |
| 96 | + |
| 97 | + @test res ≈ slicemap(fun, ten, dims=3) |
| 98 | + |
| 99 | + grad = ForwardDiff.gradient(x -> sum(sin, slicemap(fun, x, dims=3)), ten) |
| 100 | + @test grad ≈ Zygote.gradient(x -> sum(sin, slicemap(fun, x, dims=3)), ten)[1] |
8 | 101 |
|
9 | 102 | end
|
0 commit comments