mjsxbo
mjsxbo

Reputation: 2266

Dimensions Mismatch in julia

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

Upvotes: 2

Views: 2050

Answers (1)

zwlayer
zwlayer

Reputation: 1824

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

Upvotes: 4

Related Questions