예제 #1
0
 def testShape(self):
     self.assertRaggedEqual(
         ragged.range(0, 0, 1).shape.as_list(), [1, None])
     self.assertRaggedEqual(
         ragged.range([1, 2, 3]).shape.as_list(), [3, None])
     self.assertRaggedEqual(
         ragged.range([1, 2, 3], [4, 5, 6]).shape.as_list(), [3, None])
예제 #2
0
    def testDocStringExamples(self):
        """Examples from ragged_range.__doc__."""
        rt1 = ragged.range([3, 5, 2])
        self.assertRaggedEqual(rt1, [[0, 1, 2], [0, 1, 2, 3, 4], [0, 1]])

        rt2 = ragged.range([0, 5, 8], [3, 3, 12])
        self.assertRaggedEqual(rt2, [[0, 1, 2], [], [8, 9, 10, 11]])

        rt3 = ragged.range([0, 5, 8], [3, 3, 12], 2)
        self.assertRaggedEqual(rt3, [[0, 2], [], [8, 10]])
예제 #3
0
    def testBroadcast(self):
        # Specify starts and limits, broadcast deltas.
        self.assertRaggedEqual(ragged.range([0, 3, 5], [4, 4, 15], 3), [
            list(range(0, 4, 3)),
            list(range(3, 4, 3)),
            list(range(5, 15, 3))
        ])

        # Broadcast all arguments.
        self.assertRaggedEqual(ragged.range(0, 5, 1), [list(range(0, 5, 1))])
  def testDocStringExamples(self):
    """Examples from ragged_range.__doc__."""
    with self.test_session():
      rt1 = ragged.range([3, 5, 2]).eval().tolist()
      self.assertEqual(rt1, [[0, 1, 2], [0, 1, 2, 3, 4], [0, 1]])

      rt2 = ragged.range([0, 5, 8], [3, 3, 12]).eval().tolist()
      self.assertEqual(rt2, [[0, 1, 2], [], [8, 9, 10, 11]])

      rt3 = ragged.range([0, 5, 8], [3, 3, 12], 2).eval().tolist()
      self.assertEqual(rt3, [[0, 2], [], [8, 10]])
  def testBroadcast(self):
    with self.test_session():
      # Specify starts and limits, broadcast deltas.
      self.assertEqual(
          ragged.range([0, 3, 5], [4, 4, 15], 3).eval().tolist(),
          [list(range(0, 4, 3)), list(range(3, 4, 3)),
           list(range(5, 15, 3))])

      # Broadcast all arguments.
      self.assertEqual(
          ragged.range(0, 5, 1).eval().tolist(), [list(range(0, 5, 1))])
  def testNegativeDeltas(self):
    with self.test_session():
      self.assertEqual(
          ragged.range([0, 3, 5], limits=0, deltas=-1).eval().tolist(),
          [list(range(0, 0, -1)), list(range(3, 0, -1)),
           list(range(5, 0, -1))])

      self.assertEqual(
          ragged.range([0, -3, 5], limits=0, deltas=[-1, 1,
                                                     -2]).eval().tolist(),
          [list(range(0, 0, -1)), list(range(-3, 0, 1)),
           list(range(5, 0, -2))])
예제 #7
0
    def testNegativeDeltas(self):
        self.assertRaggedEqual(ragged.range([0, 3, 5], limits=0, deltas=-1), [
            list(range(0, 0, -1)),
            list(range(3, 0, -1)),
            list(range(5, 0, -1))
        ])

        self.assertRaggedEqual(
            ragged.range([0, -3, 5], limits=0, deltas=[-1, 1, -2]), [
                list(range(0, 0, -1)),
                list(range(-3, 0, 1)),
                list(range(5, 0, -2))
            ])
 def testFloatRanges(self):
   with self.test_session():
     expected = [[0.0, 0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 2.8, 3.2, 3.6], [3.0],
                 [5.0, 7.2, 9.4, 11.6, 13.8]]
     actual = ragged.range([0.0, 3.0, 5.0], [3.9, 4.0, 15.0],
                           [0.4, 1.5, 2.2]).eval().tolist()
     self.assertEqual(expected, [[round(v, 5) for v in row] for row in actual])
예제 #9
0
 def testFloatRanges(self):
     expected = [[0.0, 0.4, 0.8, 1.2, 1.6, 2.0, 2.4, 2.8, 3.2, 3.6], [3.0],
                 [5.0, 7.2, 9.4, 11.6, 13.8]]
     actual = ragged.range([0.0, 3.0, 5.0], [3.9, 4.0, 15.0],
                           [0.4, 1.5, 2.2])
     self.assertEqual(expected, [[round(v, 5) for v in row]
                                 for row in self.eval_to_list(actual)])
  def testBasicRanges(self):
    with self.test_session():
      # Specify limits only.
      self.assertEqual(
          ragged.range([0, 3, 5]).eval().tolist(),
          [list(range(0)), list(range(3)), list(range(5))])

      # Specify starts and limits.
      self.assertEqual(
          ragged.range([0, 3, 5], [2, 3, 10]).eval().tolist(),
          [list(range(0, 2)), list(range(3, 3)), list(range(5, 10))])

      # Specify starts, limits, and deltas.
      self.assertEqual(
          ragged.range([0, 3, 5], [4, 4, 15], [2, 3, 4]).eval().tolist(),
          [list(range(0, 4, 2)), list(range(3, 4, 3)),
           list(range(5, 15, 4))])
예제 #11
0
    def testBasicRanges(self):
        # Specify limits only.
        self.assertRaggedEqual(
            ragged.range([0, 3, 5]),
            [list(range(0)), list(range(3)),
             list(range(5))])

        # Specify starts and limits.
        self.assertRaggedEqual(
            ragged.range([0, 3, 5], [2, 3, 10]),
            [list(range(0, 2)),
             list(range(3, 3)),
             list(range(5, 10))])

        # Specify starts, limits, and deltas.
        self.assertRaggedEqual(ragged.range([0, 3, 5], [4, 4, 15], [2, 3, 4]),
                               [
                                   list(range(0, 4, 2)),
                                   list(range(3, 4, 3)),
                                   list(range(5, 15, 4))
                               ])
예제 #12
0
 def testShape(self):
   self.assertEqual(ragged.range(0, 0, 0).shape.as_list(), [1, None])
   self.assertEqual(ragged.range([1, 2, 3]).shape.as_list(), [3, None])
   self.assertEqual(
       ragged.range([1, 2, 3], [4, 5, 6]).shape.as_list(), [3, None])
 def testKernelErrors(self):
   with self.test_session():
     self.assertRaisesRegexp(errors.InvalidArgumentError,
                             r'Requires delta != 0',
                             ragged.range(0, 0, 0).eval)
예제 #14
0
 def testEmptyRanges(self):
     rt1 = ragged.range([0, 5, 3], [0, 3, 5])
     rt2 = ragged.range([0, 5, 5], [0, 3, 5], -1)
     self.assertRaggedEqual(rt1, [[], [], [3, 4]])
     self.assertRaggedEqual(rt2, [[], [5, 4], []])
