ValueError: Shapes (None, 3) and (None, 2) are incompatible

Issue

So this is the code I’m trying to run

X_train = data1/255.0
from sklearn.preprocessing import LabelBinarizer
lb = LabelBinarizer()
trainY =lb.fit_transform(label)
from tensorflow.keras.models import Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Denseenter 
from tensorflow.keras.layers import concatenate
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import SGD
Model = Sequential()
shape = (100,100, 1)
Model.add(Conv2D(32,(3,3),padding="same",input_shape=shape))
Model.add(Activation("relu"))
Model.add(Conv2D(32,(3,3), padding="same"))
Model.add(Activation("relu"))
Model.add(MaxPooling2D(pool_size=(2,2)))
Model.add(Conv2D(64,(3,3), padding="same"))
Model.add(Activation("relu"))
Model.add(MaxPooling2D(pool_size=(2,2)))
Model.add(Flatten())
Model.add(Dense(512))
Model.add(Activation("relu"))
Model.add(Dense(2))
Model.add(Activation("softmax"))
Model.summary()
Model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
print("start training")
Model.fit(X_train,trainY,batch_size=5,epochs=10)

But I receive this Error:
target.shape.assert_is_compatible_with(output.shape)

ValueError: Shapes (None, 3) and (None, 2) are incompatible

Solution

Shape (None, 3) belongs to the trainY that came from the LabelBinarizer.
Shape (None, 2) is the shape of the model’s output.
The loss function needs two arrays with the same shape to compute the error. As the ValueError states, these two arrays are not the same shape.
Consider checking the output shape of your binarizer or changing the number of neurons in the network’s last layer to match the number of unique values in your label column of the dataset.

Answered By – Eyenpi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published