Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product of non-dense matrix problem #83

Open
yiyuezhuo opened this issue Aug 22, 2020 · 0 comments
Open

Product of non-dense matrix problem #83

yiyuezhuo opened this issue Aug 22, 2020 · 0 comments

Comments

@yiyuezhuo
Copy link

Product of two Triangular matrix:

using Tracker
using LinearAlgebra

X = randn(3, 3)

function g1(x)
    y = UpperTriangular(x)
    sum(y * y)
end

Tracker.gradient(g1, X)

throw:

MethodError: Cannot `convert` an object of type Array{Float64,2} to an object of type UpperTriangular{Float64,Array{Float64,2}}
Closest candidates are:
  convert(::Type{var"#s826"} where var"#s826"<:UpperTriangular, !Matched::Union{UnitUpperTriangular, UpperTriangular}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/special.jl:61
  convert(::Type{var"#s826"} where var"#s826"<:UpperTriangular, !Matched::Union{LinearAlgebra.AbstractTriangular, Bidiagonal, Diagonal}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/special.jl:65
  convert(::Type{T}, !Matched::T) where T<:AbstractArray at abstractarray.jl:14
  ...

Stacktrace:
 [1] setproperty!(::Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}}, ::Symbol, ::Array{Float64,2}) at ./Base.jl:34
 [2] back(::Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}}, ::Array{Float64,2}, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:53
 [3] #13 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38 [inlined]
 [4] foreach at ./abstractarray.jl:2010 [inlined]
 [5] back_(::Tracker.Call{Tracker.var"#521#522"{TrackedArray{…,UpperTriangular{Float64,Array{Float64,2}}},TrackedArray{…,UpperTriangular{Float64,Array{Float64,2}}}},Tuple{Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}},Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}}}}, ::Array{Float64,2}, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38
 [6] back(::Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}}, ::Array{Float64,2}, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:58
 [7] #13 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38 [inlined]
 [8] foreach at ./abstractarray.jl:2010 [inlined]
 [9] back_(::Tracker.Call{Tracker.var"#494#495"{TrackedArray{…,UpperTriangular{Float64,Array{Float64,2}}}},Tuple{Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}}}}, ::Float64, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38
 [10] back(::Tracker.Tracked{Float64}, ::Int64, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:58
 [11] #back!#15 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:77 [inlined]
 [12] #back!#32 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/lib/real.jl:16 [inlined]
 [13] back!(::Tracker.TrackedReal{Float64}) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/lib/real.jl:14
 [14] gradient_(::Function, ::Array{Float64,2}) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:4
 [15] #gradient#24 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:164 [inlined]
 [16] gradient(::Function, ::Array{Float64,2}) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:164
 [17] top-level scope at In[43]:1
 [18] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

Product of two Triangular matrix (result is dense matrix):

function g2(x)
    y = UpperTriangular(x)
    sum(y' * y)
end

Tracker.gradient(g2, X)

throw:

MethodError: Cannot `convert` an object of type Base.ReshapedArray{Float64,2,Adjoint{Float64,Array{Float64,2}},Tuple{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}} to an object of type UpperTriangular{Float64,Array{Float64,2}}
Closest candidates are:
  convert(::Type{var"#s826"} where var"#s826"<:UpperTriangular, !Matched::Union{UnitUpperTriangular, UpperTriangular}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/special.jl:61
  convert(::Type{var"#s826"} where var"#s826"<:UpperTriangular, !Matched::Union{LinearAlgebra.AbstractTriangular, Bidiagonal, Diagonal}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/special.jl:65
  convert(::Type{T}, !Matched::T) where T<:AbstractArray at abstractarray.jl:14
  ...

Stacktrace:
 [1] setproperty!(::Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}}, ::Symbol, ::Base.ReshapedArray{Float64,2,Adjoint{Float64,Array{Float64,2}},Tuple{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}}) at ./Base.jl:34
 [2] back(::Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}}, ::Base.ReshapedArray{Float64,2,Adjoint{Float64,Array{Float64,2}},Tuple{Base.MultiplicativeInverses.SignedMultiplicativeInverse{Int64}}}, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:53
 [3] #13 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38 [inlined]
 [4] foreach at ./abstractarray.jl:2010 [inlined]
 [5] back_(::Tracker.Call{Tracker.var"#392#393"{TrackedArray{…,UpperTriangular{Float64,Array{Float64,2}}}},Tuple{Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}}}}, ::Array{Float64,2}, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38
 [6] back(::Tracker.Tracked{Adjoint{Float64,UpperTriangular{Float64,Array{Float64,2}}}}, ::Array{Float64,2}, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:58
 [7] #13 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38 [inlined]
 [8] foreach at ./abstractarray.jl:2010 [inlined]
 [9] back_(::Tracker.Call{Tracker.var"#521#522"{TrackedArray{…,Adjoint{Float64,UpperTriangular{Float64,Array{Float64,2}}}},TrackedArray{…,UpperTriangular{Float64,Array{Float64,2}}}},Tuple{Tracker.Tracked{Adjoint{Float64,UpperTriangular{Float64,Array{Float64,2}}}},Tracker.Tracked{UpperTriangular{Float64,Array{Float64,2}}}}}, ::Array{Float64,2}, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38
 [10] back(::Tracker.Tracked{Array{Float64,2}}, ::Array{Float64,2}, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:58
 [11] #13 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38 [inlined]
 [12] foreach at ./abstractarray.jl:2010 [inlined]
 [13] back_(::Tracker.Call{Tracker.var"#494#495"{TrackedArray{…,Array{Float64,2}}},Tuple{Tracker.Tracked{Array{Float64,2}}}}, ::Float64, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:38
 [14] back(::Tracker.Tracked{Float64}, ::Int64, ::Bool) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:58
 [15] #back!#15 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:77 [inlined]
 [16] #back!#32 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/lib/real.jl:16 [inlined]
 [17] back!(::Tracker.TrackedReal{Float64}) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/lib/real.jl:14
 [18] gradient_(::Function, ::Array{Float64,2}) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:4
 [19] #gradient#24 at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:164 [inlined]
 [20] gradient(::Function, ::Array{Float64,2}) at /home/yiyuezhuo/.julia/packages/Tracker/AMi2h/src/back.jl:164
 [21] top-level scope at In[45]:6
 [22] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

While it seems that one can get gradient if just one argument is Triangular matrix:

function g3(x)
    y = UpperTriangular(x)
    sum(2*y' .+ 1)
end

Tracker.gradient(g3, X)

gives:

([2.0 2.0 2.0; 0.0 2.0 2.0; 0.0 0.0 2.0] (tracked),)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant