Ejemplo n.º 1
0
  def testArithmeticOperators(self):
    x = ragged.constant([[1.0, -2.0], [8.0]])
    y = ragged.constant([[4.0, 4.0], [2.0]])
    with self.test_session():
      self.assertEqual(abs(x).eval().tolist(), [[1.0, 2.0], [8.0]])

      self.assertEqual((-x).eval().tolist(), [[-1.0, 2.0], [-8.0]])

      self.assertEqual((x + y).eval().tolist(), [[5.0, 2.0], [10.0]])
      self.assertEqual((3.0 + y).eval().tolist(), [[7.0, 7.0], [5.0]])
      self.assertEqual((x + 3.0).eval().tolist(), [[4.0, 1.0], [11.0]])

      self.assertEqual((x - y).eval().tolist(), [[-3.0, -6.0], [6.0]])
      self.assertEqual((3.0 - y).eval().tolist(), [[-1.0, -1.0], [1.0]])
      self.assertEqual((x + 3.0).eval().tolist(), [[4.0, 1.0], [11.0]])

      self.assertEqual((x * y).eval().tolist(), [[4.0, -8.0], [16.0]])
      self.assertEqual((3.0 * y).eval().tolist(), [[12.0, 12.0], [6.0]])
      self.assertEqual((x * 3.0).eval().tolist(), [[3.0, -6.0], [24.0]])

      self.assertEqual((x / y).eval().tolist(), [[0.25, -0.5], [4.0]])
      self.assertEqual((y / x).eval().tolist(), [[4.0, -2.0], [0.25]])
      self.assertEqual((2.0 / y).eval().tolist(), [[0.5, 0.5], [1.0]])
      self.assertEqual((x / 2.0).eval().tolist(), [[0.5, -1.0], [4.0]])

      self.assertEqual((x // y).eval().tolist(), [[0.0, -1.0], [4.0]])
      self.assertEqual((y // x).eval().tolist(), [[4.0, -2.0], [0.0]])
      self.assertEqual((2.0 // y).eval().tolist(), [[0.0, 0.0], [1.0]])
      self.assertEqual((x // 2.0).eval().tolist(), [[0.0, -1.0], [4.0]])

      self.assertEqual((x % y).eval().tolist(), [[1.0, 2.0], [0.0]])
      self.assertEqual((y % x).eval().tolist(), [[0.0, -0.0], [2.0]])
      self.assertEqual((2.0 % y).eval().tolist(), [[2.0, 2.0], [0.0]])
      self.assertEqual((x % 2.0).eval().tolist(), [[1.0, 0.0], [0.0]])
 def testShapeMismatch(self):
   x = ragged.constant([[1, 2, 3], [4, 5]])
   y = ragged.constant([[1, 2, 3], [4, 5, 6]])
   with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                'Incompatible shapes'):
     with self.cached_session():
       ragged.add(x, y).eval()
 def testOpWithKeywordArgs(self):
   x = ragged.constant([[3, 1, 4], [], [1, 5]])
   y = ragged.constant([[1, 2, 3], [], [4, 5]])
   self.assertRaggedMapInnerValuesReturns(
       op=math_ops.multiply,
       kwargs=dict(x=x, y=y),
       expected=[[3, 2, 12], [], [4, 25]])
 def testOpWithRaggedRankThree(self):
   x = ragged.constant([[[3, 1, 4]], [], [[], [1, 5]]])
   y = ragged.constant([[[1, 2, 3]], [], [[], [4, 5]]])
   self.assertRaggedMapInnerValuesReturns(
       op=math_ops.multiply,
       args=(x, y),
       expected=[[[3, 2, 12]], [], [[], [4, 25]]])
 def testOpWithRaggedTensorListArg(self):
   x = ragged.constant([[1, 2, 3], [], [4, 5]])
   y = ragged.constant([[10, 20, 30], [], [40, 50]])
   self.assertRaggedMapInnerValuesReturns(
       op=math_ops.add_n,
       args=([x, y, x],),
       expected=[[12, 24, 36], [], [48, 60]])
Ejemplo n.º 6
0
 def testOrderingOperators(self):
   x = ragged.constant([[1, 5], [3]])
   y = ragged.constant([[4, 5], [1]])
   with self.test_session():
     self.assertEqual((x > y).eval().tolist(), [[False, False], [True]])
     self.assertEqual((x >= y).eval().tolist(), [[False, True], [True]])
     self.assertEqual((x < y).eval().tolist(), [[True, False], [False]])
     self.assertEqual((x <= y).eval().tolist(), [[True, True], [False]])
 def testOpWithThreeRaggedTensorArgs(self):
   condition = ragged.constant(
       [[True, True, False], [], [True, False]])  # pyformat: disable
   x = ragged.constant([['a', 'b', 'c'], [], ['d', 'e']])
   y = ragged.constant([['A', 'B', 'C'], [], ['D', 'E']])
   self.assertRaggedMapInnerValuesReturns(
       op=array_ops.where,
       args=(condition, x, y),
       expected=[[b'a', b'b', b'C'], [], [b'd', b'E']])
Ejemplo n.º 8
0
 def testRaggedParamsAndRaggedIndices(self):
   params = ragged.constant([['a', 'b'], ['c', 'd', 'e'], ['f'], [], ['g']])
   indices = ragged.constant([[2, 1], [1, 2, 0], [3]])
   with self.test_session():
     self.assertEqual(
         ragged.gather(params, indices).eval().tolist(),
         [[[b'f'], [b'c', b'd', b'e']],                # [[p[2], p[1]      ],
          [[b'c', b'd', b'e'], [b'f'], [b'a', b'b']],  #  [p[1], p[2], p[0]],
          [[]]]                                        #  [p[3]            ]]
     )  # pyformat: disable
Ejemplo n.º 9
0
 def testRaggedSegmentIds(self):
   rt = ragged.constant([
       [[111, 112, 113, 114], [121],],  # row 0
       [],                              # row 1
       [[], [321, 322], [331]],         # row 2
       [[411, 412]]                     # row 3
   ])  # pyformat: disable
   segment_ids = ragged.constant([[1, 2], [], [1, 1, 2], [2]])
   segmented = ragged.segment_sum(rt, segment_ids, 3)
   expected = [[],
               [111+321, 112+322, 113, 114],
               [121+331+411, 412]]  # pyformat: disable
   self.assertEqual(self.evaluate(segmented).tolist(), expected)
Ejemplo n.º 10
0
 def testOutOfBoundsError(self):
   tensor_params = ['a', 'b', 'c']
   tensor_indices = [0, 1, 2]
   ragged_params = ragged.constant([['a', 'b'], ['c']])
   ragged_indices = ragged.constant([[0, 3]])
   with self.test_session():
     self.assertRaisesRegexp(errors.InvalidArgumentError,
                             r'indices\[1\] = 3 is not in \[0, 3\)',
                             ragged.gather(tensor_params, ragged_indices).eval)
     self.assertRaisesRegexp(errors.InvalidArgumentError,
                             r'indices\[2\] = 2 is not in \[0, 2\)',
                             ragged.gather(ragged_params, tensor_indices).eval)
     self.assertRaisesRegexp(errors.InvalidArgumentError,
                             r'indices\[1\] = 3 is not in \[0, 2\)',
                             ragged.gather(ragged_params, ragged_indices).eval)
Ejemplo n.º 11
0
 def testDocStringExamples(self):
   params = constant_op.constant(['a', 'b', 'c', 'd', 'e'])
   indices = constant_op.constant([3, 1, 2, 1, 0])
   ragged_params = ragged.constant([['a', 'b', 'c'], ['d'], [], ['e']])
   ragged_indices = ragged.constant([[3, 1, 2], [1], [], [0]])
   with self.test_session():
     self.assertEqual(
         ragged.gather(params, ragged_indices).eval().tolist(),
         [[b'd', b'b', b'c'], [b'b'], [], [b'a']])
     self.assertEqual(
         ragged.gather(ragged_params, indices).eval().tolist(),
         [[b'e'], [b'd'], [], [b'd'], [b'a', b'b', b'c']])
     self.assertEqual(
         ragged.gather(ragged_params, ragged_indices).eval().tolist(),
         [[[b'e'], [b'd'], []], [[b'd']], [], [[b'a', b'b', b'c']]])
Ejemplo n.º 12
0
  def testGradient(self):
    # rt1.shape == rt2.shape == [2, (D2), (D3), 2].
    rt1 = ragged.constant([[[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0]]]],
                          ragged_rank=2)
    rt2 = ragged.constant([[[[9.0, 8.0], [7.0, 6.0]], [[5.0, 4.0]]]],
                          ragged_rank=2)
    rt = ragged.map_inner_values(math_ops.add, rt1, rt2 * 2.0)
    st = ragged.to_sparse(rt)

    g1, g2 = gradients_impl.gradients(st.values, [rt1.inner_values,
                                                  rt2.inner_values])
    print(g1, g2)
    with self.test_session():
      self.assertEqual(g1.eval().tolist(), [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]])
      self.assertEqual(g2.eval().tolist(), [[2.0, 2.0], [2.0, 2.0], [2.0, 2.0]])
Ejemplo n.º 13
0
 def test4DRaggedTensorWithTwoRaggedDimensions(self):
   rt = ragged.constant([[[[1, 2], [3, 4]], [[5, 6], [7, 8], [9, 10]]],
                         [[[11, 12]], [], [[13, 14]]], []],
                        ragged_rank=2)
   with self.test_session():
     st = ragged.to_sparse(rt).eval()
     self.assertAllEqual(
         st.indices,
         [
             [0, 0, 0, 0],  # index for value=1
             [0, 0, 0, 1],  # index for value=2
             [0, 0, 1, 0],  # index for value=3
             [0, 0, 1, 1],  # index for value=4
             [0, 1, 0, 0],  # index for value=5
             [0, 1, 0, 1],  # index for value=6
             [0, 1, 1, 0],  # index for value=7
             [0, 1, 1, 1],  # index for value=8
             [0, 1, 2, 0],  # index for value=9
             [0, 1, 2, 1],  # index for value=10
             [1, 0, 0, 0],  # index for value=11
             [1, 0, 0, 1],  # index for value=12
             [1, 2, 0, 0],  # index for value=13
             [1, 2, 0, 1],  # index for value=14
         ])
     self.assertAllEqual(st.values,
                         [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
     self.assertAllEqual(st.dense_shape, [3, 3, 3, 2])
  def testRaggedMapOnStructure_RaggedOutputs(self):
    batman = ragged.constant([[1, 2, 3], [4], [5, 6, 7]])
    # [[10, 20, 30], [40], [50, 60, 70]]
    robin = ragged.map_inner_values(mo.multiply, batman, 10)

    features = {'batman': batman, 'robin': robin}

    def _increment(f):
      return {
          'batman': ragged.add(f['batman'], 1),
          'robin': ragged.add(f['robin'], 1),
      }

    output = ragged.map_fn(
        fn=_increment,
        elems=features,
        infer_shape=False,
        dtype={
            'batman':
                ragged.RaggedTensorType(dtype=dtypes.int32, ragged_rank=1),
            'robin':
                ragged.RaggedTensorType(dtype=dtypes.int32, ragged_rank=1)
        },
    )

    with self.test_session():
      self.assertAllEqual(output['batman'].eval().tolist(),
                          [[2, 3, 4], [5], [6, 7, 8]])
      self.assertAllEqual(output['robin'].eval().tolist(),
                          [[11, 21, 31], [41], [51, 61, 71]])
 def testShapeMismatchError1(self):
   dt = constant_op.constant([1, 2, 3, 4, 5, 6])
   segment_ids = ragged.constant([[1, 2], []])
   self.assertRaisesRegexp(
       ValueError, 'segment_ids.shape must be a prefix of data.shape, '
       'but segment_ids is ragged and data is not.', ragged.segment_sum, dt,
       segment_ids, 3)
 def testUnknownRankError(self):
   x = ragged.constant([[1, 2], [3]])
   y = ragged.from_row_splits(
       array_ops.placeholder_with_default([1, 2, 3], shape=None), x.row_splits)
   with self.assertRaisesRegexp(
       ValueError, r'Unable to broadcast: unknown rank'):
     ragged.add(x, y)
Ejemplo n.º 17
0
 def testUnknownIndicesRankError(self):
   params = ragged.constant([], ragged_rank=1)
   indices = constant_op.constant([0], dtype=dtypes.int64)
   indices = array_ops.placeholder_with_default(indices, None)
   self.assertRaisesRegexp(ValueError,
                           r'indices\.shape\.ndims must be known statically',
                           ragged.gather, params, indices)
Ejemplo n.º 18
0
 def testTensorParamsAndRaggedIndices(self):
   params = ['a', 'b', 'c', 'd', 'e']
   indices = ragged.constant([[2, 1], [1, 2, 0], [3]])
   with self.test_session():
     self.assertEqual(
         ragged.gather(params, indices).eval().tolist(),
         [[b'c', b'b'], [b'b', b'c', b'a'], [b'd']])
Ejemplo n.º 19
0
 def testRaggedParamsAndTensorIndices(self):
   params = ragged.constant([['a', 'b'], ['c', 'd', 'e'], ['f'], [], ['g']])
   indices = [2, 0, 2, 1]
   with self.test_session():
     self.assertEqual(
         ragged.gather(params, indices).eval().tolist(),
         [[b'f'], [b'a', b'b'], [b'f'], [b'c', b'd', b'e']])
Ejemplo n.º 20
0
 def test2DRaggedTensorWithOneRaggedDimension(self):
     rt = ragged.constant([['a', 'b'], ['c', 'd', 'e'], ['f'], [], ['g']])
     st = self.evaluate(rt.to_sparse())
     self.assertAllEqual(
         st.indices,
         [[0, 0], [0, 1], [1, 0], [1, 1], [1, 2], [2, 0], [4, 0]])
     self.assertAllEqual(st.values, b'a b c d e f g'.split())
     self.assertAllEqual(st.dense_shape, [5, 3])
    def testGradient(self):
        # rt1.shape == rt2.shape == [2, (D2), (D3), 2].
        rt1 = ragged.constant([[[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0]]]],
                              ragged_rank=2)
        rt2 = ragged.constant([[[[9.0, 8.0], [7.0, 6.0]], [[5.0, 4.0]]]],
                              ragged_rank=2)
        rt = ragged.map_inner_values(math_ops.add, rt1, rt2 * 2.0)
        st = ragged.to_sparse(rt)

        g1, g2 = gradients_impl.gradients(st.values,
                                          [rt1.inner_values, rt2.inner_values])
        print(g1, g2)
        with self.test_session():
            self.assertEqual(g1.eval().tolist(),
                             [[1.0, 1.0], [1.0, 1.0], [1.0, 1.0]])
            self.assertEqual(g2.eval().tolist(),
                             [[2.0, 2.0], [2.0, 2.0], [2.0, 2.0]])
Ejemplo n.º 22
0
  def testLogicalOperators(self):
    a = ragged.constant([[True, True], [False]])
    b = ragged.constant([[True, False], [False]])
    with self.test_session():
      self.assertEqual((~a).eval().tolist(), [[False, False], [True]])

      self.assertEqual((a & b).eval().tolist(), [[True, False], [False]])
      self.assertEqual((a & True).eval().tolist(), [[True, True], [False]])
      self.assertEqual((True & b).eval().tolist(), [[True, False], [False]])

      self.assertEqual((a | b).eval().tolist(), [[True, True], [False]])
      self.assertEqual((a | False).eval().tolist(), [[True, True], [False]])
      self.assertEqual((False | b).eval().tolist(), [[True, False], [False]])

      self.assertEqual((a ^ b).eval().tolist(), [[False, True], [False]])
      self.assertEqual((a ^ True).eval().tolist(), [[False, False], [True]])
      self.assertEqual((True ^ b).eval().tolist(), [[False, True], [True]])
 def _rt_inputs_to_tensors(self, rt_inputs, ragged_ranks=None):
   if ragged_ranks is None:
     ragged_ranks = [None] * len(rt_inputs)
   return [
       ragged.constant(rt_input, ragged_rank=rrank)
       if rrank != 0 else constant_op.constant(rt_input)
       for (rt_input, rrank) in zip(rt_inputs, ragged_ranks)
   ]
Ejemplo n.º 24
0
 def testRaggedParamsAndTensorIndices(self):
     params = ragged.constant([['a', 'b'], ['c', 'd', 'e'], ['f'], [],
                               ['g']])
     indices = [2, 0, 2, 1]
     with self.test_session():
         self.assertEqual(
             ragged.gather(params, indices).eval().tolist(),
             [[b'f'], [b'a', b'b'], [b'f'], [b'c', b'd', b'e']])
 def testUnknownRankError(self):
   x = ragged.constant([[1, 2], [3]])
   y = ragged.from_row_splits(
       array_ops.placeholder_with_default([1, 2, 3], shape=None), x.row_splits)
   with self.assertRaisesRegexp(
       ValueError, r'Ragged elementwise ops require that rank \(number '
       r'of dimensions\) be statically known.'):
     ragged.add(x, y)
Ejemplo n.º 26
0
 def test2DRaggedTensorWithOneRaggedDimension(self):
   rt = ragged.constant([['a', 'b'], ['c', 'd', 'e'], ['f'], [], ['g']])
   with self.test_session():
     st = ragged.to_sparse(rt).eval()
     self.assertAllEqual(
         st.indices, [[0, 0], [0, 1], [1, 0], [1, 1], [1, 2], [2, 0], [4, 0]])
     self.assertAllEqual(st.values, b'a b c d e f g'.split())
     self.assertAllEqual(st.dense_shape, [5, 3])
Ejemplo n.º 27
0
 def testDummyOperators(self):
   a = ragged.constant([[True, True], [False]])
   with self.assertRaisesRegexp(TypeError,
                                'RaggedTensor may not be used as a boolean.'):
     bool(a)
   with self.assertRaisesRegexp(TypeError,
                                'RaggedTensor may not be used as a boolean.'):
     if a:
       pass
 def testDummyOperators(self):
     a = ragged.constant([[True, True], [False]])
     with self.assertRaisesRegexp(
             TypeError, 'RaggedTensor may not be used as a boolean.'):
         bool(a)
     with self.assertRaisesRegexp(
             TypeError, 'RaggedTensor may not be used as a boolean.'):
         if a:
             pass
Ejemplo n.º 29
0
  def testConvertRaggedTensorError(self,
                                   pylist,
                                   message,
                                   dtype=None,
                                   preferred_dtype=None):
    rt = ragged.constant(pylist)

    with self.assertRaisesRegexp(ValueError, message):
      ragged.convert_to_tensor_or_ragged_tensor(rt, dtype, preferred_dtype)
Ejemplo n.º 30
0
 def testDocStringExample(self):
     rt = ragged.constant([[1, 2, 3], [4], [], [5, 6]])
     st = ragged.to_sparse(rt)
     expected = ('SparseTensorValue(indices='
                 'array([[0, 0], [0, 1], [0, 2], [1, 0], [3, 0], [3, 1]]), '
                 'values=array([1, 2, 3, 4, 5, 6], dtype=int32), '
                 'dense_shape=array([4, 3]))')
     with self.test_session():
         self.assertEqual(' '.join(repr(st.eval()).split()), expected)
Ejemplo n.º 31
0
    def testShape(self):
        rt = ragged.constant([[1, 2], [3, 4, 5], [6], [], [7]])
        st = ragged.to_sparse(rt)
        self.assertEqual(st.indices.shape.as_list(), [7, 2])
        self.assertEqual(st.values.shape.as_list(), [7])
        self.assertEqual(st.dense_shape.shape.as_list(), [2])

        rt = ragged.constant([[[1, 2]], [], [[3, 4]], []], ragged_rank=1)
        st = ragged.to_sparse(rt)
        self.assertEqual(st.indices.shape.as_list(), [4, 3])
        self.assertEqual(st.values.shape.as_list(), [4])
        self.assertEqual(st.dense_shape.shape.as_list(), [3])

        rt = ragged.constant([[[1], [2, 3, 4, 5, 6, 7]], [[]]])
        st = ragged.to_sparse(rt)
        self.assertEqual(st.indices.shape.as_list(), [7, 3])
        self.assertEqual(st.values.shape.as_list(), [7])
        self.assertEqual(st.dense_shape.shape.as_list(), [3])
Ejemplo n.º 32
0
    def testBinaryOpSparseAndRagged(self):
        x = ragged.constant([[1, 2, 3], [4, 5]])
        y = sparse_tensor.SparseTensor([[0, 0], [0, 1], [2, 0]], [1, 2, 3],
                                       [3, 2])
        with self.assertRaises((TypeError, ValueError)):
            self.evaluate(math_ops.add(x, y))

        with self.assertRaises((TypeError, ValueError)):
            self.evaluate(math_ops.add_n([x, y]))
Ejemplo n.º 33
0
  def testRaggedSegment_Int(self, segment_op, combiner, segment_ids):
    rt_as_list = [[0, 1, 2, 3], [4], [], [5, 6], [7], [8, 9]]
    rt = ragged.constant(rt_as_list)
    num_segments = max(segment_ids) + 1
    expected = self.expected_value(rt_as_list, segment_ids, num_segments,
                                   combiner)

    segmented = segment_op(rt, segment_ids, num_segments)
    self.assertListEqual(self.evaluate(segmented).tolist(), expected)
 def testDocStringExamples(self):
     """Test the examples in apply_op_to_ragged_values.__doc__."""
     rt = ragged.constant([[1, 2, 3], [], [4, 5], [6]])
     v1 = ragged.map_flat_values(array_ops.ones_like, rt)
     v2 = ragged.map_flat_values(math_ops.multiply, rt, rt)
     v3 = ragged.map_flat_values(math_ops.add, rt, 5)
     self.assertRaggedEqual(v1, [[1, 1, 1], [], [1, 1], [1]])
     self.assertRaggedEqual(v2, [[1, 4, 9], [], [16, 25], [36]])
     self.assertRaggedEqual(v3, [[6, 7, 8], [], [9, 10], [11]])
Ejemplo n.º 35
0
    def testBatchGather(self):
        tokens = ragged.constant([['hello', '.', 'there'], ['merhaba'],
                                  ['bonjour', '.', 'ca va', '?']])
        indices = ragged.constant([[0, 2], [0], [0, 2]])

        def gather(x):
            tokens_val, indices_val = x
            return array_ops.gather(tokens_val, indices_val)

        data = tokens, indices
        out = ragged.map_fn(gather,
                            data,
                            dtype=ragged.RaggedTensorType(dtype=dtypes.string,
                                                          ragged_rank=1),
                            infer_shape=False)

        self.assertRaggedEqual(
            out, [[b'hello', b'there'], [b'merhaba'], [b'bonjour', b'ca va']])
Ejemplo n.º 36
0
 def testDocStringExamples(self):
     """Example from ragged_to_tensor.__doc__."""
     rt = ragged.constant([[9, 8, 7], [], [6, 5], [4]])
     dt = ragged.to_tensor(rt)
     with self.test_session():
         self.assertEqual(str(dt.eval()), '[[9 8 7]\n'
                          ' [0 0 0]\n'
                          ' [6 5 0]\n'
                          ' [4 0 0]]')  # pyformat: disable
Ejemplo n.º 37
0
 def testMismatchRaggedRank(self):
   elems = ragged.constant([[[1, 2, 3]], [[4, 5], [6, 7]]])
   fn = lambda x: ragged.reduce_sum(x, axis=0)
   with self.assertRaisesWithLiteralMatch(
       ValueError, r'The declared ragged rank (23) mismatches the result (1)'):
     _ = ragged.map_fn(
         fn,
         elems,
         dtype=ragged.RaggedTensorType(dtype=dtypes.int64, ragged_rank=23))
Ejemplo n.º 38
0
    def testRaggedSegment_Float(self, segment_op, combiner, segment_ids):
        rt_as_list = [[0., 1., 2., 3.], [4.], [], [5., 6.], [7.], [8., 9.]]
        rt = ragged.constant(rt_as_list)
        num_segments = max(segment_ids) + 1
        expected = self.expected_value(rt_as_list, segment_ids, num_segments,
                                       combiner)

        segmented = segment_op(rt, segment_ids, num_segments)
        self.assertRaggedAlmostEqual(segmented, expected, places=5)
 def testOutOfBoundsError(self):
     tensor_params = ['a', 'b', 'c']
     tensor_indices = [0, 1, 2]
     ragged_params = ragged.constant([['a', 'b'], ['c']])
     ragged_indices = ragged.constant([[0, 3]])
     with self.test_session():
         self.assertRaisesRegexp(
             errors.InvalidArgumentError,
             r'indices\[1\] = 3 is not in \[0, 3\)',
             ragged.gather(tensor_params, ragged_indices).eval)
         self.assertRaisesRegexp(
             errors.InvalidArgumentError,
             r'indices\[2\] = 2 is not in \[0, 2\)',
             ragged.gather(ragged_params, tensor_indices).eval)
         self.assertRaisesRegexp(
             errors.InvalidArgumentError,
             r'indices\[1\] = 3 is not in \[0, 2\)',
             ragged.gather(ragged_params, ragged_indices).eval)
Ejemplo n.º 40
0
 def testUnknownIndicesRankError(self):
     if context.executing_eagerly():
         return
     params = ragged.constant([], ragged_rank=1)
     indices = constant_op.constant([0], dtype=dtypes.int64)
     indices = array_ops.placeholder_with_default(indices, None)
     self.assertRaisesRegexp(
         ValueError, r'indices\.shape\.ndims must be known statically',
         ragged.gather, params, indices)
  def testConvertRaggedTensorError(self,
                                   pylist,
                                   message,
                                   dtype=None,
                                   preferred_dtype=None):
    rt = ragged.constant(pylist)

    with self.assertRaisesRegexp(ValueError, message):
      ragged.convert_to_tensor_or_ragged_tensor(rt, dtype, preferred_dtype)
Ejemplo n.º 42
0
 def testMismatchRaggedRank2(self):
   elems = ragged.constant([[1, 2, 3], [4, 5], [6, 7]])
   fn = lambda x: ragged.from_row_starts(x, [0])
   with self.assertRaisesWithLiteralMatch(
       ValueError, r'The declared ragged rank (10) mismatches the result (1)'):
     _ = ragged.map_fn(
         fn,
         elems,
         dtype=ragged.RaggedTensorType(dtype=dtypes.int64, ragged_rank=10))
Ejemplo n.º 43
0
    def testRaggedSegment_Int(self, segment_op, combiner, segment_ids):
        rt_as_list = [[0, 1, 2, 3], [4], [], [5, 6], [7], [8, 9]]
        rt = ragged.constant(rt_as_list)
        num_segments = max(segment_ids) + 1
        expected = self.expected_value(rt_as_list, segment_ids, num_segments,
                                       combiner)

        segmented = segment_op(rt, segment_ids, num_segments)
        self.assertRaggedEqual(segmented, expected)
Ejemplo n.º 44
0
 def testErrors(self):
   rt_input = ragged.constant([[1, 2, 3], [4, 5]])
   axis = array_ops.placeholder_with_default(constant_op.constant([0]), None)
   self.assertRaisesRegexp(ValueError,
                           r'axis must be known at graph construction time.',
                           ragged.reduce_sum, rt_input, axis)
   self.assertRaisesRegexp(TypeError,
                           r'axis must be an int; got str.*',
                           ragged.reduce_sum, rt_input, ['x'])
Ejemplo n.º 45
0
 def testMeanNan(self):
   rt_as_list = [[0, 1, 2, 3], [4], [], [5, 6], [7], [8, 9]]
   expected = (
       np.array([0 + 1 + 2 + 3, 4, 0, 5 + 6, 7, 8 + 9]) / np.array(
           [4, 1, 0, 2, 1, 2]))
   rt_input = ragged.constant(rt_as_list)
   reduced = ragged.reduce_mean(rt_input, axis=1)
   with self.test_session():
     self.assertEqualWithNan(reduced.eval(), expected)
Ejemplo n.º 46
0
 def testElementwiseOpUnknownRankError(self):
     if context.executing_eagerly():
         return
     x = ragged.constant([[1, 2], [3]])
     y = ragged.RaggedTensor.from_row_splits(
         array_ops.placeholder_with_default([1, 2, 3], shape=None),
         x.row_splits)
     with self.assertRaisesRegexp(ValueError,
                                  r'Unable to broadcast: unknown rank'):
         math_ops.add(x, y)
Ejemplo n.º 47
0
  def testRaggedSegment_Int(self, segment_op, combiner, segment_ids):
    rt_as_list = [[0, 1, 2, 3], [4], [], [5, 6], [7], [8, 9]]
    rt = ragged.constant(rt_as_list)
    num_segments = max(segment_ids) + 1
    expected = self.expected_value(rt_as_list, segment_ids, num_segments,
                                   combiner)

    segmented = segment_op(rt, segment_ids, num_segments)
    with self.test_session():
      self.assertListEqual(segmented.eval().tolist(), expected)
Ejemplo n.º 48
0
    def testSingleTensorInput(self):
        """Tests ragged_concat with a single tensor input.

    Usually, we pass a list of values in for rt_inputs.  However, you can
    also pass in a single value (as with tf.concat), in which case it simply
    returns that tensor.  This test exercises that path.
    """
        rt_inputs = ragged.constant([[1, 2], [3, 4]])
        concatenated = ragged.concat(rt_inputs, 0)
        self.assertRaggedEqual(concatenated, [[1, 2], [3, 4]])
Ejemplo n.º 49
0
  def testSingleTensorInput(self):
    """Tests ragged_stack with a single tensor input.

    Usually, we pass a list of values in for rt_inputs.  However, you can
    also pass in a single value (as with tf.stack), in which case it is
    equivalent to expand_dims(axis=0).  This test exercises that path.
    """
    rt_inputs = ragged.constant([[1, 2], [3, 4]])
    stacked = ragged.stack(rt_inputs, 0)
    self.assertRaggedEqual(stacked, [[[1, 2], [3, 4]]])
Ejemplo n.º 50
0
  def testRaggedSegment_Float(self, segment_op, combiner, segment_ids):
    rt_as_list = [[0., 1., 2., 3.], [4.], [], [5., 6.], [7.], [8., 9.]]
    rt = ragged.constant(rt_as_list)
    num_segments = max(segment_ids) + 1
    expected = self.expected_value(rt_as_list, segment_ids, num_segments,
                                   combiner)

    segmented = segment_op(rt, segment_ids, num_segments)
    with self.test_session():
      self.assertNestedListAmostEqual(
          segmented.eval().tolist(), expected, places=5)
Ejemplo n.º 51
0
    def testBinaryOpSparseAndRagged(self):
        x = ragged.constant([[1, 2, 3], [4, 5]])
        y = sparse_tensor.SparseTensor([[0, 0], [0, 1], [2, 0]], [1, 2, 3],
                                       [3, 2])
        with self.assertRaises(TypeError):
            with self.cached_session():
                math_ops.add(x, y).eval()

        with self.assertRaises(TypeError):
            with self.cached_session():
                math_ops.add_n([x, y]).eval()
    def testRaggedGatherNdUnknownRankError(self):
        params = ragged.constant([['a', 'b'], ['c', 'd']])
        indices1 = array_ops.placeholder(dtypes.int32, shape=None)
        indices2 = array_ops.placeholder(dtypes.int32, shape=[None])

        with self.assertRaisesRegexp(ValueError,
                                     'indices.rank be statically known.'):
            ragged.gather_nd(params, indices1)
        with self.assertRaisesRegexp(
                ValueError, r'indices.shape\[-1\] must be statically known.'):
            ragged.gather_nd(params, indices2)
 def testNonElementWiseOp(self):
     x = ragged.constant(
         [[[3, 1, 4], [1, 5, 9], [2, 6, 5]], [], [[3, 5, 8], [9, 7, 9]]],
         ragged_rank=1)
     self.assertRaggedMapInnerValuesReturns(op=math_ops.reduce_sum,
                                            kwargs={
                                                'input_tensor': x,
                                                'axis': 1,
                                            },
                                            expected=[[8, 15, 13], [],
                                                      [16, 25]])
Ejemplo n.º 54
0
    def testRaggedConst(self,
                        pylist,
                        dtype=None,
                        ragged_rank=None,
                        inner_shape=None,
                        expected_shape=None,
                        expected_dtype=None):
        """Tests that `ragged_const(pylist).eval().tolist() == pylist`.

    Args:
      pylist: The `pylist` argument for `ragged_const()`.
      dtype: The `dtype` argument for `ragged_const()`.  If not None, then also
        test that the resulting ragged tensor has this `dtype`.
      ragged_rank: The `ragged_rank` argument for `ragged_const()`.  If not
        None, then also test that the resulting ragged tensor has this
        `ragged_rank`.
      inner_shape: The `inner_shape` argument for `ragged_const()`.  If not
        None, then also test that the resulting ragged tensor has this
        `inner_shape`.
      expected_shape: The expected shape for the resulting ragged tensor.
      expected_dtype: The expected dtype for the resulting ragged tensor (used
        to test default/inferred types when dtype=None).
    """
        rt = ragged.constant(pylist,
                             dtype=dtype,
                             ragged_rank=ragged_rank,
                             inner_shape=inner_shape)

        # If dtype was explicitly specified, check it.
        if dtype is not None:
            self.assertEqual(rt.dtype, dtype)
        if expected_dtype is not None:
            self.assertEqual(rt.dtype, expected_dtype)

        # If ragged_rank was explicitly specified, check it.
        if ragged_rank is not None:
            if isinstance(rt, ragged.RaggedTensor):
                self.assertEqual(rt.ragged_rank, ragged_rank)
            else:
                self.assertEqual(0, ragged_rank)

        # If inner_shape was explicitly specified, check it.
        if inner_shape is not None:
            if isinstance(rt, ragged.RaggedTensor):
                self.assertEqual(rt.flat_values.shape.as_list()[1:],
                                 list(inner_shape))
            else:
                self.assertEqual(rt.shape.as_list(), list(inner_shape))

        if expected_shape is not None:
            self.assertEqual(tuple(rt.shape.as_list()), expected_shape)

        self.assertRaggedEqual(rt, pylist)
Ejemplo n.º 55
0
    def testRaggedMap(
        self,
        fn,
        elems,
        expected_output,
        expected_ragged_rank=None,
        result_ragged_rank=None,
        elems_ragged_rank=None,
        dtype=dtypes.int64,
        result_dtype=None,
        infer_shape=False,
    ):
        elems = ragged.constant(elems, dtype, elems_ragged_rank)
        output = ragged.map_fn(fn=fn,
                               elems=elems,
                               dtype=result_dtype,
                               infer_shape=infer_shape)

        expected_rt = ragged.constant(expected_output,
                                      ragged_rank=expected_ragged_rank)
        self.assertRaggedEqual(expected_rt, output)
 def testDocStringExamples(self):
     """Test the examples in apply_op_to_ragged_values.__doc__."""
     rt = ragged.constant([[1, 2, 3], [], [4, 5], [6]])
     v1 = ragged.map_inner_values(array_ops.ones_like, rt)
     v2 = ragged.map_inner_values(math_ops.multiply, rt, rt)
     v3 = ragged.map_inner_values(math_ops.add, rt, 5)
     with self.test_session():
         self.assertEqual(v1.eval().tolist(), [[1, 1, 1], [], [1, 1], [1]])
         self.assertEqual(v2.eval().tolist(),
                          [[1, 4, 9], [], [16, 25], [36]])
         self.assertEqual(v3.eval().tolist(),
                          [[6, 7, 8], [], [9, 10], [11]])