【问题标题】:Dimensions Mismatch in juliaJulia 中的尺寸不匹配
【发布时间】:2018-02-22 11:14:24
【问题描述】:

我收到此错误:

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))

【问题讨论】:

  • 对 Flux 不太熟悉,但如果您发布完整的错误消息,人们可能会更容易提供帮助 :)
  • 您的代码由于油门而引发 undefvar 错误。你能修好吗?

标签: machine-learning neural-network julia flux-machine-learning


【解决方案1】:

您的第一个密集层有一个大小为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))

【讨论】:

  • 还是不行。维度问题在data 中,因为这是我要传递给train! 的内容。
  • 我试过data = [( reshape([i],1,1) ,i) for i in 1:20],但也没用。
  • 这与我在回答中解释的不完全相同。您在上面所做的仍然是错误的。
猜你喜欢
  • 1970-01-01
  • 2012-09-11
  • 2014-12-14
  • 2022-09-27
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多