How to do imbalanced classification in deep learning (tensorflow, RNN)? I am trying to do binary classification of News Articles (Sports/Non-Sports) using recurrent neural net in tensorflow. The training data is highly skewed [Sports:Non-Sports::1:9].

I am using cross-entropy as my cost function, which treats both classes equally.

What are the ways by which user can penalise one class? Or is there any other cost function suitable for this purpose?
This has already been answered both in stackoverflow and crossvalidated.

The suggestion in both cases was to add class weights to the loss function, by multiplying logits:


loss(x, class) = weights[class] * (-x[class] + log(\sum_j exp(x[j])))


For example, in tensorflow you could do:


ratio = 31.0 / (500.0 + 31.0)
class_weight = tf.constant([ratio, 1.0 - ratio])
logits = ... # shape [batch_size, 2]
weighted_logits = tf.mul(logits, class_weight) # shape [batch_size, 2]
xent = tf.nn.softmax_cross_entropy_with_logits(
  weighted_logits, labels, name="xent_raw")