In Julia Flux, I keep getting the error LoadError: Mutating arrays is not supported, but I don't see where I am mutating an array
In Julia Flux, I keep getting the error LoadError: Mutating arrays is not supported, but I don't see where I am mutating an array
我是 Julia 的新手,出于某种原因我无法使用这个非常简单的代码。无论我尝试什么,我都会收到错误 LoadError: Mutating arrays is not supported
。我知道当我在优化过程中改变一个数组时会发生这个错误,这样代码就不再可微了。我显然对 Julia 了解不够,看不出我在做什么。
如果有帮助,错误似乎发生在行 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
看来我确实有一个旧版本的 Flux(但不是那么旧)。我必须卸载并重新安装 Julia 才能安装新版本的 Flux。
我是 Julia 的新手,出于某种原因我无法使用这个非常简单的代码。无论我尝试什么,我都会收到错误 LoadError: Mutating arrays is not supported
。我知道当我在优化过程中改变一个数组时会发生这个错误,这样代码就不再可微了。我显然对 Julia 了解不够,看不出我在做什么。
如果有帮助,错误似乎发生在行 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
看来我确实有一个旧版本的 Flux(但不是那么旧)。我必须卸载并重新安装 Julia 才能安装新版本的 Flux。