• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 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"""Resampling dataset transformations."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20from tensorflow.python.util import deprecation
21from tensorflow.python.util.tf_export import tf_export
22
23
24@deprecation.deprecated(None, "Use `tf.data.Dataset.rejection_resample(...)`.")
25@tf_export("data.experimental.rejection_resample")
26def rejection_resample(class_func, target_dist, initial_dist=None, seed=None):
27  """A transformation that resamples a dataset to achieve a target distribution.
28
29  **NOTE** Resampling is performed via rejection sampling; some fraction
30  of the input values will be dropped.
31
32  Args:
33    class_func: A function mapping an element of the input dataset to a scalar
34      `tf.int32` tensor. Values should be in `[0, num_classes)`.
35    target_dist: A floating point type tensor, shaped `[num_classes]`.
36    initial_dist: (Optional.)  A floating point type tensor, shaped
37      `[num_classes]`.  If not provided, the true class distribution is
38      estimated live in a streaming fashion.
39    seed: (Optional.) Python integer seed for the resampler.
40
41  Returns:
42    A `Dataset` transformation function, which can be passed to
43    `tf.data.Dataset.apply`.
44  """
45  def _apply_fn(dataset):
46    """Function from `Dataset` to `Dataset` that applies the transformation."""
47
48    return dataset.rejection_resample(
49        class_func=class_func,
50        target_dist=target_dist,
51        initial_dist=initial_dist,
52        seed=seed)
53
54  return _apply_fn
55