Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/data/experimental/ops/resampling.py: 67%

9 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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 tensorflow.python.util import deprecation 

17from tensorflow.python.util.tf_export import tf_export 

18 

19 

20@deprecation.deprecated(None, "Use `tf.data.Dataset.rejection_resample(...)`.") 

21@tf_export("data.experimental.rejection_resample") 

22def rejection_resample(class_func, target_dist, initial_dist=None, seed=None): 

23 """A transformation that resamples a dataset to achieve a target distribution. 

24 

25 **NOTE** Resampling is performed via rejection sampling; some fraction 

26 of the input values will be dropped. 

27 

28 Args: 

29 class_func: A function mapping an element of the input dataset to a scalar 

30 `tf.int32` tensor. Values should be in `[0, num_classes)`. 

31 target_dist: A floating point type tensor, shaped `[num_classes]`. 

32 initial_dist: (Optional.) A floating point type tensor, shaped 

33 `[num_classes]`. If not provided, the true class distribution is 

34 estimated live in a streaming fashion. 

35 seed: (Optional.) Python integer seed for the resampler. 

36 

37 Returns: 

38 A `Dataset` transformation function, which can be passed to 

39 `tf.data.Dataset.apply`. 

40 """ 

41 def _apply_fn(dataset): 

42 """Function from `Dataset` to `Dataset` that applies the transformation.""" 

43 

44 return dataset.rejection_resample( 

45 class_func=class_func, 

46 target_dist=target_dist, 

47 initial_dist=initial_dist, 

48 seed=seed) 

49 

50 return _apply_fn