def test_output_values_with_sampled_candidates(self): """Verifies the values for given sampled_candidates.""" with self.test_session(): sp_values = sparse_tensor_lib.SparseTensor( values=["a", "a", "b", "c", "d", "e", "f"], indices=[[1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5]], dense_shape=[3, 6]) params = constant_op.constant([.1, .2, .3]) sampled_candidates = [[1, 0], [2, 1], [3, 2]] sampled_result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, sampled_candidates=constant_op.constant(sampled_candidates), hash_key=self._hash_key) full_result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, dimension=4, hash_key=self._hash_key) sampled_result_val = sampled_result.eval() full_result_val = full_result.eval() self.assertEqual(sampled_result_val.shape, (3, 2)) for i in range(len(sampled_candidates)): self.assertAllClose(sampled_result_val[i], full_result_val[i, sampled_candidates[i]])
def test_distributive_property(self): """Verifies the distributive property of matrix multiplication.""" with self.test_session(): params = constant_op.constant([.1, .2, .3]) sp_values_a = sparse_tensor_lib.SparseTensor(values=["a"], indices=[[0, 0]], dense_shape=[3, 1]) sp_values_b = sparse_tensor_lib.SparseTensor(values=["b"], indices=[[2, 0]], dense_shape=[3, 1]) sp_values_c = sparse_tensor_lib.SparseTensor(values=["c"], indices=[[2, 0]], dense_shape=[3, 1]) sp_values = sparse_tensor_lib.SparseTensor(values=["a", "b", "c"], indices=[[0, 0], [2, 0], [2, 1]], dense_shape=[3, 2]) result_a = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values_a, dimension=4, hash_key=self._hash_key) result_b = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values_b, dimension=4, hash_key=self._hash_key) result_c = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values_c, dimension=4, hash_key=self._hash_key) result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, dimension=4, hash_key=self._hash_key) result_abc = math_ops.add_n([result_a, result_b, result_c]) self.assertAllClose(result.eval(), result_abc.eval())
def test_distributive_property(self): """Verifies the distributive property of matrix multiplication.""" with self.cached_session(): params = constant_op.constant([.1, .2, .3]) sp_values_a = sparse_tensor_lib.SparseTensor( values=["a"], indices=[[0, 0]], dense_shape=[3, 1]) sp_values_b = sparse_tensor_lib.SparseTensor( values=["b"], indices=[[2, 0]], dense_shape=[3, 1]) sp_values_c = sparse_tensor_lib.SparseTensor( values=["c"], indices=[[2, 0]], dense_shape=[3, 1]) sp_values = sparse_tensor_lib.SparseTensor( values=["a", "b", "c"], indices=[[0, 0], [2, 0], [2, 1]], dense_shape=[3, 2]) result_a = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values_a, dimension=4, hash_key=self._hash_key) result_b = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values_b, dimension=4, hash_key=self._hash_key) result_c = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values_c, dimension=4, hash_key=self._hash_key) result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, dimension=4, hash_key=self._hash_key) result_abc = math_ops.add_n([result_a, result_b, result_c]) self.assertAllClose(result.eval(), result_abc.eval())
def test_output_values(self): """Verifies the values in a trivial case.""" with self.cached_session(): sp_values = sparse_tensor_lib.SparseTensor( values=["a"], indices=[[1, 0]], dense_shape=[3, 1]) params = constant_op.constant([.1, .2, .3]) result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, dimension=5, hash_key=self._hash_key) self.assertAllClose(result.eval(), [[0., 0., 0., 0., 0.], [.3, .2, .2, .3, .1], [0., 0., 0., 0., 0.]])
def test_output_shape(self): """Verifies the shape of the output tensor.""" with self.cached_session(): sp_values = sparse_tensor_lib.SparseTensor( values=["a", "a", "b", "c", "d", "e", "f"], indices=[[1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5]], dense_shape=[3, 6]) params = constant_op.constant([.1, .2, .3]) result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, dimension=4, hash_key=self._hash_key) self.assertEqual(result.eval().shape, (3, 4))
def test_output_values(self): """Verifies the values in a trivial case.""" with self.test_session(): sp_values = sparse_tensor_lib.SparseTensor( values=["a"], indices=[[1, 0]], dense_shape=[3, 1]) params = constant_op.constant([.1, .2, .3]) result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, dimension=5, hash_key=self._hash_key) self.assertAllClose(result.eval(), [[0., 0., 0., 0., 0.], [.3, .2, .2, .3, .1], [0., 0., 0., 0., 0.]])
def test_output_shape(self): """Verifies the shape of the output tensor.""" with self.test_session(): sp_values = sparse_tensor_lib.SparseTensor( values=["a", "a", "b", "c", "d", "e", "f"], indices=[[1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5]], dense_shape=[3, 6]) params = constant_op.constant([.1, .2, .3]) result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, dimension=4, hash_key=self._hash_key) self.assertEqual(result.eval().shape, (3, 4))
def test_output_values_with_sampled_candidates(self): """Verifies the values for given sampled_candidates.""" with self.test_session(): sp_values = tf.SparseTensor( values=["a", "a", "b", "c", "d", "e", "f"], indices=[[1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5]], dense_shape=[3, 6]) params = tf.constant([.1, .2, .3]) sampled_candidates = [[1, 0], [2, 1], [3, 2]] sampled_result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, sampled_candidates=tf.constant(sampled_candidates), hash_key=self._hash_key) full_result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, dimension=4, hash_key=self._hash_key) sampled_result_val = sampled_result.eval() full_result_val = full_result.eval() self.assertEqual(sampled_result_val.shape, (3, 2)) for i in range(len(sampled_candidates)): self.assertAllClose(sampled_result_val[i], full_result_val[i, sampled_candidates[i]])
def test_output_values_with_sign_hash(self): """Verifies the values in a trivial case with hash_signs=True.""" with self.test_session(): sp_values = tf.SparseTensor( values=["a"], indices=[[1, 0]], dense_shape=[3, 1]) params = tf.constant([.1, .1, .1]) result = embedding_ops._sampled_scattered_embedding_lookup_sparse( params, sp_values, dimension=4, with_sign_hash=True, hash_key=self._hash_key) self.assertAllClose(result.eval(), [[0., 0., 0., 0.], [-.1, -.1, -.1, .1], [0., 0., 0., 0.]])