def testNonRaggedSparseTensor(self):
        # "index_suffix" means the value of the innermost dimension of the index
        # (i.e., indices[i][-1]).
        # See comments in _assert_sparse_indices_are_ragged_right() for more
        # details/background.

        # index_suffix of first index is not zero.
        st1 = sparse_tensor.SparseTensor(indices=[[0, 1], [0, 2], [2, 0]],
                                         values=[1, 2, 3],
                                         dense_shape=[3, 3])
        with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                     r'.*SparseTensor is not right-ragged'):
            self.evaluate(RaggedTensor.from_sparse(st1))
        # index_suffix of an index that starts a new row is not zero.
        st2 = sparse_tensor.SparseTensor(indices=[[0, 0], [0, 1], [2, 1]],
                                         values=[1, 2, 3],
                                         dense_shape=[3, 3])
        with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                     r'.*SparseTensor is not right-ragged'):
            self.evaluate(RaggedTensor.from_sparse(st2))
        # index_suffix of an index that continues a row skips a cell.
        st3 = sparse_tensor.SparseTensor(indices=[[0, 1], [0, 1], [0, 3]],
                                         values=[1, 2, 3],
                                         dense_shape=[3, 3])
        with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                     r'.*SparseTensor is not right-ragged'):
            self.evaluate(RaggedTensor.from_sparse(st3))
  def testGoodPartialSparseTensorRank(self):
    if not context.executing_eagerly():
      st1 = sparse_tensor.SparseTensor(
          indices=[[0, 0]],
          values=[0],
          dense_shape=array_ops.placeholder(dtypes.int64))
      st2 = sparse_tensor.SparseTensor(
          indices=array_ops.placeholder(dtypes.int64),
          values=[0],
          dense_shape=[4, 3])

      # Shouldn't throw ValueError
      RaggedTensor.from_sparse(st1)
      RaggedTensor.from_sparse(st2)
    def testGoodPartialSparseTensorRank(self):
        if not context.executing_eagerly():
            st1 = sparse_tensor.SparseTensor(indices=[[0, 0]],
                                             values=[0],
                                             dense_shape=array_ops.placeholder(
                                                 dtypes.int64))
            st2 = sparse_tensor.SparseTensor(indices=array_ops.placeholder(
                dtypes.int64),
                                             values=[0],
                                             dense_shape=[4, 3])

            # Shouldn't throw ValueError
            RaggedTensor.from_sparse(st1)
            RaggedTensor.from_sparse(st2)
  def testEmpty(self):
    st = sparse_tensor.SparseTensor(
        indices=array_ops.zeros([0, 2], dtype=dtypes.int64),
        values=[],
        dense_shape=[4, 3])
    rt = RaggedTensor.from_sparse(st)

    self.assertRaggedEqual(rt, [[], [], [], []])
  def testDocStringExample(self):
    st = sparse_tensor.SparseTensor(
        indices=[[0, 0], [0, 1], [0, 2], [1, 0], [3, 0]],
        values=[1, 2, 3, 4, 5],
        dense_shape=[4, 3])
    rt = RaggedTensor.from_sparse(st)

    self.assertRaggedEqual(rt, [[1, 2, 3], [4], [], [5]])
    def testEmpty(self):
        st = sparse_tensor.SparseTensor(indices=array_ops.zeros(
            [0, 2], dtype=dtypes.int64),
                                        values=[],
                                        dense_shape=[4, 3])
        rt = RaggedTensor.from_sparse(st)

        self.assertRaggedEqual(rt, [[], [], [], []])
    def testDocStringExample(self):
        st = sparse_tensor.SparseTensor(indices=[[0, 0], [0, 1], [0, 2],
                                                 [1, 0], [3, 0]],
                                        values=[1, 2, 3, 4, 5],
                                        dense_shape=[4, 3])
        rt = RaggedTensor.from_sparse(st)

        self.assertRaggedEqual(rt, [[1, 2, 3], [4], [], [5]])
  def testNonRaggedSparseTensor(self):
    # "index_suffix" means the value of the innermost dimension of the index
    # (i.e., indices[i][-1]).
    # See comments in _assert_sparse_indices_are_ragged_right() for more
    # details/background.

    # index_suffix of first index is not zero.
    st1 = sparse_tensor.SparseTensor(
        indices=[[0, 1], [0, 2], [2, 0]], values=[1, 2, 3], dense_shape=[3, 3])
    with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                 r'.*SparseTensor is not right-ragged'):
      self.evaluate(RaggedTensor.from_sparse(st1))
    # index_suffix of an index that starts a new row is not zero.
    st2 = sparse_tensor.SparseTensor(
        indices=[[0, 0], [0, 1], [2, 1]], values=[1, 2, 3], dense_shape=[3, 3])
    with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                 r'.*SparseTensor is not right-ragged'):
      self.evaluate(RaggedTensor.from_sparse(st2))
    # index_suffix of an index that continues a row skips a cell.
    st3 = sparse_tensor.SparseTensor(
        indices=[[0, 1], [0, 1], [0, 3]], values=[1, 2, 3], dense_shape=[3, 3])
    with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                 r'.*SparseTensor is not right-ragged'):
      self.evaluate(RaggedTensor.from_sparse(st3))