2

I'm getting this error:

DimensionMismatch("second dimension of A, 1, does not match length of x, 20")

for the following code. I'm trying to train a model on some sample data. I'm using the Flux machine learning library in Julia.

I've checked my dimensions and they seem right to me. What is the problem?

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))
catastrophic-failure
  • 3,759
  • 1
  • 24
  • 43
mjsxbo
  • 2,116
  • 3
  • 22
  • 34

1 Answers1

4

Your first dense layer has a weight matrix whose size is 10x1. You can check it as follows:

m.layers[1].W

So, your data should be size of 1x20 so that you can multiply it with the weights in the chain.

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))
zwlayer
  • 1,752
  • 1
  • 18
  • 41
  • It still doesn't work. The dimension problem is in `data` since that is what I'm passing to `train!` . – mjsxbo Feb 23 '18 at 08:22
  • I tried `data = [( reshape([i],1,1) ,i) for i in 1:20]` but that doesn't work either. – mjsxbo Feb 23 '18 at 08:39
  • It is not exactly the same as I explained in my answer. What you did in above is still wrong. – zwlayer Feb 23 '18 at 19:32