您的问题是多标签分类之一,在Keras的上下文中,我们将对此进行讨论,例如:https://github.com/fchollet/keras/issues/741
简而言之,keras中建议的解决方案是用sigmoid层替换softmax层,并使用binary_crossentropy作为成本函数.
该线程的一个例子:
# Build a classifier optimized for maximizing f1_score (uses class_weights) clf = Sequential() clf.add(Dropout(0.3)) clf.add(Dense(xt.shape[1], 1600, activation='relu')) clf.add(Dropout(0.6)) clf.add(Dense(1600, 1200, activation='relu')) clf.add(Dropout(0.6)) clf.add(Dense(1200, 800, activation='relu')) clf.add(Dropout(0.6)) clf.add(Dense(800, yt.shape[1], activation='sigmoid')) clf.compile(optimizer=Adam(), loss='binary_crossentropy') clf.fit(xt, yt, batch_size=64, nb_epoch=300, validation_data=(xs, ys), class_weight=W, verbose=0) preds = clf.predict(xs) preds[preds>=0.5] = 1 preds[preds<0.5] = 0 print f1_score(ys, preds, average='macro')