code: def soft_triplet_loss(
      anchor, positive, negative, extra=True, scope="soft_triplet_loss"
  ):
      r"""Loss for triplet networks as described in the paper:
      `Deep Metric Learning using Triplet Network
      <https://arxiv.org/abs/1412.6622>`_ by Hoffer et al.
  
      It is a softmax loss using :math:`(anchor-positive)^2` and
      :math:`(anchor-negative)^2` as logits.
  
      Args:
          anchor (tf.Tensor): anchor feature vectors of shape [Batch, N].
          positive (tf.Tensor): features of positive match of the same shape.
          negative (tf.Tensor): features of negative match of the same shape.
          extra (bool): also return distances for pos and neg.
  
      Returns:
          tf.Tensor: triplet-loss as scalar (and optionally average_pos_dist, average_neg_dist)
      """
  
      eps = 1e-10
      with tf.name_scope(scope):
          d_pos = tf.sqrt(
              tf.reduce_sum(tf.square(anchor - positive), 1) + eps
          )
          d_neg = tf.sqrt(
              tf.reduce_sum(tf.square(anchor - negative), 1) + eps
          )
  
          logits = tf.stack([d_pos, d_neg], axis=1)
          ones = tf.ones_like(tf.squeeze(d_pos), dtype="int32")
  
          loss = tf.reduce_mean(
              tf.nn.sparse_softmax_cross_entropy_with_logits(
                  logits=logits, labels=ones
              )
          )
  
          if extra:
              pos_dist = tf.reduce_mean(d_pos, name="pos-dist")
              neg_dist = tf.reduce_mean(d_neg, name="neg-dist")
              return loss, pos_dist, neg_dist
          else:
              return loss
  
imports: import argparse
  import numpy
  import tensorflow
  import tensorflow.contrib.slim
  from tensorpack import *
  from tensorpack.tfutils.summary import add_moving_summary
  from tensorpack.utils.gpu import change_gpu
  from embedding_data import MnistPairs, MnistTriplets, get_test_data
label_desc: Use image node_id 4 for calling a global function with example usage: soft_triplet_loss(anchor, positive, negative, extra, scope) and returns: loss,  pos_dist,  neg_dist, loss