How to use weighted loss function in model fit

Viewed 259

I have a LSTM model build on time series dataset, which is class imbalanced, 5000k(pos):7k (neg).

I tried to use a weighted loss function weighted_cross_entropy_with_logits as follows

    model = tf.keras.Sequential([
      tf.keras.layers.LSTM(64, activation='tanh', input_shape=(n_input, n_features),return_sequences = True),
      tf.keras.layers.LSTM(64, activation='tanh',return_sequences = True),
      tf.keras.layers.LSTM(64, activation='tanh',return_sequences = True),
      tf.keras.layers.LSTM(64, activation='tanh',return_sequences = True),
      tf.keras.layers.LSTM(64, activation='tanh',return_sequences = True),
      tf.keras.layers.LSTM(64, activation='tanh',return_sequences = False),
      tf.keras.layers.Dense(128, activation='relu')
    ])

    last_layer = tf.keras.layers.Dense(1,activation='sigmoid')
    model.add(last_layer)


   model.compile(optimizer='adam', loss= tf.nn.weighted_cross_entropy_with_logits ,metrics=['accuracy'])

The result shows I need a positional argument called pos_weight, how should I set pos_weight and use it in model.compile ?

I've searched on this, but did not find any example code about this Thank you!

0 Answers
Related