From f5edcc5873c76dee6add37f608e7d4603bad4939 Mon Sep 17 00:00:00 2001 From: Pavel Shashkin Date: Tue, 26 Mar 2019 21:44:09 +0300 Subject: [PATCH] grad for `reverse` --- src/lib/array.jl | 7 +++++++ test/tracker.jl | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/lib/array.jl b/src/lib/array.jl index 82d3c819..c5284688 100644 --- a/src/lib/array.jl +++ b/src/lib/array.jl @@ -240,6 +240,13 @@ Base.permutedims(xs::TrackedArray, perm) = track(permutedims, xs, perm) Base.PermutedDimsArray(xs::TrackedArray, perm) = track(PermutedDimsArray, xs, perm) @grad PermutedDimsArray(xs, perm) = PermutedDimsArray(data(xs), perm), Δ -> (PermutedDimsArray(Δ, invperm(perm)),nothing) +Base.reverse(xs::TrackedArray; dims) = track(reverse, xs, dims = dims) +@grad reverse(xs; dims) = reverse(data(xs), dims = dims), Δ -> (reverse(Δ, dims = dims), nothing) +Base.reverse(xs::TrackedVector) = track(reverse, xs) +@grad reverse(xs) = reverse(data(xs)), Δ -> (reverse(Δ),) +Base.reverse(xs::TrackedVector, start, stop) = track(reverse, xs, start, stop) +@grad reverse(xs, start, stop) = reverse(data(xs), start, stop), Δ -> (reverse(Δ, start, stop), nothing, nothing) + function _kron(mat1::AbstractMatrix,mat2::AbstractMatrix) m1, n1 = size(mat1) mat1_rsh = reshape(mat1,(1,m1,1,n1)) diff --git a/test/tracker.jl b/test/tracker.jl index a5412355..052e0fb8 100644 --- a/test/tracker.jl +++ b/test/tracker.jl @@ -125,6 +125,10 @@ end @test gradtest(x -> permutedims(x, [3,1,2]), rand(4,5,6)) @test gradtest(x -> PermutedDimsArray(x, [3,1,2]), rand(4,5,6)) +@test gradtest(reverse, rand(5)) +@test gradtest(x -> reverse(x, dims=2), rand(4,5,6)) +@test gradtest(x -> reverse(x, 2, 4), rand(5)) + @test gradtest(x -> repeat(x; inner=2), rand(5)) @test gradtest(x -> repeat(x; inner=2, outer=3), rand(5)) @test gradtest(x -> repeat(x; inner=(2,2,1), outer=(1,1,3)), rand(5,4,3))