Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从非急切tensorflow中的每一行3d矩阵中选择随机非零元素_Python_Tensorflow_Loss Function_Triplet - Fatal编程技术网

Python 从非急切tensorflow中的每一行3d矩阵中选择随机非零元素

Python 从非急切tensorflow中的每一行3d矩阵中选择随机非零元素,python,tensorflow,loss-function,triplet,Python,Tensorflow,Loss Function,Triplet,我试图用随机负三重态选择实现三重态丢失。 现在我有一个形状张量(batch_size,batch_size,batch_size),元素(I,j,k)等于 dist(i,j)-dist(i,k)+margin(i是锚,j是正对,k是负对) 我将所有无效元素归零,并取tf.最大值(张量,0.) 现在,对于每一对i,j,我想随机选择一个非零元素,如果它存在,并计算所有这些选定元素的平均值。我需要禁用渴望执行,所以我不需要遍历任何内容 现在,我的代码如下所示: def random_negative_

我试图用随机负三重态选择实现三重态丢失。 现在我有一个形状张量(batch_size,batch_size,batch_size),元素(I,j,k)等于
dist(i,j)-dist(i,k)+margin
(i是锚,j是正对,k是负对)

我将所有无效元素归零,并取
tf.最大值(张量,0.)

现在,对于每一对i,j,我想随机选择一个非零元素,如果它存在,并计算所有这些选定元素的平均值。我需要禁用渴望执行,所以我不需要遍历任何内容

现在,我的代码如下所示:

def random_negative_triplet_loss(labels, embeddings):

    margin = 1.
    # Get the pairwise distance matrix
    pairwise_dist = _pairwise_distances(embeddings)

    # shape (batch_size, batch_size, 1)
    anchor_positive_dist = tf.expand_dims(pairwise_dist, 2)
    assert anchor_positive_dist.shape[2] == 1, "{}".format(anchor_positive_dist.shape)
    # shape (batch_size, 1, batch_size)
    anchor_negative_dist = tf.expand_dims(pairwise_dist, 1)
    assert anchor_negative_dist.shape[1] == 1, "{}".format(anchor_negative_dist.shape)

    # Compute a 3D tensor of size (batch_size, batch_size, batch_size)
    # triplet_loss[i, j, k] will contain the triplet loss of anchor=i, positive=j, negative=k
    # Uses broadcasting where the 1st argument has shape (batch_size, batch_size, 1)
    # and the 2nd (batch_size, 1, batch_size)
    triplet_loss = anchor_positive_dist - anchor_negative_dist + margin
    # Put to zero the invalid triplets
    # (where label(a) != label(p) or label(n) == label(a) or a == p)
    mask = _get_triplet_mask(labels)

    mask = tf.to_float(mask)
    triplet_loss = tf.multiply(mask, triplet_loss)
    # Remove negative losses (i.e. the easy triplets)
    triplet_loss = tf.maximum(triplet_loss, 0.0)
    num_classes = 5
    the_loss = 0
    the_count = 0
    num_valid = tf.reduce_sum(mask, axis=2)
    valid_count = tf.reduce_sum(tf.to_int32(tf.greater(num_valid, 1e-16)))
    sampler = tf.distributions.Uniform(0., tf.to_float(50) - 1e-3)

我假设使用tf.distributions.Uniform可以实现随机性,但由于每对I,j具有不同数量的有效索引k,我不知道如何应用它。

请显示一些代码。请显示一些代码。