Tensorflow - keras - 多标签分类的形状和损失

Tensorflow - keras - shapes and loss for multilabel classification

X_np_new.shape, y.shape 

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

代码:

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)

我收到错误:

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

这里出了什么问题?

替换

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

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

解释:

X_np_new.shape[1] 放在最后一层意味着你有 2304 类 因为 X_np_new.shape[1]=2304 但你实际上有 9 类 你可以从 y.shape[1].

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

表示您的模型需要尺寸为 [*, 2304] 的标签,但您的标签尺寸为 [*, 9]