The weight of the classes is multiplied by logits, so it works for sparse_softmax_cross_entropy_with_logits. See this solution for the “Losses for binary class classifier with imbalance in tensor flow” function.
As a side note, you can pass the scales directly to sparse_softmax_cross_entropy
tf.contrib.losses.sparse_softmax_cross_entropy(logits, labels, weight=1.0, scope=None)
This method is intended for the loss of transverse entropy using
tf.nn.sparse_softmax_cross_entropy_with_logits.
Weight acts as a loss factor. If a scalar is specified, then the loss is simply scaled by the given value. If the weight is a size tensor [batch_size], then the weight loss is applied to each corresponding sample.
source share