예제 #1
0
  def testBasicQuantileBuckets(self):
    """Sets up the quantile summary op test as follows.

    Create a batch of 6 examples having a dense and sparse features.
    The data looks like this
    | Instance | instance weights | Dense 0  | Sparse 0
    | 0        |     10           |   1      |
    | 1        |     1            |   2      |    2
    | 2        |     1            |   3      |    3
    | 3        |     1            |   4      |    4
    | 4        |     1            |   4      |    5
    | 5        |     1            |   5      |    6
    """

    dense_float_tensor_0 = constant_op.constant(
        [1, 2, 3, 4, 4, 5], dtype=dtypes.float32)
    sparse_indices_0 = constant_op.constant(
        [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0]], dtype=dtypes.int64)
    sparse_values_0 = constant_op.constant(
        [2, 3, 4, 5, 6], dtype=dtypes.float32)
    sparse_shape_0 = constant_op.constant([6, 1], dtype=dtypes.int64)
    example_weights = constant_op.constant(
        [10, 1, 1, 1, 1, 1], dtype=dtypes.float32)

    with self.test_session():
      config = self._gen_config(0.33, 3)
      dense_buckets, sparse_buckets = quantile_ops.quantile_buckets(
          [dense_float_tensor_0], [sparse_indices_0], [sparse_values_0],
          [sparse_shape_0],
          example_weights=example_weights,
          dense_config=[config],
          sparse_config=[config])

      self.assertAllEqual([1, 3, 5], dense_buckets[0].eval())
      self.assertAllEqual([2, 4, 6.], sparse_buckets[0].eval())
  def testFixedNonUniform(self):
    """Sets up the quantile summary op test as follows.

    Creates array dividing range [0, 1] to 1<<16 elements equally spaced
    with weight same as the value.
    """
    dense_float_tensor_0 = constant_op.constant(
        [(1.0 * i) / math.pow(2.0, 16)
         for i in range(0, int(math.pow(2, 16)) + 1)])
    example_weights = constant_op.constant(
        [(1.0 * i) / math.pow(2.0, 16)
         for i in range(0, int(math.pow(2, 16)) + 1)])

    config = self._gen_config(0.1, 10)

    with self.test_session():
      dense_buckets, _ = quantile_ops.quantile_buckets(
          [dense_float_tensor_0], [], [], [],
          example_weights=example_weights,
          dense_config=[config],
          sparse_config=[])
      self.assertAllClose(
          [0] + [math.sqrt((i + 1.0) / 10) for i in range(0, 10)],
          dense_buckets[0].eval(),
          atol=0.1)
예제 #3
0
    def testBasicQuantileBuckets(self):
        """Sets up the quantile summary op test as follows.

    Create a batch of 6 examples having a dense and sparse features. SparseM is
    a sparse multi-dimensional (multivalent) feature.
    The data looks like this
    | Instance | instance weights | Dense 0  | Sparse 0 | SparseM
    | 0        |     10           |   1      |          |   |   |
    | 1        |     1            |   2      |    2     | 2 |   |
    | 2        |     1            |   3      |    3     | 3 |   |
    | 3        |     1            |   4      |    4     |   | 4 |
    | 4        |     1            |   4      |    5     |   | 5 |
    | 5        |     1            |   5      |    6     |   | 6 |
    """

        dense_float_tensor_0 = constant_op.constant([1, 2, 3, 4, 4, 5],
                                                    dtype=dtypes.float32)
        sparse_indices_0 = constant_op.constant(
            [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0]], dtype=dtypes.int64)
        sparse_values_0 = constant_op.constant([2, 3, 4, 5, 6],
                                               dtype=dtypes.float32)
        sparse_shape_0 = constant_op.constant([6, 1], dtype=dtypes.int64)
        # Multi-dimensional feature that should have the same quantiles as Sparse 0.
        sparse_indices_m = constant_op.constant(
            [[1, 1], [2, 0], [3, 1], [4, 1], [5, 1]], dtype=dtypes.int64)
        sparse_values_m = constant_op.constant([2, 3, 4, 5, 6],
                                               dtype=dtypes.float32)
        sparse_shape_m = constant_op.constant([6, 2], dtype=dtypes.int64)

        example_weights = constant_op.constant([10, 1, 1, 1, 1, 1],
                                               dtype=dtypes.float32)

        with self.test_session():
            config = self._gen_config(0.33, 3)
            dense_buckets, sparse_buckets = quantile_ops.quantile_buckets(
                [dense_float_tensor_0], [sparse_indices_0, sparse_indices_m],
                [sparse_values_0, sparse_values_m],
                [sparse_shape_0, sparse_shape_m],
                example_weights=example_weights,
                dense_config=[config],
                sparse_config=[config, config])

            self.assertAllEqual([1, 3, 5], dense_buckets[0].eval())
            self.assertAllEqual([2, 4, 6.], sparse_buckets[0].eval())
            # Multidimensional sparse.
            self.assertAllEqual([2, 4, 6.], sparse_buckets[1].eval())
예제 #4
0
  def testBasicQuantileBuckets(self):
    """Sets up the quantile summary op test as follows.

    Create a batch of 6 examples having a dense and sparse features. SparseM is
    a sparse multi-dimensional (multivalent) feature.
    The data looks like this
    | Instance | instance weights | Dense 0  | Sparse 0 | SparseM
    | 0        |     10           |   1      |          |   |   |
    | 1        |     1            |   2      |    2     | 2 |   |
    | 2        |     1            |   3      |    3     | 3 |   |
    | 3        |     1            |   4      |    4     |   | 4 |
    | 4        |     1            |   4      |    5     |   | 5 |
    | 5        |     1            |   5      |    6     |   | 6 |
    """

    dense_float_tensor_0 = constant_op.constant(
        [1, 2, 3, 4, 4, 5], dtype=dtypes.float32)
    sparse_indices_0 = constant_op.constant(
        [[1, 0], [2, 0], [3, 0], [4, 0], [5, 0]], dtype=dtypes.int64)
    sparse_values_0 = constant_op.constant(
        [2, 3, 4, 5, 6], dtype=dtypes.float32)
    sparse_shape_0 = constant_op.constant([6, 1], dtype=dtypes.int64)
    # Multi-dimensional feature that should have the same quantiles as Sparse 0.
    sparse_indices_m = constant_op.constant(
        [[1, 1], [2, 0], [3, 1], [4, 1], [5, 1]], dtype=dtypes.int64)
    sparse_values_m = constant_op.constant(
        [2, 3, 4, 5, 6], dtype=dtypes.float32)
    sparse_shape_m = constant_op.constant([6, 2], dtype=dtypes.int64)

    example_weights = constant_op.constant(
        [10, 1, 1, 1, 1, 1], dtype=dtypes.float32)

    with self.test_session():
      config = self._gen_config(0.33, 3)
      dense_buckets, sparse_buckets = quantile_ops.quantile_buckets(
          [dense_float_tensor_0], [sparse_indices_0, sparse_indices_m],
          [sparse_values_0, sparse_values_m], [sparse_shape_0, sparse_shape_m],
          example_weights=example_weights,
          dense_config=[config],
          sparse_config=[config, config])

      self.assertAllEqual([1, 3, 5], dense_buckets[0].eval())
      self.assertAllEqual([2, 4, 6.], sparse_buckets[0].eval())
      # Multidimensional sparse.
      self.assertAllEqual([2, 4, 6.], sparse_buckets[1].eval())