Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement multi-class hinge loss in tensorflow

I want to implement multi-class hinge loss in tensorflow. The formulation is as follows:

multi-class hinge loss function

I find it difficult to get the second max prediction probability when the prediction is correct. I tried to use tf.nn.top_k to calculate it, but unfortunately tf.nn.top_k doesn't implement the gradient operation. So how can I implement this?

like image 363
csz-carrot Avatar asked Apr 28 '16 02:04

csz-carrot


Video Answer


2 Answers

top_k has gradients, added in version 0.8 here

like image 79
Yaroslav Bulatov Avatar answered Sep 25 '22 15:09

Yaroslav Bulatov


Adding another implementation with three lines of code scores: unscaled scores, tensor, shape=(n_classes, batch_size), dtype=float32 classes: tensor, shape=(batch_size, batch_size), dtype=float32

For implementing above loss with choosing the most violated class instead of considering all classes

#H - hard negative for each sample
H = tf.reduce_max(scores * (1 - classes), 0)    
L = tf.nn.relu((1 - scores + H) * classes)
final_loss = tf.reduce_mean(tf.reduce_max(L, 0))

Another implementation where we sum over all negative classes

# implements loss as sum_(j~=y) max(0, 1 - s(x, y) + s(x, j))
def multiclasshingeloss1(scores, classes):
    true_classes = tf.argmax(classes, 0)
    idx_flattened = tf.range(0, scores.get_shape()[1]) * scores.get_shape()[0]+\
    tf.cast(true_classes, dtype=tf.int32)
    true_scores = tf.gather(tf.reshape(tf.transpose(scores), [-1]),
                            idx_flattened)
    L = tf.nn.relu((1 - true_scores + scores) * (1 - classes))
    final_loss = tf.reduce_mean(L)
    return final_loss

You can minimize the transposes here based on your implementation.

like image 31
ksikka Avatar answered Sep 22 '22 15:09

ksikka