Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when checking model target: expected dense_24 to have shape...but got array with shape... in Keras

Tags:

arrays

keras

I can't understand what this error message is trying to tell me.

Looking at the summary of my model, I am looking at the last few layers .

enter image description here

But when I fit my model, I get the this:

enter image description here

I don't understand. If I read this closely, Keras seems to be saying, "I looked at the labels (targets) for the validation set, and sensibly, this made me think the last layer in your model should be shaped (None, 2). But, instead of actually getting a last layer shaped (None, 2) in the model, the layer consisted of an actual array. Which was of some other shape."

This makes no sense.

I think that I suspect that this error really should say, in general, is:

"ValueError: Error when checking model target: although dense_n has shape (x, y), the shape of the target, (a,b), in incompatible." .

Does anyone care to agree or disagree? Thanks.

(There's a similar question here, but not very helpful.)

like image 939
Monica Heddneck Avatar asked Dec 28 '16 03:12

Monica Heddneck


2 Answers

I'm not sure if the answer you expect is this, but...

First: I agree - the error message seems weird, it should talk about incompatibility between dense_24 and target array.

Now, to solve your problem, you should either reshape your target array or create a different Dense at the end to match your array.

About your target array, for a classification in two classes, it should be shaped as:

  • (46000,2) if your classification uses two values, one for likelihood of class A and another for likelyhood of class B
  • (46000,1) if your classification uses a single value being 0 class A and 1 class B (in which case your dense layer should be (None,1))

What I think is the easiest solution:

  • Instead of having Dense(2,...) at the end of your model, use Dense(1, activation='sigmoid').

Why? Because your target data is shaped like (46000,1), meaning you have only one number for two classes. 0 is one class, 1 is another.

like image 55
Daniel Möller Avatar answered Oct 01 '22 05:10

Daniel Möller


Keras is saying the network you constructed outputs arrays of length two, but the your training data contains data with shape (0,1). However, training data shape should match network output shape.

The shape (None, 2) simply means that the network accepts a batch with any number of elements that themselves are arrays of two elements (The first element is batch size in Keras shapes). So the correct input data shape would be (2,).

From the error message we see you have 4600 pieces of training data with shape (0, 1). This the reason for conflict. I suspect something goes wrong with reading the training data and you are not actually fitting with arrays of two elements, like you have intended.

like image 45
milez Avatar answered Oct 01 '22 05:10

milez