我收到此错误:
DimensionMismatch("second dimension of A, 1, does not match length of x, 20")
以下代码。我正在尝试使用一些示例数据来训练模型。我正在 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))
最佳答案
您的第一个密集层有一个大小为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))
关于machine-learning - Julia 尺寸不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48926030/