Commit ec30c145 authored by Shakthi Visagan's avatar Shakthi Visagan
Browse files

more work

parent ffe97adf
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -429,8 +429,7 @@ def _cosine_dist(x, y):
  """
  x_norm = tf.nn.l2_normalize(x, axis=1)
  y_norm = tf.nn.l2_normalize(y, axis=1)
  # the cosine distance is 1 - the cosine similarity
  return 1. - tf.reduce_sum(tf.multiply(x_norm, y_norm), axis=1)
  return 1. - backend.dot(x_norm, tf.transpose(y_norm))


class AttnLSTMEmbedding(tf.keras.layers.Layer):
+7 −5
Original line number Diff line number Diff line
@@ -9,11 +9,13 @@ class TestLayers(test_util.TensorFlowTestCase):

  def test_cosine_dist(self):
    """Test invoking _cosine_dist."""
    x = np.ones((5, 4)).astype(np.float32)
    y_same = np.ones((5, 4)).astype(np.float32)
    y_far = np.zeros((5, 4)).astype(np.float32)
    assert sum(layers._cosine_dist(x, y_same)) == 0
    assert sum(layers._cosine_dist(x, y_far)) == 5
    x = tf.ones((5,4), dtype=tf.dtypes.float32, name=None)
    y_same = tf.ones((5,4), dtype=tf.dtypes.float32, name=None)
    y_far = -1. * tf.ones((5,4), dtype=tf.dtypes.float32, name=None) 
    close_cosine_dist = layers._cosine_dist(x, y_same)
    far_close_dist = layers._cosine_dist(x, y_far)
    assert tf.reduce_sum(close_cosine_dist) == 0.
    assert tf.reduce_sum(far_close_dist) == 2.*5.*5.

  def test_highway(self):
    """Test invoking Highway."""