# Scalar indexing error when loading MNIST

**URL:** <https://discourse.julialang.org/t/scalar-indexing-error-when-loading-mnist/77496>\
**Category:** Machine Learning\
**Created:** [March 7, 2022, 8:09am UTC](https://discourse.julialang.org/t/scalar-indexing-error-when-loading-mnist/77496 "2022-03-07T08:09:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [March 7, 2022, 8:09am UTC](https://discourse.julialang.org/t/scalar-indexing-error-when-loading-mnist/77496/1 "2022-03-07T08:09:42Z")

</div>

The following code in a fresh session produces a scalar indexing error:

```julia
using Flux
using MLDatasets
categories = 0:9
xtrain, ytrain = MNIST.traindata(Float32)
xtest, ytest = MNIST.testdata(Float32)
ytrain, ytest = Flux.onehotbatch(ytrain, categories) |> gpu, Flux.onehotbatch(ytest, categories) |> gpu

```

What am I doing wrong, and how could I fix it?

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [March 7, 2022, 8:22am UTC](https://discourse.julialang.org/t/scalar-indexing-error-when-loading-mnist/77496/2 "2022-03-07T08:22:39Z")

</div>

I have found the sinner. Changing the original code to the following results in no error:

```julia
using Flux
using MLDatasets
categories = 0:9
xtrain, ytrain = MNIST.traindata(Float32)
xtest, ytest = MNIST.testdata(Float32)
ytrain = Flux.onehotbatch(ytrain, categories) |> gpu
ytest = Flux.onehotbatch(ytest, categories) |> gpu

```

I am note sure if it makes any difference, as it is possible that scalar indexing only occurs in the assignment, but this does not trigger the error.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [March 8, 2022, 5:37pm UTC](https://discourse.julialang.org/t/scalar-indexing-error-when-loading-mnist/77496/3 "2022-03-08T17:37:14Z")

</div>

The assignment should be succeeding fine, but it appears scalar indexing is happening in the repl when trying to show the tuple of `ytrain, xtest` in the first example:

```julia-repl
julia> Flux.onehotbatch(1:2, 1:2) |> cpu, Flux.onehotbatch(1:2, 1:2) |> cpu
(Bool[1 0; 0 1], Bool[1 0; 0 1])

julia> Flux.onehotbatch(1:2, 1:2) |> gpu, Flux.onehotbatch(1:2, 1:2) |> gpu
(Bool[Error showing value of type Tuple{Flux.OneHotArray{UInt32, 2, 1, 2, CuArray{UInt32, 1, CUDA.Mem.DeviceBuffer}}, Flux.OneHotArray{UInt32, 2, 1, 2, CuArray{UInt32, 1, CUDA.Mem.DeviceBuffer}}}:
ERROR: Scalar indexing is disallowed.
Invocation of getindex resulted in scalar indexing of a GPU array.
This is typically caused by calling an iterating implementation of a method.
Such implementations *do not* execute on the GPU, but very slowly on the CPU,
and therefore are only permitted from the REPL for prototyping purposes.
If you did intend to index this array, annotate the caller with @allowscalar.
...

```

I’ve opened [https://github.com/FluxML/Flux.jl/issues/1905](https://github.com/FluxML/Flux.jl/issues/1905) to track this.
