Scalar getindex is disallowed

tgautam03
Wed, 13 Jan 2021 11:35:15 +0000
General Usage

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.

3 posts - 2 participants