Пример #1
0
 def testApplyBucketsWithInterpolationRaises(self):
     # We should raise an exception if you try to scale a non-numeric tensor.
     with self.test_session():
         x = tf.constant(['a', 'b', 'c'], dtype=tf.string)
         boundaries = tf.constant([.2, .4], dtype=tf.float32)
         with self.assertRaises(ValueError):
             mappers.apply_buckets_with_interpolation(x, boundaries)
Пример #2
0
 def testBucketsWithInterpolationUnsortedBoundaries(self):
     with self.test_session() as sess:
         x = tf.constant([1, 2, 3, 5], dtype=tf.float32)
         boundaries = tf.constant([[4, 2]], dtype=tf.float32)
         output = mappers.apply_buckets_with_interpolation(x, boundaries)
         # Boundaries must be sorted, so an exception is raised.
         with self.assertRaises(tf.errors.InvalidArgumentError):
             sess.run(output)
Пример #3
0
 def testApplyBucketsWithInterpolationRaggedTensor(self):
   inputs = tf.RaggedTensor.from_row_splits(
       values=[15, 10, 20, 17, -1111, 21], row_splits=[0, 1, 1, 2, 4, 5, 6])
   boundaries = [[10, 20]]
   expected_bucketized = tf.RaggedTensor.from_row_splits(
       values=[.5, 0, 1, .7, 0, 1], row_splits=[0, 1, 1, 2, 4, 5, 6])
   bucketized = mappers.apply_buckets_with_interpolation(inputs, boundaries)
   self.assertAllEqual(expected_bucketized, bucketized)
Пример #4
0
 def testApplyBucketsWithInterpolationAllNanBoundariesRaises(self):
   with tf.compat.v1.Graph().as_default():
     with self.test_session() as sess:
       x = tf.constant([float('-inf'), float('nan'), 0.0, 1.0])
       boundaries = tf.constant([[float('nan'), float('nan'), float('nan')]])
       with self.assertRaisesRegexp(tf.errors.InvalidArgumentError,
                                    'num_boundaries'):
         sess.run(mappers.apply_buckets_with_interpolation(x, boundaries))
Пример #5
0
 def testBucketsWithInterpolationUnknownShapeBoundary(self):
   with self.test_session() as sess:
     x = tf.constant([0, 1, 5, 12], dtype=tf.float32)
     # The shape used to generate the boundaries is random, and therefore
     # the size of the boundaries tensor is not known.
     num_boundaries = tf.random.uniform([1], 1, 2, dtype=tf.int64)[0]
     boundaries = tf.random.uniform([1, num_boundaries], 0, 10)
     # We don't assert anything about the outcome because we are intentionally
     # using randomized boundaries, but we ensure the operations succeed.
     _ = sess.run(mappers.apply_buckets_with_interpolation(x, boundaries))
Пример #6
0
 def testApplyBucketsWithInterpolation(self,
                                       x,
                                       boundaries,
                                       expected_results,
                                       input_dtype=tf.float32,
                                       boundaries_dtype=tf.float32):
     with self.test_session() as sess:
         x = tf.constant(x, dtype=input_dtype)
         boundaries = tf.constant([boundaries], dtype=boundaries_dtype)
         output = mappers.apply_buckets_with_interpolation(x, boundaries)
         self.assertAllClose(sess.run(output), expected_results, 1e-6)
Пример #7
0
 def testApplyBucketsWithInterpolationSparseTensor(self):
   with self.test_session() as sess:
     x = tf.SparseTensor(
         indices=[[0, 0], [1, 2], [3, 4], [1, 4], [6, 1], [3, 2]],
         values=[15, 10, 20, 17, -1111, 21],
         dense_shape=[7, 5])
     boundaries = tf.constant([[10, 20]], dtype=tf.int64)
     output = mappers.apply_buckets_with_interpolation(x, boundaries)
     expected_results = tf.SparseTensor(
         indices=[[0, 0], [1, 2], [3, 4], [1, 4], [6, 1], [3, 2]],
         values=[.5, 0, 1, .7, 0, 1],
         dense_shape=[7, 5])
     actual_results = sess.run(output)
     self.assertAllClose(actual_results.values, expected_results.values, 1e-6)
     self.assertAllClose(
         actual_results.indices, expected_results.indices, 1e-6)
Пример #8
0
 def testApplyBucketsWithInterpolationSparseTensor(self):
     with tf.compat.v1.Graph().as_default():
         with self.test_session() as sess:
             x = tf.SparseTensor(indices=[[0, 0, 0], [1, 1, 2], [3, 1, 4],
                                          [1, 1, 4], [6, 1, 1], [3, 1, 2]],
                                 values=[15, 10, 20, 17, -1111, 21],
                                 dense_shape=[7, 3, 5])
             boundaries = [[10, 20]]
             output = mappers.apply_buckets_with_interpolation(
                 x, boundaries)
             expected_results = tf.SparseTensor(indices=x.indices,
                                                values=[.5, 0, 1, .7, 0, 1],
                                                dense_shape=x.dense_shape)
             actual_results = sess.run(output)
             self.assertAllClose(actual_results.values,
                                 expected_results.values, 1e-6)
             self.assertAllEqual(actual_results.indices,
                                 expected_results.indices)
             self.assertAllEqual(actual_results.dense_shape,
                                 expected_results.dense_shape)