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