denizyuret / AutoGrad.jl

Julia port of the Python autograd package.
Other
169 stars 26 forks source link

gradient of std(Array{Float32,N}) throws error #87

Closed rened closed 6 years ago

rened commented 6 years ago

For example, when adding the following lines to test/statistics.jl, 6 of these tests fail:

@test gradcheck(mean, randn(Float32,2,3))
@test gradcheck(mean, randn(Float32,2,3), kwargs=[:dims=>1])
@test gradcheck(mean, randn(Float32,2,3), kwargs=[:dims=>(1,2)])
@test gradcheck(meanabs, randn(Float32,2,3))
@test gradcheck(meanabs2, randn(Float32,2,3))
@test gradcheck(var, randn(Float32,2,3))
@test gradcheck(var, randn(Float32,2,3), kwargs=[:dims=>1])
@test gradcheck(var, randn(Float32,2,3), kwargs=[:dims=>(1,2)])
@test gradcheck(std, randn(Float32,2,3))
@test gradcheck(std, randn(Float32,2,3), kwargs=[:dims=>1])
@test gradcheck(std, randn(Float32,2,3), kwargs=[:dims=>(1,2)])

It seems that the eltype of the input data is not taken into account when allocating the output:

  Expression: gradcheck(var, randn(Float32, 2, 3), kwargs=[:dims => (1, 2)])
  MethodError: no method matching sum_outgrads(::Array{Float32,2}, ::Array{Float64,2})
  Closest candidates are:
    sum_outgrads(!Matched::Nothing, ::Any) at /home/rene/.julia/dev/AutoGrad/src/core.jl:499
    sum_outgrads(::AbstractArray{T,N} where N, !Matched::AbstractArray{T,N} where N) where T at /home/rene/.julia/dev/AutoGrad/src/core.jl:486
    sum_outgrads(!Matched::Rec, ::Any) at /home/rene/.julia/dev/AutoGrad/src/core.jl:490
    ...
  Stacktrace:
   [1] backward_pass(::Rec{Array{Float32,2}}, ::Rec{Float32}, ::Array{AutoGrad.Node,1}) at /home/rene/.julia/dev/AutoGrad/src/core.jl:252
   [2] (::getfield(AutoGrad, Symbol("##gradfun#1#2")){getfield(Main, Symbol("#g#54")){getfield(Main, Symbol("##g#52#53")){typeof(var)}},Int64})(::Base.Iterators.Pairs{Symbol,Tuple{Int64,Int64},Tuple{Symbol},NamedTuple{(:dims,),Tuple{Tuple{Int64,Int64}}}}, ::Function, ::
Array{Float32,2}) at /home/rene/.julia/dev/AutoGrad/src/core.jl:41
ekinakyurek commented 6 years ago

Fixed in #91

ekinakyurek commented 6 years ago

It fails if dims argument exists.

rened commented 6 years ago

Yes, with #91

@test gradcheck(std, randn(Float64,2,3))
@test gradcheck(std, randn(Float32,2,3))

works now, but std/var does not work with dims for neither Float32 nor Float64:

@test gradcheck(std, randn(Float64,2,3), kwargs=[:dims=>1])  # fails
@test gradcheck(std, randn(Float32,2,3), kwargs=[:dims=>1])  # fails
denizyuret commented 6 years ago

@ekinakyurek we should test with Float32 arrays in other files as well to check for type consistency. On Sun, Aug 19, 2018 at 6:46 AM Rene Donner notifications@github.com wrote:

Yes, with #91 https://github.com/denizyuret/AutoGrad.jl/pull/91

@test gradcheck(std, randn(Float64,2,3))@test gradcheck(std, randn(Float32,2,3))

works now, but std/var does not work with dims for neither Float32 nor Float64:

@test gradcheck(std, randn(Float64,2,3), kwargs=[:dims=>1]) # fails@test gradcheck(std, randn(Float32,2,3), kwargs=[:dims=>1]) # fails

— You are receiving this because you were assigned.

Reply to this email directly, view it on GitHub https://github.com/denizyuret/AutoGrad.jl/issues/87#issuecomment-414119114, or mute the thread https://github.com/notifications/unsubscribe-auth/ABvNpg9jpdsOCg7u269Ln7AABi4eAvtCks5uSUIQgaJpZM4WCmLQ .

denizyuret commented 6 years ago

Fixed this in latest master. Please check and if it is working add these tests to test/statistics.jl.

rened commented 6 years ago

Thanks, this works! Will add tests.

ekinakyurek commented 6 years ago

No need, I am currently adding different type tests to all test files.

On 19 Aug 2018, at 09:53, Rene Donner notifications@github.com wrote:

Thanks, this works! Will add tests.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/denizyuret/AutoGrad.jl/issues/87#issuecomment-414129356, or mute the thread https://github.com/notifications/unsubscribe-auth/AOpr8deNAx_mtAYvM5CJDMv9sF9wJXzLks5uSW3IgaJpZM4WCmLQ.