denizyuret/AutoGrad.jl

incorrect gradient when indexing into a matrix of vectors

ylxdzsw opened this issue · 3 comments

I'm using latest master of AutoGrad.jl in Julia 0.6.3 on Windows and it gives an unexpected gradient when indexing into a matrix of vectors:

julia> a = Matrix{Array{Float32}}(2, 1)
2×1 Array{Array{Float32,N} where N,2}:
 #undef
 #undef

julia> a[1] = [2,3,4]
3-element Array{Int64,1}:
 2
 3
 4

julia> a[2] = [4,5,6]
3-element Array{Int64,1}:
 4
 5
 6

julia> g = grad(x->x[1]' * x[2])
(::gradfun) (generic function with 1 method)

julia> g(a)
2×1 Array{Any,2}:
 Float32[4.0, 5.0, 6.0]
 Float32[2.0, 3.0, 4.0]

julia> g = grad(x->x[1, 1]' * x[2, 1])
(::gradfun) (generic function with 1 method)

julia> g(a)
2×1 Array{Any,2}:
 4.0
 2.0

The two functions are identical in my opinion, but the latter one gives only the gradient of the first element in each vector.

Also, ideally in both definitions the result should be a 2×1 Array{Array{Float32,N} where N,2} (note that the first definition of g instead is an Array{Any,2})

@ylxdzsw fixed in latest master, please test.

@CarloLucibello I cannot do this in general because I want to be able to represent zero gradients for large arrays using nothing. So non-bitstype arrays may get converted to Any arrays.

Yes it works, thanks.