losses.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:youtube-8m 作者: wangheda 项目源码 文件源码
def calculate_loss(self, predictions, labels, margin=0.2, adaptive=3.0, origin=1.0, **unused_params):
    batch_size = FLAGS.batch_size
    num_classes = FLAGS.num_classes
    with tf.name_scope("loss_hinge"):
      # get sim_neg
      mask = tf.cast(labels, tf.float32)
      reverse_mask = 1.0 - mask
      min_true_pred = tf.reduce_min((predictions - 1.0) * mask, axis=1, keep_dims=True) + 1.0
      mask_wrong = tf.stop_gradient(tf.cast(predictions > (min_true_pred - margin), tf.float32) * reverse_mask)
      # get positve samples
      int_labels = tf.cast(labels, tf.int32)
      sample_labels = tf.unstack(int_labels, num=batch_size, axis=0)
      sample_predictions = tf.unstack(predictions, num=batch_size, axis=0)
      positive_predictions = []
      for sample_label, sample_prediction in zip(sample_labels, sample_predictions):
        indices = tf.where(sample_label > 0)
        expanded_indices = tf.tile(indices[:,0], [num_classes])[:num_classes]
        rand_arrange = tf.random_uniform([num_classes], minval=0, maxval=num_classes, dtype=tf.int32)
        positive_indices = tf.stop_gradient(tf.gather(expanded_indices, rand_arrange))
        positive_prediction = tf.gather(sample_prediction, positive_indices)
        positive_predictions.append(positive_prediction)
      positive_predictions = tf.stack(positive_predictions)
      # hinge_loss
      hinge_loss = tf.maximum(predictions - positive_predictions + margin, 0.0)
      adaptive_loss = hinge_loss * mask_wrong
      adaptive_loss = tf.reduce_mean(tf.reduce_sum(adaptive_loss, axis=1))
      origin_loss = hinge_loss * reverse_mask
      origin_loss = tf.reduce_mean(tf.reduce_sum(origin_loss, axis=1))
      loss = adaptive * adaptive_loss + origin * origin_loss
      return loss
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号