def test_local_pairwise_distances_two_dimensional(self): a = np.array([[[1.0, -5.0], [7.0, 2.0]], [[1.0, 3.0], [3.0, 4.0]]]) b = np.array([[[2.0, 1.0], [0.0, -9.0]], [[4.0, 3.0], [3.0, 1.0]]]) g = tf.Graph() with g.as_default(): with self.test_session(graph=g) as sess: a_tf = tf.constant(a, dtype=tf.float32) b_tf = tf.constant(b, dtype=tf.float32) d = embedding_utils.local_pairwise_distances(a_tf, b_tf, max_distance=1) d_val = sess.run(d) for y in range(2): for x in range(2): for dy in range(-1, 2): for dx in range(-1, 2): a_slice = a[y, x, :] if y + dy < 0 or y + dy > 1 or x + dx < 0 or x + dx > 1: expected = np.float('inf') else: b_slice = b[y + dy, x + dx, :] expected = ((a_slice - b_slice)**2).sum() dy0 = dy + 1 dx0 = dx + 1 self.assertAlmostEqual( d_val[y, x, 3 * dy0 + dx0], expected)
def test_local_pairwise_distances_shape(self): a = np.zeros((4, 5, 2)) b = np.zeros((4, 5, 2)) g = tf.Graph() with g.as_default(): with self.test_session(graph=g) as sess: a_tf = tf.constant(a, dtype=tf.float32) b_tf = tf.constant(b, dtype=tf.float32) d = embedding_utils.local_pairwise_distances(a_tf, b_tf, max_distance=4) d_val = sess.run(d) self.assertAllEqual(d_val.shape, (4, 5, 81))
def test_local_pairwise_distances_two_dimensional(self): a = np.array([[[1.0, -5.0], [7.0, 2.0]], [[1.0, 3.0], [3.0, 4.0]]]) b = np.array([[[2.0, 1.0], [0.0, -9.0]], [[4.0, 3.0], [3.0, 1.0]]]) g = tf.Graph() with g.as_default(): with self.test_session(graph=g) as sess: a_tf = tf.constant(a, dtype=tf.float32) b_tf = tf.constant(b, dtype=tf.float32) d = embedding_utils.local_pairwise_distances(a_tf, b_tf, max_distance=1) d_val = sess.run(d) for y in range(2): for x in range(2): for dy in range(-1, 2): for dx in range(-1, 2): a_slice = a[y, x, :] if y + dy < 0 or y + dy > 1 or x + dx < 0 or x + dx > 1: expected = np.float('inf') else: b_slice = b[y + dy, x + dx, :] expected = ((a_slice - b_slice) ** 2).sum() dy0 = dy + 1 dx0 = dx + 1 self.assertAlmostEqual(d_val[y, x, 3 * dy0 + dx0], expected)