# MLP - DimensionMisMatch Error Just Before Training

**URL:** <https://discourse.julialang.org/t/mlp-dimensionmismatch-error-just-before-training/74007>\
**Category:** Machine Learning\
**Tags:** question, flux, machine-learning\
**Created:** [January 3, 2022, 10:48pm UTC](https://discourse.julialang.org/t/mlp-dimensionmismatch-error-just-before-training/74007 "2022-01-03T22:48:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sherlock\_holmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sherlock_holmes/32/32280_2.png) [@sherlock\_holmes](https://discourse.julialang.org/u/sherlock_holmes)\
**Post date:** [January 3, 2022, 10:48pm UTC](https://discourse.julialang.org/t/mlp-dimensionmismatch-error-just-before-training/74007/1 "2022-01-03T22:48:03Z")

</div>

Hi, I’m trying to train a MLP model to predict number of real roots of polynomials. x\_train and y\_train include array of arrays such as [[-204, 20, 13, 1, 0]] which are coefficients of polynomials. x\_test and y\_test include number of real roots of each polynomial such 1,2,5… I have polynomials up to 100 degree therefore 101 expresses that. I’m stuck in this error. I’m trying to give data to MLP properly but I couldn’t do it even though I tried so hard. Can you please help me about this issue?

Here is my code:

```julia
using Flux

function input()
	## x_train
	lines = Tuple(readlines("/home/user/Desktop/x_train.txt"))
	x_train = []

	for i in lines
		push!(x_train, convert(Array{Float32},eval(Meta.parse(i))))
	end

	x_train = reshape(x_train, 2, :)
	## y_train
	lines = Tuple(readlines("/home/user/Desktop/y_train.txt"))
	y_train = []

	for i in lines
		push!(y_train, eval(Meta.parse(i)))
	end
	
	y_train = reshape(y_train, 2, :)

	## x_test
	lines = Tuple(readlines("/home/user/Desktop/x_test.txt"))
	x_test = []

	for i in lines
		push!(x_test, convert(Array{Float32},eval(Meta.parse(i))))
	end

	x_test = reshape(x_test, 1, :)

	## y_test
	lines = Tuple(readlines("/home/user/Desktop/y_test.txt"))
	y_test = []

	for i in lines
		push!(y_test, eval(Meta.parse(i)))
	end
	
	y_test = reshape(y_test, 1, :)

	return x_train, x_test, y_train, y_test
end

x_train, x_test, y_train, y_test = input()

train_size = 18

function NeuralNetwork()
    return Chain(
            Dense(2, 9,relu),
            Dense(9,101), softmax
            )
end

# Organizing the data in batches
X = hcat(x_train,y_train)
Y = vcat(ones(train_size),zeros(train_size))
Y = reshape(Y, 18, 2)
println(size(X))
println(size(Y))
data = Flux.Data.DataLoader((X, Y'), batchsize=100,shuffle=true);
# Defining our model, optimization algorithm and loss function
m = NeuralNetwork()
opt = Descent(0.05)
loss(x, y) = sum(Flux.Losses.binarycrossentropy(m(x), y))

ps = Flux.params(m)
epochs = 20
for i in 1:epochs
    Flux.train!(loss, ps, data, opt)
end

```

Error Message I Got:

```julia
ERROR: LoadError: DimensionMismatch("dimensions must match: a has dims (Base.OneTo(5),), b has dims (Base.OneTo(4),), mismatch at 1")

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [January 3, 2022, 10:55pm UTC](https://discourse.julialang.org/t/mlp-dimensionmismatch-error-just-before-training/74007/2 "2022-01-03T22:55:04Z")

</div>

My advice here is the same as [LSTM Method Error - Time Series - #2 by ToucheSir](https://discourse.julialang.org/t/lstm-method-error-time-series/73973/2) work backwards and make sure all your shapes are correct.

---

<div class="post-metadata">

**Author:** ![sherlock\_holmes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sherlock_holmes/32/32280_2.png) [@sherlock\_holmes](https://discourse.julialang.org/u/sherlock_holmes)\
**Post date:** [January 3, 2022, 10:57pm UTC](https://discourse.julialang.org/t/mlp-dimensionmismatch-error-just-before-training/74007/3 "2022-01-03T22:57:21Z")

</div>

Thank you very much for your answer!
