diff --git a/src/tracker/lib/array.jl b/src/tracker/lib/array.jl index 3f47e89ce4..8e931acd2a 100644 --- a/src/tracker/lib/array.jl +++ b/src/tracker/lib/array.jl @@ -1,7 +1,7 @@ import Base: * import LinearAlgebra -import LinearAlgebra: inv, \, / +import LinearAlgebra: inv, det, logdet, logabsdet, \, / using Statistics using LinearAlgebra: Transpose, Adjoint, diagm, diag @@ -124,6 +124,15 @@ Base.adjoint(xs::TrackedArray) = track(adjoint, xs) @grad transpose(xs) = transpose(data(xs)), Δ -> (trim(xs, transpose(Δ)),) @grad adjoint(xs) = data(xs)', Δ -> (trim(xs, Δ'),) +det(xs::TrackedArray) = track(det, xs) +@grad det(xs) = det(data(xs)), Δ -> (Δ * det(xs) * transpose(inv(xs)),) + +logdet(xs::TrackedArray) = track(logdet, xs) +@grad logdet(xs) = logdet(data(xs)), Δ -> (Δ * transpose(inv(xs)),) + +logabsdet(xs::TrackedArray) = track(logabsdet, xs) +@grad logabsdet(xs) = logabsdet(data(xs)), Δ -> (Δ[1] * transpose(inv(xs)),) + Base.repeat(xs::TrackedArray; kw...) = track(repeat, xs; kw...) @grad function repeat(xs; inner=ntuple(x->1, ndims(xs)), outer=ntuple(x->1, ndims(xs))) diff --git a/test/tracker.jl b/test/tracker.jl index bb64f01a7e..edc81d0210 100644 --- a/test/tracker.jl +++ b/test/tracker.jl @@ -3,7 +3,7 @@ using Flux.Tracker, Test, NNlib using Flux.Tracker: TrackedReal, gradient, gradcheck, grad, checkpoint, forwarddiff using NNlib: conv, depthwiseconv using Printf: @sprintf -using LinearAlgebra: diagm, dot, LowerTriangular, norm +using LinearAlgebra: diagm, dot, LowerTriangular, norm, det, logdet, logabsdet using Statistics: mean, std using Random # using StatsBase @@ -34,6 +34,10 @@ gradtest(f, dims...) = gradtest(f, rand.(Float64, dims)...) @test gradtest(x -> x', rand(5)) +@test gradtest(det, (4, 4)) +@test gradtest(logdet, map((x) -> x*x', (rand(4, 4),))[1]) +@test gradtest((x) -> logabsdet(x)[1], (4, 4)) + @testset "indexing & slicing" begin gradtest(x->view(x, 1:2, 1:2), rand(4, 4)) end