How to do imbalanced classification in deep learning (tensorflow, RNN)?
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")