Example #1
0
def test_gradients_pairwise_distances():
    """Check that the gradients of the pairwise distances are not nan.

    This happens if one of the distance is exactly 0.0 (or negative), as the gradient of the
    square root will be infinite.
    """

    num_data = 64
    feat_dim = 6

    embeddings = np.random.randn(num_data, feat_dim).astype(np.float32)
    # Make the first two embeddings equal to get d(0, 1) = 0.0
    embeddings[1] = embeddings[0]
    # Make the last 10 embeddings very close to each other
    embeddings[num_data -
               10:num_data] = 1.0 + 2e-7 * np.random.randn(10, feat_dim)
    embeddings = tf.constant(embeddings)

    with tf.Session() as sess:
        for squared in [True, False]:
            dists = _pairwise_distances(embeddings, squared=squared)
            grads = tf.gradients(dists, embeddings)

            g = sess.run(grads)
            assert not np.any(np.isnan(
                g)), "Gradient shouldn't be nan, squared={}".format(squared)
Example #2
0
def calculate_easier_accuracy(embeddings, labels):
    pairwise_dist = _pairwise_distances(embeddings)
    distance_vector = tf.squeeze(tf.slice(pairwise_dist, [1, 0], [-1, 1]))
    labels_vector = tf.slice(labels, [1], [-1])
    pos_index = tf.argmin(labels_vector)
    pos_distance = tf.slice(distance_vector, [pos_index], [1])
    num_successes = tf.count_nonzero(
        tf.less_equal(pos_distance, distance_vector)) - 1
    batch_size_minus_two = tf.squeeze(tf.slice(tf.shape(labels), [0], [1])) - 2
    batch_size_minus_two = tf.cast(batch_size_minus_two, dtype=tf.int64)
    return tf.divide(num_successes, batch_size_minus_two)
Example #3
0
def test_pairwise_distances():
    """Test the pairwise distances function."""
    num_data = 64
    feat_dim = 6

    embeddings = np.random.randn(num_data, feat_dim).astype(np.float32)
    embeddings[1] = embeddings[0]  # to get distance 0

    with tf.Session() as sess:
        for squared in [True, False]:
            res_np = pairwise_distance_np(embeddings, squared=squared)
            res_tf = sess.run(_pairwise_distances(embeddings, squared=squared))
            assert np.allclose(res_np, res_tf)
def test_pairwise_distances():
    """Test the pairwise distances function."""
    num_data = 64
    feat_dim = 6

    embeddings = np.random.randn(num_data, feat_dim)
    embeddings[1] = embeddings[0]  # to get distance 0

    for squared in [True, False]:
        res_np = pairwise_distance_np(embeddings, squared=squared)
        res_pt = _pairwise_distances(torch.as_tensor(embeddings),
                                     squared=squared)
        assert np.allclose(res_np, res_pt)
def test_pairwise_distances_are_positive():
    """Test that the pairwise distances are always positive. Use a tricky case
    where numerical errors are common.
    """
    num_data = 64
    feat_dim = 6

    # Create embeddings very close to each other in [1.0 - 2e-7, 1.0 + 2e-7]
    # This will encourage errors in the computation
    embeddings = 1.0 + 2e-7 * np.random.randn(num_data, feat_dim)
    embeddings[1] = embeddings[0]  # to get distance 0
    embeddings = torch.as_tensor(embeddings)

    for squared in [True, False]:
        res_pt = _pairwise_distances(embeddings, squared=squared)
        assert (res_pt >= 0.0).all()
Example #6
0
def test_pairwise_distances_are_positive():
    """Test that the pairwise distances are always positive.

    Use a tricky case where numerical errors are common.
    """
    num_data = 64
    feat_dim = 6

    # Create embeddings very close to each other in [1.0 - 2e-7, 1.0 + 2e-7]
    # This will encourage errors in the computation
    embeddings = 1.0 + 2e-7 * np.random.randn(num_data, feat_dim).astype(np.float32)
    embeddings[1] = embeddings[0]  # to get distance 0

    with tf.Session() as sess:
        for squared in [True, False]:
            res_tf = sess.run(_pairwise_distances(embeddings, squared=squared))
            assert np.all(res_tf >= 0.0)