Scalar getindex is disallowed

Hi, I just wrote a function that computes chain rule using Finite Difference derivatives and ran into a weird issue.

The function that calculates the derivative is defined as

function chain_deriv(chain:: Vector{<:Function}, x:: CuArray)
    n = size(chain)[1]
    df_dinp = 1
    for i in 1:n
        df_dinp = df_dinp .* fd_deriv(chain[i], x)
        x = chain[i](x)
    end
    return df_dinp
end 

and when I execute this

x = CuArray(-3:3)
chain1 = Vector([square, sigmoid])
y1 = chain_deriv(chain1, x)

I get an error

scalar getindex is disallowed

Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] assertscalar(::String) at /home/tushar/.julia/packages/GPUArrays/WV76E/src/host/indexing.jl:41
 [3] getindex at /home/tushar/.julia/packages/GPUArrays/WV76E/src/host/indexing.jl:96 [inlined]
 [4] _broadcast_getindex at ./broadcast.jl:614 [inlined]
 [5] _getindex at ./broadcast.jl:645 [inlined]
 [6] _broadcast_getindex at ./broadcast.jl:620 [inlined]
 [7] _getindex at ./broadcast.jl:645 [inlined]
 [8] _getindex at ./broadcast.jl:644 [inlined]
 [9] _broadcast_getindex at ./broadcast.jl:620 [inlined]
 [10] _getindex at ./broadcast.jl:645 [inlined]
 [11] _getindex at ./broadcast.jl:644 [inlined]
 [12] _broadcast_getindex at ./broadcast.jl:620 [inlined]
 [13] getindex at ./broadcast.jl:575 [inlined]
 [14] copy at ./broadcast.jl:876 [inlined]
 [15] materialize at ./broadcast.jl:837 [inlined]
 [16] sigmoid(::CuArray{Int64,1}) at ./In[6]:6
 [17] chain_deriv(::Array{Function,1}, ::CuArray{Int64,1}) at ./In[11]:6
 [18] top-level scope at In[17]:5
 [19] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

Here’s how I defined the FD derivative and sigmoid

#=
Apply sigmoid function to each element 
of the input CUDA Array
=#
function sigmoid(x:: CuArray)
    return 1 ./ (1 .+ exp.(-x))
end
#=
Evaluates the derivative of a function "func"
at every element in the CUDA Array "x"
=#
function fd_deriv(func:: Function, x:: CuArray)
    delta = .001
    return (func(x .+ delta) .- func(x .- delta)) / (2 * delta)
end

I don’t understand how I’m getting a scalar value here, because I’m trying to compute derivatives for elements in the CuArray.

I believe this is due to the scalar value e in function sigmoid.

I’ve found a workaround to this but I’m not sure if this is the most efficient way. I just modified it to something like this

#=
Apply sigmopid function to each element 
of the input CUDA Array
=#
function sigmoid(x:: CuArray)
    return 1 ./ (1 .+ CuArray(ones(size(x)) .* ℯ) .^(-x))
end

Try defining the array as Float64 not Int i.e.

x = CuArray(-3.0:3)

at least it makes exp.(x) work.