Home | History | Annotate | Download | only in ops
      1 # Copyright 2018 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #     http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 # ==============================================================================
     15 """Coder operations tests."""
     16 
     17 from __future__ import absolute_import
     18 from __future__ import division
     19 from __future__ import print_function
     20 
     21 from tensorflow.contrib.coder.python.ops import coder_ops
     22 from tensorflow.python.framework import dtypes
     23 from tensorflow.python.ops import array_ops
     24 from tensorflow.python.ops import math_ops
     25 from tensorflow.python.ops import random_ops
     26 from tensorflow.python.platform import test
     27 
     28 
     29 class CoderOpsTest(test.TestCase):
     30   """Coder ops test.
     31 
     32   Coder ops have C++ tests. Python test just ensures that Python binding is not
     33   broken.
     34   """
     35 
     36   def testReadmeExample(self):
     37     data = random_ops.random_uniform((128, 128), 0, 10, dtype=dtypes.int32)
     38     histogram = math_ops.bincount(data, minlength=10, maxlength=10)
     39     cdf = math_ops.cumsum(histogram, exclusive=False)
     40     cdf = array_ops.pad(cdf, [[1, 0]])
     41     cdf = array_ops.reshape(cdf, [1, 1, -1])
     42 
     43     data = math_ops.cast(data, dtypes.int16)
     44     encoded = coder_ops.range_encode(data, cdf, precision=14)
     45     decoded = coder_ops.range_decode(
     46         encoded, array_ops.shape(data), cdf, precision=14)
     47 
     48     with self.test_session() as sess:
     49       self.assertAllEqual(*sess.run((data, decoded)))
     50 
     51 
     52 if __name__ == '__main__':
     53   test.main()
     54