Exemple #1
0
def test_unweighted():
    nl_obj = npairs.NpairsLoss()
    # batch size = 4, hidden size = 2
    y_true = tf.constant([0, 1, 2, 3], dtype=tf.int64)
    # features of anchors
    f = tf.constant(
        [[1.0, 1.0], [1.0, -1.0], [-1.0, 1.0], [-1.0, -1.0]], dtype=tf.float32
    )
    # features of positive samples
    fp = tf.constant(
        [[1.0, 1.0], [1.0, -1.0], [-1.0, 1.0], [-1.0, -1.0]], dtype=tf.float32
    )
    # similarity matrix
    y_pred = tf.matmul(f, fp, transpose_a=False, transpose_b=True)
    loss = nl_obj(y_true, y_pred)

    # Loss = 1/4 * \sum_i log(1 + \sum_{j != i} exp(f_i*fp_j^T-f_i*f_i^T))
    # Compute loss for i = 0, 1, 2, 3 without multiplier 1/4
    # i = 0 => log(1 + sum([exp(-2), exp(-2), exp(-4)])) = 0.253846
    # i = 1 => log(1 + sum([exp(-2), exp(-4), exp(-2)])) = 0.253846
    # i = 2 => log(1 + sum([exp(-2), exp(-4), exp(-2)])) = 0.253846
    # i = 3 => log(1 + sum([exp(-4), exp(-2), exp(-2)])) = 0.253846
    # Loss = (0.253856 + 0.253856 + 0.253856 + 0.253856) / 4 = 0.253856

    np.testing.assert_allclose(loss, 0.253856, rtol=1e-06, atol=1e-06)
Exemple #2
0
 def test_config(self):
     nl_obj = npairs.NpairsLoss(name="nl")
     self.assertEqual(nl_obj.name, "nl")
     self.assertEqual(nl_obj.reduction, tf.keras.losses.Reduction.NONE)
Exemple #3
0
def test_config():
    nl_obj = npairs.NpairsLoss(name="nl")
    assert nl_obj.name == "nl"
    assert nl_obj.reduction == tf.keras.losses.Reduction.NONE