Yet another "Error when checking target: expected dense_2 to have shape (4,) but got array with shape (1,)"

936 Views Asked by At

I'm using Keras in Python 3. The issue I'm having seems to be similar to many others, and the best I can tell I might need to use Flatten(), though I am not seeing how to set the parameters correctly. I get the error:

ValueError: Error when checking target: expected dense_2 to have shape (4,) but got array with shape (1,)

My data is not of images (yet) but they are sequences I've turned in to data frames.

model = Sequential()
model.add(Dense(30, input_dim=16, activation='relu'))
model.add(Dense(len(TheBinsizeList), activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])


model.fit(Train_Columns, TrainTarget_Columns.to_frame(), epochs=100, batch_size=64)

print(Train_Columns.shape)
# Gives a value of (1627, 16)


print((TrainTarget_Columns.to_frame()).shape)
# Gives a value of (1627,1)

Now the value of TrainTarget_Columns are 1627 of these two tuples:

(1494, 3) (1080, 2) (1863, 2) (919, 4) (1700, 2) (710, 4) (1365, 4) (1815, 3) (1477, 2) (1618, 1)...

The subject number is the first entry in each of the tuble, and the second entry is the value that is the training target.

While I see that changing TheBinsizeList from 4 to 2 in dense_2 causes the expected shape to go from (4,) to (2,) I don't see how to use Flatten (if that is what is needed) to correctly format the values.

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 30)                510       
_________________________________________________________________
dense_2 (Dense)              (None, 4)                 124       
=================================================================
Total params: 634
Trainable params: 634
Non-trainable params: 0

Any help is appreciated and thanks.

1

There are 1 best solutions below

0
qmeeus On BEST ANSWER

Considering your model summary, the model expects an input of shape (batch_size, 16) and a target of shape (batch_size, 4).

If your target's shape is (1627,1) there is your problem.

Solution: Change it to a one hot variable (e.g. using tf.one_hot(y, n_classes)) and the error should disappear

import numpy as np
import tensorflow as tf

input_dim = 16
hidden_dim = 30
n_classes = 4

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(hidden_dim, input_dim=input_dim, activation='relu'))
model.add(tf.keras.layers.Dense(n_classes, input_dim=hidden_dim, activation='relu'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

X = np.random.randn(100, input_dim)
y = np.random.randint(0, n_classes, size=(100,))

model.fit(X, y)
# ValueError: Shapes (None, 1) and (None, 4) are incompatible

y = tf.one_hot(y, n_classes)
model.fit(X, y)
# Works !