|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2019 The TensorFlow Datasets Authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Test utils for tensorflow RaggedTensors. |
| 17 | +
|
| 18 | +Copied from the tensorflow/python/ops/ragged/ragged_test_util.py |
| 19 | +
|
| 20 | +TODO(epot): Delete this with the next TF public release. |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import absolute_import |
| 24 | +from __future__ import division |
| 25 | +from __future__ import print_function |
| 26 | + |
| 27 | +import numpy as np |
| 28 | + |
| 29 | +import tensorflow as tf |
| 30 | + |
| 31 | + |
| 32 | +class RaggedTensorTestCase(tf.test.TestCase): |
| 33 | + """Base class for RaggedTensor test cases.""" |
| 34 | + |
| 35 | + def _GetPyList(self, a): |
| 36 | + """Converts a to a nested python list.""" |
| 37 | + if isinstance(a, tf.RaggedTensor): |
| 38 | + return self.evaluate(a).to_list() |
| 39 | + elif isinstance(a, tf.Tensor): |
| 40 | + a = self.evaluate(a) |
| 41 | + return a.tolist() if isinstance(a, np.ndarray) else a |
| 42 | + elif isinstance(a, np.ndarray): |
| 43 | + return a.tolist() |
| 44 | + elif isinstance(a, tf.ragged.RaggedTensorValue): |
| 45 | + return a.to_list() |
| 46 | + else: |
| 47 | + return np.array(a).tolist() |
| 48 | + |
| 49 | + def assertRaggedEqual(self, a, b): |
| 50 | + """Asserts that two potentially ragged tensors are equal.""" |
| 51 | + a_list = self._GetPyList(a) |
| 52 | + b_list = self._GetPyList(b) |
| 53 | + self.assertEqual(a_list, b_list) |
| 54 | + |
| 55 | + if not (isinstance(a, (list, tuple)) or isinstance(b, (list, tuple))): |
| 56 | + a_ragged_rank = a.ragged_rank if is_ragged(a) else 0 |
| 57 | + b_ragged_rank = b.ragged_rank if is_ragged(b) else 0 |
| 58 | + self.assertEqual(a_ragged_rank, b_ragged_rank) |
| 59 | + |
| 60 | + def assertRaggedAlmostEqual(self, a, b, places=7): |
| 61 | + a_list = self._GetPyList(a) |
| 62 | + b_list = self._GetPyList(b) |
| 63 | + self.assertNestedListAlmostEqual(a_list, b_list, places, context='value') |
| 64 | + |
| 65 | + if not (isinstance(a, (list, tuple)) or isinstance(b, (list, tuple))): |
| 66 | + a_ragged_rank = a.ragged_rank if is_ragged(a) else 0 |
| 67 | + b_ragged_rank = b.ragged_rank if is_ragged(b) else 0 |
| 68 | + self.assertEqual(a_ragged_rank, b_ragged_rank) |
| 69 | + |
| 70 | + def assertNestedListAlmostEqual(self, a, b, places=7, context='value'): |
| 71 | + self.assertEqual(type(a), type(b)) |
| 72 | + if isinstance(a, (list, tuple)): |
| 73 | + self.assertLen(a, len(b), 'Length differs for %s' % context) |
| 74 | + for i in range(len(a)): |
| 75 | + self.assertNestedListAlmostEqual(a[i], b[i], places, |
| 76 | + '%s[%s]' % (context, i)) |
| 77 | + else: |
| 78 | + self.assertAlmostEqual( |
| 79 | + a, b, places, |
| 80 | + '%s != %s within %s places at %s' % (a, b, places, context)) |
| 81 | + |
| 82 | + def eval_to_list(self, tensor): |
| 83 | + value = self.evaluate(tensor) |
| 84 | + if is_ragged(value): |
| 85 | + return value.to_list() |
| 86 | + elif isinstance(value, np.ndarray): |
| 87 | + return value.tolist() |
| 88 | + else: |
| 89 | + return value |
| 90 | + |
| 91 | + def _eval_tensor(self, tensor): |
| 92 | + if is_ragged(tensor): |
| 93 | + return tf.ragged.RaggedTensorValue( |
| 94 | + self._eval_tensor(tensor.values), |
| 95 | + self._eval_tensor(tensor.row_splits)) |
| 96 | + else: |
| 97 | + return tf.test.TestCase._eval_tensor(self, tensor) |
| 98 | + |
| 99 | + @staticmethod |
| 100 | + def _normalize_pylist(item): |
| 101 | + """Convert all (possibly nested) np.arrays contained in item to list.""" |
| 102 | + # convert np.arrays in current level to list |
| 103 | + if np.ndim(item) == 0: |
| 104 | + return item |
| 105 | + level = (x.tolist() if isinstance(x, np.ndarray) else x for x in item) |
| 106 | + _normalize = RaggedTensorTestCase._normalize_pylist # pylint: disable=invalid-name |
| 107 | + return [_normalize(el) if np.ndim(el) != 0 else el for el in level] |
| 108 | + |
| 109 | + |
| 110 | +def is_ragged(value): |
| 111 | + """Returns true if `value` is a ragged tensor or ragged tensor value.""" |
| 112 | + return isinstance(value, (tf.RaggedTensor, tf.ragged.RaggedTensorValue)) |
0 commit comments