def _build_multilabel_adjacency(sparse_labels):
    """Builds multilabel adjacency matrix.

  As of March 14th, 2017, there's no op for the dot product between
  two sparse tensors in TF. However, there is `sparse_minimum` op which is
  equivalent to an AND op between two sparse boolean tensors.
  This computes the dot product between two sparse boolean inputs.

  Args:
    sparse_labels: List of 1-D boolean sparse tensors.

  Returns:
    adjacency_matrix: 2-D dense `Tensor`.
  """
    num_pairs = len(sparse_labels)
    adjacency_matrix = array_ops.zeros([num_pairs, num_pairs])
    for i in range(num_pairs):
        for j in range(num_pairs):
            sparse_dot_product = math_ops.to_float(
                sparse_ops.sparse_reduce_sum(
                    sparse_ops.sparse_minimum(sparse_labels[i],
                                              sparse_labels[j])))
            sparse_dot_product = array_ops.expand_dims(sparse_dot_product, 0)
            sparse_dot_product = array_ops.expand_dims(sparse_dot_product, 1)
            one_hot_matrix = array_ops.pad(
                sparse_dot_product,
                [[i, num_pairs - i - 1], [j, num_pairs - j - 1]], 'CONSTANT')
            adjacency_matrix += one_hot_matrix

    return adjacency_matrix
    def testRandom(self):
        np.random.seed(1618)
        shapes = [(13, ), (6, 8), (1, 7, 1)]
        for shape in shapes:
            for dtype in [
                    np.int32, np.int64, np.float16, np.float32, np.float64
            ]:
                a_np = np.random.randn(*shape).astype(dtype)
                b_np = np.random.randn(*shape).astype(dtype)
                sp_a, unused_a_nnz = _sparsify(a_np, thresh=-.5)
                sp_b, unused_b_nnz = _sparsify(b_np, thresh=-.5)

                with self.cached_session(use_gpu=False):
                    maximum_tf = sparse_ops.sparse_maximum(sp_a, sp_b)
                    maximum_tf_densified = sparse_ops.sparse_tensor_to_dense(
                        maximum_tf).eval()
                    minimum_tf = sparse_ops.sparse_minimum(sp_a, sp_b)
                    minimum_tf_densified = sparse_ops.sparse_tensor_to_dense(
                        minimum_tf).eval()

                    a_densified = sparse_ops.sparse_tensor_to_dense(
                        sp_a).eval()
                    b_densified = sparse_ops.sparse_tensor_to_dense(
                        sp_b).eval()

                self.assertAllEqual(np.maximum(a_densified, b_densified),
                                    maximum_tf_densified)
                self.assertAllEqual(np.minimum(a_densified, b_densified),
                                    minimum_tf_densified)
  def testRandom(self):
    np.random.seed(1618)
    shapes = [(13,), (6, 8), (1, 7, 1)]
    for shape in shapes:
      for dtype in [np.int32, np.int64, np.float16, np.float32, np.float64]:
        a_np = np.random.randn(*shape).astype(dtype)
        b_np = np.random.randn(*shape).astype(dtype)
        sp_a, unused_a_nnz = _sparsify(a_np, thresh=-.5)
        sp_b, unused_b_nnz = _sparsify(b_np, thresh=-.5)

        with self.test_session(use_gpu=False):
          maximum_tf = sparse_ops.sparse_maximum(sp_a, sp_b)
          maximum_tf_densified = sparse_ops.sparse_tensor_to_dense(
              maximum_tf).eval()
          minimum_tf = sparse_ops.sparse_minimum(sp_a, sp_b)
          minimum_tf_densified = sparse_ops.sparse_tensor_to_dense(
              minimum_tf).eval()

          a_densified = sparse_ops.sparse_tensor_to_dense(sp_a).eval()
          b_densified = sparse_ops.sparse_tensor_to_dense(sp_b).eval()

        self.assertAllEqual(
            np.maximum(a_densified, b_densified), maximum_tf_densified)
        self.assertAllEqual(
            np.minimum(a_densified, b_densified), minimum_tf_densified)
