我收到了这个错误:
DimensionMismatch("second dimension of A, 1, does not match length of x, 20")
以下代码。我正在尝试在一些样本data
上训练模型。我在Julia使用Flux
机器学习库。
我检查了我的尺寸,他们似乎对我而言。有什么问题?
using Flux
using Flux: mse
data = [(i,i) for i in 1:20]
x = [i for i in 1:20]
y = [i for i in 1:20]
m = Chain(
Dense(1, 10, relu),
Dense(10, 1),
softmax)
opt = ADAM(params(m))
loss(x, y) = mse(m(x), y)
evalcb = () -> @show(loss(x, y))
accuracy(x, y) = mean(argmax(m(x)) .== argmax(y))
#this line gives the error
Flux.train!(loss, data, opt,cb = throttle(evalcb, 10))
答案 0 :(得分:2)
您的第一个致密层有一个重量矩阵,其大小为10x1
。您可以按如下方式检查:
m.layers[1].W
因此,您的数据应该是1x20
的大小,以便您可以将其与链中的权重相乘。
x = reshape(x,1,20)
opt = ADAM(params(m))
loss(x, y) = mse(m(x), y)
evalcb = () -> @show(loss(x, y))
accuracy(x, y) = mean(argmax(m(x)) .== argmax(y))
#Now it should work.
Flux.train!(loss, data, opt,cb = Flux.throttle(evalcb, 10))