Jie HE
Jie HE

Reputation: 173

Class weights in binary classification model with Keras

We know that we can pass a class weights dictionary in the fit method for imbalanced data in binary classification model. My question is that, when using only 1 node in the output layer with sigmoid activation, can we still apply the class weights during the training?

model = Sequential()
model.add(Dense(64, activation='tanh',input_shape=(len(x_train[0]),)))
model.add(Dense(1, activation='sigmoid')) 

model.compile(
    optimizer=optimizer, 
    loss=loss, 
    metrics=metrics)

model.fit(
    x_train, y_train, 
    epochs=args.e, 
    batch_size=batch_size,
    class_weight={0: 1, 1: 3})

Upvotes: 14

Views: 8850

Answers (1)

pitfall
pitfall

Reputation: 2621

If you want to fully control this weight, why not write a custom loss function?

from keras import backend as K
def weighted_binary_crossentropy( y_true, y_pred, weight=1. ) :
    y_true = K.clip(y_true, K.epsilon(), 1-K.epsilon())
    y_pred = K.clip(y_pred, K.epsilon(), 1-K.epsilon())
    logloss = -(y_true * K.log(y_pred) * weight + (1 - y_true) * K.log(1 - y_pred))
    return K.mean( logloss, axis=-1)

Upvotes: 10

Related Questions