def _build_multilabel_adjacency(sparse_labels):
  """Builds multilabel adjacency matrix.

  As of March 14th, 2017, there's no op for the dot product between
  two sparse tensors in TF. However, there is `sparse_minimum` op which is
  equivalent to an AND op between two sparse boolean tensors.
  This computes the dot product between two sparse boolean inputs.

  Args:
    sparse_labels: List of 1-D boolean sparse tensors.

  Returns:
    adjacency_matrix: 2-D dense `Tensor`.
  """
  num_pairs = len(sparse_labels)
  adjacency_matrix = array_ops.zeros([num_pairs, num_pairs])
  for i in range(num_pairs):
    for j in range(num_pairs):
      sparse_dot_product = math_ops.to_float(
          sparse_ops.sparse_reduce_sum(sparse_ops.sparse_minimum(
              sparse_labels[i], sparse_labels[j])))
      sparse_dot_product = array_ops.expand_dims(sparse_dot_product, 0)
      sparse_dot_product = array_ops.expand_dims(sparse_dot_product, 1)
      one_hot_matrix = array_ops.pad(sparse_dot_product,
                                     [[i, num_pairs-i-1],
                                      [j, num_pairs-j-1]], 'CONSTANT')
      adjacency_matrix += one_hot_matrix

  return adjacency_matrix
    def testBasic(self):
        with test_util.force_cpu():
            # 1-D, values at index 0.
            sp_zero = sparse_tensor.SparseTensor([[0]], [0], [7])
            sp_one = sparse_tensor.SparseTensor([[0]], [1], [7])
            max_tf = sparse_ops.sparse_maximum(sp_zero, sp_one)
            min_tf = sparse_ops.sparse_minimum(sp_zero, sp_one)
            self._assertSparseTensorValueEqual(sp_one, max_tf)
            self._assertSparseTensorValueEqual(sp_zero, min_tf)

            # Values at different indices.
            sp_zero = sparse_tensor.SparseTensor([[0]], [0], [7])
            sp_zero_2 = sparse_tensor.SparseTensor([[1]], [0], [7])
            expected = sparse_tensor.SparseTensor([[0], [1]], [0, 0], [7])
            max_tf = sparse_ops.sparse_maximum(sp_zero, sp_zero_2)
            min_tf = sparse_ops.sparse_minimum(sp_zero, sp_zero_2)
            self._assertSparseTensorValueEqual(expected, max_tf)
            self._assertSparseTensorValueEqual(expected, min_tf)
    def testBasic(self):
        with self.test_session(use_gpu=False):
            # 1-D, values at index 0.
            sp_zero = sparse_tensor.SparseTensor([[0]], [0], [7])
            sp_one = sparse_tensor.SparseTensor([[0]], [1], [7])
            max_tf = sparse_ops.sparse_maximum(sp_zero, sp_one).eval()
            min_tf = sparse_ops.sparse_minimum(sp_zero, sp_one).eval()
            self._assertSparseTensorValueEqual(sp_one.eval(), max_tf)
            self._assertSparseTensorValueEqual(sp_zero.eval(), min_tf)

            # Values at different indices.
            sp_zero = sparse_tensor.SparseTensor([[0]], [0], [7])
            sp_zero_2 = sparse_tensor.SparseTensor([[1]], [0], [7])
            expected = sparse_tensor.SparseTensor([[0], [1]], [0, 0], [7])
            max_tf = sparse_ops.sparse_maximum(sp_zero, sp_zero_2).eval()
            min_tf = sparse_ops.sparse_minimum(sp_zero, sp_zero_2).eval()
            self._assertSparseTensorValueEqual(expected.eval(), max_tf)
            self._assertSparseTensorValueEqual(expected.eval(), min_tf)
  def testBasic(self):
    with self.test_session(use_gpu=False):
      # 1-D, values at index 0.
      sp_zero = sparse_tensor.SparseTensor([[0]], [0], [7])
      sp_one = sparse_tensor.SparseTensor([[0]], [1], [7])
      max_tf = sparse_ops.sparse_maximum(sp_zero, sp_one).eval()
      min_tf = sparse_ops.sparse_minimum(sp_zero, sp_one).eval()
      self._assertSparseTensorValueEqual(sp_one.eval(), max_tf)
      self._assertSparseTensorValueEqual(sp_zero.eval(), min_tf)

      # Values at different indices.
      sp_zero = sparse_tensor.SparseTensor([[0]], [0], [7])
      sp_zero_2 = sparse_tensor.SparseTensor([[1]], [0], [7])
      expected = sparse_tensor.SparseTensor([[0], [1]], [0, 0], [7])
      max_tf = sparse_ops.sparse_maximum(sp_zero, sp_zero_2).eval()
      min_tf = sparse_ops.sparse_minimum(sp_zero, sp_zero_2).eval()
      self._assertSparseTensorValueEqual(expected.eval(), max_tf)
      self._assertSparseTensorValueEqual(expected.eval(), min_tf)
  def testBasic(self):
    with test_util.force_cpu():
      # 1-D, values at index 0.
      sp_zero = sparse_tensor.SparseTensor([[0]], [0], [7])
      sp_one = sparse_tensor.SparseTensor([[0]], [1], [7])
      max_tf = sparse_ops.sparse_maximum(sp_zero, sp_one)
      min_tf = sparse_ops.sparse_minimum(sp_zero, sp_one)
      self._assertSparseTensorValueEqual(sp_one, max_tf)
      self._assertSparseTensorValueEqual(sp_zero, min_tf)

      # Values at different indices.
      sp_zero = sparse_tensor.SparseTensor([[0]], [0], [7])
      sp_zero_2 = sparse_tensor.SparseTensor([[1]], [0], [7])
      expected = sparse_tensor.SparseTensor([[0], [1]], [0, 0], [7])
      max_tf = sparse_ops.sparse_maximum(sp_zero, sp_zero_2)
      min_tf = sparse_ops.sparse_minimum(sp_zero, sp_zero_2)
      self._assertSparseTensorValueEqual(expected, max_tf)
      self._assertSparseTensorValueEqual(expected, min_tf)