Reputation: 31
I am trying to solve a simple problem of multi-class classification. But somehow I am getting this error. The program works when I use a single layer and with 2 neurons but results are not good but that doesn't make sense because its a multi-class problem so the output layer should have 4 neurons at the output. Following that it does not work. I think there is a very silly mistake which I am doing here. Below is my code.
import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.optimizers import Adam
# Generate dummy data
import numpy as np
x_train = np.array([[1, 1],
[1, 2],
[2, 2],
[-1,0],
[-1,-2],
[2, 1],
[-1,-2],
[-1,-2]])
print(x_train.shape)
y_train = np.array([[0,0],
[0,0],
[0,1],
[0,1],
[1,0],
[1,0],
[1,1],
[1,1]])
# from keras.utils.np_utils import to_categorical
# y_train = to_categorical(y_train)
print(y_train.shape)
model = Sequential()
model.add(Dense(8, activation='relu', input_dim=2))
# model.add(Dropout(0.5))
# model.add(Dense(4, activation='relu'))
model.add(Dense(4, activation='softmax'))
# model.add(Dropout(0.5))
adm = Adam(lr=0.001)
# sgd = SGD(lr=0.001)
model.compile(loss='categorical_crossentropy',
optimizer=adm,
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=20, batch_size=1)
score = model.evaluate(x_train, y_train, batch_size=1)
Error:
ValueError: Shapes (1, 2) and (1, 4) are incompatible
Upvotes: 0
Views: 553
Reputation: 31
I solved this error. The reason was my labels i.e. y_train was of not the appropriate shape which caused the problem. Here, the number of classes is 4 and my training data is labeled in this format [0,0,1,1,2,2,3,3]. In my problem statement above the way I represented my labels in a categorical format was totally wrong. So, to properly encode the data I used ** keras.utils.to_categorical(y)** to convert my labels into one-hot encoding which worked and solved the shape error.
y_train = keras.utils.to_categorical(y)
After this the shape becomes:
(8, 4)
So, whenever there is a multi-class problem the shape of the labels when encoding it into a one-hot encoding type should be
(size_of_data, num_classes).
I learned now how to properly encode the data when working with multi-class problem and if such error arises where to look to solve the issue.
Upvotes: 3