diff --git a/python/paddle/compat.py b/python/paddle/compat.py index 1fa251a851fa923ca74fcc76d263de2bdc27c8b3..7c753815c5ccd32cfa65668b71b22e19b381b2b6 100644 --- a/python/paddle/compat.py +++ b/python/paddle/compat.py @@ -35,7 +35,8 @@ else: # str and bytes related functions def to_text(obj, encoding='utf-8', inplace=False): """ - All string in PaddlePaddle should be represented as a literal string. + All string in PaddlePaddle should be represented as a literal string. + This function will convert object to a literal string without any encoding. Especially, if the object type is a list or set container, we will iterate all items in the object and convert them to literal string. @@ -53,6 +54,17 @@ def to_text(obj, encoding='utf-8', inplace=False): Returns: Decoded result of obj + + Examples: + + .. code-block:: python + + import paddle + + data = "paddlepaddle" + data = paddle.compat.to_text(data) + # paddlepaddle + """ if obj is None: return obj @@ -119,7 +131,8 @@ def _to_text(obj, encoding): def to_bytes(obj, encoding='utf-8', inplace=False): """ - All string in PaddlePaddle should be represented as a literal string. + All string in PaddlePaddle should be represented as a literal string. + This function will convert object to a bytes with specific encoding. Especially, if the object type is a list or set container, we will iterate all items in the object and convert them to bytes. @@ -138,6 +151,17 @@ def to_bytes(obj, encoding='utf-8', inplace=False): Returns: Decoded result of obj + + Examples: + + .. code-block:: python + + import paddle + + data = "paddlepaddle" + data = paddle.compat.to_bytes(data) + # b'paddlepaddle' + """ if obj is None: return obj diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index 0f92c32f252cde08248d9d2b9592918ae4da1a3f..64038c78d30a49bfdd50898afdb43e13c9d49467 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -2979,11 +2979,7 @@ class GroupNorm(layers.Layer): class SpectralNorm(layers.Layer): - r""" - :alias_main: paddle.nn.SpectralNorm - :alias: paddle.nn.SpectralNorm,paddle.nn.layer.SpectralNorm,paddle.nn.layer.norm.SpectralNorm - :old_api: paddle.fluid.dygraph.SpectralNorm - + """ This interface is used to construct a callable object of the ``SpectralNorm`` class. For more details, refer to code examples. It implements the function of the Spectral Normalization Layer. This layer calculates the spectral normalization value of weight parameters of @@ -3031,13 +3027,13 @@ class SpectralNorm(layers.Layer): Examples: .. code-block:: python - import paddle.fluid as fluid - import numpy as np + import paddle + x = paddle.rand((2,8,32,32)) - with fluid.dygraph.guard(): - weight = np.random.random((2, 8, 32, 32)).astype('float32') - spectralNorm = fluid.dygraph.nn.SpectralNorm(weight.shape, dim=1, power_iters=2) - ret = spectralNorm(fluid.dygraph.base.to_variable(weight)) + spectral_norm = paddle.nn.SpectralNorm(x.shape, dim=1, power_iters=2) + spectral_norm_out = spectral_norm(x) + + print(spectral_norm_out.shape) # [2, 8, 32, 32] """ diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 5a21623e45c004e04dea7069065d6d61b3d5ed42..429b9b0b5afcfa40cde9ecca808e8ff82061354c 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -3554,7 +3554,7 @@ def group_norm(input, Refer to `Group Normalization `_ . Parameters: - input(Variable): 4-D Tensor, the data type is float32 or float64. + input(Tensor): 4-D Tensor, the data type is float32 or float64. groups(int): The number of groups that divided from channels, the data type is int32. epsilon(float, optional): The small value added to the variance to prevent @@ -3576,26 +3576,17 @@ def group_norm(input, property. For more information, please refer to :ref:`api_guide_Name` . Returns: - Variable: A 4-D Tensor has same data type and data format with `input`. - - Raises: - ValueError: If `data_layout` is neither 'NCHW' nor 'NHWC'. - ValueError: If `groups` is greater than the number of input channels. - ValueError: If `groups` is less than 1. - ShapeError: If the param_attr(Scale) is not 1-D Tensor. - ShapeError: If the param_attr(Scale)'s first dimension size is not equal to the input channels. - ShapeError: If the bias_attr(Bias) is not 1-D Tensor. - ShapeError: If the bias_attr(Bias)'s first dimension size is not equal to the input channels. + Tensor: A 4-D Tensor has same data type and data format with `input`. Examples: .. code-block:: python - import paddle.fluid as fluid import paddle paddle.enable_static() - data = fluid.data(name='data', shape=[None, 8, 32, 32], dtype='float32') - x = fluid.layers.group_norm(input=data, groups=4) + data = paddle.static.data(name='data', shape=[2, 8, 32, 32], dtype='float32') + x = paddle.static.nn.group_norm(input=data, groups=4) + print(x.shape) # [2, 8, 32, 32] """ helper = LayerHelper('group_norm', **locals()) dtype = helper.input_dtype() @@ -3685,7 +3676,7 @@ def spectral_norm(weight, dim=0, power_iters=1, eps=1e-12, name=None): Refer to `Spectral Normalization `_ . Args: - weight(${weight_type}): ${weight_comment} + weight(Tensor): ${weight_comment} dim(int): ${dim_comment} power_iters(int): ${power_iters_comment} eps(float): ${eps_comment} @@ -3694,7 +3685,7 @@ def spectral_norm(weight, dim=0, power_iters=1, eps=1e-12, name=None): None by default. Returns: - Variable: A tensor variable of weight parameters after spectral normalization. + Tensor: A tensor of weight parameters after spectral normalization. The data type and shape is same as input tensor. Examples: @@ -3703,8 +3694,9 @@ def spectral_norm(weight, dim=0, power_iters=1, eps=1e-12, name=None): import paddle paddle.enable_static() - weight = paddle.data(name='weight', shape=[2, 8, 32, 32], dtype='float32') + weight = paddle.static.data(name='weight', shape=[2, 8, 32, 32], dtype='float32') x = paddle.static.nn.spectral_norm(weight=weight, dim=1, power_iters=2) + print(x.shape) # [2, 8, 32, 32] """ helper = LayerHelper('spectral_norm', **locals()) check_variable_and_dtype(weight, 'weight', ['float32', 'float64'],