# Flux CNN error: Scalar indexing is disallowed

**URL:** <https://discourse.julialang.org/t/flux-cnn-error-scalar-indexing-is-disallowed/88973>\
**Category:** Machine Learning\
**Tags:** question, flux, machine-learning\
**Created:** [October 19, 2022, 4:41pm UTC](https://discourse.julialang.org/t/flux-cnn-error-scalar-indexing-is-disallowed/88973 "2022-10-19T16:41:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitjamadagni](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amitjamadagni/32/3570_2.png) [@amitjamadagni](https://discourse.julialang.org/u/amitjamadagni)\
**Post date:** [October 19, 2022, 4:41pm UTC](https://discourse.julialang.org/t/flux-cnn-error-scalar-indexing-is-disallowed/88973/1 "2022-10-19T16:41:12Z")

</div>

Hi!

I have 1D data which I can successfully train and test with all to all connected 3 layer model as below:

```julia
function build_model_ffn(; nclasses=2)
    return Chain(
            Dense(99, 48, relu),
            Dense(48, 24, relu),
            Dense(24, 12, relu),
            Dense(12, nclasses))
end

```

I am currently trying to adapt the 1D data to CNN by converting it into a (33, 3, 1) with 1 channel (reshaping the input data), using the following model for training:

```julia
function build_model_dnn(; nclasses=2)
    return Chain(
            Conv((3,3), 1=>1, relu, pad=(1,1)),
            MaxPool((3,3)),
            flatten,
            Dense(11, 5, relu),
            Dense(5, nclasses)
          )
end

```

The core functionality of training and testing is adapted from the model zoo feed forward example and remains the same for both the above models. While the first one, `build_model_ffn` runs successfully, the latter fails with a scalar indexing error. I also tried adapting the convolution layer to be 1D which results in the same error. It would be helpful to know any insights on the error resulting in a successful run of the second model.

The following includes more details of the error:

> <https://gist.github.com/amitjamadagni/17748ef758550c7f8c686e9701e4e817>

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [October 19, 2022, 10:06pm UTC](https://discourse.julialang.org/t/flux-cnn-error-scalar-indexing-is-disallowed/88973/2 "2022-10-19T22:06:36Z")

</div>

```julia
 [11] conv(x::CuArray{Int64, 4, CUDA.Mem.DeviceBuffer}, w::CuArray{Float32, 4, CUDA.Mem.DeviceBuffer}, cdims::DenseConvDims{2, 2, 2, 4, 2}; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ NNlib /user/jlib/packages/NNlib/0QnJJ/src/conv.jl:88

```

Something is producing an array of integers `x` here, maybe your input data? The GPU `conv` wants all Float32, so instead you get some slow fallback, which works by indexing.

---

<div class="post-metadata">

**Author:** ![amitjamadagni](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amitjamadagni/32/3570_2.png) [@amitjamadagni](https://discourse.julialang.org/u/amitjamadagni)\
**Post date:** [October 20, 2022, 9:15am UTC](https://discourse.julialang.org/t/flux-cnn-error-scalar-indexing-is-disallowed/88973/3 "2022-10-20T09:15:53Z")

</div>

Many thanks! It was indeed that the input data is of the type int and converting it to float makes it work!

---

<div class="post-metadata">

**Author:** ![hamza1av](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hamza1av/32/208606_2.png) [@hamza1av](https://discourse.julialang.org/u/hamza1av)\
**Post date:** [May 10, 2024, 9:50pm UTC](https://discourse.julialang.org/t/flux-cnn-error-scalar-indexing-is-disallowed/88973/4 "2024-05-10T21:50:28Z")

</div>

Hello there, I got a similar error but on Apple Silicon using the MtlArrays. When I run a forward pass on the GPU with a model that has linear layers with relus it runs fine.  
However when I use Conv Layers I run into the error of scalar indexing.

I wonder what is going on under the hood, so that this error occurs.  
It is a bit frustrating, because It makes training models on larger datasets impossible in my use case.

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [May 11, 2024, 5:39am UTC](https://discourse.julialang.org/t/flux-cnn-error-scalar-indexing-is-disallowed/88973/5 "2024-05-11T05:39:49Z")

</div>

gpu convolution is not yet supported on apple sylicon