예제 #15
0
class RaggedMapOpTest(test_util.TensorFlowTestCase, parameterized.TestCase):
  @parameterized.parameters([
      # The following test sets map over a RaggedTensor and apply a
      # transformation that returns with shape:
      # [d1, (d2)] -> [d1]
      dict(
          fn=mo.reduce_mean,
          elems=[[1, 2, 3], [4, 5], [6, 7]],
          expected_output=[2, 4, 6],
      ),
      dict(
          fn=string_ops.reduce_join,
          elems=[['foo', 'bar', 'baz'], ['a'], ['b', 'c']],
          expected_output=[b'foobarbaz', b'a', b'bc'],
          dtype=dtypes.string,
      ),
      # [d1, (d2)] -> [d1, 2]
      dict(
          fn=lambda x: array_ops.stack([mo.reduce_mean(x), mo.reduce_sum(x)]),
          # fn=self.stack_mean_and_sum,
          elems=[[1, 2, 3], [4, 5], [6, 7]],
          expected_output=[[2, 6], [4.5, 9], [6.5, 13]],
          dtype=dtypes.float32,
      ),
      # [d1, (d2)] -> [d1, (d2)]
      dict(
          fn=lambda x: x+1,
          elems=[[1, 2, 3], [4, 5], [6, 7]],
          expected_output=[[2, 3, 4], [5, 6], [7, 8]],
          dtype=dtypes.int64,
          result_dtype=ragged.RaggedTensorType(dtype=dtypes.int64,
                                               ragged_rank=1),
      ),
      # [d1, (d2), d3] -> [d1, (d2), d3]
      dict(
          fn=lambda x: x+1,
          elems=[[[1, 2], [3, 4]], [], [[5, 6], [7, 8], [9, 0]]],
          elems_ragged_rank=1,
          expected_ragged_rank=1,
          result_dtype=ragged.RaggedTensorType(dtype=dtypes.int64,
                                               ragged_rank=1),
          expected_output=[[[2, 3], [4, 5]], [], [[6, 7], [8, 9], [10, 1]]],
      ),
      # [d1, (d2)] -> [d1, (d2), (d3)]
      dict(
          fn=lambda x: ragged.from_row_starts(x, [0]),
          elems=[[1, 2, 3], [4, 5], [6, 7]],
          expected_output=[[[1, 2, 3]], [[4, 5]], [[6, 7]]],
          result_dtype=ragged.RaggedTensorType(dtype=dtypes.int64,
                                               ragged_rank=2),
      ),
      # [d1, (d2), (d3)] -> [d1, (d2), (d3)]
      dict(
          fn=lambda x: ragged.map_inner_values(mo.add, x, 1),
          elems=[[[1, 2, 3]], [[4, 5], [6, 7]]],
          expected_output=[[[2, 3, 4]], [[5, 6], [7, 8]]],
          result_dtype=ragged.RaggedTensorType(dtype=dtypes.int64,
                                               ragged_rank=2),
      ),
      # [d1, (d2), (d3)] -> [d1, (d2)]
      dict(
          fn=lambda x: ragged.reduce_sum(x, axis=1),
          elems=[[[1, 2, 3]], [[4, 5], [6, 7]]],
          expected_output=[[6], [9, 13]],
          result_dtype=ragged.RaggedTensorType(dtype=dtypes.int64,
                                               ragged_rank=1),
      ),
      # [d1, (d2), (d3)] -> [d1, (d3)]
      dict(
          fn=lambda x: ragged.reduce_sum(x, axis=0),
          elems=[[[1, 2, 3]], [[4, 5], [6, 7]]],
          expected_output=[[1, 2, 3], [10, 12]],
          result_dtype=ragged.RaggedTensorType(dtype=dtypes.int64,
                                               ragged_rank=1),
      ),
      # [d1, (d2), (d3)] -> [d1]
      dict(
          fn=ragged.reduce_sum,
          elems=[[[1, 2, 3]], [[4, 5], [6, 7]]],
          expected_output=[6, 22],
          result_dtype=dtypes.int64,
      ),
      # [d1] -> [d1, (d2)]
      dict(
          fn=mo.range,
          elems=[4, 0, 2],
          expected_output=[[0, 1, 2, 3], [], [0, 1]],
          result_dtype=ragged.RaggedTensorType(dtype=dtypes.int64,
                                               ragged_rank=1),
      ),
      # [d1] -> [d1, (d2), (d3)]
      dict(
          fn=lambda x: ragged.range(mo.range(x)),
          elems=[5, 0, 3],
          expected_output=[
              [[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3]], [], [[], [0], [0, 1]]
          ],
          result_dtype=ragged.RaggedTensorType(dtype=dtypes.int64,
                                               ragged_rank=2),
      ),
      # [d1, (d2), (d3), (d4a), (d5)] ->  [d1, (d2), (d3), (d4b), (d5)]
      dict(
          fn=lambda x: ragged.add(x, 1),
          elems=[[[[[1, 2, 3]], [[4], [5]]]], [[[[6, 7]]], [[[8], []]]]],
          expected_output=[[[[[2, 3, 4]], [[5], [6]]]],
                           [[[[7, 8]]], [[[9], []]]]],
          result_dtype=ragged.RaggedTensorType(dtype=dtypes.int64,
                                               ragged_rank=4),
      ),
  ])

  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)
    with self.test_session():
      if ragged.is_ragged(expected_output):
        self.assertEqual(output.ragged_rank, expected_rt.ragged_rank)
      output_values = self.evaluate(output)
      self.assertAllEqual(expected_output, output_values.tolist())

  def testRaggedMapOnStructure(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 _reduce_sum_from_all(f):
      return mo.reduce_sum(f['batman']) + mo.reduce_sum(f['robin'])

    output = ragged.map_fn(
        fn=_reduce_sum_from_all,
        elems=features,
        dtype=dtypes.int32,
    )

    with self.test_session():
      self.assertAllEqual(output.eval().tolist(), [66, 44, 198])

  # Test mapping over a dict of RTs can produce a dict of RTs.
  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 testZip(self):
    x = ragged.constant([[10, 20], [30, 40], [50, 60], [70], [80, 90, 100]],
                        dtypes.int64)
    y = array_ops.expand_dims(
        mo.range(ragged.nrows(x), dtype=dtypes.int64), axis=1)

    def _zip(foo):
      y_val, x_val = foo
      bar = backend.tile(y_val, array_ops.shape(x_val))
      return array_ops.stack([bar, x_val], axis=1)

    output = ragged.map_fn(
        _zip, (y, x),
        dtype=ragged.RaggedTensorType(dtype=dtypes.int64, ragged_rank=1),
        infer_shape=False)

    with self.test_session():
      result = self.evaluate(output).tolist()
      self.assertAllEqual(
          result, [[[0, 10], [0, 20]], [[1, 30], [1, 40]], [[2, 50], [2, 60]],
                   [[3, 70]], [[4, 80], [4, 90], [4, 100]]])

  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)

    with self.test_session():
      self.assertAllEqual(
          self.evaluate(out).tolist(),
          [[b'hello', b'there'], [b'merhaba'], [b'bonjour', b'ca va']])

  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))

  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))
 def testEmptyRanges(self):
   rt1 = ragged.range([0, 5, 3], [0, 3, 5])
   rt2 = ragged.range([0, 5, 5], [0, 3, 5], -1)
   with self.test_session():
     self.assertEqual(rt1.eval().tolist(), [[], [], [3, 4]])
     self.assertEqual(rt2.eval().tolist(), [[], [5, 4], []])
예제 #17
0
 def testKernelErrors(self):
     with self.assertRaisesRegexp(errors.InvalidArgumentError,
                                  r'Requires delta != 0'):
         self.evaluate(ragged.range(0, 0, 0))