denizyuret/AutoGrad.jl

bug with sum of number

CarloLucibello opened this issue · 2 comments

Hi found this strange bug:

julia> a=rand(3)
3-element Array{Float64,1}:
 0.35653  
 0.0676971
 0.12812  

julia> grad(x->sum(x'x))(a)
ERROR: InexactError()
Stacktrace:
 [1] ones at ./array.jl:263 [inlined]
 [2] ones at ./array.jl:264 [inlined]
 [3] ones at ./array.jl:266 [inlined]
 [4] ones at ./<missing>:0 [inlined]
 [5] sum(::Type{AutoGrad.Grad{1}}, ::Float64, ::Float64, ::AutoGrad.Rec{Float64}) at ./<missing>:0
 [6] backward_pass(::AutoGrad.Rec{Array{Float64,1}}, ::AutoGrad.Rec{Float64}, ::Array{AutoGrad.Node,1}) at /home/carlo/.julia/v0.6/AutoGrad/src/core.jl:256
 [7] (::AutoGrad.##gradfun#1#3{##5#6,Int64})(::Array{Any,1}, ::Function, ::Array{Float64,1}, ::Vararg{Array{Float64,1},N} where N) at /home/carlo/.julia/v0.6/AutoGrad/src/core.jl:40
 [8] (::AutoGrad.#gradfun#2)(::Array{Float64,1}, ::Vararg{Array{Float64,1},N} where N) at /home/carlo/.julia/v0.6/AutoGrad/src/core.jl:39
 [9] macro expansion at /home/carlo/.julia/v0.6/Revise/src/Revise.jl:775 [inlined]
 [10] (::Revise.##17#18{Base.REPL.REPLBackend})() at ./event.jl:73

It is strange because all of the following works (altough there are some inconsistencies)

julia> grad(x->x'x)(a)
3-element Array{Float64,1}:
 0.71306 
 0.135394
 0.25624 

julia> grad(x->x'x)(1)
2

julia> grad(x->sum(x'x))(1)
1-element Array{Float64,1}:
 2.0

julia> grad(x->sum(x'))(1)
1×1 RowVector{Float64,Array{Float64,1}}:
 1.0

julia> grad(x->sum(x'))(a)
3-element Array{Float64,1}:
 1.0
 1.0
 1.0

the problem is with sum(w) when w is a number:

#bug
julia> grad(w->sum(cosh(w)))(1.)
ERROR: InexactError()
Stacktrace:
 [1] ones at ./array.jl:263 [inlined]
 [2] ones at ./array.jl:264 [inlined]
 [3] ones at ./array.jl:266 [inlined]
 [4] ones at ./<missing>:0 [inlined]
 [5] sum(::Type{AutoGrad.Grad{1}}, ::Float64, ::Float64, ::AutoGrad.Rec{Float64}) at ./<missing>:0
 [6] backward_pass(::AutoGrad.Rec{Float64}, ::AutoGrad.Rec{Float64}, ::Array{AutoGrad.Node,1}) at /home/carlo/.julia/v0.6/AutoGrad/src/core.jl:256
 [7] (::AutoGrad.##gradfun#1#3{##33#34,Int64})(::Array{Any,1}, ::Function, ::Float64, ::Vararg{Float64,N} where N) at /home/carlo/.julia/v0.6/AutoGrad/src/core.jl:40
 [8] (::AutoGrad.#gradfun#2)(::Float64, ::Vararg{Float64,N} where N) at /home/carlo/.julia/v0.6/AutoGrad/src/core.jl:39
 [9] macro expansion at ./REPL.jl:97 [inlined]
 [10] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73

#ok
julia> grad(w->sum(cosh(w)))([1.])
1-element Array{Float64,1}:
 1.1752

and here another bug, this is really weird:

#ok 
julia> grad(w->sum(w))(1)
1-element Array{Float64,1}:
 1.0

#WTF?
julia> grad(w->sum(w))(2)
2-element Array{Float64,1}:
 1.0
 1.0

julia> grad(w->sum(w))(3)
3-element Array{Float64,1}:
 1.0
 1.0
 1.0