Home > Enterprise >  Tensorflow - keras - shapes and loss for multilabel classification
Tensorflow - keras - shapes and loss for multilabel classification

Time:02-22

X_np_new.shape, y.shape 

((50876, 2304), (50876, 9))

Code:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.optimizers import SGD

model = Sequential()
model.add(Dense(5000, activation='relu', input_dim=X_np_new.shape[1]))
model.add(Dropout(0.1))
model.add(Dense(600, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(X_np_new.shape[1], activation='sigmoid'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd)

model.fit(X_np_new, y, epochs=5, batch_size=2000)

preds = model.predict(X_np_new)

I get error:

 ValueError: Shapes (None, 9) and (None, 2304) are incompatible

What went wrong here?

CodePudding user response:

Replace

model.add(Dense(X_np_new.shape[1], activation='sigmoid'))

With

model.add(Dense(y.shape[1], activation='sigmoid'))

Explanation:

Putting X_np_new.shape[1] in the last layer means you have 2304 classes because X_np_new.shape[1]=2304 but you actually have 9 classes that you can get that from y.shape[1].

ValueError: Shapes (None, 9) and (None, 2304) are incompatible

means that your model is expecting labels of Size [*, 2304] but your labels size is [*, 9].

  • Related