I am new to Julia and for some reason I can't get this very simple code to work. No matter what I try, I get the error LoadError: Mutating arrays is not supported. I understand that this error occurs when I mutate an array during the course of optimization so that the code is no longer differentiable. I clearly do not understand Julia enough to see where I am doing this.

If it helps the error seems to be occurring in the line for d in dataset.

using Statistics
using Flux: onehotbatch, onehot, onecold, crossentropy, throttle
using Flux
using Base.Iterators:repeated
using Plots:heatmap
using ImageView:imshow

images = Flux.Data.MNIST.images()[1:10]
labels = Flux.Data.MNIST.labels()[1:10]

heatmap(images[4], color=:grays, aspect_ratio=1)

X = float.(reshape.(images, :))
encode(x) = onehot(x, 0:9)
Y = encode.(labels)

m = Chain(Dense(28^2, 32, relu), Dense(32, 10), softmax)

loss(x, y) = crossentropy(m(x), y)
opt = ADAM()

accuracy(x, y) = mean(onecold(m(x)) .== onecold(y))

dataset = zip(X, Y)

print(size(X))

evalcb = () -> @show(loss(X, Y))

print("Training...")
# Flux.train!(loss, params(m), dataset, opt, cb=throttle(evalcb, 5));
for d in dataset
    print(d[2])
    gs = gradient(params(m)) do
        l = loss(d...)
    end
    update!(opt, params(m), gs)
end
1

There are 1 answers

0
user023049 On BEST ANSWER

It looks like I did have an old version of Flux (but not that old). I had to uninstall and reinstall Julia to install the new version of Flux.