From a0f473504bdddb414497861335c83b7a10917c68 Mon Sep 17 00:00:00 2001 From: Vvsmile <450864116@qq.com> Date: Wed, 23 Nov 2022 17:46:06 +0800 Subject: [PATCH] [Clean Fluid API]Remove API: label_smooth (#47952) * Remove API: label_smooth (replace with paddle.nn.functional.label_smooth) Replace the paddle.fluid.layers.label_smooth with the paddle.nn.functional.label_smooth * modify the call of label_smooth from old style to new style --- python/paddle/fluid/layers/nn.py | 90 ------------------- .../fleet/parallel_dygraph_transformer.py | 3 +- .../fluid/tests/unittests/dist_transformer.py | 3 +- .../transformer_dygraph_model.py | 3 +- ..._imperative_transformer_sorted_gradient.py | 3 +- .../unittests/test_label_smooth_functional.py | 3 +- .../fluid/tests/unittests/test_layers.py | 5 +- 7 files changed, 11 insertions(+), 99 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index d782bf973a..3d26fc1260 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -103,7 +103,6 @@ __all__ = [ 'lod_reset', 'lod_append', 'pad', - 'label_smooth', 'roi_pool', 'roi_align', 'image_resize', @@ -5843,95 +5842,6 @@ def pad(x, paddings, pad_value=0.0, name=None): return out -def label_smooth( - label, prior_dist=None, epsilon=0.1, dtype="float32", name=None -): - r""" - :alias_main: paddle.nn.functional.label_smooth - :alias: paddle.nn.functional.label_smooth,paddle.nn.functional.common.label_smooth - :old_api: paddle.fluid.layers.label_smooth - - Label smoothing is a mechanism to regularize the classifier layer and is called - label-smoothing regularization (LSR). - - Label smoothing is proposed to encourage the model to be less confident, - since optimizing the log-likelihood of the correct label directly may - cause overfitting and reduce the ability of the model to adapt. Label - smoothing replaces the ground-truth label :math:`y` with the weighted sum - of itself and some fixed distribution :math:`\mu`. For class :math:`k`, - i.e. - - .. math:: - - \\tilde{y_k} = (1 - \epsilon) * y_k + \epsilon * \mu_k, - - where :math:`1 - \epsilon` and :math:`\epsilon` are the weights - respectively, and :math:`\\tilde{y}_k` is the smoothed label. Usually - uniform distribution is used for :math:`\mu`. - - See more details about label smoothing in https://arxiv.org/abs/1512.00567. - - Parameters: - label(Variable): The input variable containing the label data. The - label data should use one-hot representation. It's - a multidimensional tensor with a shape of - :math:`[N_1, ..., Depth]`, where Depth is class number. The dtype can be "float32" and "float64". - prior_dist(Variable, optional): The prior distribution to be used to smooth - labels. If not provided, an uniform distribution - is used. It's a multidimensional tensor with a shape of - :math:`[1, class\_num]` . The default value is None. - epsilon(float, optional): The weight used to mix up the original ground-truth - distribution and the fixed distribution. The default value is - 0.1. - dtype(np.dtype|core.VarDesc.VarType|str, optional): The data type can be set - as 'float32', 'float64'. The default value is 'float32'. - name(str, optional): The default value is None. Normally there is no need for user - to set this property. For more information, please refer to - :ref:`api_guide_Name`. - - Returns: - Variable: The tensor variable containing the smoothed labels. - - Examples: - .. code-block:: python - - import paddle.fluid as fluid - import paddle.fluid.layers as layers - - label = layers.data(name="label", shape=[1], dtype="int32") - one_hot_label = layers.one_hot(input=label, depth=10) - smooth_label = layers.label_smooth( - label=one_hot_label, epsilon=0.1, dtype="float32") - """ - if in_dygraph_mode(): - return _C_ops.label_smooth(label, prior_dist, float(epsilon)) - - if epsilon > 1.0 or epsilon < 0.0: - raise ValueError("The value of epsilon must be between 0 and 1.") - - if _non_static_mode(): - return _legacy_C_ops.label_smooth( - label, prior_dist, 'epsilon', float(epsilon) - ) - - check_variable_and_dtype( - label, 'label', ['float32', 'float64'], 'label_smooth' - ) - - helper = LayerHelper("label_smooth", **locals()) - label.stop_gradient = True - smooth_label = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type="label_smooth", - inputs={"X": label, "PriorDist": prior_dist} - if prior_dist - else {"X": label}, - outputs={"Out": smooth_label}, - attrs={"epsilon": float(epsilon)}, - ) - return smooth_label - - @templatedoc() def roi_pool( input, diff --git a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_transformer.py b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_transformer.py index d3fd734d6a..07d43ccd55 100644 --- a/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_transformer.py +++ b/python/paddle/fluid/tests/unittests/collective/fleet/parallel_dygraph_transformer.py @@ -26,6 +26,7 @@ from paddle.fluid.dygraph import ( from paddle.optimizer.lr import NoamDecay from test_dist_base import runtime_main, TestParallelDyGraphRunnerBase +import paddle.nn.functional as F """ Note(chenweihang): To compare loss of single-card and multi-card @@ -934,7 +935,7 @@ class TransFormer(Layer): enc_output = self._wrap_encoder_layer(enc_inputs) predict = self._wrap_decoder_layer(dec_inputs, enc_output) if self._label_smooth_eps: - label_out = fluid.layers.label_smooth( + label_out = F.label_smooth( label=fluid.layers.one_hot( input=label, depth=self._trg_vocab_size ), diff --git a/python/paddle/fluid/tests/unittests/dist_transformer.py b/python/paddle/fluid/tests/unittests/dist_transformer.py index 24de04dc6f..8ed0eae0ff 100644 --- a/python/paddle/fluid/tests/unittests/dist_transformer.py +++ b/python/paddle/fluid/tests/unittests/dist_transformer.py @@ -27,6 +27,7 @@ import paddle import paddle.fluid as fluid import paddle.fluid.layers as layers from test_dist_base import TestDistRunnerBase, runtime_main, RUN_STEP +import paddle.nn.functional as F import paddle const_para_attr = fluid.ParamAttr(initializer=fluid.initializer.Constant(0.001)) @@ -1580,7 +1581,7 @@ def transformer( # cancel padding index in calculating the loss. label, weights = make_all_inputs(label_data_input_fields) if label_smooth_eps: - label = layers.label_smooth( + label = F.label_smooth( label=layers.one_hot(input=label, depth=trg_vocab_size), epsilon=label_smooth_eps, ) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py index 796329ab55..ee11e045d9 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py @@ -26,6 +26,7 @@ from paddle.fluid.dygraph import ( from paddle.fluid.dygraph.jit import dygraph_to_static_func from paddle.fluid.layers.utils import map_structure import paddle +import paddle.nn.functional as F def position_encoding_init(n_position, d_pos_vec): @@ -574,7 +575,7 @@ class CrossEntropyCriterion: def __call__(self, predict, label, weights): if self.label_smooth_eps: - label_out = layers.label_smooth( + label_out = F.label_smooth( label=layers.one_hot(input=label, depth=predict.shape[-1]), epsilon=self.label_smooth_eps, ) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py b/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py index 700ae9a9c8..f8671a76e2 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_transformer_sorted_gradient.py @@ -22,6 +22,7 @@ from test_imperative_base import new_program_scope from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard from paddle.fluid import core import numpy as np +import paddle.nn.functional as F np.set_printoptions(suppress=True) @@ -1088,7 +1089,7 @@ class TransFormer(Layer): enc_output = self._wrap_encoder_layer(enc_inputs) predict = self._wrap_decoder_layer(dec_inputs, enc_output) if self._label_smooth_eps: - label_out = fluid.layers.label_smooth( + label_out = F.label_smooth( label=fluid.layers.one_hot( input=label, depth=self._trg_vocab_size ), diff --git a/python/paddle/fluid/tests/unittests/test_label_smooth_functional.py b/python/paddle/fluid/tests/unittests/test_label_smooth_functional.py index 8fad7dc1ea..7f6e6d8434 100644 --- a/python/paddle/fluid/tests/unittests/test_label_smooth_functional.py +++ b/python/paddle/fluid/tests/unittests/test_label_smooth_functional.py @@ -48,11 +48,10 @@ class LabelSmoothTestCase(unittest.TestCase): label_var = fluid.data( "input", self.label_shape, dtype=self.dtype ) - y_var = fluid.layers.label_smooth( + y_var = F.label_smooth( label_var, prior_dist=self.prior_dist, epsilon=self.epsilon, - dtype=self.dtype, ) feed_dict = {"input": self.label} exe = fluid.Executor(place) diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index cc33db2385..60e0543287 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -33,6 +33,7 @@ from paddle.fluid.dygraph import nn from paddle.fluid.dygraph import base from paddle.fluid.dygraph import to_variable from paddle.fluid.framework import _test_eager_guard +import paddle.nn.functional as F class LayerTest(unittest.TestCase): @@ -3359,9 +3360,7 @@ class TestBook(LayerTest): with fluid.framework._dygraph_place_guard(place=fluid.CPUPlace()): label = self._get_data(name="label", shape=[1], dtype="int32") one_hot_label = layers.one_hot(input=label, depth=10) - smooth_label = layers.label_smooth( - label=one_hot_label, epsilon=0.1, dtype="int32" - ) + smooth_label = F.label_smooth(label=one_hot_label, epsilon=0.1) return smooth_label def make_topk(self): -- GitLab