Skip to content

Commit

Permalink
grad for reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Shashkin committed Mar 26, 2019
1 parent 38e3a88 commit f5edcc5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions test/tracker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit f5edcc5

Please sign in to comment.