Beispiel #1
0
 def testApplyBucketsWithKeys(self):
   with tf.compat.v1.Graph().as_default():
     values = tf.constant([
         -100, -0.05, 0.05, 0.25, 0.15, 100, -100, 0, 4.3, 4.5, 4.4, 4.6, 100
     ],
                          dtype=tf.float32)
     keys = tf.constant([
         'a', 'a', 'a', 'a', 'a', 'a', 'b', 'missing', 'b', 'b', 'b', 'b', 'b'
     ])
     key_vocab = tf.constant(['a', 'b'])
     # Pre-normalization boundaries: [[0, 0.1, 0.2], [4.33, 4.43, 4.53]]
     bucket_boundaries = tf.constant([0.0, 0.5, 1.0, 1.5, 2.0],
                                     dtype=tf.float32)
     scales = 1.0 / (
         tf.constant([0.2, 4.53], dtype=tf.float32) -
         tf.constant([0, 4.33], dtype=tf.float32))
     shifts = tf.constant([0, 1.0 - (4.33 * 5)], dtype=tf.float32)
     num_buckets = tf.constant(4, dtype=tf.int64)
     buckets = mappers._apply_buckets_with_keys(values, keys, key_vocab,
                                                bucket_boundaries, scales,
                                                shifts, num_buckets)
     with self.test_session() as sess:
       sess.run(tf.compat.v1.tables_initializer())
       output = sess.run(buckets)
       self.assertAllEqual([0, 0, 1, 3, 2, 3, 0, -1, 0, 2, 1, 3, 3], output)
 def testApplyBucketsWithKeys(self):
   values = tf.constant(
       [-100, -0.05, 0.05, 0.25, 0.15, 100, -100, 4.3, 4.5, 4.4, 4.6, 100],
       dtype=tf.float32)
   keys = tf.constant(
       ['a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'b'])
   key_vocab = tf.constant(['a', 'b'])
   bucket_boundaries = tf.constant([[0, .1, .2], [4.33, 4.43, 4.53]],
                                   dtype=tf.float32)
   buckets = mappers._apply_buckets_with_keys(values, keys, key_vocab,
                                              bucket_boundaries)
   with self.test_session() as sess:
     sess.run(tf.tables_initializer())
     output = sess.run(buckets)
     self.assertAllEqual([0, 0, 1, 3, 2, 3, 0, 0, 2, 1, 3, 3], output)