Max margin loss in TensorFlow

后端 未结 1 1631
野趣味
野趣味 2021-02-04 18:16

I\'m trying to implement a max margin loss in TensorFlow. the idea is that I have some positive example and i sample some negative examples and want to compute something like

1条回答
  •  深忆病人
    2021-02-04 18:26

    First we need to clean the input:

    • we want an array of positive scores, of shape [B, 1]
    • we want a matrix of negative scores, of shape [B, N]
    import tensorflow as tf
    
    B = 2
    N = 2
    scores = tf.constant([0.5, 0.2, -0.1, 1., -0.5, 0.3])  # shape B * (N+1)
    
    scores = tf.reshape(scores, [B, N+1])
    
    scores_pos = tf.slice(scores, [0, 0], [B, 1])
    
    scores_neg = tf.slice(scores, [0, 1], [B, N])
    

    Now we only have to compute the matrix of the loss, i.e. all the individual loss for every pair (positive, negative), and compute its sum.

    loss_matrix = tf.maximum(0., 1. - scores_pos + scores_neg)  # we could also use tf.nn.relu here
    loss = tf.reduce_sum(loss_matrix)
    

    0 讨论(0)
提交回复
热议问题