コード例 #1
0
    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])
        # 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])
        # 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])
        rt1 = ragged.from_sparse(st1)
        rt2 = ragged.from_sparse(st2)
        rt3 = ragged.from_sparse(st3)
        with self.test_session():
            self.assertRaisesRegexp(errors.InvalidArgumentError,
                                    r'.*SparseTensor is not right-ragged',
                                    rt1.eval)
            self.assertRaisesRegexp(errors.InvalidArgumentError,
                                    r'.*SparseTensor is not right-ragged',
                                    rt2.eval)
            self.assertRaisesRegexp(errors.InvalidArgumentError,
                                    r'.*SparseTensor is not right-ragged',
                                    rt3.eval)
コード例 #2
0
  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])
    # 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])
    # 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])
    rt1 = ragged.from_sparse(st1)
    rt2 = ragged.from_sparse(st2)
    rt3 = ragged.from_sparse(st3)
    with self.test_session():
      self.assertRaisesRegexp(errors.InvalidArgumentError,
                              r'.*SparseTensor is not right-ragged', rt1.eval)
      self.assertRaisesRegexp(errors.InvalidArgumentError,
                              r'.*SparseTensor is not right-ragged', rt2.eval)
      self.assertRaisesRegexp(errors.InvalidArgumentError,
                              r'.*SparseTensor is not right-ragged', rt3.eval)
コード例 #3
0
    def testGoodPartialSparseTensorRank(self):
        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
        ragged.from_sparse(st1)
        ragged.from_sparse(st2)
コード例 #4
0
  def testGoodPartialSparseTensorRank(self):
    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
    ragged.from_sparse(st1)
    ragged.from_sparse(st2)
コード例 #5
0
    def testEmpty(self):
        st = sparse_tensor.SparseTensor(indices=array_ops.zeros(
            [0, 2], dtype=dtypes.int64),
                                        values=[],
                                        dense_shape=[4, 3])
        rt = ragged.from_sparse(st)

        with self.test_session():
            self.assertEqual(rt.eval().tolist(), [[], [], [], []])
コード例 #6
0
    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 = ragged.from_sparse(st)

        with self.test_session():
            self.assertEqual(rt.eval().tolist(), [[1, 2, 3], [4], [], [5]])
コード例 #7
0
  def testEmpty(self):
    st = sparse_tensor.SparseTensor(
        indices=array_ops.zeros([0, 2], dtype=dtypes.int64),
        values=[],
        dense_shape=[4, 3])
    rt = ragged.from_sparse(st)

    with self.test_session():
      self.assertEqual(rt.eval().tolist(), [[], [], [], []])
コード例 #8
0
  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 = ragged.from_sparse(st)

    with self.test_session():
      self.assertEqual(rt.eval().tolist(), [[1, 2, 3], [4], [], [5]])