# Scalar getindex is disallowed

**URL:** <https://discourse.julialang.org/t/scalar-getindex-is-disallowed/53280>\
**Category:** General Usage\
**Tags:** gpu, gpuarrays\
**Created:** [January 13, 2021, 11:35am UTC](https://discourse.julialang.org/t/scalar-getindex-is-disallowed/53280 "2021-01-13T11:35:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgautam03](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgautam03/32/20545_2.png) [@tgautam03](https://discourse.julialang.org/u/tgautam03)\
**Post date:** [January 13, 2021, 11:35am UTC](https://discourse.julialang.org/t/scalar-getindex-is-disallowed/53280/1 "2021-01-13T11:35:15Z")

</div>

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

```julia
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

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

```

I get an error

```julia
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

```julia
#=
Apply sigmoid function to each element 
of the input CUDA Array
=#
function sigmoid(x:: CuArray)
    return 1 ./ (1 .+ exp.(-x))
end

```

```julia
#=
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.

---

<div class="post-metadata">

**Author:** ![tgautam03](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgautam03/32/20545_2.png) [@tgautam03](https://discourse.julialang.org/u/tgautam03)\
**Post date:** [January 13, 2021, 12:02pm UTC](https://discourse.julialang.org/t/scalar-getindex-is-disallowed/53280/2 "2021-01-13T12:02:11Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![mikkoku](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikkoku/32/16274_2.png) [@mikkoku](https://discourse.julialang.org/u/mikkoku)\
**Post date:** [January 13, 2021, 4:20pm UTC](https://discourse.julialang.org/t/scalar-getindex-is-disallowed/53280/3 "2021-01-13T16:20:22Z")

</div>

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

```julia
x = CuArray(-3.0:3)

```

at least it makes `exp.(x)` work.
