diff --git a/README.md b/README.md index 4437045721287418d87754ca9733c6363f70f6e6..5135d64d4026fcd64fe5a2cdea42afe4b319ea4e 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,8 @@ We provide [English](https://www.paddlepaddle.org.cn/documentation/docs/en/guide ## Courses -- [Server Deployments](https://aistudio.baidu.com/aistudio/course/introduce/19084): Courses intorducing high performance server deployments via local and remote services. -- [Edge Deployments](https://aistudio.baidu.com/aistudio/course/introduce/22690): Courses intorducing edge deployments from mobile, IoT to web and applets. +- [Server Deployments](https://aistudio.baidu.com/aistudio/course/introduce/19084): Courses introducing high performance server deployments via local and remote services. +- [Edge Deployments](https://aistudio.baidu.com/aistudio/course/introduce/22690): Courses introducing edge deployments from mobile, IoT to web and applets. ## Copyright and License PaddlePaddle is provided under the [Apache-2.0 license](LICENSE). diff --git a/README_cn.md b/README_cn.md index f4cb6f4fff78eb9a3ab3f3a2abd1f8fc80cd9e4e..da38b813f4da71dee88773192347b5ca51c8d7ca 100644 --- a/README_cn.md +++ b/README_cn.md @@ -88,7 +88,7 @@ PaddlePaddle用户可领取**免费Tesla V100在线算力资源**,训练模型 ## 课程 - [服务器部署](https://aistudio.baidu.com/aistudio/course/introduce/19084): 详细介绍高性能服务器端部署实操,包含本地端及服务化Serving部署等 -- [端侧部署](https://aistudio.baidu.com/aistudio/course/introduce/22690): 详细介绍端侧多场景部署实操,从移端端设备、IoT、网页到小程序部署 +- [端侧部署](https://aistudio.baidu.com/aistudio/course/introduce/22690): 详细介绍端侧多场景部署实操,从移动端设备、IoT、网页到小程序部署 ## 版权和许可证 PaddlePaddle由[Apache-2.0 license](LICENSE)提供 diff --git a/paddle/fluid/operators/activation_op.cc b/paddle/fluid/operators/activation_op.cc index 29e5e9c0acef4ad3dc9abfbea9847b2507e0eade..f3ff19b78c06e2575e4c82c0a52eeef29431544a 100644 --- a/paddle/fluid/operators/activation_op.cc +++ b/paddle/fluid/operators/activation_op.cc @@ -172,9 +172,9 @@ class ActivationOpGrad : public framework::OperatorWithKernel { }; UNUSED constexpr char SigmoidDoc[] = R"DOC( -Sigmoid Activation Operator +Sigmoid Activation -$$out = \\frac{1}{1 + e^{-x}}$$ +$$out = \frac{1}{1 + e^{-x}}$$ )DOC"; diff --git a/python/paddle/autograd/py_layer.py b/python/paddle/autograd/py_layer.py index 7e04d02e903762edc8542b5fe774b97c59334274..254aabb04b3cb136ae624c70916112a449cbfaf1 100644 --- a/python/paddle/autograd/py_layer.py +++ b/python/paddle/autograd/py_layer.py @@ -55,7 +55,7 @@ class LegacyPyLayerContext(object): """ Saves given tensors that backward need. Use ``saved_tensor`` in the `backward` to get the saved tensors. - .. note:: + Note: This API should be called at most once, and only inside `forward`. Args: @@ -341,7 +341,7 @@ class EagerPyLayerContext(object): """ Saves given tensors that backward need. Use ``saved_tensor`` in the `backward` to get the saved tensors. - .. note:: + Note: This API should be called at most once, and only inside `forward`. Args: diff --git a/python/paddle/device/cuda/__init__.py b/python/paddle/device/cuda/__init__.py index 4460e05d797c27762f88b9345d3c0577384a7757..316f9de61226543a7a9b8b50b92fbbff64f7b253 100644 --- a/python/paddle/device/cuda/__init__.py +++ b/python/paddle/device/cuda/__init__.py @@ -203,7 +203,7 @@ def max_memory_allocated(device=None): ''' Return the peak size of gpu memory that is allocated to tensor of the given device. - .. note:: + Note: The size of GPU memory allocated to tensor is 256-byte aligned in Paddle, which may larger than the memory size that tensor actually need. For instance, a float32 tensor with shape [1] in GPU will take up 256 bytes memory, even though storing a float32 data requires only 4 bytes. @@ -269,7 +269,7 @@ def memory_allocated(device=None): ''' Return the current size of gpu memory that is allocated to tensor of the given device. - .. note:: + Note: The size of GPU memory allocated to tensor is 256-byte aligned in Paddle, which may be larger than the memory size that tensor actually need. For instance, a float32 tensor with shape [1] in GPU will take up 256 bytes memory, even though storing a float32 data requires only 4 bytes. diff --git a/python/paddle/distributed/collective.py b/python/paddle/distributed/collective.py index 91805016105275cfc4641e543b7ea24a3312e8c6..d2e972c4d02792a443c250364a19b4bdde23afca 100644 --- a/python/paddle/distributed/collective.py +++ b/python/paddle/distributed/collective.py @@ -1349,7 +1349,7 @@ def alltoall_single( """ Scatter a single input tensor to all participators and gather the received tensors in out_tensor. - .. note:: + Note: ``alltoall_single`` is only supported in eager mode. Args: diff --git a/python/paddle/distributed/fleet/base/private_helper_function.py b/python/paddle/distributed/fleet/base/private_helper_function.py index 42cd25fb38adc5ed4bfc5544883580f2cfef3326..ff18a44ac0c42d1726e60796ffe73feecf0a5ab9 100644 --- a/python/paddle/distributed/fleet/base/private_helper_function.py +++ b/python/paddle/distributed/fleet/base/private_helper_function.py @@ -30,9 +30,9 @@ def wait_server_ready(endpoints): ["127.0.0.1:8080", "127.0.0.1:8081"] Examples: - .. code-block:: python + .. code-block:: python - wait_server_ready(["127.0.0.1:8080", "127.0.0.1:8081"]) + wait_server_ready(["127.0.0.1:8080", "127.0.0.1:8081"]) """ assert not isinstance(endpoints, str) while True: diff --git a/python/paddle/distributed/parallel.py b/python/paddle/distributed/parallel.py index b498ca6a31bc624ec73902cc81aa05b794f3ba55..35bac463b981db6122297eabc4de6207c3ce5b17 100644 --- a/python/paddle/distributed/parallel.py +++ b/python/paddle/distributed/parallel.py @@ -105,7 +105,7 @@ def init_parallel_env(): """ Initialize parallel training environment in dynamic graph mode. - .. note:: + Note: Now initialize both `NCCL` and `GLOO` contexts for communication. Args: diff --git a/python/paddle/distributed/sharding/group_sharded.py b/python/paddle/distributed/sharding/group_sharded.py index 7c19b9ca64d0983a36aa8b083a91c8a7bd11e20f..e0a5bb69f962a45a3f848dcb29c7acfeff675572 100644 --- a/python/paddle/distributed/sharding/group_sharded.py +++ b/python/paddle/distributed/sharding/group_sharded.py @@ -209,7 +209,7 @@ def save_group_sharded_model(model, output, optimizer=None): """ Group sharded encapsulated model and optimizer state saving module. - .. note:: + Note: If using save_group_sharded_model saves the model. When loading again, you need to set the model or optimizer state before using group_sharded_parallel. Args: diff --git a/python/paddle/distribution/distribution.py b/python/paddle/distribution/distribution.py index 6e36976e5ee03fbd535b6bd4734a4c33c62b1517..75934052da98ff2feaa18979cb3f26f151beb089 100644 --- a/python/paddle/distribution/distribution.py +++ b/python/paddle/distribution/distribution.py @@ -140,7 +140,7 @@ class Distribution(object): def probs(self, value): """Probability density/mass function. - .. note:: + Note: This method will be deprecated in the future, please use `prob` instead. diff --git a/python/paddle/distribution/kl.py b/python/paddle/distribution/kl.py index f6ab37c3a630d65f8e7e61b21706d3e22611f6d8..34b18dd06b00b1d2e8c555267f9e9be500a23fd3 100644 --- a/python/paddle/distribution/kl.py +++ b/python/paddle/distribution/kl.py @@ -38,11 +38,11 @@ def kl_divergence(p, q): KL(p||q) = \int p(x)log\frac{p(x)}{q(x)} \mathrm{d}x Args: - p (Distribution): ``Distribution`` object. - q (Distribution): ``Distribution`` object. + p (Distribution): ``Distribution`` object. Inherits from the Distribution Base class. + q (Distribution): ``Distribution`` object. Inherits from the Distribution Base class. Returns: - Tensor: Batchwise KL-divergence between distribution p and q. + Tensor, Batchwise KL-divergence between distribution p and q. Examples: @@ -71,8 +71,8 @@ def register_kl(cls_p, cls_q): implemention funciton by the decorator. Args: - cls_p(Distribution): Subclass derived from ``Distribution``. - cls_q(Distribution): Subclass derived from ``Distribution``. + cls_p (Distribution): The Distribution type of Instance p. Subclass derived from ``Distribution``. + cls_q (Distribution): The Distribution type of Instance q. Subclass derived from ``Distribution``. Examples: .. code-block:: python diff --git a/python/paddle/distribution/normal.py b/python/paddle/distribution/normal.py index 69b473c037eb0bb9a31e7ef40fb713f321065f45..91c795f334904169bfc06aa66b0d10f12999c634 100644 --- a/python/paddle/distribution/normal.py +++ b/python/paddle/distribution/normal.py @@ -47,7 +47,7 @@ class Normal(distribution.Distribution): .. math:: - pdf(x; \mu, \sigma) = \\frac{1}{Z}e^{\\frac {-0.5 (x - \mu)^2} {\sigma^2} } + pdf(x; \mu, \sigma) = \frac{1}{Z}e^{\frac {-0.5 (x - \mu)^2} {\sigma^2} } .. math:: @@ -60,43 +60,43 @@ class Normal(distribution.Distribution): * :math:`Z`: is the normalization constant. Args: - loc(int|float|list|tuple|numpy.ndarray|Tensor): The mean of normal distribution.The data type is int, float, list, numpy.ndarray or Tensor. - scale(int|float|list|tuple|numpy.ndarray|Tensor): The std of normal distribution.The data type is int, float, list, numpy.ndarray or Tensor. + loc(int|float|list|tuple|numpy.ndarray|Tensor): The mean of normal distribution.The data type is float32 and float64. + scale(int|float|list|tuple|numpy.ndarray|Tensor): The std of normal distribution.The data type is float32 and float64. name(str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Examples: .. code-block:: python - import paddle - from paddle.distribution import Normal - - # Define a single scalar Normal distribution. - dist = Normal(loc=0., scale=3.) - # Define a batch of two scalar valued Normals. - # The first has mean 1 and standard deviation 11, the second 2 and 22. - dist = Normal(loc=[1., 2.], scale=[11., 22.]) - # Get 3 samples, returning a 3 x 2 tensor. - dist.sample([3]) - - # Define a batch of two scalar valued Normals. - # Both have mean 1, but different standard deviations. - dist = Normal(loc=1., scale=[11., 22.]) - - # Complete example - value_tensor = paddle.to_tensor([0.8], dtype="float32") - - normal_a = Normal([0.], [1.]) - normal_b = Normal([0.5], [2.]) - sample = normal_a.sample([2]) - # a random tensor created by normal distribution with shape: [2, 1] - entropy = normal_a.entropy() - # [1.4189385] with shape: [1] - lp = normal_a.log_prob(value_tensor) - # [-1.2389386] with shape: [1] - p = normal_a.probs(value_tensor) - # [0.28969154] with shape: [1] - kl = normal_a.kl_divergence(normal_b) - # [0.34939718] with shape: [1] + import paddle + from paddle.distribution import Normal + + # Define a single scalar Normal distribution. + dist = Normal(loc=0., scale=3.) + # Define a batch of two scalar valued Normals. + # The first has mean 1 and standard deviation 11, the second 2 and 22. + dist = Normal(loc=[1., 2.], scale=[11., 22.]) + # Get 3 samples, returning a 3 x 2 tensor. + dist.sample([3]) + + # Define a batch of two scalar valued Normals. + # Both have mean 1, but different standard deviations. + dist = Normal(loc=1., scale=[11., 22.]) + + # Complete example + value_tensor = paddle.to_tensor([0.8], dtype="float32") + + normal_a = Normal([0.], [1.]) + normal_b = Normal([0.5], [2.]) + sample = normal_a.sample([2]) + # a random tensor created by normal distribution with shape: [2, 1] + entropy = normal_a.entropy() + # [1.4189385] with shape: [1] + lp = normal_a.log_prob(value_tensor) + # [-1.2389386] with shape: [1] + p = normal_a.probs(value_tensor) + # [0.28969154] with shape: [1] + kl = normal_a.kl_divergence(normal_b) + # [0.34939718] with shape: [1] """ def __init__(self, loc, scale, name=None): @@ -153,11 +153,11 @@ class Normal(distribution.Distribution): """Generate samples of the specified shape. Args: - shape (list): 1D `int32`. Shape of the generated samples. - seed (int): Python integer number. + shape (list): 1D `int32`. Shape of the generated samples. + seed (int): Python integer number. Returns: - Tensor: A tensor with prepended dimensions shape.The data type is float32. + Tensor, A tensor with prepended dimensions shape.The data type is float32. """ if not _non_static_mode(): @@ -198,14 +198,14 @@ class Normal(distribution.Distribution): .. math:: - entropy(\sigma) = 0.5 \\log (2 \pi e \sigma^2) + entropy(\sigma) = 0.5 \log (2 \pi e \sigma^2) In the above equation: * :math:`scale = \sigma`: is the std. Returns: - Tensor: Shannon entropy of normal distribution.The data type is float32. + Tensor, Shannon entropy of normal distribution.The data type is float32. """ name = self.name + '_entropy' @@ -244,10 +244,10 @@ class Normal(distribution.Distribution): """Probability density/mass function. Args: - value (Tensor): The input tensor. + value (Tensor): The input tensor. Returns: - Tensor: probability.The data type is same with value. + Tensor, probability. The data type is same with value. """ name = self.name + '_probs' @@ -269,11 +269,11 @@ class Normal(distribution.Distribution): .. math:: - KL\_divergence(\mu_0, \sigma_0; \mu_1, \sigma_1) = 0.5 (ratio^2 + (\\frac{diff}{\sigma_1})^2 - 1 - 2 \\ln {ratio}) + KL\_divergence(\mu_0, \sigma_0; \mu_1, \sigma_1) = 0.5 (ratio^2 + (\frac{diff}{\sigma_1})^2 - 1 - 2 \ln {ratio}) .. math:: - ratio = \\frac{\sigma_0}{\sigma_1} + ratio = \frac{\sigma_0}{\sigma_1} .. math:: @@ -292,7 +292,7 @@ class Normal(distribution.Distribution): other (Normal): instance of Normal. Returns: - Tensor: kl-divergence between two normal distributions.The data type is float32. + Tensor, kl-divergence between two normal distributions.The data type is float32. """ if not _non_static_mode(): diff --git a/python/paddle/distribution/transform.py b/python/paddle/distribution/transform.py index 7e2d7f447cb8c6908fdbf2019d69dc42961b4d61..986416e3c59555aaa7804972ed29b4ce9c4a0ff0 100644 --- a/python/paddle/distribution/transform.py +++ b/python/paddle/distribution/transform.py @@ -67,11 +67,11 @@ class Transform(object): used for transforming a random sample generated by ``Distribution`` instance. - Suppose :math:`X` is a K-dimensional random variable with probability - density function :math:`p_X(x)`. A new random variable :math:`Y = f(X)` may - be defined by transforming :math:`X` with a suitably well-behaved funciton - :math:`f`. It suffices for what follows to note that if f is one-to-one and - its inverse :math:`f^{-1}` have a well-defined Jacobian, then the density of + Suppose :math:`X` is a K-dimensional random variable with probability + density function :math:`p_X(x)`. A new random variable :math:`Y = f(X)` may + be defined by transforming :math:`X` with a suitably well-behaved funciton + :math:`f`. It suffices for what follows to note that if `f` is one-to-one and + its inverse :math:`f^{-1}` have a well-defined Jacobian, then the density of :math:`Y` is .. math:: @@ -1049,8 +1049,9 @@ class StackTransform(Transform): specific axis. Args: - transforms(Sequence[Transform]): The sequence of transformations. - axis(int): The axis along which will be transformed. + transforms (Sequence[Transform]): The sequence of transformations. + axis (int, optional): The axis along which will be transformed. default + value is 0. Examples: @@ -1058,7 +1059,6 @@ class StackTransform(Transform): import paddle - x = paddle.stack( (paddle.to_tensor([1., 2., 3.]), paddle.to_tensor([1, 2., 3.])), 1) t = paddle.distribution.StackTransform( @@ -1071,11 +1071,13 @@ class StackTransform(Transform): # [[2.71828175 , 1. ], # [7.38905621 , 4. ], # [20.08553696, 9. ]]) + print(t.inverse(t.forward(x))) # Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, # [[1., 1.], # [2., 2.], # [3., 3.]]) + print(t.forward_log_det_jacobian(x)) # Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, # [[1. , 0.69314718], diff --git a/python/paddle/distribution/uniform.py b/python/paddle/distribution/uniform.py index 769d60eeb23e5c3c121d330e2d8b4388289bbc72..aca3608ba6283d8da1f7e70072fb13761eaa6fa2 100644 --- a/python/paddle/distribution/uniform.py +++ b/python/paddle/distribution/uniform.py @@ -52,7 +52,7 @@ class Uniform(distribution.Distribution): .. math:: - pdf(x; a, b) = \\frac{1}{Z}, \ a <=x threshold.} + softplus(x)=\begin{cases} + \frac{1}{\beta} * \log(1 + e^{\beta * x}),&x\leqslant\frac{\varepsilon}{\beta};\\ + x,&x>\frac{\varepsilon}{\beta}. + \end{cases} Parameters: x (Tensor): The input Tensor with data type float32, float64. - beta (float, optional): The value of beta for softplus. Default is 1 - threshold (float, optional): The value of threshold for softplus. Default is 20 - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + beta (float, optional): The value of :math:`\beta` for softplus. Default is 1 + threshold (float, optional): The value of :math:`\varepsilon` for softplus. Default is 20 + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: A Tensor with the same data type and shape as ``x`` . @@ -1258,9 +1239,8 @@ def softplus(x, beta=1, threshold=20, name=None): import paddle import paddle.nn.functional as F - import numpy as np - x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3])) + x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3], dtype='float32') out = F.softplus(x) # [0.513015, 0.598139, 0.744397, 0.854355] """ @@ -1302,8 +1282,7 @@ def softshrink(x, threshold=0.5, name=None): Parameters: x (Tensor): The input Tensor with data type float32, float64. threshold (float, optional): The value of threshold(must be no less than zero) for softplus. Default is 0.5 - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: A Tensor with the same data type and shape as ``x`` . @@ -1356,8 +1335,7 @@ def softsign(x, name=None): Parameters: x (Tensor): The input Tensor with data type float32, float64. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: A Tensor with the same data type and shape as ``x`` . @@ -1398,8 +1376,7 @@ def swish(x, name=None): Parameters: x (Tensor): The input Tensor with data type float32, float64. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: A Tensor with the same data type and shape as ``x`` . @@ -1445,8 +1422,7 @@ def mish(x, name=None): Parameters: x (Tensor): The input Tensor with data type float32, float64. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: A Tensor with the same data type and shape as ``x`` . @@ -1482,8 +1458,7 @@ def tanhshrink(x, name=None): Args: x (Tensor): The input Tensor with data type float32, float64. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: A Tensor with the same data type and shape as ``x`` . @@ -1533,8 +1508,7 @@ def thresholded_relu(x, threshold=1.0, name=None): Parameters: x (Tensor): The input Tensor with data type float32, float64. threshold (float, optional): The value of threshold for thresholded_relu. Default is 1.0 - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: A Tensor with the same data type and shape as ``x`` . @@ -1596,8 +1570,7 @@ def log_softmax(x, axis=-1, dtype=None, name=None): preventing data type overflows. Supported dtype: float32, float64. If ``dtype`` is None, the output Tensor has the same dtype as x. Default is None. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: A Tensor with the same shape and data type (use ``dtype`` if it is @@ -1693,8 +1666,7 @@ def glu(x, axis=-1, name=None): should be in range [-D, D), where D is the dimensions of ``x`` . If ``axis`` < 0, it works the same way as :math:`axis + D` . Default is -1. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: A Tensor with the same data type as x. The size of the given aixs is @@ -1757,8 +1729,7 @@ def gumbel_softmax(x, temperature=1.0, hard=False, axis=-1, name=None): in autograd. Default is False. axis (int, optional): The axis along will be calculated softmax value. Default is -1. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Sampled tensor of same shape as ``x`` from the Gumbel-Softmax distribution. diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index cc969e2a807c1e7237374a141c4d61aa9d4d112b..747df9baf5d0bc4fb9a2c7b5da9784f9e441dbff 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -193,6 +193,7 @@ def interpolate( """ This API resizes a batch of images. + The input must be a 3-D Tensor of the shape (num_batches, channels, in_w) or 4-D (num_batches, channels, in_h, in_w), or a 5-D Tensor of the shape (num_batches, channels, in_d, in_h, in_w) or (num_batches, in_d, in_h, in_w, channels), @@ -201,12 +202,13 @@ def interpolate( and the resizing only applies on the three dimensions(depth, height and width). Supporting resample methods: - 'linear' : Linear interpolation - 'bilinear' : Bilinear interpolation - 'trilinear' : Trilinear interpolation - 'nearest' : Nearest neighbor interpolation - 'bicubic' : Bicubic interpolation - 'area': Area interpolation + + - 'linear' : Linear interpolation + - 'bilinear' : Bilinear interpolation + - 'trilinear' : Trilinear interpolation + - 'nearest' : Nearest neighbor interpolation + - 'bicubic' : Bicubic interpolation + - 'area': Area interpolation Linear interpolation is the method of using a line connecting two known quantities to determine the value of an unknown quantity between the two known quantities. @@ -243,13 +245,13 @@ def interpolate( .. code-block:: text - For scale_factor: + # For scale_factor: if align_corners = True && out_size > 1 : scale_factor = (in_size-1.0)/(out_size-1.0) else: scale_factor = float(in_size/out_size) - Linear interpolation: + # Linear interpolation: if: align_corners = False , align_mode = 0 input : (N,C,W_in) @@ -260,7 +262,7 @@ def interpolate( output: (N,C,W_out) where: W_out = W_{in} * scale_{factor} - Nearest neighbor interpolation: + # Nearest neighbor interpolation: align_corners = False input : (N,C,H_in,W_in) @@ -268,7 +270,7 @@ def interpolate( H_out = floor (H_{in} * scale_{factor}) W_out = floor (W_{in} * scale_{factor}) - Bilinear interpolation: + # Bilinear interpolation: if: align_corners = False , align_mode = 0 input : (N,C,H_in,W_in) @@ -281,7 +283,7 @@ def interpolate( H_out = H_{in} * scale_{factor} W_out = W_{in} * scale_{factor} - Bicubic interpolation: + # Bicubic interpolation: if: align_corners = False input : (N,C,H_in,W_in) @@ -294,7 +296,7 @@ def interpolate( H_out = H_{in} * scale_{factor} W_out = W_{in} * scale_{factor} - Trilinear interpolation: + # Trilinear interpolation: if: align_corners = False , align_mode = 0 input : (N,C,D_in,H_in,W_in) @@ -969,15 +971,16 @@ def dropout( training (bool, optional): A flag indicating whether it is in train phrase or not. Default True. mode(str, optional): ['upscale_in_train'(default) | 'downscale_in_infer']. - 1. upscale_in_train(default), upscale the output at training time + 1. upscale_in_train(default), upscale the output at training time + + - train: out = input * mask / ( 1.0 - dropout_prob ) + - inference: out = input - - train: out = input * mask / ( 1.0 - dropout_prob ) - - inference: out = input + 2. downscale_in_infer, downscale the output at inference - 2. downscale_in_infer, downscale the output at inference + - train: out = input * mask + - inference: out = input * (1.0 - dropout_prob) - - train: out = input * mask - - inference: out = input * (1.0 - dropout_prob) name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -1923,12 +1926,12 @@ def linear(x, weight, bias=None, name=None): def label_smooth(label, prior_dist=None, epsilon=0.1, name=None): r""" Label smoothing is a mechanism to regularize the classifier layer and is called - label-smoothing regularization (LSR). + 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 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 + 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. diff --git a/python/paddle/nn/functional/conv.py b/python/paddle/nn/functional/conv.py index 2e7b82462f0fac562cf52580a96e7e1b31027cbf..d62ddc6a1ed5882b095ebd8d6988b4c6e9a4df52 100644 --- a/python/paddle/nn/functional/conv.py +++ b/python/paddle/nn/functional/conv.py @@ -379,18 +379,6 @@ def conv1d( A tensor representing the conv1d, whose data type is the same with input. - Raises: - ValueError: If the channel dimension of the input is less than or equal to zero. - ValueError: If `data_format` is not "NCL" or "NLC". - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is a list/tuple, but the element corresponding to the input's batch size is not 0 - or the element corresponding to the input's channel is not 0. - ShapeError: If the input is not 3-D Tensor. - ShapeError: If the input's dimension size and filter's dimension size not equal. - ShapeError: If the dimension size of input minus the size of `stride` is not 1. - ShapeError: If the number of input channels is not equal to filter's channels * groups. - ShapeError: If the number of output channels is not be divided by groups. - Examples: .. code-block:: python @@ -672,18 +660,6 @@ def conv2d( Returns: A Tensor representing the conv2d result, whose data type is the same with input. - Raises: - ValueError: If `data_format` is not "NCHW" or "NHWC". - ValueError: If the channel dimension of the input is less than or equal to zero. - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is a list/tuple, but the element corresponding to the input's batch size is not 0 - or the element corresponding to the input's channel is not 0. - ShapeError: If the input is not 4-D Tensor. - ShapeError: If the input's dimension size and filter's dimension size not equal. - ShapeError: If the dimension size of input minus the size of `stride` is not 2. - ShapeError: If the number of input channels is not equal to filter's channels * groups. - ShapeError: If the number of output channels is not be divided by groups. - Examples: .. code-block:: python @@ -929,19 +905,6 @@ def conv1d_transpose( when data_format is `"NCL"` and (num_batches, length, channels) when data_format is `"NLC"`. - Raises: - ValueError: If `data_format` is a string, but not "NCL" or "NLC". - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is a list/tuple, but the element corresponding to the input's batch size is not 0 - or the element corresponding to the input's channel is not 0. - ValueError: If `output_size` and filter_size are None at the same time. - ValueError: If `output_padding` is greater than `stride`. - ShapeError: If the input is not 3-D Tensor. - ShapeError: If the input's dimension size and filter's dimension size not equal. - ShapeError: If the dimension size of input minus the size of `stride` is not 1. - ShapeError: If the number of input channels is not equal to filter's channels. - ShapeError: If the size of `output_size` is not equal to that of `stride`. - Examples: .. code-block:: python @@ -1255,18 +1218,6 @@ def conv2d_transpose( out_w) or (num_batches, out_h, out_w, channels). The tensor variable storing transposed convolution result. - Raises: - ValueError: If `data_format` is not "NCHW" or "NHWC". - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is a list/tuple, but the element corresponding to the input's batch size is not 0 - or the element corresponding to the input's channel is not 0. - ValueError: If `output_size` and kernel_size are None at the same time. - ShapeError: If the input is not 4-D Tensor. - ShapeError: If the input's dimension size and filter's dimension size not equal. - ShapeError: If the dimension size of input minus the size of `stride` is not 2. - ShapeError: If the number of input channels is not equal to filter's channels. - ShapeError: If the size of `output_size` is not equal to that of `stride`. - Examples: .. code-block:: python @@ -1771,18 +1722,6 @@ def conv3d_transpose( variable storing the transposed convolution result, and if act is not None, the tensor variable storing transposed convolution and non-linearity activation result. - Raises: - ValueError: If `data_format` is not "NCDHW" or "NDHWC". - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is a list/tuple, but the element corresponding to the input's batch size is not 0 - or the element corresponding to the input's channel is not 0. - ValueError: If `output_size` and kernel_size are None at the same time. - ShapeError: If the input is not 5-D Tensor. - ShapeError: If the input's dimension size and filter's dimension size not equal. - ShapeError: If the dimension size of input minus the size of `stride` is not 2. - ShapeError: If the number of input channels is not equal to filter's channels. - ShapeError: If the size of `output_size` is not equal to that of `stride`. - Examples: .. code-block:: python diff --git a/python/paddle/nn/functional/extension.py b/python/paddle/nn/functional/extension.py index 631516b7032f2cf89c5d4cc64d5d9e0d81202743..ce92e4aba200e8dbc2753da2755b395aea61663d 100644 --- a/python/paddle/nn/functional/extension.py +++ b/python/paddle/nn/functional/extension.py @@ -366,9 +366,6 @@ def temporal_shift(x, seg_num, shift_ratio=0.25, name=None, data_format="NCHW"): out(Tensor): The temporal shifting result is a tensor with the same shape and same data type as the input. - Raises: - TypeError: seg_num must be int type. - Examples: .. code-block:: python diff --git a/python/paddle/nn/functional/loss.py b/python/paddle/nn/functional/loss.py index 48f98959897d8e479160cc80cf2366fac6854618..f5b6965544fadf1a66b8041b3e31e8b94eee6931 100755 --- a/python/paddle/nn/functional/loss.py +++ b/python/paddle/nn/functional/loss.py @@ -939,15 +939,18 @@ def hsigmoid_loss( """ The hierarchical sigmoid organizes the classes into a complete binary tree to reduce the computational complexity and speed up the model training, especially the training of language model. + Each leaf node of the complete binary tree represents a class(word) and each non-leaf node acts as a binary classifier. For each class(word), there's a unique path from root to itself, hsigmoid calculate the cost for each non-leaf node on the path, and sum them to get a total cost. - Comparing to softmax, the OP can reduce the computational complexity from :math:`O(N)` to :math:`O(logN)`, where :math:`N` + + Comparing to softmax, hsigmoid can reduce the computational complexity from :math:`O(N)` to :math:`O(logN)`, where :math:`N` represents the number of classes or the size of word dict. - The OP supports default tree and custom tree. For the default tree, you can refer to `Hierarchical Probabilistic Neural - Network Language Model `_. For the custom - tree, you need to set :attr:`is_custom` to True, and do the following steps (take the language model as an example): + The API supports default tree and custom tree. For the default tree, you can refer to `Hierarchical Probabilistic Neural + Network Language Model `_. + + For the custom tree, you need to set :attr:`is_custom` to True, and do the following steps (take the language model as an example): 1. Using a custom word dict to build a binary tree, each leaf node should be an word in the word dict. 2. Creating a dict map word_id -> path that from the word to the root node, we call it path_table. @@ -1102,17 +1105,17 @@ def smooth_l1_loss(input, label, reduction='mean', delta=1.0, name=None): .. math:: - loss(x,y) = \frac{1}{n}\sum_{i}z_i + loss(x,y) = \frac{1}{n}\sum_{i}z_i - where z_i is given by: + where :math:`z_i` is given by: .. math:: \mathop{z_i} = \left\{\begin{array}{rcl} - 0.5(x_i - y_i)^2 & & {if |x_i - y_i| < delta} \\ - delta * |x_i - y_i| - 0.5 * delta^2 & & {otherwise} - \end{array} \right. + 0.5(x_i - y_i)^2 & & {if |x_i - y_i| < \delta} \\ + \delta * |x_i - y_i| - 0.5 * \delta^2 & & {otherwise} + \end{array} \right. Parameters: input (Tensor): Input tensor, the data type is float32 or float64. Shape is @@ -1126,12 +1129,11 @@ def smooth_l1_loss(input, label, reduction='mean', delta=1.0, name=None): If :attr:`reduction` is ``'sum'``, the reduced sum loss is returned. If :attr:`reduction` is ``'none'``, the unreduced loss is returned. Default is ``'mean'``. - delta (float, optional): Specifies the hyperparameter delta to be used. + delta (float, optional): Specifies the hyperparameter :math:`\delta` to be used. The value determines how large the errors need to be to use L1. Errors smaller than delta are minimized with L2. Parameter is ignored for negative/zero values. Default = 1.0 - name (str, optional): Name for the operation (optional, default is - None). For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Returns: Tensor, The tensor variable storing the smooth_l1_loss of input and label. @@ -1140,14 +1142,12 @@ def smooth_l1_loss(input, label, reduction='mean', delta=1.0, name=None): .. code-block:: python import paddle - import numpy as np - input_data = np.random.rand(3,3).astype("float32") - label_data = np.random.rand(3,3).astype("float32") - input = paddle.to_tensor(input_data) - label = paddle.to_tensor(label_data) + input = paddle.rand([3, 3]).astype('float32') + label = paddle.rand([3, 3]).astype('float32') output = paddle.nn.functional.smooth_l1_loss(input, label) print(output) + # [0.068004] """ check_variable_and_dtype( input, 'input', ['float32', 'float64'], 'smooth_l1_loss' @@ -1310,7 +1310,7 @@ def margin_ranking_loss( def l1_loss(input, label, reduction='mean', name=None): r""" - This operator computes the L1 Loss of Tensor ``input`` and ``label`` as follows. + Computes the L1 Loss of Tensor ``input`` and ``label`` as follows. If `reduction` set to ``'none'``, the loss is: @@ -1341,8 +1341,8 @@ def l1_loss(input, label, reduction='mean', name=None): Returns: Tensor, the L1 Loss of Tensor ``input`` and ``label``. - If `reduction` is ``'none'``, the shape of output loss is [N, *], the same as ``input`` . - If `reduction` is ``'mean'`` or ``'sum'``, the shape of output loss is [1]. + If `reduction` is ``'none'``, the shape of output loss is [N, *], the same as ``input`` . + If `reduction` is ``'mean'`` or ``'sum'``, the shape of output loss is [1]. Examples: .. code-block:: python @@ -1536,7 +1536,7 @@ def nll_loss( def kl_div(input, label, reduction='mean', name=None): r""" - This operator calculates the Kullback-Leibler divergence loss + Calculate the Kullback-Leibler divergence loss between Input(X) and Input(Target). Notes that Input(X) is the log-probability and Input(Target) is the probability. @@ -1581,31 +1581,26 @@ def kl_div(input, label, reduction='mean', name=None): .. code-block:: python import paddle - import numpy as np import paddle.nn.functional as F shape = (5, 20) - input = np.random.uniform(-10, 10, shape).astype('float32') - target = np.random.uniform(-10, 10, shape).astype('float32') + x = paddle.uniform(shape, min=-10, max=10).astype('float32') + target = paddle.uniform(shape, min=-10, max=10).astype('float32') # 'batchmean' reduction, loss shape will be [1] - pred_loss = F.kl_div(paddle.to_tensor(input), - paddle.to_tensor(target), reduction='batchmean') + pred_loss = F.kl_div(x, target, reduction='batchmean') # shape=[1] # 'mean' reduction, loss shape will be [1] - pred_loss = F.kl_div(paddle.to_tensor(input), - paddle.to_tensor(target), reduction='mean') + pred_loss = F.kl_div(x, target, reduction='mean') # shape=[1] # 'sum' reduction, loss shape will be [1] - pred_loss = F.kl_div(paddle.to_tensor(input), - paddle.to_tensor(target), reduction='sum') + pred_loss = F.kl_div(x, target, reduction='sum') # shape=[1] # 'none' reduction, loss shape is same with input shape - pred_loss = F.kl_div(paddle.to_tensor(input), - paddle.to_tensor(target), reduction='none') + pred_loss = F.kl_div(x, target, reduction='none') # shape=[5, 20] """ @@ -1862,9 +1857,7 @@ def margin_cross_entropy( .. hint:: The API supports single GPU and multi GPU, and don't supports CPU. - For data parallel mode, set ``group=False``. - For model parallel mode, set ``group=None`` or the group instance return by paddle.distributed.new_group. And logits.shape[-1] can be different at each rank. @@ -1876,7 +1869,7 @@ def margin_cross_entropy( margin2 (float, optional): m2 of margin loss, default value is `0.5`. margin3 (float, optional): m3 of margin loss, default value is `0.0`. scale (float, optional): s of margin loss, default value is `64.0`. - group (Group, optional): The group instance return by paddle.distributed.new_group + group (Group, optional): The group instance return by paddle.distributed.new_group or ``None`` for global default group or ``False`` for data parallel (do not communication cross ranks). Default is ``None``. return_softmax (bool, optional): Whether return softmax probability. Default value is `False`. @@ -1887,12 +1880,12 @@ def margin_cross_entropy( Default value is `'mean'`. Returns: - ``Tensor`` or Tuple of two ``Tensor`` : Return the cross entropy loss if \ - `return_softmax` is False, otherwise the tuple \ - (loss, softmax), softmax is shard_softmax when \ - using model parallel, otherwise softmax is in \ - the same shape with input logits. If ``reduction == None``, \ - the shape of loss is ``[N, 1]``, otherwise the shape is ``[1]``. + Tensor|tuple[Tensor, Tensor], return the cross entropy loss if + `return_softmax` is False, otherwise the tuple (loss, softmax), + softmax is shard_softmax when using model parallel, otherwise + softmax is in the same shape with input logits. If + ``reduction == None``, the shape of loss is ``[N, 1]``, otherwise + the shape is ``[1]``. Examples: @@ -1932,7 +1925,7 @@ def margin_cross_entropy( print(label) print(loss) print(softmax) - + #Tensor(shape=[2, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, # [[ 0.85204151, -0.55557678, 0.04994566, 0.71986042], # [-0.20198586, -0.35270476, -0.55182702, 0.09749021]]) @@ -1993,7 +1986,7 @@ def margin_cross_entropy( print(loss) print(softmax) - # python -m paddle.distributed.launch --gpus=0,1 test_margin_cross_entropy.py + # python -m paddle.distributed.launch --gpus=0,1 test_margin_cross_entropy.py ## for rank0 input #Tensor(shape=[4, 4], dtype=float64, place=CUDAPlace(0), stop_gradient=True, # [[ 0.32888934, 0.02408748, -0.02763289, 0.18173063], @@ -3245,6 +3238,19 @@ def multi_label_soft_margin_loss( input, label, weight=None, reduction="mean", name=None ): r""" + Calculate a multi-class multi-classification + hinge loss (margin-based loss) between input :math:`x` (a 2D mini-batch `Tensor`) + and output :math:`y` (which is a 2D `Tensor` of target class indices). + For each sample in the mini-batch: + + .. math:: + \text{loss}(x, y) = \sum_{ij}\frac{\max(0, 1 - (x[y[j]] - x[i]))}{\text{x.size}(0)} + + where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`, \ + :math:`y \in \left\{0, \; \cdots , \; \text{y.size}(0) - 1\right\}`, \ + :math:`0 \leq y[j] \leq \text{x.size}(0)-1`, \ + and :math:`i \neq y[j]` for all :math:`i` and :math:`j`. + :math:`y` and :math:`x` must have the same size. Parameters: input (Tensor): Input tensor, the data type is float32 or float64. Shape is (N, C), where C is number of classes, and if shape is more than 2D, this is (N, C, D1, D2,..., Dk), k >= 1. @@ -3338,7 +3344,7 @@ def multi_label_soft_margin_loss( def hinge_embedding_loss(input, label, margin=1.0, reduction='mean', name=None): r""" - This operator calculates hinge_embedding_loss. Measures the loss given an input tensor :math:`x` and a labels tensor :math:`y`(containing 1 or -1). + Calculates hinge_embedding_loss. Measures the loss given an input tensor :math:`x` and a labels tensor :math:`y`(containing 1 or -1). This is usually used for measuring whether two inputs are similar or dissimilar, e.g. using the L1 pairwise distance as :math:`x`, and is typically used for learning nonlinear embeddings or semi-supervised learning. diff --git a/python/paddle/nn/functional/norm.py b/python/paddle/nn/functional/norm.py index 2c69e475be4c838575df84beeeb6a6e5d380dcff..ac5829ea0dbdea7e4b243cced47c987c103ff902 100644 --- a/python/paddle/nn/functional/norm.py +++ b/python/paddle/nn/functional/norm.py @@ -36,7 +36,7 @@ __all__ = [] def normalize(x, p=2, axis=1, epsilon=1e-12, name=None): r""" - This op normalizes ``x`` along dimension ``axis`` using :math:`L_p` norm. This layer computes + Normalize ``x`` along dimension ``axis`` using :math:`L_p` norm. This layer computes .. math:: @@ -50,7 +50,7 @@ def normalize(x, p=2, axis=1, epsilon=1e-12, name=None): Parameters: x (Tensor): The input tensor could be N-D tensor, and the input data type could be float32 or float64. - p (float|int, optional): The exponent value in the norm formulation. Default: 2 + p (float|int, optional): The exponent value in the norm formulation. Default: 2. axis (int, optional): The axis on which to apply normalization. If `axis < 0`, the dimension to normalization is `x.ndim + axis`. -1 is the last dimension. epsilon (float, optional): Small float added to denominator to avoid dividing by zero. Default is 1e-12. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. @@ -357,11 +357,8 @@ def layer_norm( .. code-block:: python import paddle - import numpy as np - np.random.seed(123) - x_data = np.random.random(size=(2, 2, 2, 3)).astype('float32') - x = paddle.to_tensor(x_data) + x = paddle.rand((2, 2, 2, 3)) layer_norm_out = paddle.nn.functional.layer_norm(x, x.shape[1:]) print(layer_norm_out) """ @@ -468,14 +465,14 @@ def instance_norm( Parameters: x(Tensor): Input Tensor. It's data type should be float32, float64. - running_mean(Tensor): running mean. Default None. - running_var(Tensor): running variance. Default None. + running_mean(Tensor, optional): running mean. Default None. + running_var(Tensor, optional): running variance. Default None. weight(Tensor, optional): The weight tensor of instance_norm. Default: None. bias(Tensor, optional): The bias tensor of instance_norm. Default: None. eps(float, optional): A value added to the denominator for numerical stability. Default is 1e-5. momentum(float, optional): The value used for the moving_mean and moving_var computation. Default: 0.9. - use_input_stats(bool): Default True. - data_format(str, optional): Specify the input data format, may be "NC", "NCL", "NCHW" or "NCDHW". Default "NCHW". + use_input_stats(bool, optional): Default True. + data_format(str, optional): Specify the input data format, may be "NC", "NCL", "NCHW" or "NCDHW". Defalut "NCHW". name(str, optional): Name for the InstanceNorm, default is None. For more information, please refer to :ref:`api_guide_Name`.. Returns: @@ -486,11 +483,8 @@ def instance_norm( .. code-block:: python import paddle - import numpy as np - np.random.seed(123) - x_data = np.random.random(size=(2, 2, 2, 3)).astype('float32') - x = paddle.to_tensor(x_data) + x = paddle.rand((2, 2, 2, 3)) instance_norm_out = paddle.nn.functional.instance_norm(x) print(instance_norm_out) diff --git a/python/paddle/nn/functional/pooling.py b/python/paddle/nn/functional/pooling.py index 56ad4692715a85d195fbd8c46a3c24d5a43f3bcd..c64a79a43467d3c5543159d8eb7ae77754731d32 100755 --- a/python/paddle/nn/functional/pooling.py +++ b/python/paddle/nn/functional/pooling.py @@ -665,12 +665,6 @@ def max_pool1d( Returns: Tensor: The output tensor of pooling result. The data type is same as input tensor. - Raises: - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is "VALID", but `ceil_mode` is True. - ShapeError: If the input is not a 3-D tensor. - ShapeError: If the output's shape calculated is not greater than 0. - Examples: .. code-block:: python @@ -1313,11 +1307,6 @@ def max_pool2d( Returns: Tensor: The output tensor of pooling result. The data type is same as input tensor. - Raises: - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is "VALID", but `ceil_mode` is True. - ShapeError: If the output's shape calculated is not greater than 0. - Examples: .. code-block:: python @@ -1507,11 +1496,6 @@ def max_pool3d( Returns: Tensor: The output tensor of pooling result. The data type is same as input tensor. - Raises: - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is "VALID", but `ceil_mode` is True. - ShapeError: If the output's shape calculated is not greater than 0. - Examples: .. code-block:: python @@ -2053,8 +2037,7 @@ def adaptive_max_pool1d(x, output_size, return_mask=False, name=None): Returns: Tensor: The output tensor of adaptive pooling result. The data type is same as input tensor. - Raises: - ValueError: 'output_size' should be an integer. + Examples: .. code-block:: python diff --git a/python/paddle/nn/functional/vision.py b/python/paddle/nn/functional/vision.py index 7b0142044164989cee5d91bc7c5f0d7701cada62..c6c425a3a295fd65e1c37915850ac52c218a2138 100644 --- a/python/paddle/nn/functional/vision.py +++ b/python/paddle/nn/functional/vision.py @@ -37,7 +37,7 @@ def affine_grid(theta, out_shape, align_corners=True, name=None): Args: theta (Tensor) - A tensor with shape [N, 2, 3] or [N, 3, 4]. It contains a batch of affine transform parameters. The data type can be float32 or float64. - out_shape (Tensor | list | tuple): Type can be a 1-D Tensor, list, or tuple. It is used to represent the shape of the output in an affine transformation, in the format ``[N, C, H, W]`` or ``[N, C, D, H, W]``. + out_shape (Tensor | list | tuple): Type can be a 1-D Tensor, list, or tuple. It is used to represent the shape of the output in an affine transformation, in the format ``[N, C, H, W]`` or ``[N, C, D, H, W]``. When the format is ``[N, C, H, W]``, it represents the batch size, number of channels, height and width. When the format is ``[N, C, D, H, W]``, it represents the batch size, number of channels, depth, height and width. The data type must be int32. align_corners(bool, optional): if True, aligns the centers of the 4 (4D) or 8 (5D) corner pixels of the input and output tensors, and preserves the value of the corner pixels. Default: True @@ -60,7 +60,7 @@ def affine_grid(theta, out_shape, align_corners=True, name=None): [1, 2, 3, 3], align_corners=False) print(y_t) - + #[[[[ 1.0333333 0.76666665] # [ 0.76666665 1.0999999 ] # [ 0.5 1.4333333 ]] @@ -84,62 +84,82 @@ def affine_grid(theta, out_shape, align_corners=True, name=None): if theta.shape[1] == 3: use_cudnn = False if is_compiled_with_rocm(): - use_cudnn = False # ROCM platform do not have MIOPEN kernel for affine_grid + use_cudnn = ( + False # ROCM platform do not have MIOPEN kernel for affine_grid + ) if in_dygraph_mode(): - _out_shape = out_shape.numpy().tolist() if isinstance( - out_shape, Variable) else out_shape + _out_shape = ( + out_shape.numpy().tolist() + if isinstance(out_shape, Variable) + else out_shape + ) return _C_ops.affine_grid(theta, _out_shape, use_cudnn, align_corners) elif in_dynamic_mode(): - _out_shape = out_shape.numpy().tolist() if isinstance( - out_shape, Variable) else out_shape - return _legacy_C_ops.affine_grid(theta, "output_shape", _out_shape, - "align_corners", align_corners, - "use_cudnn", use_cudnn) + _out_shape = ( + out_shape.numpy().tolist() + if isinstance(out_shape, Variable) + else out_shape + ) + return _legacy_C_ops.affine_grid( + theta, + "output_shape", + _out_shape, + "align_corners", + align_corners, + "use_cudnn", + use_cudnn, + ) helper = LayerHelper('affine_grid') - check_variable_and_dtype(theta, 'theta', ['float32', 'float64'], - 'affine_grid') + check_variable_and_dtype( + theta, 'theta', ['float32', 'float64'], 'affine_grid' + ) out = helper.create_variable_for_type_inference(theta.dtype) ipts = {'Theta': theta} attrs = {"align_corners": align_corners, "use_cudnn": use_cudnn} if isinstance(out_shape, Variable): ipts['OutputShape'] = out_shape - check_variable_and_dtype(out_shape, 'out_shape', ['int32'], - 'affine_grid') + check_variable_and_dtype( + out_shape, 'out_shape', ['int32'], 'affine_grid' + ) else: attrs['output_shape'] = out_shape - helper.append_op(type='affine_grid', - inputs=ipts, - outputs={'Output': out}, - attrs=None if len(attrs) == 0 else attrs) + helper.append_op( + type='affine_grid', + inputs=ipts, + outputs={'Output': out}, + attrs=None if len(attrs) == 0 else attrs, + ) return out -def grid_sample(x, - grid, - mode='bilinear', - padding_mode='zeros', - align_corners=True, - name=None): +def grid_sample( + x, + grid, + mode='bilinear', + padding_mode='zeros', + align_corners=True, + name=None, +): """ - This operation samples input X by using bilinear interpolation or + Sample input X by using bilinear interpolation or nearest interpolation based on flow field grid, which is usually - generated by :code:`affine_grid` . When the input X is 4-D Tensor, - the grid of shape [N, H, W, 2] is the concatenation of (x, y) - coordinates with shape [N, H, W] each, where x is indexing the 4th - dimension (in width dimension) of input data x and y is indexing - the 3rd dimension (in height dimension), finally results is the + generated by :code:`affine_grid` . When the input X is 4-D Tensor, + the grid of shape [N, H, W, 2] is the concatenation of (x, y) + coordinates with shape [N, H, W] each, where x is indexing the 4th + dimension (in width dimension) of input data x and y is indexing + the 3rd dimension (in height dimension), finally results is the bilinear interpolation or nearest value of 4 nearest corner - points. The output tensor shape will be [N, C, H, W]. When the input X - is 5-D Tensor, the grid of shape [N, D, H, W, 3] is the concatenation - of (x, y, z) coordinates with shape [N, D, H, W] each, where x is - indexing the 5th dimension (in width dimension) of input data x, y is - indexing the 4th dimension (in height dimension) and z is indexing the - 3rd dimension (in depth dimension) finally results is the bilinear - interpolation or nearest value of 8 nearest cornerpoints. The output - tensor shape will be [N, C, D, H, W]. + points. The output tensor shape will be [N, C, H, W]. When the input X + is 5-D Tensor, the grid of shape [N, D, H, W, 3] is the concatenation + of (x, y, z) coordinates with shape [N, D, H, W] each, where x is + indexing the 5th dimension (in width dimension) of input data x, y is + indexing the 4th dimension (in height dimension) and z is indexing the + 3rd dimension (in depth dimension) finally results is the bilinear + interpolation or nearest value of 8 nearest cornerpoints. The output + tensor shape will be [N, C, D, H, W]. @@ -153,7 +173,7 @@ def grid_sample(x, grid_y = 0.5 * (grid[:, :, :, 1] + 1) * (H - 1) Step 2: - + Indices input data X with grid (x, y) in each [H, W] area, and bilinear interpolate point value by 4 nearest points or nearest interpolate point value by nearest point. @@ -189,12 +209,12 @@ def grid_sample(x, Args: x(Tensor): The input tensor, which is a 4-d tensor with shape - [N, C, H, W] or a 5-d tensor with shape [N, C, D, H, W], - N is the batch size, C is the channel number, + [N, C, H, W] or a 5-d tensor with shape [N, C, D, H, W], + N is the batch size, C is the channel number, D, H and W is the feature depth, height and width. The data type is float32 or float64. - grid(Tensor): Input grid tensor, which is a 4-d tensor with shape [N, grid_H, - grid_W, 2] or a 5-d tensor with shape [N, grid_D, grid_H, + grid(Tensor): Input grid tensor, which is a 4-d tensor with shape [N, grid_H, + grid_W, 2] or a 5-d tensor with shape [N, grid_D, grid_H, grid_W, 3]. The data type is float32 or float64. mode(str, optional): The interpolation method which can be 'bilinear' or 'nearest'. Default: 'bilinear'. @@ -209,17 +229,18 @@ def grid_sample(x, None by default. Returns: - Tensor, The shape of output is [N, C, grid_H, grid_W] or [N, C, grid_D, grid_H, grid_W] in which `grid_D` is the depth of grid, + + Tensor, The shape of output is [N, C, grid_H, grid_W] or [N, C, grid_D, grid_H, grid_W] in which `grid_D` is the depth of grid, `grid_H` is the height of grid and `grid_W` is the width of grid. The data type is same as input tensor. Examples: .. code-block:: python - + import paddle import paddle.nn.functional as F - - # x shape=[1, 1, 3, 3] + + # x shape=[1, 1, 3, 3] x = paddle.to_tensor([[[[-0.6, 0.8, -0.5], [-0.5, 0.2, 1.2], [ 1.4, 0.3, -0.2]]]],dtype='float64') @@ -243,7 +264,7 @@ def grid_sample(x, padding_mode='border', align_corners=True) print(y_t) - + # output shape = [1, 1, 3, 4] # [[[[ 0.34 0.016 0.086 -0.448] # [ 0.55 -0.076 0.35 0.59 ] @@ -254,22 +275,33 @@ def grid_sample(x, _padding_modes = ['zeros', 'reflection', 'border'] if mode not in _modes: raise ValueError( - "The mode of grid sample function should be in {}, but got: {}". - format(_modes, mode)) + "The mode of grid sample function should be in {}, but got: {}".format( + _modes, mode + ) + ) if padding_mode not in _padding_modes: raise ValueError( - "The padding mode of grid sample function should be in {}, but got: {}" - .format(_padding_modes, padding_mode)) + "The padding mode of grid sample function should be in {}, but got: {}".format( + _padding_modes, padding_mode + ) + ) if not isinstance(align_corners, bool): - raise ValueError("The align corners should be bool, but got: {}".format( - align_corners)) + raise ValueError( + "The align corners should be bool, but got: {}".format( + align_corners + ) + ) cudnn_version = get_cudnn_version() use_cudnn = False - if not is_compiled_with_rocm() and ( - cudnn_version is not None - ) and align_corners and mode == 'bilinear' and padding_mode == 'zeros': + if ( + not is_compiled_with_rocm() + and (cudnn_version is not None) + and align_corners + and mode == 'bilinear' + and padding_mode == 'zeros' + ): use_cudnn = True # CUDNN always computes gradients for all inputs x.stop_gradient = False @@ -281,26 +313,37 @@ def grid_sample(x, if in_dygraph_mode(): return _C_ops.grid_sample(x, grid, mode, padding_mode, align_corners) elif in_dynamic_mode(): - attrs = ('mode', mode, 'padding_mode', padding_mode, 'align_corners', - align_corners, 'use_cudnn', use_cudnn) + attrs = ( + 'mode', + mode, + 'padding_mode', + padding_mode, + 'align_corners', + align_corners, + 'use_cudnn', + use_cudnn, + ) out = getattr(_legacy_C_ops, 'grid_sampler')(x, grid, *attrs) else: helper = LayerHelper("grid_sample", **locals()) check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'grid_sample') - check_variable_and_dtype(grid, 'grid', ['float32', 'float64'], - 'grid_sample') + check_variable_and_dtype( + grid, 'grid', ['float32', 'float64'], 'grid_sample' + ) ipts = {'X': x, 'Grid': grid} attrs = { 'mode': mode, 'padding_mode': padding_mode, 'align_corners': align_corners, - 'use_cudnn': use_cudnn + 'use_cudnn': use_cudnn, } out = helper.create_variable_for_type_inference(x.dtype) - helper.append_op(type='grid_sampler', - inputs=ipts, - attrs=attrs, - outputs={'Output': out}) + helper.append_op( + type='grid_sampler', + inputs=ipts, + attrs=attrs, + outputs={'Output': out}, + ) return out @@ -337,24 +380,25 @@ def pixel_shuffle(x, upscale_factor, data_format="NCHW", name=None): if data_format not in ["NCHW", "NHWC"]: raise ValueError( "Attr(data_format) should be 'NCHW' or 'NHWC'." - "But recevie Attr(data_format): {} ".format(data_format)) + "But recevie Attr(data_format): {} ".format(data_format) + ) if in_dygraph_mode(): return _C_ops.pixel_shuffle(x, upscale_factor, data_format) if _in_legacy_dygraph(): - return _legacy_C_ops.pixel_shuffle(x, "upscale_factor", upscale_factor, - "data_format", data_format) + return _legacy_C_ops.pixel_shuffle( + x, "upscale_factor", upscale_factor, "data_format", data_format + ) helper = LayerHelper("pixel_shuffle", **locals()) check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'pixel_shuffle') out = helper.create_variable_for_type_inference(dtype=x.dtype) - helper.append_op(type="pixel_shuffle", - inputs={"X": x}, - outputs={"Out": out}, - attrs={ - "upscale_factor": upscale_factor, - "data_format": data_format - }) + helper.append_op( + type="pixel_shuffle", + inputs={"X": x}, + outputs={"Out": out}, + attrs={"upscale_factor": upscale_factor, "data_format": data_format}, + ) return out @@ -384,8 +428,10 @@ def pixel_unshuffle(x, downscale_factor, data_format="NCHW", name=None): """ if len(x.shape) != 4: raise ValueError( - "Input x should be 4D tensor, but received x with the shape of {}". - format(x.shape)) + "Input x should be 4D tensor, but received x with the shape of {}".format( + x.shape + ) + ) if not isinstance(downscale_factor, int): raise TypeError("Downscale factor must be int type") @@ -396,23 +442,26 @@ def pixel_unshuffle(x, downscale_factor, data_format="NCHW", name=None): if data_format not in ["NCHW", "NHWC"]: raise ValueError( "Attr(data_format) should be 'NCHW' or 'NHWC'." - "But recevie Attr(data_format): {} ".format(data_format)) + "But recevie Attr(data_format): {} ".format(data_format) + ) if _non_static_mode(): - return _legacy_C_ops.pixel_unshuffle(x, "downscale_factor", - downscale_factor, "data_format", - data_format) + return _legacy_C_ops.pixel_unshuffle( + x, "downscale_factor", downscale_factor, "data_format", data_format + ) helper = LayerHelper("pixel_unshuffle", **locals()) check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'pixel_unshuffle') out = helper.create_variable_for_type_inference(dtype=x.dtype) - helper.append_op(type="pixel_unshuffle", - inputs={"X": x}, - outputs={"Out": out}, - attrs={ - "downscale_factor": downscale_factor, - "data_format": data_format - }) + helper.append_op( + type="pixel_unshuffle", + inputs={"X": x}, + outputs={"Out": out}, + attrs={ + "downscale_factor": downscale_factor, + "data_format": data_format, + }, + ) return out @@ -453,8 +502,10 @@ def channel_shuffle(x, groups, data_format="NCHW", name=None): """ if len(x.shape) != 4: raise ValueError( - "Input x should be 4D tensor, but received x with the shape of {}". - format(x.shape)) + "Input x should be 4D tensor, but received x with the shape of {}".format( + x.shape + ) + ) if not isinstance(groups, int): raise TypeError("groups must be int type") @@ -465,20 +516,21 @@ def channel_shuffle(x, groups, data_format="NCHW", name=None): if data_format not in ["NCHW", "NHWC"]: raise ValueError( "Attr(data_format) should be 'NCHW' or 'NHWC'." - "But recevie Attr(data_format): {} ".format(data_format)) + "But recevie Attr(data_format): {} ".format(data_format) + ) if _non_static_mode(): - return _legacy_C_ops.channel_shuffle(x, "groups", groups, "data_format", - data_format) + return _legacy_C_ops.channel_shuffle( + x, "groups", groups, "data_format", data_format + ) helper = LayerHelper("channel_shuffle", **locals()) check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'channel_shuffle') out = helper.create_variable_for_type_inference(dtype=x.dtype) - helper.append_op(type="channel_shuffle", - inputs={"X": x}, - outputs={"Out": out}, - attrs={ - "groups": groups, - "data_format": data_format - }) + helper.append_op( + type="channel_shuffle", + inputs={"X": x}, + outputs={"Out": out}, + attrs={"groups": groups, "data_format": data_format}, + ) return out diff --git a/python/paddle/nn/layer/activation.py b/python/paddle/nn/layer/activation.py index 25ba9c6edfcf860ee68c75cf7531d408718e8a25..27e18e73842c39ffdda91c540c781ee9e676cd05 100644 --- a/python/paddle/nn/layer/activation.py +++ b/python/paddle/nn/layer/activation.py @@ -215,9 +215,8 @@ class Hardshrink(Layer): class Hardswish(Layer): r""" - Hardswish activation - - Hardswish is proposed in MobileNetV3, and performs better in computational stability + Hardswish activation. Create a callable object of `Hardswish`. Hardswish + is proposed in MobileNetV3, and performs better in computational stability and efficiency compared to swish function. For more details please refer to: https://arxiv.org/pdf/1905.02244.pdf @@ -307,7 +306,7 @@ class Tanh(Layer): class Hardtanh(Layer): r""" - Hardtanh Activation + Hardtanh Activation. Create a callable object of `Hardtanh`. .. math:: @@ -669,7 +668,8 @@ class SELU(Layer): class LeakyReLU(Layer): r""" - Leaky ReLU Activation. + Leaky ReLU Activation. Create a callable object of `LeakyReLU` to calculate + the `LeakyReLU` of input `x`. .. math:: @@ -696,10 +696,9 @@ class LeakyReLU(Layer): .. code-block:: python import paddle - import numpy as np m = paddle.nn.LeakyReLU() - x = paddle.to_tensor(np.array([-2, 0, 1], 'float32')) + x = paddle.to_tensor([-2.0, 0, 1]) out = m(x) # [-0.02, 0., 1.] """ @@ -717,15 +716,15 @@ class LeakyReLU(Layer): class Sigmoid(Layer): - """ + r""" this interface is used to construct a callable object of the ``Sigmoid`` class. This layer calcluate the `sigmoid` of input x. .. math:: - Sigmoid(x) = \\frac{1}{1 + e^{-x}} + sigmoid(x) = \frac{1}{1 + e^{-x}} Parameters: - name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Shape: x: N-D tensor, available dtype is float16, float32, float64. @@ -737,11 +736,11 @@ class Sigmoid(Layer): .. code-block:: python - import paddle + import paddle - m = paddle.nn.Sigmoid() - x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0]) - out = m(x) # [0.7310586, 0.880797, 0.95257413, 0.98201376] + m = paddle.nn.Sigmoid() + x = paddle.to_tensor([1.0, 2.0, 3.0, 4.0]) + out = m(x) # [0.7310586, 0.880797, 0.95257413, 0.98201376] """ def __init__(self, name=None): @@ -758,8 +757,8 @@ class Sigmoid(Layer): class Hardsigmoid(Layer): r""" - This interface is used to construct a callable object of the ``Hardsigmoid`` class. - This layer calcluate the `hardsigmoid` of input x. + ``Hardsigmoid`` Activiation Layers, Construct a callable object of + the ``Hardsigmoid`` class. This layer calcluate the `hardsigmoid` of input x. A 3-part piecewise linear approximation of sigmoid(https://arxiv.org/abs/1603.00391), which is much faster than sigmoid. @@ -775,7 +774,6 @@ class Hardsigmoid(Layer): \end{array} \right. - Parameters: name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. @@ -813,15 +811,15 @@ class Softplus(Layer): Softplus Activation .. math:: - - Softplus(x) = \frac{1}{beta} * \log(1 + e^{beta * x}) \\ - \text{For numerical stability, the implementation reverts to the linear function when: beta * x > threshold.} + softplus(x)=\begin{cases} + \frac{1}{\beta} * \log(1 + e^{\beta * x}),&x\leqslant\frac{\varepsilon}{\beta};\\ + x,&x>\frac{\varepsilon}{\beta}. + \end{cases} Parameters: - beta (float, optional): The value of beta for Softplus. Default is 1 - threshold (float, optional): The value of threshold for Softplus. Default is 20 - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + beta (float, optional): The value of :math:`\beta` for Softplus. Default is 1 + threshold (float, optional): The value of :math:`\varepsilon` for Softplus. Default is 20 + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Shape: - input: Tensor with any shape. @@ -831,9 +829,8 @@ class Softplus(Layer): .. code-block:: python import paddle - import numpy as np - x = paddle.to_tensor(np.array([-0.4, -0.2, 0.1, 0.3])) + x = paddle.to_tensor([-0.4, -0.2, 0.1, 0.3], dtype='float32') m = paddle.nn.Softplus() out = m(x) # [0.513015, 0.598139, 0.744397, 0.854355] """ @@ -1124,16 +1121,17 @@ class ThresholdedReLU(Layer): class Silu(Layer): - """ - Silu Activation. + r""" + Silu Activation + .. math:: - Silu(x) = \frac{x}{1 + e^{-x}} + silu(x) = \frac{x}{1 + \mathrm{e}^{-x}} + + Where :math:`x` is the input Tensor. Parameters: - x (Tensor): The input Tensor with data type float32, or float64. - name (str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Shape: - input: Tensor with any shape. @@ -1294,15 +1292,13 @@ class Softmax(Layer): .. code-block:: python import paddle - import numpy as np - x = np.array([[[2.0, 3.0, 4.0, 5.0], + x = paddle.to_tensor([[[2.0, 3.0, 4.0, 5.0], [3.0, 4.0, 5.0, 6.0], [7.0, 8.0, 8.0, 9.0]], [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], - [6.0, 7.0, 8.0, 9.0]]], 'float32') - x = paddle.to_tensor(x) + [6.0, 7.0, 8.0, 9.0]]], dtype='float32') m = paddle.nn.Softmax() out = m(x) # [[[0.0320586 , 0.08714432, 0.23688282, 0.64391426], @@ -1387,7 +1383,7 @@ class LogSoftmax(Layer): class Maxout(Layer): r""" - Maxout Activation. + Maxout Activation. Create a callable object of `Maxout`. Assumed the input shape is (N, Ci, H, W). The output shape is (N, Co, H, W). diff --git a/python/paddle/nn/layer/common.py b/python/paddle/nn/layer/common.py index c5f399b3d76f977e2f6e5b17573a8398d8782b10..45c08bf2b4d10e7e2a2da5d39ef05f2d18af9d40 100644 --- a/python/paddle/nn/layer/common.py +++ b/python/paddle/nn/layer/common.py @@ -360,22 +360,6 @@ class Upsample(Layer): A 3-D Tensor of the shape (num_batches, channels, out_w) or (num_batches, out_w, channels), A 4-D Tensor of the shape (num_batches, channels, out_h, out_w) or (num_batches, out_h, out_w, channels), or 5-D Tensor of the shape (num_batches, channels, out_d, out_h, out_w) or (num_batches, out_d, out_h, out_w, channels). - Raises: - TypeError: size should be a list or tuple or Tensor. - ValueError: The 'mode' of image_resize can only be 'linear', 'bilinear', - 'trilinear', 'bicubic', or 'nearest' currently. - ValueError: 'linear' only support 3-D tensor. - ValueError: 'bilinear' and 'bicubic' only support 4-D tensor. - ValueError: 'trilinear' only support 5-D tensor. - ValueError: 'nearest' only support 4-D or 5-D tensor. - ValueError: One of size and scale_factor must not be None. - ValueError: size length should be 1 for input 3-D tensor. - ValueError: size length should be 2 for input 4-D tensor. - ValueError: size length should be 3 for input 5-D tensor. - ValueError: scale_factor should be greater than zero. - TypeError: align_corners should be a bool value - ValueError: align_mode can only be '0' or '1' - ValueError: data_format can only be 'NCW', 'NWC', 'NCHW', 'NHWC', 'NCDHW' or 'NDHWC'. Examples: .. code-block:: python diff --git a/python/paddle/nn/layer/conv.py b/python/paddle/nn/layer/conv.py index 78f5f506ae614226627007235a8e51ae8193e000..08056508f9170b6bdefbac0813c5dc123683f124 100644 --- a/python/paddle/nn/layer/conv.py +++ b/python/paddle/nn/layer/conv.py @@ -305,9 +305,6 @@ class Conv1D(_ConvNd): - weight: 3-D tensor with shape: (out_channels, in_channels, kernel_size) - bias: 1-D tensor with shape: (out_channels) - output: 3-D tensor with same shape as input x. - - Raises: - None Examples: .. code-block:: python @@ -986,10 +983,6 @@ class Conv3D(_ConvNd): W_{out}&= \frac{(W_{in} + 2 * paddings[2] - (dilations[2] * (kernel\_size[2] - 1) + 1))}{strides[2]} + 1 - Raises: - ValueError: If the shapes of input, filter_size, stride, padding and - groups mismatch. - Examples: .. code-block:: python @@ -1171,10 +1164,6 @@ class Conv3DTranspose(_ConvNd): H^\prime_{out} &= (H_{in} - 1) * strides[1] - 2 * paddings[1] + dilations[1] * (kernel\_size[1] - 1) + 1 W^\prime_{out} &= (W_{in} - 1) * strides[2] - 2 * paddings[2] + dilations[2] * (kernel\_size[2] - 1) + 1 - - Raises: - ValueError: If the shapes of input, filter_size, stride, padding and - groups mismatch. Examples: .. code-block:: python diff --git a/python/paddle/nn/layer/loss.py b/python/paddle/nn/layer/loss.py index b446b305a2984a2faed5a37e462050497b23333f..a80d3706fbfde55dcfc2e458c23af40e9503b617 100644 --- a/python/paddle/nn/layer/loss.py +++ b/python/paddle/nn/layer/loss.py @@ -590,15 +590,11 @@ class MSELoss(Layer): Examples: .. code-block:: python - import numpy as np import paddle - input_data = np.array([1.5]).astype("float32") - label_data = np.array([1.7]).astype("float32") - mse_loss = paddle.nn.loss.MSELoss() - input = paddle.to_tensor(input_data) - label = paddle.to_tensor(label_data) + input = paddle.to_tensor([1.5]) + label = paddle.to_tensor([1.7]) output = mse_loss(input, label) print(output) # [0.04000002] @@ -638,10 +634,10 @@ class MSELoss(Layer): class L1Loss(Layer): r""" - This interface is used to construct a callable object of the ``L1Loss`` class. + Construct a callable object of the ``L1Loss`` class. The L1Loss layer calculates the L1 Loss of ``input`` and ``label`` as follows. - If `reduction` set to ``'none'``, the loss is: + If `reduction` set to ``'none'``, the loss is: .. math:: Out = \lvert input - label\rvert @@ -677,12 +673,9 @@ class L1Loss(Layer): .. code-block:: python import paddle - import numpy as np - input_data = np.array([[1.5, 0.8], [0.2, 1.3]]).astype("float32") - label_data = np.array([[1.7, 1], [0.4, 0.5]]).astype("float32") - input = paddle.to_tensor(input_data) - label = paddle.to_tensor(label_data) + input = paddle.to_tensor([[1.5, 0.8], [0.2, 1.3]]) + label = paddle.to_tensor([[1.7, 1], [0.4, 0.5]]) l1_loss = paddle.nn.L1Loss() output = l1_loss(input, label) @@ -921,9 +914,10 @@ class NLLLoss(Layer): class KLDivLoss(Layer): r""" - This interface calculates the Kullback-Leibler divergence loss - between Input(X) and Input(Target). Notes that Input(X) is the - log-probability and Input(Target) is the probability. + Generate a callable object of 'KLDivLoss' to calculate the + Kullback-Leibler divergence loss between Input(X) and + Input(Target). Notes that Input(X) is the log-probability + and Input(Target) is the probability. KL divergence loss is calculated as follows: @@ -951,35 +945,30 @@ class KLDivLoss(Layer): .. code-block:: python import paddle - import numpy as np import paddle.nn as nn shape = (5, 20) - x = np.random.uniform(-10, 10, shape).astype('float32') - target = np.random.uniform(-10, 10, shape).astype('float32') + x = paddle.uniform(shape, min=-10, max=10).astype('float32') + target = paddle.uniform(shape, min=-10, max=10).astype('float32') # 'batchmean' reduction, loss shape will be [1] kldiv_criterion = nn.KLDivLoss(reduction='batchmean') - pred_loss = kldiv_criterion(paddle.to_tensor(x), - paddle.to_tensor(target)) + pred_loss = kldiv_criterion(x, target) # shape=[1] # 'mean' reduction, loss shape will be [1] kldiv_criterion = nn.KLDivLoss(reduction='mean') - pred_loss = kldiv_criterion(paddle.to_tensor(x), - paddle.to_tensor(target)) + pred_loss = kldiv_criterion(x, target) # shape=[1] # 'sum' reduction, loss shape will be [1] kldiv_criterion = nn.KLDivLoss(reduction='sum') - pred_loss = kldiv_criterion(paddle.to_tensor(x), - paddle.to_tensor(target)) + pred_loss = kldiv_criterion(x, target) # shape=[1] # 'none' reduction, loss shape is same with X shape kldiv_criterion = nn.KLDivLoss(reduction='none') - pred_loss = kldiv_criterion(paddle.to_tensor(x), - paddle.to_tensor(target)) + pred_loss = kldiv_criterion(x, target) # shape=[5, 20] """ @@ -1171,16 +1160,16 @@ class SmoothL1Loss(Layer): .. math:: - loss(x,y) = \frac{1}{n}\sum_{i}z_i + loss(x, y) = \frac{1}{n}\sum_{i}z_i - where z_i is given by: + where :math:`z_i` is given by: .. math:: \mathop{z_i} = \left\{\begin{array}{rcl} - 0.5(x_i - y_i)^2 & & {if |x_i - y_i| < delta} \\ - delta * |x_i - y_i| - 0.5 * delta^2 & & {otherwise} - \end{array} \right. + 0.5(x_i - y_i)^2 & & {if |x_i - y_i| < \delta} \\ + \delta * |x_i - y_i| - 0.5 * \delta^2 & & {otherwise} + \end{array} \right. Parameters: reduction (str, optional): Indicate how to average the loss by batch_size, @@ -1189,12 +1178,11 @@ class SmoothL1Loss(Layer): If :attr:`reduction` is ``'sum'``, the reduced sum loss is returned. If :attr:`reduction` is ``'none'``, the unreduced loss is returned. Default is ``'mean'``. - delta (float, optional): Specifies the hyperparameter delta to be used. + delta (float, optional): Specifies the hyperparameter :math:`\delta` to be used. The value determines how large the errors need to be to use L1. Errors smaller than delta are minimized with L2. Parameter is ignored for - negative/zero values. Default = 1.0 - name (str, optional): Name for the operation (optional, default is - None). For more information, please refer to :ref:`api_guide_Name`. + negative/zero values. Default value is :math:`1.0`. + name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None. Call Parameters: @@ -1212,14 +1200,12 @@ class SmoothL1Loss(Layer): .. code-block:: python import paddle - import numpy as np - input_data = np.random.rand(3,3).astype("float32") - label_data = np.random.rand(3,3).astype("float32") - input = paddle.to_tensor(input_data) - label = paddle.to_tensor(label_data) + input = paddle.rand([3, 3]).astype("float32") + label = paddle.rand([3, 3]).astype("float32") loss = paddle.nn.SmoothL1Loss() output = loss(input, label) print(output) + # [0.049606] """ def __init__(self, reduction='mean', delta=1.0, name=None): @@ -1321,7 +1307,7 @@ class MultiLabelSoftMarginLoss(Layer): class HingeEmbeddingLoss(Layer): r""" - This operator calculates hinge_embedding_loss. Measures the loss given an input tensor :math:`x` and a labels tensor :math:`y`(containing 1 or -1). + Create a callable object of `HingeEmbeddingLoss` to calculates hinge_embedding_loss. Measures the loss given an input tensor :math:`x` and a labels tensor :math:`y`(containing 1 or -1). This is usually used for measuring whether two inputs are similar or dissimilar, e.g. using the L1 pairwise distance as :math:`x`, and is typically used for learning nonlinear embeddings or semi-supervised learning. diff --git a/python/paddle/nn/layer/norm.py b/python/paddle/nn/layer/norm.py index 0d1fcbfa7364724ab8ff7241c1f1d8b4fc699e81..1bf7fb179e5d8770531447bca9e6e2fe38434063 100644 --- a/python/paddle/nn/layer/norm.py +++ b/python/paddle/nn/layer/norm.py @@ -117,7 +117,7 @@ class _InstanceNormBase(Layer): class InstanceNorm1D(_InstanceNormBase): r""" - Applies Instance Normalization over a 3D input (a mini-batch of 1D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization . + Create a callable object of `InstanceNorm1D`. Applies Instance Normalization over a 3D input (a mini-batch of 1D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization . DataLayout: NCL `[batch, in_channels, length]` @@ -133,8 +133,7 @@ class InstanceNorm1D(_InstanceNormBase): \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift - Note: - `H` means height of feature map, `W` means width of feature map. +Where `H` means height of feature map, `W` means width of feature map. Parameters: num_features(int): Indicate the number of channels of the input ``Tensor``. @@ -168,11 +167,8 @@ class InstanceNorm1D(_InstanceNormBase): .. code-block:: python import paddle - import numpy as np - np.random.seed(123) - x_data = np.random.random(size=(2, 2, 3)).astype('float32') - x = paddle.to_tensor(x_data) + x = paddle.rand((2, 2, 3)) instance_norm = paddle.nn.InstanceNorm1D(2) instance_norm_out = instance_norm(x) @@ -191,7 +187,7 @@ class InstanceNorm1D(_InstanceNormBase): class InstanceNorm2D(_InstanceNormBase): r""" - Applies Instance Normalization over a 4D input (a mini-batch of 2D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization . + Create a callable object of `InstanceNorm2D`. Applies Instance Normalization over a 4D input (a mini-batch of 2D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization . DataLayout: NCHW `[batch, in_channels, in_height, in_width]` @@ -208,8 +204,7 @@ class InstanceNorm2D(_InstanceNormBase): \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift - Note: - `H` means height of feature map, `W` means width of feature map. +Where `H` means height of feature map, `W` means width of feature map. Parameters: num_features(int): Indicate the number of channels of the input ``Tensor``. @@ -242,11 +237,8 @@ class InstanceNorm2D(_InstanceNormBase): .. code-block:: python import paddle - import numpy as np - np.random.seed(123) - x_data = np.random.random(size=(2, 2, 2, 3)).astype('float32') - x = paddle.to_tensor(x_data) + x = paddle.rand((2, 2, 2, 3)) instance_norm = paddle.nn.InstanceNorm2D(2) instance_norm_out = instance_norm(x) @@ -262,7 +254,7 @@ class InstanceNorm2D(_InstanceNormBase): class InstanceNorm3D(_InstanceNormBase): r""" - Applies Instance Normalization over a 5D input (a mini-batch of 3D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization . + Create a callable object of `InstanceNorm3D`. Applies Instance Normalization over a 5D input (a mini-batch of 3D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization . DataLayout: NCHW `[batch, in_channels, D, in_height, in_width]` @@ -279,8 +271,7 @@ class InstanceNorm3D(_InstanceNormBase): \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift - Note: - `H` means height of feature map, `W` means width of feature map. +Where `H` means height of feature map, `W` means width of feature map. Parameters: num_features(int): Indicate the number of channels of the input ``Tensor``. @@ -313,11 +304,8 @@ class InstanceNorm3D(_InstanceNormBase): .. code-block:: python import paddle - import numpy as np - np.random.seed(123) - x_data = np.random.random(size=(2, 2, 2, 2, 3)).astype('float32') - x = paddle.to_tensor(x_data) + x = paddle.rand((2, 2, 2, 2, 3)) instance_norm = paddle.nn.InstanceNorm3D(2) instance_norm_out = instance_norm(x) @@ -494,11 +482,7 @@ class GroupNorm(Layer): class LayerNorm(Layer): r""" - :alias_main: paddle.nn.LayerNorm - :alias: paddle.nn.LayerNorm,paddle.nn.layer.LayerNorm,paddle.nn.layer.norm.LayerNorm - :old_api: paddle.fluid.dygraph.LayerNorm - - This interface is used to construct a callable object of the ``LayerNorm`` class. + Construct a callable object of the ``LayerNorm`` class. For more details, refer to code examples. It implements the function of the Layer Normalization Layer and can be applied to mini-batch input data. Refer to `Layer Normalization `_ @@ -546,12 +530,9 @@ class LayerNorm(Layer): .. code-block:: python import paddle - import numpy as np - np.random.seed(123) - x_data = np.random.random(size=(2, 2, 2, 3)).astype('float32') - x = paddle.to_tensor(x_data) - layer_norm = paddle.nn.LayerNorm(x_data.shape[1:]) + x = paddle.rand((2, 2, 2, 3)) + layer_norm = paddle.nn.LayerNorm(x.shape[1:]) layer_norm_out = layer_norm(x) print(layer_norm_out) @@ -819,11 +800,8 @@ class BatchNorm1D(_BatchNormBase): .. code-block:: python import paddle - import numpy as np - np.random.seed(123) - x_data = np.random.random(size=(2, 1, 3)).astype('float32') - x = paddle.to_tensor(x_data) + x = paddle.rand((2, 1, 3)) batch_norm = paddle.nn.BatchNorm1D(1) batch_norm_out = batch_norm(x) @@ -934,11 +912,8 @@ class BatchNorm2D(_BatchNormBase): .. code-block:: python import paddle - import numpy as np - np.random.seed(123) - x_data = np.random.random(size=(2, 1, 2, 3)).astype('float32') - x = paddle.to_tensor(x_data) + x = paddle.rand((2, 1, 2, 3)) batch_norm = paddle.nn.BatchNorm2D(1) batch_norm_out = batch_norm(x) @@ -1023,11 +998,8 @@ class BatchNorm3D(_BatchNormBase): .. code-block:: python import paddle - import numpy as np - np.random.seed(123) - x_data = np.random.random(size=(2, 1, 2, 2, 3)).astype('float32') - x = paddle.to_tensor(x_data) + x = paddle.rand((2, 1, 2, 2, 3)) batch_norm = paddle.nn.BatchNorm3D(1) batch_norm_out = batch_norm(x) diff --git a/python/paddle/nn/layer/pooling.py b/python/paddle/nn/layer/pooling.py index 13299a5075c7a888b47df771c8a8cc7e01589dff..8371bbeaa582d3cdb9340a594977a3b16294adeb 100755 --- a/python/paddle/nn/layer/pooling.py +++ b/python/paddle/nn/layer/pooling.py @@ -355,14 +355,6 @@ class MaxPool1D(Layer): Returns: A callable object of MaxPool1D. - Raises: - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is "VALID", but `ceil_mode` is True. - ValueError: If `padding` is a list or tuple but its length greater than 1. - ShapeError: If the input is not a 3-D. - ShapeError: If the output's shape calculated is not greater than 0. - - Shape: - x(Tensor): The input tensor of max pool1d operator, which is a 3-D tensor. The data type can be float32, float64. @@ -468,10 +460,6 @@ class MaxPool2D(Layer): Returns: A callable object of MaxPool2D. - Raises: - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is "VALID", but `ceil_mode` is True. - ShapeError: If the output's shape calculated is not greater than 0. Shape: - x(Tensor): The input tensor of max pool2d operator, which is a 4-D tensor. @@ -569,10 +557,6 @@ class MaxPool3D(Layer): Returns: A callable object of MaxPool3D. - Raises: - ValueError: If `padding` is a string, but not "SAME" or "VALID". - ValueError: If `padding` is "VALID", but `ceil_mode` is True. - ShapeError: If the output's shape calculated is not greater than 0. Shape: - x(Tensor): The input tensor of max pool3d operator, which is a 5-D tensor. @@ -904,9 +888,6 @@ class AdaptiveMaxPool1D(Layer): Returns: A callable object of AdaptiveMaxPool1D. - Raises: - ValueError: 'pool_size' should be a integer or list or tuple with length as 1. - Shape: - x(Tensor): The input tensor of adaptive max pool1d operator, which is a 3-D tensor. The data type can be float32, float64. diff --git a/python/paddle/nn/utils/spectral_norm_hook.py b/python/paddle/nn/utils/spectral_norm_hook.py index eaf1817efc18c26e0657b3be4468d75bf5a60375..ec9abf46fff1a1f794b3b7caec094312f9a6fdbe 100644 --- a/python/paddle/nn/utils/spectral_norm_hook.py +++ b/python/paddle/nn/utils/spectral_norm_hook.py @@ -141,7 +141,7 @@ def spectral_norm( layer, name='weight', n_power_iterations=1, eps=1e-12, dim=None ): r""" - This spectral_norm layer applies spectral normalization to a parameter according to the + Applies spectral normalization to a parameter according to the following Calculation: Step 1: @@ -179,7 +179,7 @@ def spectral_norm( dim(int, optional): The index of dimension which should be permuted to the first before reshaping Input(Weight) to matrix, it should be set as 0 if Input(Weight) is the weight of fc layer, and should be set as 1 if Input(Weight) is the weight of conv layer. Default: None. Returns: - The original layer with the spectral norm hook + Layer, the original layer with the spectral norm hook. Examples: .. code-block:: python diff --git a/python/paddle/nn/utils/weight_norm_hook.py b/python/paddle/nn/utils/weight_norm_hook.py index 8ef9f75c5cba2d863d01bfec72efdb3b8d57fb47..55e2d408ffaeadb618986aa6b921ff5eb478c8a7 100755 --- a/python/paddle/nn/utils/weight_norm_hook.py +++ b/python/paddle/nn/utils/weight_norm_hook.py @@ -164,7 +164,7 @@ class WeightNorm(object): def weight_norm(layer, name='weight', dim=0): r""" - This weight_norm layer applies weight normalization to a parameter according to the + Applies weight normalization to a parameter according to the following formula: .. math:: @@ -193,11 +193,9 @@ def weight_norm(layer, name='weight', dim=0): Examples: .. code-block:: python - import numpy as np from paddle.nn import Conv2D from paddle.nn.utils import weight_norm - x = np.array([[[[0.3, 0.4], [0.3, 0.07]], [[0.83, 0.37], [0.18, 0.93]]]]).astype('float32') conv = Conv2D(3, 5, 3) wn = weight_norm(conv) print(conv.weight_g.shape) @@ -218,7 +216,7 @@ def remove_weight_norm(layer, name='weight'): name(str, optional): Name of the weight parameter. Default: 'weight'. Returns: - Origin layer without weight norm + Layer, the origin layer without weight norm Examples: .. code-block:: python diff --git a/python/paddle/optimizer/lr.py b/python/paddle/optimizer/lr.py index f4c9505936fecbc755b901c9cee0ff616ce7f784..4e2dbafcabbc0b474c6a4b8fd83f7593032fb239 100644 --- a/python/paddle/optimizer/lr.py +++ b/python/paddle/optimizer/lr.py @@ -1622,7 +1622,6 @@ class MultiplicativeDecay(LRScheduler): .. code-block:: python import paddle - import numpy as np # train on default dynamic graph mode linear = paddle.nn.Linear(10, 10) @@ -1937,7 +1936,7 @@ class CyclicLR(LRScheduler): verbose: (bool, optional): If ``True``, prints a message to stdout for each update. Default: ``False`` . Returns: - ``CyclicLR`` instance to schedule learning rate. + ``CyclicLR`` instance to schedule learning rate. Examples: .. code-block:: python diff --git a/python/paddle/optimizer/rmsprop.py b/python/paddle/optimizer/rmsprop.py index 6a79d762684767f25c588e627e806384c134f78e..10532c8f62846b2ddfdb1212274d2494ccf9ffc0 100644 --- a/python/paddle/optimizer/rmsprop.py +++ b/python/paddle/optimizer/rmsprop.py @@ -71,12 +71,12 @@ class RMSProp(Optimizer): Parameters: learning_rate (float|LRScheduler): The learning rate used to update ``Parameter``. It can be a float value or a LRScheduler. - rho(float): rho is :math:`\rho` in equation, default is 0.95. - epsilon(float): :math:`\epsilon` in equation is smoothing term to + rho(float, optional): rho is :math:`\rho` in equation, default is 0.95. + epsilon(float, optional): :math:`\epsilon` in equation is smoothing term to avoid division by zero, default is 1e-6. - momentum(float): :math:`\beta` in equation is the momentum term, + momentum(float, optional): :math:`\beta` in equation is the momentum term, default is 0.0. - centered(bool): If True, gradients are normalized by the estimated variance of + centered(bool, optional): If True, gradients are normalized by the estimated variance of the gradient; if False, by the uncentered second moment. Setting this to True may help with training, but is slightly more expensive in terms of computation and memory. Defaults to False. @@ -100,9 +100,6 @@ class RMSProp(Optimizer): name (str, optional): This parameter is used by developers to print debugging information. For details, please refer to :ref:`api_guide_Name`. Default is None. - Raises: - ValueError: If learning_rate, rho, epsilon, momentum are None. - Examples: .. code-block:: python diff --git a/python/paddle/profiler/profiler.py b/python/paddle/profiler/profiler.py index c44d2f0f611ad35facd533d4c3a39b3dcb29bed4..03cfb3a9a42231f639c022aebaebcb5c07fc1b93 100644 --- a/python/paddle/profiler/profiler.py +++ b/python/paddle/profiler/profiler.py @@ -22,11 +22,16 @@ import importlib import json import paddle -from paddle.fluid.core import (_Profiler, _ProfilerResult, ProfilerOptions, - TracerEventType, enable_memory_recorder, - enable_input_shape_recorder, - disable_memory_recorder, - disable_input_shape_recorder) +from paddle.fluid.core import ( + _Profiler, + _ProfilerResult, + ProfilerOptions, + TracerEventType, + enable_memory_recorder, + enable_input_shape_recorder, + disable_memory_recorder, + disable_input_shape_recorder, +) from .utils import RecordEvent, wrap_optimizers from .profiler_statistic import StatisticData, _build_table, SortedKeys @@ -105,12 +110,14 @@ class ProfilerTarget(Enum): CUSTOM_DEVICE = 3 -def make_scheduler(*, - closed: int, - ready: int, - record: int, - repeat: int = 0, - skip_first: int = 0) -> Callable: +def make_scheduler( + *, + closed: int, + ready: int, + record: int, + repeat: int = 0, + skip_first: int = 0 +) -> Callable: r""" Return a scheduler function, which scheduler the :ref:`state ` according to the setting. The state transform confirms to: @@ -132,12 +139,12 @@ def make_scheduler(*, skip_first(int, optional): The number of first steps to drop, not participate in the state transform, and at ProfilerState.CLOSED state. Default value is 0. Returns: - A scheduler function, conforms to above state transform setting. The function will takes one parameter step_num, and returns corresponding ProfilerState. + A scheduler function, conforms to above state transform setting. The function will takes one parameter `step_num`, and returns corresponding ProfilerState. Examples: - 1. profiling range [2, 5] + 1. profiling range [2, 5]. - Assume batch 0: closed, batch 1: ready, batch [2, 5] record + Assume batch 0: closed, batch 1: ready, batch [2, 5] record. .. code-block:: python :name: code-example1 @@ -146,9 +153,9 @@ def make_scheduler(*, profiler.make_scheduler(closed=1, ready=1, record=4, repeat=1) - 2. profiling range [3,6], [9,12], [15,18]... + 2. profiling range [3,6], [9,12], [15,18]. - Assume batch 0: skiped, batch 1: closed, batch 2: ready, batch [3,6]: record, repeat + Assume batch 0: skiped, batch 1: closed, batch 2: ready, batch [3,6]: record, repeat. .. code-block:: python :name: code-example2 @@ -164,7 +171,9 @@ def make_scheduler(*, step = step - skip_first period_steps = closed + ready + record has_repeated = step // period_steps - if repeat > 0 and has_repeated >= repeat: # the period has repeated repeat times, return CLOSED state + if ( + repeat > 0 and has_repeated >= repeat + ): # the period has repeated repeat times, return CLOSED state return ProfilerState.CLOSED mod_step = step % period_steps if mod_step < closed: @@ -176,12 +185,19 @@ def make_scheduler(*, return ProfilerState.RECORD else: return ProfilerState.RECORD_AND_RETURN - assert closed >= 0 and ready >= 0 and record > 0 and \ - repeat >= 0 and skip_first >= 0, "Invalid profiler scheduler arguments" + + assert ( + closed >= 0 + and ready >= 0 + and record > 0 + and repeat >= 0 + and skip_first >= 0 + ), "Invalid profiler scheduler arguments" if ready == 0: - warn("Profiler will record data after enabling profiler immediately, \ + warn( + "Profiler will record data after enabling profiler immediately, \ some data collected at the beginning of profiling may be 'noisy' because of overhead." - ) + ) return getScheduleState @@ -192,17 +208,18 @@ def _default_state_scheduler(step: int): return ProfilerState.RECORD -def export_chrome_tracing(dir_name: str, - worker_name: Optional[str] = None) -> Callable: +def export_chrome_tracing( + dir_name: str, worker_name: Optional[str] = None +) -> Callable: r""" Return a callable, used for outputing tracing data to chrome tracing format file. - The output file will be saved in directory ``dir_name``, and file name will be set as worker_name. - if worker_name is not set, the default name is [hostname]_[pid]. + The output file will be saved in directory ``dir_name``, and file name will be set as `worker_name`. + if `worker_name` is not set, the default name is `[hostname]_[pid]`. Args: dir_name(str): Directory to save profiling data. - worker_name(str, optional): Prefix of the file name saved, default is [hostname]_[pid]. - + worker_name(str, optional): Prefix of the file name saved, default is `[hostname]_[pid]`. + Returns: A callable, which takes a Profiler object as parameter and calls its export method to save data to chrome tracing format file. @@ -226,32 +243,37 @@ def export_chrome_tracing(dir_name: str, os.makedirs(dir_name, exist_ok=True) except Exception: raise RuntimeError( - "Can not create directory '{}' for saving profiling results.". - format(dir_name)) + "Can not create directory '{}' for saving profiling results.".format( + dir_name + ) + ) def handle_fn(prof): nonlocal worker_name if not worker_name: - worker_name = "host_{}pid_{}".format(socket.gethostname(), - str(os.getpid())) + worker_name = "host_{}pid_{}".format( + socket.gethostname(), str(os.getpid()) + ) now = datetime.datetime.now() filename = '{}_time_{}.paddle_trace.json'.format( - worker_name, now.strftime('%Y_%m_%d_%H_%M_%S_%f')) + worker_name, now.strftime('%Y_%m_%d_%H_%M_%S_%f') + ) prof.export(os.path.join(dir_name, filename), "json") return handle_fn -def export_protobuf(dir_name: str, - worker_name: Optional[str] = None) -> Callable: +def export_protobuf( + dir_name: str, worker_name: Optional[str] = None +) -> Callable: r""" Return a callable, used for outputing tracing data to protobuf file. - The output file will be saved in directory ``dir_name``, and file name will be set as worker_name. - if worker_name is not set, the default name is [hostname]_[pid]. + The output file will be saved in directory ``dir_name``, and file name will be set as ``worker_name``. + if ``worker_name`` is not set, the default name is `[hostname]_[pid]`. Args: dir_name(str): Directory to save profiling data. - worker_name(str, optional): Prefix of the file name saved, default is [hostname]_[pid]. + worker_name(str, optional): Prefix of the file name saved, default is `[hostname]_[pid]`. Returns: A callable, which takes a Profiler object as parameter and calls its export method to save data to protobuf file. @@ -276,17 +298,21 @@ def export_protobuf(dir_name: str, os.makedirs(dir_name, exist_ok=True) except Exception: raise RuntimeError( - "Can not create directory '{}' for saving profiling results.". - format(dir_name)) + "Can not create directory '{}' for saving profiling results.".format( + dir_name + ) + ) def handle_fn(prof): nonlocal worker_name if not worker_name: - worker_name = "host_{}pid_{}".format(socket.gethostname(), - str(os.getpid())) + worker_name = "host_{}pid_{}".format( + socket.gethostname(), str(os.getpid()) + ) now = datetime.datetime.now() filename = '{}_time_{}.paddle_trace.pb'.format( - worker_name, now.strftime('%Y_%m_%d_%H_%M_%S_%f')) + worker_name, now.strftime('%Y_%m_%d_%H_%M_%S_%f') + ) prof.export(os.path.join(dir_name, filename), "pb") return handle_fn @@ -298,11 +324,15 @@ def _get_supported_targets() -> Iterable[ProfilerTarget]: """ if _Profiler.is_cupti_supported(): return [ - ProfilerTarget.CPU, ProfilerTarget.GPU, ProfilerTarget.CUSTOM_DEVICE + ProfilerTarget.CPU, + ProfilerTarget.GPU, + ProfilerTarget.CUSTOM_DEVICE, ] if _Profiler.is_cnpapi_supported(): return [ - ProfilerTarget.CPU, ProfilerTarget.MLU, ProfilerTarget.CUSTOM_DEVICE + ProfilerTarget.CPU, + ProfilerTarget.MLU, + ProfilerTarget.CUSTOM_DEVICE, ] return [ProfilerTarget.CPU, ProfilerTarget.CUSTOM_DEVICE] @@ -317,7 +347,7 @@ class Profiler: If not provided (None), the default scheduler will keep tracing until the profiler exits. If it is a tuple, it has two values start_batch and end_batch, which means profiling range [start_batch, end_batch). on_trace_ready (Callable, optional): Callable object, serves as callback function, and takes the Profiler object as parameter, which provides a way for users to do post-processing. - This callable object will be called when ``scheduler`` returns ``ProfilerState.RECORD_AND_RETURN``. The default value is :ref:`export_chrome_tracing ` (./profiler_log/). + This callable object will be called when ``scheduler`` returns ``ProfilerState.RECORD_AND_RETURN``. The default value is :ref:`export_chrome_tracing `. timer_only (bool, optional): If it is True, the cost of Dataloader and every step of the model will be count without profiling. Otherwise, the model will be timed and profiled. Default: False. record_shapes (bool, optional): If it is True, collect op's input shape information. Default: False. @@ -339,7 +369,7 @@ class Profiler: #train() p.step() - 2. profiling range [2,4], [7, 9], [11,13] + 2. profiling range [2,4], [7, 9], [11,13]. .. code-block:: python :name: code-example2 @@ -354,7 +384,7 @@ class Profiler: #train() p.step() - 3. Use profiler without context manager, and use default parameters + 3. Use profiler without context manager, and use default parameters. .. code-block:: python :name: code-example3 @@ -369,38 +399,37 @@ class Profiler: p.stop() p.summary() - 4. Use profiler to get throughput and cost of the model + 4. Use profiler to get throughput and cost of the model. .. code-block:: python :name: code-example-timer1 import paddle import paddle.profiler as profiler - + class RandomDataset(paddle.io.Dataset): def __init__(self, num_samples): self.num_samples = num_samples - + def __getitem__(self, idx): image = paddle.rand(shape=[100], dtype='float32') label = paddle.randint(0, 10, shape=[1], dtype='int64') return image, label - + def __len__(self): return self.num_samples - + class SimpleNet(paddle.nn.Layer): def __init__(self): super(SimpleNet, self).__init__() self.fc = paddle.nn.Linear(100, 10) - + def forward(self, image, label=None): return self.fc(image) - + dataset = RandomDataset(20 * 4) simple_net = SimpleNet() - opt = paddle.optimizer.SGD(learning_rate=1e-3, - parameters=simple_net.parameters()) + opt = paddle.optimizer.SGD(learning_rate=1e-3, parameters=simple_net.parameters()) BATCH_SIZE = 4 loader = paddle.io.DataLoader( dataset, @@ -433,36 +462,40 @@ class Profiler: # | ips | 1086.42904 | 1227.30604 | 959.92796 | """ - def __init__(self, - *, - targets: Optional[Iterable[ProfilerTarget]] = None, - scheduler: Union[Callable[[int], ProfilerState], tuple, - None] = None, - on_trace_ready: Optional[Callable[..., Any]] = None, - record_shapes: Optional[bool] = False, - profile_memory=False, - timer_only: Optional[bool] = False, - emit_nvtx: Optional[bool] = False, - custom_device_types: Optional[list] = []): + def __init__( + self, + *, + targets: Optional[Iterable[ProfilerTarget]] = None, + scheduler: Union[Callable[[int], ProfilerState], tuple, None] = None, + on_trace_ready: Optional[Callable[..., Any]] = None, + record_shapes: Optional[bool] = False, + profile_memory=False, + timer_only: Optional[bool] = False, + emit_nvtx: Optional[bool] = False, + custom_device_types: Optional[list] = [] + ): supported_targets = _get_supported_targets() if targets: self.targets = set(targets) for target in targets: if target not in supported_targets: self.targets.remove(target) - warn("Profiling {} is not supported in current context.". - format(target)) + warn( + "Profiling {} is not supported in current context.".format( + target + ) + ) else: self.targets = supported_targets profileoption = ProfilerOptions() if ProfilerTarget.CPU in self.targets: profileoption.trace_switch |= 1 if ProfilerTarget.GPU in self.targets: - profileoption.trace_switch |= (1 << 1) + profileoption.trace_switch |= 1 << 1 if ProfilerTarget.MLU in self.targets: - profileoption.trace_switch |= (1 << 2) + profileoption.trace_switch |= 1 << 2 if ProfilerTarget.CUSTOM_DEVICE in self.targets: - profileoption.trace_switch |= (1 << 3) + profileoption.trace_switch |= 1 << 3 if not custom_device_types: custom_device_types = paddle.device.get_all_custom_device_type() wrap_optimizers() @@ -474,17 +507,19 @@ class Profiler: start_batch, end_batch = scheduler start_batch = max(start_batch, 0) if start_batch >= 1: - self.scheduler = make_scheduler(closed=max(start_batch - 1, 0), - ready=1, - record=(end_batch - - start_batch), - repeat=1) + self.scheduler = make_scheduler( + closed=max(start_batch - 1, 0), + ready=1, + record=(end_batch - start_batch), + repeat=1, + ) else: - self.scheduler = make_scheduler(closed=0, - ready=0, - record=(end_batch - - start_batch), - repeat=1) + self.scheduler = make_scheduler( + closed=0, + ready=0, + record=(end_batch - start_batch), + repeat=1, + ) else: self.scheduler = _default_state_scheduler @@ -531,7 +566,7 @@ class Profiler: prof.stop() ''' - # Timing only without profiling + # Timing only without profiling. benchmark().begin() if not self.timer_only or self.emit_nvtx: utils._is_profiler_used = True @@ -550,9 +585,10 @@ class Profiler: elif self.current_state == ProfilerState.RECORD_AND_RETURN: self.profiler.prepare() self.profiler.start() - self.record_event = RecordEvent(name="ProfileStep#{}".format( - self.step_num), - event_type=TracerEventType.ProfileStep) + self.record_event = RecordEvent( + name="ProfileStep#{}".format(self.step_num), + event_type=TracerEventType.ProfileStep, + ) self.record_event.begin() def stop(self): @@ -584,7 +620,7 @@ class Profiler: if self.profile_memory: disable_memory_recorder() # self.current_state -> CLOSED - # In this situation, RECORD state is regarded as RECORD_AND_RETURN + # In this situation, RECORD state is regarded as RECORD_AND_RETURN. if self.record_event: self.record_event.end() self.record_event = None @@ -594,7 +630,10 @@ class Profiler: ) self.profiler.start() self.profiler.stop() - if self.current_state == ProfilerState.RECORD or self.current_state == ProfilerState.RECORD_AND_RETURN: + if ( + self.current_state == ProfilerState.RECORD + or self.current_state == ProfilerState.RECORD_AND_RETURN + ): self.profiler_result = self.profiler.stop() if self.on_trace_ready: self.on_trace_ready(self) @@ -607,7 +646,7 @@ class Profiler: Args: num_samples (int|None, optional): Specifies the batch size of every step of the model - that is used to compute throughput when timer_only is True. Default: None. + that is used to compute throughput when `timer_only` is True. Default: None. Examples: .. code-block:: python @@ -636,16 +675,17 @@ class Profiler: self.step_num += 1 self.current_state = self.scheduler(self.step_num) self._trigger_action() - self.record_event = RecordEvent(name="ProfileStep#{}".format( - self.step_num), - event_type=TracerEventType.ProfileStep) + self.record_event = RecordEvent( + name="ProfileStep#{}".format(self.step_num), + event_type=TracerEventType.ProfileStep, + ) self.record_event.begin() def step_info(self, unit=None): r""" Get statistics for current step. If the function is called at certain iteration intervals, the result is the average of all steps between the previous call and - this call. Statistics are as follows: + this call. Statistics are as follows: 1. reader_cost: the cost of loading data measured in seconds. @@ -695,7 +735,9 @@ class Profiler: if self.current_state == ProfilerState.RECORD: # CLOSED -> RECORD self.profiler.prepare() self.profiler.start() - if self.current_state == ProfilerState.RECORD_AND_RETURN: # CLOSED -> RECORD_AND_RETURN + if ( + self.current_state == ProfilerState.RECORD_AND_RETURN + ): # CLOSED -> RECORD_AND_RETURN self.profiler.prepare() self.profiler.start() @@ -708,7 +750,9 @@ class Profiler: self.profiler.stop() if self.current_state == ProfilerState.RECORD: # READY -> RECORD self.profiler.start() - if self.current_state == ProfilerState.RECORD_AND_RETURN: # READY -> RECORD_AND_RETURN + if ( + self.current_state == ProfilerState.RECORD_AND_RETURN + ): # READY -> RECORD_AND_RETURN self.profiler.start() elif self.previous_state == ProfilerState.RECORD: @@ -724,21 +768,31 @@ class Profiler: ) self.profiler.stop() self.profiler.prepare() - if self.current_state == ProfilerState.RECORD_AND_RETURN: # RECORD -> RECORD_AND_RETURN + if ( + self.current_state == ProfilerState.RECORD_AND_RETURN + ): # RECORD -> RECORD_AND_RETURN pass else: assert self.previous_state == ProfilerState.RECORD_AND_RETURN - if self.current_state == ProfilerState.CLOSED: # RECORD_AND_RETURN -> CLOSED + if ( + self.current_state == ProfilerState.CLOSED + ): # RECORD_AND_RETURN -> CLOSED self.profiler_result = self.profiler.stop() - if self.current_state == ProfilerState.READY: # RECORD_AND_RETURN -> READY + if ( + self.current_state == ProfilerState.READY + ): # RECORD_AND_RETURN -> READY self.profiler_result = self.profiler.stop() self.profiler.prepare() - if self.current_state == ProfilerState.RECORD: # RECORD_AND_RETURN -> RECORD + if ( + self.current_state == ProfilerState.RECORD + ): # RECORD_AND_RETURN -> RECORD self.profiler_result = self.profiler.stop() self.profiler.prepare() self.profiler.start() - if self.current_state == ProfilerState.RECORD_AND_RETURN: # RECORD_AND_RETURN -> RECORD_AND_RETURN + if ( + self.current_state == ProfilerState.RECORD_AND_RETURN + ): # RECORD_AND_RETURN -> RECORD_AND_RETURN self.profiler_result = self.profiler.stop() self.profiler.prepare() self.profiler.start() @@ -751,7 +805,7 @@ class Profiler: Args: path(str): file path of the output. - format(str, optional): output format, can be chosen from ['json', 'pb], 'json' for chrome tracing and 'pb' for protobuf, default value is "json". + format(str, optional): output format, can be chosen from ['json', 'pb'], 'json' for chrome tracing and 'pb' for protobuf, default value is 'json'. Examples: @@ -773,12 +827,14 @@ class Profiler: if self.profiler_result: self.profiler_result.save(path, format) - def summary(self, - sorted_by=SortedKeys.CPUTotal, - op_detail=True, - thread_sep=False, - time_unit='ms', - views=None): + def summary( + self, + sorted_by=SortedKeys.CPUTotal, + op_detail=True, + thread_sep=False, + time_unit='ms', + views=None, + ): r""" Print the Summary table. Currently support overview, model, distributed, operator, memory manipulation and userdefined summary. @@ -812,14 +868,18 @@ class Profiler: if self.profiler_result: statistic_data = StatisticData( self.profiler_result.get_data(), - self.profiler_result.get_extra_info()) + self.profiler_result.get_extra_info(), + ) print( - _build_table(statistic_data, - sorted_by=sorted_by, - op_detail=op_detail, - thread_sep=thread_sep, - time_unit=time_unit, - views=views)) + _build_table( + statistic_data, + sorted_by=sorted_by, + op_detail=op_detail, + thread_sep=thread_sep, + time_unit=time_unit, + views=views, + ) + ) def get_profiler(config_path): @@ -852,17 +912,20 @@ def get_profiler(config_path): method = getattr(module, key) if not use_direct: translated_config_dict['scheduler'] = method( - *value['args'], **value['kwargs']) + *value['args'], **value['kwargs'] + ) else: translated_config_dict['scheduler'] = method else: translated_config_dict['scheduler'] = [ - config_dict['scheduler'][0], config_dict['scheduler'][1] + config_dict['scheduler'][0], + config_dict['scheduler'][1], ] except: print( - 'Set scheduler parameter error, use default parameter instead.') + 'Set scheduler parameter error, use default parameter instead.' + ) translated_config_dict['scheduler'] = None if "on_trace_ready" in config_dict: try: @@ -874,7 +937,8 @@ def get_profiler(config_path): method = getattr(module, key) if not use_direct: translated_config_dict['on_trace_ready'] = method( - *value['args'], **value['kwargs']) + *value['args'], **value['kwargs'] + ) else: translated_config_dict['on_trace_ready'] = method except: diff --git a/python/paddle/profiler/utils.py b/python/paddle/profiler/utils.py index c0146fe92763fe867c9dec415b0fbb68196d848d..efe3975f1445246678aeed42c2cf7414e4dc188a 100644 --- a/python/paddle/profiler/utils.py +++ b/python/paddle/profiler/utils.py @@ -18,16 +18,19 @@ import functools from contextlib import ContextDecorator from paddle.fluid import core -from paddle.fluid.core import (_RecordEvent, TracerEventType) +from paddle.fluid.core import _RecordEvent, TracerEventType _is_profiler_used = False _has_optimizer_wrapped = False _AllowedEventTypeList = [ - TracerEventType.Dataloader, TracerEventType.ProfileStep, - TracerEventType.Forward, TracerEventType.Backward, - TracerEventType.Optimization, TracerEventType.PythonOp, - TracerEventType.PythonUserDefined + TracerEventType.Dataloader, + TracerEventType.ProfileStep, + TracerEventType.Forward, + TracerEventType.Backward, + TracerEventType.Optimization, + TracerEventType.PythonOp, + TracerEventType.PythonUserDefined, ] @@ -36,8 +39,10 @@ class RecordEvent(ContextDecorator): Interface for recording a time range by user defined. Args: - name(str): Name of the record event - event_type(TracerEventType, optional): Optional, default value is TracerEventType.PythonUserDefined. It is reserved for internal purpose, and it is better not to specify this parameter. + name (str): Name of the record event. + event_type (TracerEventType, optional): Optional, default value is + `TracerEventType.PythonUserDefined`. It is reserved for internal + purpose, and it is better not to specify this parameter. Examples: .. code-block:: python @@ -59,13 +64,14 @@ class RecordEvent(ContextDecorator): record_event.end() **Note**: - RecordEvent will take effect only when :ref:`Profiler ` is on and at the state of RECORD. + RecordEvent will take effect only when :ref:`Profiler ` is on and at the state of `RECORD`. """ def __init__( - self, - name: str, - event_type: TracerEventType = TracerEventType.PythonUserDefined): + self, + name: str, + event_type: TracerEventType = TracerEventType.PythonUserDefined, + ): self.name = name self.event_type = event_type self.event = None @@ -98,8 +104,12 @@ class RecordEvent(ContextDecorator): if not _is_profiler_used: return if self.event_type not in _AllowedEventTypeList: - warn("Only TracerEvent Type in [{}, {}, {}, {}, {}, {},{}]\ - can be recorded.".format(*_AllowedEventTypeList)) + warn( + "Only TracerEvent Type in [{}, {}, {}, {}, {}, {},{}]\ + can be recorded.".format( + *_AllowedEventTypeList + ) + ) self.event = None else: self.event = _RecordEvent(self.name, self.event_type) @@ -134,7 +144,7 @@ def load_profiler_result(filename: str): filename(str): Name of the exported protobuf file of profiler data. Returns: - ProfilerResult object, which stores profiling data. + ``ProfilerResult`` object, which stores profiling data. Examples: .. code-block:: python @@ -158,14 +168,13 @@ def in_profiler_mode(): def wrap_optimizers(): - def optimizer_warpper(func): - @functools.wraps(func) def warpper(*args, **kwargs): if in_profiler_mode(): - with RecordEvent('Optimization Step', - event_type=TracerEventType.Optimization): + with RecordEvent( + 'Optimization Step', event_type=TracerEventType.Optimization + ): return func(*args, **kwargs) else: return func(*args, **kwargs) @@ -176,6 +185,7 @@ def wrap_optimizers(): if _has_optimizer_wrapped == True: return import paddle.optimizer as optimizer + for classname in optimizer.__all__: if classname != 'Optimizer': classobject = getattr(optimizer, classname) diff --git a/python/paddle/reader/decorator.py b/python/paddle/reader/decorator.py index 9e11a6c0b5e8cb02799bbf396662b2c76bfa7304..20874215ddc268216cd44cbb04847abb6e92cb81 100644 --- a/python/paddle/reader/decorator.py +++ b/python/paddle/reader/decorator.py @@ -264,9 +264,6 @@ def compose(*readers, **kwargs): Returns: the new data reader (Reader). - Raises: - ComposeNotAligned: outputs of readers are not aligned. This will not raise if check_alignment is set to False. - Examples: .. code-block:: python diff --git a/python/paddle/sparse/creation.py b/python/paddle/sparse/creation.py index a960e895ff2d3f3035b4e3255290deda40f6f893..6258f6e05a76c0af024d84b460f707bfc593677e 100644 --- a/python/paddle/sparse/creation.py +++ b/python/paddle/sparse/creation.py @@ -98,12 +98,6 @@ def sparse_coo_tensor( Returns: Tensor: A Tensor constructed from ``indices`` and ``values`` . - Raises: - TypeError: If the data type of ``values`` is not list, tuple, numpy.ndarray, paddle.Tensor - ValueError: If ``values`` is tuple|list, it can't contain nested tuple|list with different lengths , such as: [[1, 2], [3, 4, 5]]. If the ``indices`` is not a 2-D. - TypeError: If ``dtype`` is not bool, float16, float32, float64, int8, int16, int32, int64, uint8, complex64, complex128 - ValueError: If ``place`` is not paddle.CPUPlace, paddle.CUDAPinnedPlace, paddle.CUDAPlace or specified pattern string. - Examples: .. code-block:: python @@ -222,12 +216,6 @@ def sparse_csr_tensor( Returns: Tensor: A Tensor constructed from ``crows``, ``cols`` and ``values`` . - Raises: - TypeError: If the data type of ``values`` is not list, tuple, numpy.ndarray, paddle.Tensor - ValueError: If ``values`` is tuple|list, it can't contain nested tuple|list with different lengths , such as: [[1, 2], [3, 4, 5]]. If the ``crow``, ``cols`` and ``values`` is not a 2-D. - TypeError: If ``dtype`` is not bool, float16, float32, float64, int8, int16, int32, int64, uint8, complex64, complex128 - ValueError: If ``place`` is not paddle.CPUPlace, paddle.CUDAPinnedPlace, paddle.CUDAPlace or specified pattern string. - Examples: .. code-block:: python diff --git a/python/paddle/static/io.py b/python/paddle/static/io.py index 53acfdb727b29a923787c52820bc5c1917b3158e..b880da6abc59db4ff77b454b028e4387e2f7096a 100644 --- a/python/paddle/static/io.py +++ b/python/paddle/static/io.py @@ -143,11 +143,6 @@ def normalize_program(program, feed_vars, fetch_vars): Returns: Program: Normalized/Optimized program. - Raises: - TypeError: If `program` is not a Program, an exception is thrown. - TypeError: If `feed_vars` is not a Variable or a list of Variable, an exception is thrown. - TypeError: If `fetch_vars` is not a Variable or a list of Variable, an exception is thrown. - Examples: .. code-block:: python @@ -285,10 +280,6 @@ def serialize_program(feed_vars, fetch_vars, **kwargs): Returns: bytes: serialized program. - Raises: - ValueError: If `feed_vars` is not a Variable or a list of Variable, an exception is thrown. - ValueError: If `fetch_vars` is not a Variable or a list of Variable, an exception is thrown. - Examples: .. code-block:: python @@ -348,10 +339,6 @@ def serialize_persistables(feed_vars, fetch_vars, executor, **kwargs): Returns: bytes: serialized program. - Raises: - ValueError: If `feed_vars` is not a Variable or a list of Variable, an exception is thrown. - ValueError: If `fetch_vars` is not a Variable or a list of Variable, an exception is thrown. - Examples: .. code-block:: python @@ -497,10 +484,6 @@ def save_inference_model( Returns: None - Raises: - ValueError: If `feed_vars` is not a Variable or a list of Variable, an exception is thrown. - ValueError: If `fetch_vars` is not a Variable or a list of Variable, an exception is thrown. - Examples: .. code-block:: python @@ -783,9 +766,6 @@ def load_inference_model(path_prefix, executor, **kwargs): ``Variable`` (refer to :ref:`api_guide_Program_en`). It contains variables from which we can get inference results. - Raises: - ValueError: If `path_prefix.pdmodel` or `path_prefix.pdiparams` doesn't exist. - Examples: .. code-block:: python diff --git a/python/paddle/static/nn/common.py b/python/paddle/static/nn/common.py index 05a06596c710bfc5b016371f45306e14063f4eb7..98c5e81a2e8e063243cc994cefcdbff1da09fc1c 100755 --- a/python/paddle/static/nn/common.py +++ b/python/paddle/static/nn/common.py @@ -124,9 +124,6 @@ def fc( Returns: Tensor, its shape is :math:`[batch\_size, *, size]` , and the data type is same with input. - Raises: - ValueError: If dimensions of the input tensor is less than 2. - Examples: .. code-block:: python @@ -281,9 +278,7 @@ def deform_conv2d( Returns: Tensor: The tensor storing the deformable convolution \ result. A Tensor with type float32, float64. - Raises: - ValueError: If the shapes of input, filter_size, stride, padding and - groups mismatch. + Examples: .. code-block:: python diff --git a/python/paddle/tensor/linalg.py b/python/paddle/tensor/linalg.py index c695ec212263804b3103a5e52efa0eefc924f652..ace9c8e98d6df92ee0fbd8feecd1d74c6a0d1612 100644 --- a/python/paddle/tensor/linalg.py +++ b/python/paddle/tensor/linalg.py @@ -282,7 +282,7 @@ def norm(x, p='fro', axis=None, keepdim=False, name=None): Returns the matrix norm (Frobenius) or vector norm (the 1-norm, the Euclidean or 2-norm, and in general the p-norm for p > 0) of a given tensor. - .. note:: + Note: This norm API is different from `numpy.linalg.norm`. This api supports high-order input tensors (rank >= 3), and certain axis need to be pointed out to calculate the norm. But `numpy.linalg.norm` only supports 1-D vector or 2-D matrix as input tensor. @@ -1170,7 +1170,7 @@ def dot(x, y, name=None): """ This operator calculates inner product for vectors. - .. note:: + Note: Support 1-d and 2-d Tensor. When it is 2d, the first dimension of this matrix is the batch dimension, which means that the vectors of multiple batches are dotted. @@ -1516,10 +1516,12 @@ def cholesky(x, upper=False, name=None): Its data type should be float32 or float64. upper (bool): The flag indicating whether to return upper or lower triangular matrices. Default: False. + name (str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. Returns: - Tensor: A Tensor with same shape and data type as `x`. It represents \ - triangular matrices generated by Cholesky decomposition. + Tensor, A Tensor with same shape and data type as `x`. It represents + triangular matrices generated by Cholesky decomposition. Examples: .. code-block:: python @@ -1911,24 +1913,27 @@ def mv(x, vec, name=None): def det(x, name=None): """ Calculates determinant value of a square matrix or batches of square matrices. + Args: - x (Tensor): input (Tensor): the input matrix of size `(n, n)` or the batch of matrices of size - `(*, n, n)` where `*` is one or more batch dimensions. + x (Tensor): input (Tensor): the input matrix of size `(n, n)` or the + batch of matrices of size `(*, n, n)` where `*` is one or more + batch dimensions. + Returns: - y (Tensor):the determinant value of a square matrix or batches of square matrices. + Tensor, the determinant value of a square matrix or batches of square matrices. Examples: .. code-block:: python - import paddle + import paddle - x = paddle.randn([3,3,3]) + x = paddle.randn([3,3,3]) - A = paddle.linalg.det(x) + A = paddle.linalg.det(x) - print(A) + print(A) - # [ 0.02547996, 2.52317095, -6.15900707]) + # [ 0.02547996, 2.52317095, -6.15900707]) """ @@ -1978,18 +1983,18 @@ def slogdet(x, name=None): of the absolute value of determinant, respectively. Examples: - .. code-block:: python + .. code-block:: python - import paddle + import paddle - x = paddle.randn([3,3,3]) + x = paddle.randn([3,3,3]) - A = paddle.linalg.slogdet(x) + A = paddle.linalg.slogdet(x) - print(A) + print(A) - # [[ 1. , 1. , -1. ], - # [-0.98610914, -0.43010661, -0.10872950]]) + # [[ 1. , 1. , -1. ], + # [-0.98610914, -0.43010661, -0.10872950]]) """ if in_dygraph_mode(): @@ -2102,13 +2107,11 @@ def matrix_power(x, n, name=None): Specifically, - - If `n > 0`, it returns the matrix or a batch of matrices raised to the power - of `n`. + - If `n > 0`, it returns the matrix or a batch of matrices raised to the power of `n`. - If `n = 0`, it returns the identity matrix or a batch of identity matrices. - - If `n < 0`, it returns the inverse of each matrix (if invertible) raised to - the power of `abs(n)`. + - If `n < 0`, it returns the inverse of each matrix (if invertible) raised to the power of `abs(n)`. Args: x (Tensor): A square matrix or a batch of square matrices to be raised @@ -2243,10 +2246,12 @@ def lu(x, pivot=True, get_infos=False, name=None): Pivoting is done if pivot is set to True. P mat can be get by pivots: - # ones = eye(rows) #eye matrix of rank rows - # for i in range(cols): - # swap(ones[i], ones[pivots[i]]) - # return ones + + .. code-block:: text + ones = eye(rows) #eye matrix of rank rows + for i in range(cols): + swap(ones[i], ones[pivots[i]]) + return ones Args: @@ -2260,15 +2265,15 @@ def lu(x, pivot=True, get_infos=False, name=None): For more information, please refer to :ref:`api_guide_Name`. Returns: - factorization (Tensor): LU matrix, the factorization of input X. + factorization (Tensor), LU matrix, the factorization of input X. - pivots (IntTensor): the pivots of size(∗(N-2), min(m,n)). `pivots` stores all the - intermediate transpositions of rows. The final permutation `perm` could be - reconstructed by this, details refer to upper example. + pivots (IntTensor), the pivots of size(∗(N-2), min(m,n)). `pivots` stores all the + intermediate transpositions of rows. The final permutation `perm` could be + reconstructed by this, details refer to upper example. - infos (IntTensor, optional): if `get_infos` is `True`, this is a tensor of size (∗(N-2)) - where non-zero values indicate whether factorization for the matrix or each minibatch - has succeeded or failed. + infos (IntTensor, optional), if `get_infos` is `True`, this is a tensor of size (∗(N-2)) + where non-zero values indicate whether factorization for the matrix or each minibatch + has succeeded or failed. Examples: @@ -2342,9 +2347,11 @@ def lu_unpack(x, y, unpack_ludata=True, unpack_pivots=True, name=None): unpack L and U matrix from LU, unpack permutation matrix P from Pivtos . P mat can be get by pivots: - # ones = eye(rows) #eye matrix of rank rows - # for i in range(cols): - # swap(ones[i], ones[pivots[i]]) + + .. code-block:: text + ones = eye(rows) #eye matrix of rank rows + for i in range(cols): + swap(ones[i], ones[pivots[i]]) Args: @@ -2360,11 +2367,11 @@ def lu_unpack(x, y, unpack_ludata=True, unpack_pivots=True, name=None): For more information, please refer to :ref:`api_guide_Name`. Returns: - P (Tensor): Permutation matrix P of lu factorization. + P (Tensor), Permutation matrix P of lu factorization. - L (Tensor): The lower triangular matrix tensor of lu factorization. + L (Tensor), The lower triangular matrix tensor of lu factorization. - U (Tensor): The upper triangular matrix tensor of lu factorization. + U (Tensor), The upper triangular matrix tensor of lu factorization. Examples: @@ -2437,14 +2444,14 @@ def lu_unpack(x, y, unpack_ludata=True, unpack_pivots=True, name=None): def eig(x, name=None): """ - This API performs the eigenvalue decomposition of a square matrix or a batch of square matrices. + Performs the eigenvalue decomposition of a square matrix or a batch of square matrices. - .. note:: - If the matrix is a Hermitian or a real symmetric matrix, please use :ref:`paddle.linalg.eigh` instead, which is much faster. - If only eigenvalues is needed, please use :ref:`paddle.linalg.eigvals` instead. - If the matrix is of any shape, please use :ref:`paddle.linalg.svd`. - This API is only supported on CPU device. - The output datatype is always complex for both real and complex input. + Note: + - If the matrix is a Hermitian or a real symmetric matrix, please use :ref:`paddle.linalg.eigh` instead, which is much faster. + - If only eigenvalues is needed, please use :ref:`paddle.linalg.eigvals` instead. + - If the matrix is of any shape, please use :ref:`paddle.linalg.svd`. + - This API is only supported on CPU device. + - The output datatype is always complex for both real and complex input. Args: x (Tensor): A tensor with shape math:`[*, N, N]`, The data type of the x should be one of ``float32``, @@ -2460,16 +2467,14 @@ def eig(x, name=None): .. code-block:: python import paddle - import numpy as np paddle.device.set_device("cpu") - x_data = np.array([[1.6707249, 7.2249975, 6.5045543], + x = paddle.to_tensor([[1.6707249, 7.2249975, 6.5045543], [9.956216, 8.749598, 6.066444 ], - [4.4251957, 1.7983172, 0.370647 ]]).astype("float32") - x = paddle.to_tensor(x_data) + [4.4251957, 1.7983172, 0.370647 ]]) w, v = paddle.linalg.eig(x) - print(w) + print(v) # Tensor(shape=[3, 3], dtype=complex128, place=CPUPlace, stop_gradient=False, # [[(-0.5061363550800655+0j) , (-0.7971760990842826+0j) , # (0.18518077798279986+0j)], @@ -2478,7 +2483,7 @@ def eig(x, name=None): # [(-0.23142567697893396+0j), (0.4944999840400175+0j) , # (0.7058765252952796+0j) ]]) - print(v) + print(w) # Tensor(shape=[3], dtype=complex128, place=CPUPlace, stop_gradient=False, # [ (16.50471283351188+0j) , (-5.5034820550763515+0j) , # (-0.21026087843552282+0j)]) @@ -2520,8 +2525,8 @@ def eigvals(x, name=None): For more information, please refer to :ref:`api_guide_Name`. Returns: - Tensor: A tensor containing the unsorted eigenvalues which has the same batch dimensions with `x`. - The eigenvalues are complex-valued even when `x` is real. + Tensor, A tensor containing the unsorted eigenvalues which has the same batch + dimensions with `x`. The eigenvalues are complex-valued even when `x` is real. Examples: .. code-block:: python @@ -2662,18 +2667,17 @@ def eigh(x, UPLO='L', name=None): property. For more information, please refer to :ref:`api_guide_Name`. Returns: - - out_value(Tensor): A Tensor with shape [*, N] and data type of float32 and float64. The eigenvalues of eigh op. - out_vector(Tensor): A Tensor with shape [*, N, N] and data type of float32,float64,complex64 and complex128. The eigenvectors of eigh op. + - out_value(Tensor): A Tensor with shape [*, N] and data type of float32 and float64. + The eigenvalues of eigh op. + - out_vector(Tensor): A Tensor with shape [*, N, N] and data type of float32,float64, + complex64 and complex128. The eigenvectors of eigh op. Examples: .. code-block:: python - import numpy as np import paddle - x_data = np.array([[1, -2j], [2j, 5]]) - x = paddle.to_tensor(x_data) + x = paddle.to_tensor([[1, -2j], [2j, 5]]) out_value, out_vector = paddle.linalg.eigh(x, UPLO='L') print(out_value) #[0.17157288, 5.82842712] @@ -3060,8 +3064,8 @@ def solve(x, y, name=None): .. math:: Out = X^-1 * Y - Specifically, - - This system of linear equations has one solution if and only if input 'X' is invertible. + + Specifically, this system of linear equations has one solution if and only if input 'X' is invertible. Args: x (Tensor): A square matrix or a batch of square matrices. Its shape should be `[*, M, M]`, where `*` is zero or @@ -3076,23 +3080,21 @@ def solve(x, y, name=None): Its data type should be the same as that of `x`. Examples: - .. code-block:: python - # a square system of linear equations: - # 2*X0 + X1 = 9 - # X0 + 2*X1 = 8 + .. code-block:: python - import paddle - import numpy as np + # a square system of linear equations: + # 2*X0 + X1 = 9 + # X0 + 2*X1 = 8 + + import paddle - np_x = np.array([[3, 1],[1, 2]]) - np_y = np.array([9, 8]) - x = paddle.to_tensor(np_x, dtype="float64") - y = paddle.to_tensor(np_y, dtype="float64") - out = paddle.linalg.solve(x, y) + x = paddle.to_tensor([[3, 1],[1, 2]], dtype="float64") + y = paddle.to_tensor([9, 8], dtype="float64") + out = paddle.linalg.solve(x, y) - print(out) - # [2., 3.]) + print(out) + # [2., 3.]) """ if in_dygraph_mode(): return _C_ops.solve(x, y) @@ -3122,23 +3124,23 @@ def triangular_solve( Input `x` and `y` is 2D matrices or batches of 2D matrices. If the inputs are batches, the outputs is also batches. - Args: - x (Tensor): The input triangular coefficient matrix. Its shape should be `[*, M, M]`, where `*` is zero or - more batch dimensions. Its data type should be float32 or float64. - y (Tensor): Multiple right-hand sides of system of equations. Its shape should be `[*, M, K]`, where `*` is - zero or more batch dimensions. Its data type should be float32 or float64. - upper (bool, optional): Whether to solve the upper-triangular system of equations (default) or the lower-triangular - system of equations. Default: True. - transpose (bool, optional): whether `x` should be transposed before calculation. Default: False. - unitriangular (bool, optional): whether `x` is unit triangular. If True, the diagonal elements of `x` are assumed - to be 1 and not referenced from `x` . Default: False. - name(str, optional): Name for the operation (optional, default is None). - For more information, please refer to :ref:`api_guide_Name`. - - Returns: - Tensor: The solution of the system of equations. Its data type should be the same as that of `x`. - - Examples: + Args: + x (Tensor): The input triangular coefficient matrix. Its shape should be `[*, M, M]`, where `*` is zero or + more batch dimensions. Its data type should be float32 or float64. + y (Tensor): Multiple right-hand sides of system of equations. Its shape should be `[*, M, K]`, where `*` is + zero or more batch dimensions. Its data type should be float32 or float64. + upper (bool, optional): Whether to solve the upper-triangular system of equations (default) or the lower-triangular + system of equations. Default: True. + transpose (bool, optional): whether `x` should be transposed before calculation. Default: False. + unitriangular (bool, optional): whether `x` is unit triangular. If True, the diagonal elements of `x` are assumed + to be 1 and not referenced from `x` . Default: False. + name(str, optional): Name for the operation (optional, default is None). + For more information, please refer to :ref:`api_guide_Name`. + + Returns: + Tensor: The solution of the system of equations. Its data type should be the same as that of `x`. + + Examples: .. code-block:: python # a square system of linear equations: @@ -3146,12 +3148,7 @@ def triangular_solve( # 2*x2 + x3 = -9 # -x3 = 5 - <<<<<<< HEAD import paddle - import numpy as np - ======= - import paddle - >>>>>>> 912be4f897 (fix numpy issue in codeblock examples for operators under python/paddle/tensor folder (#46765)) x = paddle.to_tensor([[1, 1, 1], [0, 2, 1], @@ -3216,18 +3213,18 @@ def cholesky_solve(x, y, upper=False, name=None): Tensor: The solution of the system of equations. Its data type is the same as that of `x`. Examples: - .. code-block:: python + .. code-block:: python - import paddle + import paddle - u = paddle.to_tensor([[1, 1, 1], - [0, 2, 1], - [0, 0,-1]], dtype="float64") - b = paddle.to_tensor([[0], [-9], [5]], dtype="float64") - out = paddle.linalg.cholesky_solve(b, u, upper=True) + u = paddle.to_tensor([[1, 1, 1], + [0, 2, 1], + [0, 0,-1]], dtype="float64") + b = paddle.to_tensor([[0], [-9], [5]], dtype="float64") + out = paddle.linalg.cholesky_solve(b, u, upper=True) - print(out) - # [-2.5, -7, 9.5] + print(out) + # [-2.5, -7, 9.5] """ if in_dygraph_mode(): return _C_ops.cholesky_solve(x, y, upper) diff --git a/python/paddle/tensor/logic.py b/python/paddle/tensor/logic.py index b9ba83cdc0194c2a13e68e05fd41d2713f735379..c998c198d49216b33aab9dfd911655ba1d5c9e01 100755 --- a/python/paddle/tensor/logic.py +++ b/python/paddle/tensor/logic.py @@ -96,7 +96,7 @@ def logical_and(x, y, out=None, name=None): out = x \&\& y - .. note:: + Note: ``paddle.logical_and`` supports broadcasting. If you want know more about broadcasting, please refer to :ref:`user_guide_broadcasting`. Args: @@ -136,7 +136,7 @@ def logical_or(x, y, out=None, name=None): out = x || y - .. note:: + Note: ``paddle.logical_or`` supports broadcasting. If you want know more about broadcasting, please refer to :ref:`user_guide_broadcasting`. Args: @@ -178,7 +178,7 @@ def logical_xor(x, y, out=None, name=None): out = (x || y) \&\& !(x \&\& y) - .. note:: + Note: ``paddle.logical_xor`` supports broadcasting. If you want know more about broadcasting, please refer to :ref:`user_guide_broadcasting`. Args: @@ -974,13 +974,6 @@ def isclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None): Returns: Tensor: ${out_comment}. - Raises: - TypeError: The data type of ``x`` must be one of float32, float64. - TypeError: The data type of ``y`` must be one of float32, float64. - TypeError: The type of ``rtol`` must be float. - TypeError: The type of ``atol`` must be float. - TypeError: The type of ``equal_nan`` must be bool. - Examples: .. code-block:: python diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py old mode 100755 new mode 100644 index c2587881739f5ce7fe7e3db58d53f2e597c7526c..f987e8b89cf2549996cdcf09f374808086dc4224 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -177,10 +177,6 @@ def slice(input, axes, starts, ends): Returns: Tensor: A ``Tensor``. The data type is same as ``input``. - Raises: - TypeError: The type of ``starts`` must be list, tuple or Tensor. - TypeError: The type of ``ends`` must be list, tuple or Tensor. - Examples: .. code-block:: python @@ -510,9 +506,6 @@ def unstack(x, axis=0, num=None): Returns: list(Tensor): The unstacked Tensors list. The list elements are N-D Tensors of data types float32, float64, int32, int64. - Raises: - ValueError: If x.shape[axis] <= 0 or axis is not in range [-D, D). - Examples: .. code-block:: python @@ -1229,8 +1222,10 @@ def broadcast_tensors(input, name=None): """ This OP broadcast a list of tensors following broadcast semantics - .. note:: - If you want know more about broadcasting, please refer to :ref:`user_guide_broadcasting`. + Note: + If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . + + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor Args: input (list|tuple): ``input`` is a Tensor list or Tensor tuple which is with data type bool, @@ -1545,10 +1540,6 @@ def flatten(x, start_axis=0, stop_axis=-1, name=None): axes flattened by indicated start axis and end axis. \ A Tensor with data type same as input x. - Raises: - ValueError: If x is not a Tensor. - ValueError: If start_axis or stop_axis is illegal. - Examples: .. code-block:: python @@ -2250,7 +2241,8 @@ def unique_consecutive( r""" Eliminates all but the first element from every consecutive group of equivalent elements. - .. note:: This function is different from :func:`paddle.unique` in the sense that this function + Note: + This function is different from :func:`paddle.unique` in the sense that this function only eliminates consecutive duplicate values. This semantics is similar to `std::unique` in C++. Args: @@ -4626,8 +4618,9 @@ def put_along_axis(arr, indices, values, axis, reduce='assign'): indices (Tensor) : Indices to put along each 1d slice of arr. This must match the dimension of arr, and need to broadcast against arr. Supported data type are int and int64. axis (int) : The axis to put 1d slices along. - reduce (string | optinal) : The reduce operation, default is 'assign', support 'add', 'assign', 'mul' and 'multiply'. - Returns : + reduce (str, optional): The reduce operation, default is 'assign', support 'add', 'assign', 'mul' and 'multiply'. + + Returns: Tensor: The indexed element, same dtype with arr Examples: diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index d72a1b2de2b1edd66a86fca1ba02a733148de2b4..eb7fe5263b14594dc170ed03439e6335a1d7eba5 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4146,9 +4146,8 @@ def lerp_(x, y, weight, name=None): def erfinv(x, name=None): r""" - The inverse error function of x. + The inverse error function of x. Please refer to :ref:`api_paddle_erf` - Equation: .. math:: erfinv(erf(x)) = x. @@ -4158,7 +4157,7 @@ def erfinv(x, name=None): name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: - out (Tensor): An N-D Tensor, the shape and data type is the same with input. + out (Tensor), an N-D Tensor, the shape and data type is the same with input. Example: .. code-block:: python @@ -4260,8 +4259,6 @@ def rad2deg(x, name=None): def deg2rad(x, name=None): r""" Convert each of the elements of input x from degrees to angles in radians. - - Equation: .. math:: deg2rad(x)=\pi * x / 180 @@ -4277,8 +4274,6 @@ def deg2rad(x, name=None): .. code-block:: python import paddle - import numpy as np - x1 = paddle.to_tensor([180.0, -180.0, 360.0, -360.0, 90.0, -90.0]) result1 = paddle.deg2rad(x1) print(result1) @@ -4705,18 +4700,18 @@ def angle(x, name=None): return out def heaviside(x, y, name=None): - """ + r""" Computes the Heaviside step function determined by corresponding element in y for each element in x. The equation is .. math:: heaviside(x, y)= \left\{ - \\begin{array}{lcl} - 0,& &\\text{if} \ x < 0, \\\\ - y,& &\\text{if} \ x = 0, \\\\ - 1,& &\\text{if} \ x > 0. + \begin{array}{lcl} + 0,& &\text{if} \ x < 0, \\ + y,& &\text{if} \ x = 0, \\ + 1,& &\text{if} \ x > 0. \end{array} - \\right. + \right. Note: ``paddle.heaviside`` supports broadcasting. If you want know more about broadcasting, please refer to :ref:`user_guide_broadcasting`. @@ -4742,7 +4737,7 @@ def heaviside(x, y, name=None): paddle.heaviside(x, y) # [[0. , 0.20000000, 1. ], # [0. , 1. , 0.30000001]] - """ + """ op_type = 'elementwise_heaviside' axis = -1 act = None diff --git a/python/paddle/utils/cpp_extension/cpp_extension.py b/python/paddle/utils/cpp_extension/cpp_extension.py index 0e265d51a7e8cfd57bba4de6edde21e75fcde217..a8821e7a03e0f8c8c39676926e20de33900cea3b 100644 --- a/python/paddle/utils/cpp_extension/cpp_extension.py +++ b/python/paddle/utils/cpp_extension/cpp_extension.py @@ -106,7 +106,7 @@ def setup(**attr): If the above conditions are not met, the corresponding warning will be printed, and a fatal error may occur because of ABI compatibility. - .. note:: + Note: 1. Currently we support Linux, MacOS and Windows platfrom. 2. On Linux platform, we recommend to use GCC 8.2 as soft linking condidate of ``/usr/bin/cc`` . @@ -266,7 +266,7 @@ def CppExtension(sources, *args, **kwargs): ) - .. note:: + Note: It is mainly used in ``setup`` and the nama of built shared library keeps same as ``name`` argument specified in ``setup`` interface. @@ -318,7 +318,7 @@ def CUDAExtension(sources, *args, **kwargs): ) - .. note:: + Note: It is mainly used in ``setup`` and the nama of built shared library keeps same as ``name`` argument specified in ``setup`` interface. @@ -329,7 +329,7 @@ def CUDAExtension(sources, *args, **kwargs): **kwargs(dict[option], optional): Specify other arguments same as ``setuptools.Extension`` . Returns: - setuptools.Extension: An instance of setuptools.Extension + setuptools.Extension: An instance of setuptools.Extension. """ kwargs = normalize_extension_kwargs(kwargs, use_cuda=True) # Note(Aurelius84): While using `setup` and `jit`, the Extension `name` will @@ -840,7 +840,7 @@ def load( ``python setup.py install`` command. The interface contains all compiling and installing process underground. - .. note:: + Note: 1. Currently we support Linux, MacOS and Windows platfrom. 2. On Linux platform, we recommend to use GCC 8.2 as soft linking condidate of ``/usr/bin/cc`` . diff --git a/python/paddle/vision/datasets/cifar.py b/python/paddle/vision/datasets/cifar.py index 8c4d9ac7815908851213ac69dc8b83bdbb357700..b274ab42a23f0a17ad1e642c55331104844d9911 100644 --- a/python/paddle/vision/datasets/cifar.py +++ b/python/paddle/vision/datasets/cifar.py @@ -116,11 +116,7 @@ class Cifar10(Dataset): assert mode.lower() in [ 'train', 'test', - 'train', - 'test', - ], "mode should be 'train10', 'test10', 'train100' or 'test100', but got {}".format( - mode - ) + ], "mode.lower() should be 'train' or 'test', but got {}".format(mode) self.mode = mode.lower() if backend is None: diff --git a/python/paddle/vision/ops.py b/python/paddle/vision/ops.py index d497da8f85ebf5eacd178612fec67acfbb36a6e0..850e9988ee139c3101c6e21bb551f7360ee3e4de 100755 --- a/python/paddle/vision/ops.py +++ b/python/paddle/vision/ops.py @@ -175,16 +175,6 @@ def yolo_loss( Returns: Tensor: A 1-D tensor with shape [N], the value of yolov3 loss - Raises: - TypeError: Input x of yolov3_loss must be Tensor - TypeError: Input gtbox of yolov3_loss must be Tensor - TypeError: Input gtlabel of yolov3_loss must be Tensor - TypeError: Input gtscore of yolov3_loss must be None or Tensor - TypeError: Attr anchors of yolov3_loss must be list or tuple - TypeError: Attr class_num of yolov3_loss must be an integer - TypeError: Attr ignore_thresh of yolov3_loss must be a float number - TypeError: Attr use_label_smooth of yolov3_loss must be a bool value - Examples: .. code-block:: python @@ -397,12 +387,6 @@ def yolo_box( and a 3-D tensor with shape [N, M, :attr:`class_num`], the classification scores of boxes. - Raises: - TypeError: Input x of yolov_box must be Tensor - TypeError: Attr anchors of yolo box must be list or tuple - TypeError: Attr class_num of yolo box must be an integer - TypeError: Attr conf_thresh of yolo box must be a float number - Examples: .. code-block:: python @@ -957,9 +941,7 @@ def deform_conv2d( Returns: Tensor: The tensor variable storing the deformable convolution \ result. A Tensor with type float32, float64. - Raises: - ValueError: If the shapes of input, filter_size, stride, padding and - groups mismatch. + Examples: .. code-block:: python diff --git a/python/paddle/vision/transforms/transforms.py b/python/paddle/vision/transforms/transforms.py index 301252a048b7a7498afb83cc9ad7555f7676063a..25cd2600ea0bbcd0327ff1caa43f71cfb5b155e8 100644 --- a/python/paddle/vision/transforms/transforms.py +++ b/python/paddle/vision/transforms/transforms.py @@ -51,33 +51,38 @@ def _get_image_size(img): return img.shape[2:][::-1] # nchw -> wh else: raise ValueError( - "The dim for input Tensor should be 3-D or 4-D, but received {}" - .format(len(img.shape))) + "The dim for input Tensor should be 3-D or 4-D, but received {}".format( + len(img.shape) + ) + ) else: raise TypeError("Unexpected type {}".format(type(img))) -def _check_input(value, - name, - center=1, - bound=(0, float('inf')), - clip_first_on_zero=True): +def _check_input( + value, name, center=1, bound=(0, float('inf')), clip_first_on_zero=True +): if isinstance(value, numbers.Number): if value < 0: raise ValueError( "If {} is a single number, it must be non negative.".format( - name)) + name + ) + ) value = [center - value, center + value] if clip_first_on_zero: value[0] = max(value[0], 0) elif isinstance(value, (tuple, list)) and len(value) == 2: if not bound[0] <= value[0] <= value[1] <= bound[1]: - raise ValueError("{} values should be between {}".format( - name, bound)) + raise ValueError( + "{} values should be between {}".format(name, bound) + ) else: raise TypeError( - "{} should be a single number or a list/tuple with lenght 2.". - format(name)) + "{} should be a single number or a list/tuple with lenght 2.".format( + name + ) + ) if value[0] == value[1] == center: value = None @@ -97,7 +102,7 @@ class Compose(object): object will call each given :attr:`transforms` sequencely. Examples: - + .. code-block:: python from paddle.vision.datasets import Flowers @@ -121,8 +126,10 @@ class Compose(object): data = f(data) except Exception as e: stack_info = traceback.format_exc() - print("fail to perform transform [{}] with error: " - "{} and stack:\n{}".format(f, e, str(stack_info))) + print( + "fail to perform transform [{}] with error: " + "{} and stack:\n{}".format(f, e, str(stack_info)) + ) raise e return data @@ -139,12 +146,12 @@ class BaseTransform(object): """ Base class of all transforms used in computer vision. - calling logic: + calling logic: if keys is None: _get_params -> _apply_image() else: - _get_params -> _apply_*() for * in keys + _get_params -> _apply_*() for * in keys If you want to implement a self-defined transform method for image, rewrite _apply_* method in subclass. @@ -153,25 +160,25 @@ class BaseTransform(object): keys (list[str]|tuple[str], optional): Input type. Input is a tuple contains different structures, key is used to specify the type of input. For example, if your input is image type, then the key can be None or ("image"). if your input - is (image, image) type, then the keys should be ("image", "image"). + is (image, image) type, then the keys should be ("image", "image"). if your input is (image, boxes), then the keys should be ("image", "boxes"). Current available strings & data type are describe below: - - "image": input image, with shape of (H, W, C) - - "coords": coordinates, with shape of (N, 2) - - "boxes": bounding boxes, with shape of (N, 4), "xyxy" format, - - the 1st "xy" represents top left point of a box, + - "image": input image, with shape of (H, W, C) + - "coords": coordinates, with shape of (N, 2) + - "boxes": bounding boxes, with shape of (N, 4), "xyxy" format, + + the 1st "xy" represents top left point of a box, the 2nd "xy" represents right bottom point. - "mask": map used for segmentation, with shape of (H, W, 1) - + You can also customize your data types only if you implement the corresponding _apply_*() methods, otherwise ``NotImplementedError`` will be raised. - + Examples: - + .. code-block:: python import numpy as np @@ -220,7 +227,7 @@ class BaseTransform(object): maxxy = coords.max(axis=1) trans_boxes = np.concatenate((minxy, maxxy), axis=1) return trans_boxes - + # if you only want to transform image, do not need to rewrite this function def _apply_mask(self, mask): if self.params['flip']: @@ -245,14 +252,16 @@ class BaseTransform(object): def __init__(self, keys=None): if keys is None: - keys = ("image", ) + keys = ("image",) elif not isinstance(keys, Sequence): raise ValueError( - "keys should be a sequence, but got keys={}".format(keys)) + "keys should be a sequence, but got keys={}".format(keys) + ) for k in keys: if self._get_apply(k) is None: raise NotImplementedError( - "{} is unsupported data structure".format(k)) + "{} is unsupported data structure".format(k) + ) self.keys = keys # storage some params get from function get_params() @@ -264,7 +273,7 @@ class BaseTransform(object): def __call__(self, inputs): """Apply transform on single input data""" if not isinstance(inputs, tuple): - inputs = (inputs, ) + inputs = (inputs,) self.params = self._get_params(inputs) @@ -276,7 +285,7 @@ class BaseTransform(object): else: outputs.append(apply_func(inputs[i])) if len(inputs) > len(self.keys): - outputs.extend(inputs[len(self.keys):]) + outputs.extend(inputs[len(self.keys) :]) if len(outputs) == 1: outputs = outputs[0] @@ -302,22 +311,22 @@ class ToTensor(BaseTransform): Converts a PIL.Image or numpy.ndarray (H x W x C) to a paddle.Tensor of shape (C x H x W). - If input is a grayscale image (H x W), it will be converted to a image of shape (H x W x 1). + If input is a grayscale image (H x W), it will be converted to an image of shape (H x W x 1). And the shape of output tensor will be (1 x H x W). If you want to keep the shape of output tensor as (H x W x C), you can set data_format = ``HWC`` . - Converts a PIL.Image or numpy.ndarray in the range [0, 255] to a paddle.Tensor in the - range [0.0, 1.0] if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, - RGBA, CMYK, 1) or if the numpy.ndarray has dtype = np.uint8. + Converts a PIL.Image or numpy.ndarray in the range [0, 255] to a paddle.Tensor in the + range [0.0, 1.0] if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, + RGBA, CMYK, 1) or if the numpy.ndarray has dtype = np.uint8. In the other cases, tensors are returned without scaling. Args: - data_format (str, optional): Data format of output tensor, should be 'HWC' or + data_format (str, optional): Data format of output tensor, should be 'HWC' or 'CHW'. Default: 'CHW'. keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. - + Shape: - img(PIL.Image|np.ndarray): The input image with shape (H x W x C). - output(np.ndarray): A tensor with shape (C x H x W) or (H x W x C) according option data_format. @@ -326,7 +335,7 @@ class ToTensor(BaseTransform): A callable object of ToTensor. Examples: - + .. code-block:: python import numpy as np @@ -340,10 +349,10 @@ class ToTensor(BaseTransform): transform = T.ToTensor() tensor = transform(fake_img) - + print(tensor.shape) # [3, 4, 5] - + print(tensor.dtype) # paddle.float32 """ @@ -372,19 +381,19 @@ class Resize(BaseTransform): smaller edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to (size * height / width, size) - interpolation (int|str, optional): Interpolation method. Default: 'bilinear'. - when use pil backend, support method are as following: - - "nearest": Image.NEAREST, - - "bilinear": Image.BILINEAR, - - "bicubic": Image.BICUBIC, - - "box": Image.BOX, - - "lanczos": Image.LANCZOS, + interpolation (int|str, optional): Interpolation method. Default: 'bilinear'. + when use pil backend, support method are as following: + - "nearest": Image.NEAREST, + - "bilinear": Image.BILINEAR, + - "bicubic": Image.BICUBIC, + - "box": Image.BOX, + - "lanczos": Image.LANCZOS, - "hamming": Image.HAMMING - when use cv2 backend, support method are as following: - - "nearest": cv2.INTER_NEAREST, - - "bilinear": cv2.INTER_LINEAR, - - "area": cv2.INTER_AREA, - - "bicubic": cv2.INTER_CUBIC, + when use cv2 backend, support method are as following: + - "nearest": cv2.INTER_NEAREST, + - "bilinear": cv2.INTER_LINEAR, + - "area": cv2.INTER_AREA, + - "bicubic": cv2.INTER_CUBIC, - "lanczos": cv2.INTER_LANCZOS4 keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. @@ -396,7 +405,7 @@ class Resize(BaseTransform): A callable object of Resize. Examples: - + .. code-block:: python import numpy as np @@ -418,8 +427,9 @@ class Resize(BaseTransform): def __init__(self, size, interpolation='bilinear', keys=None): super(Resize, self).__init__(keys) - assert isinstance(size, int) or (isinstance(size, Iterable) - and len(size) == 2) + assert isinstance(size, int) or ( + isinstance(size, Iterable) and len(size) == 2 + ) self.size = size self.interpolation = interpolation @@ -435,22 +445,22 @@ class RandomResizedCrop(BaseTransform): Args: size (int|list|tuple): Target size of output image, with (height, width) shape. - scale (list|tuple): Scale range of the cropped image before resizing, relatively to the origin + scale (list|tuple): Scale range of the cropped image before resizing, relatively to the origin image. Default: (0.08, 1.0) ratio (list|tuple): Range of aspect ratio of the origin aspect ratio cropped. Default: (0.75, 1.33) - interpolation (int|str, optional): Interpolation method. Default: 'bilinear'. when use pil backend, - support method are as following: - - "nearest": Image.NEAREST, - - "bilinear": Image.BILINEAR, - - "bicubic": Image.BICUBIC, - - "box": Image.BOX, - - "lanczos": Image.LANCZOS, + interpolation (int|str, optional): Interpolation method. Default: 'bilinear'. when use pil backend, + support method are as following: + - "nearest": Image.NEAREST, + - "bilinear": Image.BILINEAR, + - "bicubic": Image.BICUBIC, + - "box": Image.BOX, + - "lanczos": Image.LANCZOS, - "hamming": Image.HAMMING - when use cv2 backend, support method are as following: - - "nearest": cv2.INTER_NEAREST, - - "bilinear": cv2.INTER_LINEAR, - - "area": cv2.INTER_AREA, - - "bicubic": cv2.INTER_CUBIC, + when use cv2 backend, support method are as following: + - "nearest": cv2.INTER_NEAREST, + - "bilinear": cv2.INTER_LINEAR, + - "area": cv2.INTER_AREA, + - "bicubic": cv2.INTER_CUBIC, - "lanczos": cv2.INTER_LANCZOS4 keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. @@ -462,7 +472,7 @@ class RandomResizedCrop(BaseTransform): A callable object of RandomResizedCrop. Examples: - + .. code-block:: python import numpy as np @@ -478,19 +488,21 @@ class RandomResizedCrop(BaseTransform): """ - def __init__(self, - size, - scale=(0.08, 1.0), - ratio=(3. / 4, 4. / 3), - interpolation='bilinear', - keys=None): + def __init__( + self, + size, + scale=(0.08, 1.0), + ratio=(3.0 / 4, 4.0 / 3), + interpolation='bilinear', + keys=None, + ): super(RandomResizedCrop, self).__init__(keys) if isinstance(size, int): self.size = (size, size) else: self.size = size - assert (scale[0] <= scale[1]), "scale should be of kind (min, max)" - assert (ratio[0] <= ratio[1]), "ratio should be of kind (min, max)" + assert scale[0] <= scale[1], "scale should be of kind (min, max)" + assert ratio[0] <= ratio[1], "ratio should be of kind (min, max)" self.scale = scale self.ratio = ratio self.interpolation = interpolation @@ -550,7 +562,7 @@ class CenterCrop(BaseTransform): A callable object of CenterCrop. Examples: - + .. code-block:: python import numpy as np @@ -591,7 +603,7 @@ class RandomHorizontalFlip(BaseTransform): A callable object of RandomHorizontalFlip. Examples: - + .. code-block:: python import numpy as np @@ -632,7 +644,7 @@ class RandomVerticalFlip(BaseTransform): A callable object of RandomVerticalFlip. Examples: - + .. code-block:: python import numpy as np @@ -668,7 +680,7 @@ class Normalize(BaseTransform): Args: mean (int|float|list|tuple, optional): Sequence of means for each channel. std (int|float|list|tuple, optional): Sequence of standard deviations for each channel. - data_format (str, optional): Data format of img, should be 'HWC' or + data_format (str, optional): Data format of img, should be 'HWC' or 'CHW'. Default: 'CHW'. to_rgb (bool, optional): Whether to convert to rgb. Default: False. keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. @@ -681,7 +693,7 @@ class Normalize(BaseTransform): A callable object of Normalize. Examples: - + .. code-block:: python :name: code-example import paddle @@ -698,15 +710,12 @@ class Normalize(BaseTransform): # (300, 320, 3) print(fake_img.max(), fake_img.min()) # 0.99999905 -0.999974 - + """ - def __init__(self, - mean=0.0, - std=1.0, - data_format='CHW', - to_rgb=False, - keys=None): + def __init__( + self, mean=0.0, std=1.0, data_format='CHW', to_rgb=False, keys=None + ): super(Normalize, self).__init__(keys) if isinstance(mean, numbers.Number): mean = [mean, mean, mean] @@ -720,30 +729,31 @@ class Normalize(BaseTransform): self.to_rgb = to_rgb def _apply_image(self, img): - return F.normalize(img, self.mean, self.std, self.data_format, - self.to_rgb) + return F.normalize( + img, self.mean, self.std, self.data_format, self.to_rgb + ) class Transpose(BaseTransform): """Transpose input data to a target format. For example, most transforms use HWC mode image, while the Neural Network might use CHW mode input tensor. - output image will be an instance of numpy.ndarray. + output image will be an instance of numpy.ndarray. Args: order (list|tuple, optional): Target order of input data. Default: (2, 0, 1). keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. - + Shape: - img(PIL.Image|np.ndarray|Paddle.Tensor): The input image with shape (H x W x C). - - output(np.ndarray|Paddle.Tensor): A transposed array or tensor. If input + - output(np.ndarray|Paddle.Tensor): A transposed array or tensor. If input is a PIL.Image, output will be converted to np.ndarray automatically. Returns: A callable object of Transpose. Examples: - + .. code-block:: python import numpy as np @@ -756,7 +766,7 @@ class Transpose(BaseTransform): fake_img = transform(fake_img) print(fake_img.shape) - + """ def __init__(self, order=(2, 0, 1), keys=None): @@ -791,7 +801,7 @@ class BrightnessTransform(BaseTransform): A callable object of BrightnessTransform. Examples: - + .. code-block:: python import numpy as np @@ -803,7 +813,7 @@ class BrightnessTransform(BaseTransform): fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) fake_img = transform(fake_img) - + """ def __init__(self, value, keys=None): @@ -834,7 +844,7 @@ class ContrastTransform(BaseTransform): A callable object of ContrastTransform. Examples: - + .. code-block:: python import numpy as np @@ -879,7 +889,7 @@ class SaturationTransform(BaseTransform): A callable object of SaturationTransform. Examples: - + .. code-block:: python import numpy as np @@ -889,7 +899,7 @@ class SaturationTransform(BaseTransform): transform = SaturationTransform(0.4) fake_img = Image.fromarray((np.random.rand(224, 224, 3) * 255.).astype(np.uint8)) - + fake_img = transform(fake_img) """ @@ -922,7 +932,7 @@ class HueTransform(BaseTransform): A callable object of HueTransform. Examples: - + .. code-block:: python import numpy as np @@ -939,11 +949,9 @@ class HueTransform(BaseTransform): def __init__(self, value, keys=None): super(HueTransform, self).__init__(keys) - self.value = _check_input(value, - 'hue', - center=0, - bound=(-0.5, 0.5), - clip_first_on_zero=False) + self.value = _check_input( + value, 'hue', center=0, bound=(-0.5, 0.5), clip_first_on_zero=False + ) def _apply_image(self, img): if self.value is None: @@ -975,7 +983,7 @@ class ColorJitter(BaseTransform): A callable object of ColorJitter. Examples: - + .. code-block:: python import numpy as np @@ -990,12 +998,9 @@ class ColorJitter(BaseTransform): """ - def __init__(self, - brightness=0, - contrast=0, - saturation=0, - hue=0, - keys=None): + def __init__( + self, brightness=0, contrast=0, saturation=0, hue=0, keys=None + ): super(ColorJitter, self).__init__(keys) self.brightness = brightness self.contrast = contrast @@ -1038,8 +1043,9 @@ class ColorJitter(BaseTransform): Returns: PIL Image: Color jittered image. """ - transform = self._get_param(self.brightness, self.contrast, - self.saturation, self.hue) + transform = self._get_param( + self.brightness, self.contrast, self.saturation, self.hue + ) return transform(img) @@ -1051,7 +1057,7 @@ class RandomCrop(BaseTransform): int instead of sequence like (h, w), a square crop (size, size) is made. padding (int|sequence, optional): Optional padding on each border - of the image. If a sequence of length 4 is provided, it is used to pad left, + of the image. If a sequence of length 4 is provided, it is used to pad left, top, right, bottom borders respectively. Default: None, without padding. pad_if_needed (boolean, optional): It will pad the image if smaller than the desired size to avoid raising an exception. Default: False. @@ -1074,7 +1080,7 @@ class RandomCrop(BaseTransform): padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode will result in [2, 1, 1, 2, 3, 4, 4, 3] keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. - + Shape - img(PIL.Image|np.ndarray|Paddle.Tensor): The input image with shape (H x W x C). - output(PIL.Image|np.ndarray|Paddle.Tensor): A random cropped image. @@ -1083,7 +1089,7 @@ class RandomCrop(BaseTransform): A callable object of RandomCrop. Examples: - + .. code-block:: python :name: code-example1 @@ -1098,13 +1104,15 @@ class RandomCrop(BaseTransform): print(crop_img.shape) # [3, 224, 224] """ - def __init__(self, - size, - padding=None, - pad_if_needed=False, - fill=0, - padding_mode='constant', - keys=None): + def __init__( + self, + size, + padding=None, + pad_if_needed=False, + fill=0, + padding_mode='constant', + keys=None, + ): super(RandomCrop, self).__init__(keys) if isinstance(size, numbers.Number): self.size = (int(size), int(size)) @@ -1149,12 +1157,14 @@ class RandomCrop(BaseTransform): # pad the width if needed if self.pad_if_needed and w < self.size[1]: - img = F.pad(img, (self.size[1] - w, 0), self.fill, - self.padding_mode) + img = F.pad( + img, (self.size[1] - w, 0), self.fill, self.padding_mode + ) # pad the height if needed if self.pad_if_needed and h < self.size[0]: - img = F.pad(img, (0, self.size[0] - h), self.fill, - self.padding_mode) + img = F.pad( + img, (0, self.size[0] - h), self.fill, self.padding_mode + ) i, j, h, w = self._get_param(img, self.size) @@ -1174,16 +1184,16 @@ class Pad(BaseTransform): length 3, it is used to fill R, G, B channels respectively. This value is only used when the padding_mode is constant padding_mode (str): Type of padding. Should be: constant, edge, reflect or symmetric. Default is constant. - ``constant`` means pads with a constant value, this value is specified with fill. - ``edge`` means pads with the last value at the edge of the image. - ``reflect`` means pads with reflection of image (without repeating the last value on the edge) - padding ``[1, 2, 3, 4]`` with 2 elements on both sides in reflect mode + ``constant`` means pads with a constant value, this value is specified with fill. + ``edge`` means pads with the last value at the edge of the image. + ``reflect`` means pads with reflection of image (without repeating the last value on the edge) + padding ``[1, 2, 3, 4]`` with 2 elements on both sides in reflect mode will result in ``[3, 2, 1, 2, 3, 4, 3, 2]``. ``symmetric`` menas pads with reflection of image (repeating the last value on the edge) - padding ``[1, 2, 3, 4]`` with 2 elements on both sides in symmetric mode + padding ``[1, 2, 3, 4]`` with 2 elements on both sides in symmetric mode will result in ``[2, 1, 1, 2, 3, 4, 4, 3]``. keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. - + Shape: - img(PIL.Image|np.ndarray|Paddle.Tensor): The input image with shape (H x W x C). - output(PIL.Image|np.ndarray|Paddle.Tensor): A paded image. @@ -1192,7 +1202,7 @@ class Pad(BaseTransform): A callable object of Pad. Examples: - + .. code-block:: python import numpy as np @@ -1219,8 +1229,9 @@ class Pad(BaseTransform): if isinstance(padding, Sequence) and len(padding) not in [2, 4]: raise ValueError( - "Padding must be an int or a 2, or 4 element tuple, not a " + - "{} element tuple".format(len(padding))) + "Padding must be an int or a 2, or 4 element tuple, not a " + + "{} element tuple".format(len(padding)) + ) super(Pad, self).__init__(keys) self.padding = padding @@ -1239,19 +1250,23 @@ class Pad(BaseTransform): def _check_sequence_input(x, name, req_sizes): - msg = req_sizes[0] if len(req_sizes) < 2 else " or ".join( - [str(s) for s in req_sizes]) + msg = ( + req_sizes[0] + if len(req_sizes) < 2 + else " or ".join([str(s) for s in req_sizes]) + ) if not isinstance(x, Sequence): raise TypeError(f"{name} should be a sequence of length {msg}.") if len(x) not in req_sizes: raise ValueError(f"{name} should be sequence of length {msg}.") -def _setup_angle(x, name, req_sizes=(2, )): +def _setup_angle(x, name, req_sizes=(2,)): if isinstance(x, numbers.Number): if x < 0: raise ValueError( - f"If {name} is a single number, it must be positive.") + f"If {name} is a single number, it must be positive." + ) x = [-x, x] else: _check_sequence_input(x, name, req_sizes) @@ -1268,25 +1283,25 @@ class RandomAffine(BaseTransform): will be (-degrees, +degrees) in clockwise order. If set 0, will not rotate. translate (tuple, optional): Maximum absolute fraction for horizontal and vertical translations. For example translate=(a, b), then horizontal shift is randomly sampled in the range -img_width * a < dx < img_width * a - and vertical shift is randomly sampled in the range -img_height * b < dy < img_height * b. + and vertical shift is randomly sampled in the range -img_height * b < dy < img_height * b. Default is None, will not translate. - scale (tuple, optional): Scaling factor interval, e.g (a, b), then scale is randomly sampled from the range a <= scale <= b. + scale (tuple, optional): Scaling factor interval, e.g (a, b), then scale is randomly sampled from the range a <= scale <= b. Default is None, will keep original scale and not scale. shear (sequence or number, optional): Range of degrees to shear, ranges from -180 to 180 in clockwise order. - If set as a number, a shear parallel to the x axis in the range (-shear, +shear) will be applied. - Else if set as a sequence of 2 values a shear parallel to the x axis in the range (shear[0], shear[1]) will be applied. + If set as a number, a shear parallel to the x axis in the range (-shear, +shear) will be applied. + Else if set as a sequence of 2 values a shear parallel to the x axis in the range (shear[0], shear[1]) will be applied. Else if set as a sequence of 4 values, a x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied. Default is None, will not apply shear. - interpolation (str, optional): Interpolation method. If omitted, or if the - image has only one channel, it is set to PIL.Image.NEAREST or cv2.INTER_NEAREST - according the backend. - When use pil backend, support method are as following: - - "nearest": Image.NEAREST, - - "bilinear": Image.BILINEAR, + interpolation (str, optional): Interpolation method. If omitted, or if the + image has only one channel, it is set to PIL.Image.NEAREST or cv2.INTER_NEAREST + according the backend. + When use pil backend, support method are as following: + - "nearest": Image.NEAREST, + - "bilinear": Image.BILINEAR, - "bicubic": Image.BICUBIC - When use cv2 backend, support method are as following: - - "nearest": cv2.INTER_NEAREST, - - "bilinear": cv2.INTER_LINEAR, + When use cv2 backend, support method are as following: + - "nearest": cv2.INTER_NEAREST, + - "bilinear": cv2.INTER_LINEAR, - "bicubic": cv2.INTER_CUBIC fill (int|list|tuple, optional): Pixel fill value for the area outside the transformed image. If given a number, the value is used for all bands respectively. @@ -1303,7 +1318,7 @@ class RandomAffine(BaseTransform): A callable object of RandomAffine. Examples: - + .. code-block:: python import paddle @@ -1317,31 +1332,34 @@ class RandomAffine(BaseTransform): print(fake_img.shape) """ - def __init__(self, - degrees, - translate=None, - scale=None, - shear=None, - interpolation='nearest', - fill=0, - center=None, - keys=None): - self.degrees = _setup_angle(degrees, name="degrees", req_sizes=(2, )) + def __init__( + self, + degrees, + translate=None, + scale=None, + shear=None, + interpolation='nearest', + fill=0, + center=None, + keys=None, + ): + self.degrees = _setup_angle(degrees, name="degrees", req_sizes=(2,)) super(RandomAffine, self).__init__(keys) assert interpolation in ['nearest', 'bilinear', 'bicubic'] self.interpolation = interpolation if translate is not None: - _check_sequence_input(translate, "translate", req_sizes=(2, )) + _check_sequence_input(translate, "translate", req_sizes=(2,)) for t in translate: if not (0.0 <= t <= 1.0): raise ValueError( - "translation values should be between 0 and 1") + "translation values should be between 0 and 1" + ) self.translate = translate if scale is not None: - _check_sequence_input(scale, "scale", req_sizes=(2, )) + _check_sequence_input(scale, "scale", req_sizes=(2,)) for s in scale: if s <= 0: raise ValueError("scale values should be positive") @@ -1359,15 +1377,12 @@ class RandomAffine(BaseTransform): self.fill = fill if center is not None: - _check_sequence_input(center, "center", req_sizes=(2, )) + _check_sequence_input(center, "center", req_sizes=(2,)) self.center = center - def _get_param(self, - img_size, - degrees, - translate=None, - scale_ranges=None, - shears=None): + def _get_param( + self, img_size, degrees, translate=None, scale_ranges=None, shears=None + ): """Get parameters for affine transformation Returns: @@ -1410,14 +1425,17 @@ class RandomAffine(BaseTransform): w, h = _get_image_size(img) img_size = [w, h] - ret = self._get_param(img_size, self.degrees, self.translate, - self.scale, self.shear) + ret = self._get_param( + img_size, self.degrees, self.translate, self.scale, self.shear + ) - return F.affine(img, - *ret, - interpolation=self.interpolation, - fill=self.fill, - center=self.center) + return F.affine( + img, + *ret, + interpolation=self.interpolation, + fill=self.fill, + center=self.center, + ) class RandomRotation(BaseTransform): @@ -1427,15 +1445,15 @@ class RandomRotation(BaseTransform): degrees (sequence or float or int): Range of degrees to select from. If degrees is a number instead of sequence like (min, max), the range of degrees will be (-degrees, +degrees) clockwise order. - interpolation (str, optional): Interpolation method. If omitted, or if the - image has only one channel, it is set to PIL.Image.NEAREST or cv2.INTER_NEAREST - according the backend. when use pil backend, support method are as following: - - "nearest": Image.NEAREST, - - "bilinear": Image.BILINEAR, + interpolation (str, optional): Interpolation method. If omitted, or if the + image has only one channel, it is set to PIL.Image.NEAREST or cv2.INTER_NEAREST + according the backend. when use pil backend, support method are as following: + - "nearest": Image.NEAREST, + - "bilinear": Image.BILINEAR, - "bicubic": Image.BICUBIC - when use cv2 backend, support method are as following: - - "nearest": cv2.INTER_NEAREST, - - "bilinear": cv2.INTER_LINEAR, + when use cv2 backend, support method are as following: + - "nearest": cv2.INTER_NEAREST, + - "bilinear": cv2.INTER_LINEAR, - "bicubic": cv2.INTER_CUBIC expand (bool|optional): Optional expansion flag. Default: False. If true, expands the output to make it large enough to hold the entire rotated image. @@ -1445,7 +1463,7 @@ class RandomRotation(BaseTransform): Origin is the upper left corner. Default is the center of the image. keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. - + Shape: - img(PIL.Image|np.ndarray|Paddle.Tensor): The input image with shape (H x W x C). - output(PIL.Image|np.ndarray|Paddle.Tensor): A rotated image. @@ -1454,7 +1472,7 @@ class RandomRotation(BaseTransform): A callable object of RandomRotation. Examples: - + .. code-block:: python import numpy as np @@ -1469,22 +1487,26 @@ class RandomRotation(BaseTransform): print(fake_img.size) """ - def __init__(self, - degrees, - interpolation='nearest', - expand=False, - center=None, - fill=0, - keys=None): + def __init__( + self, + degrees, + interpolation='nearest', + expand=False, + center=None, + fill=0, + keys=None, + ): if isinstance(degrees, numbers.Number): if degrees < 0: raise ValueError( - "If degrees is a single number, it must be positive.") + "If degrees is a single number, it must be positive." + ) self.degrees = (-degrees, degrees) else: if len(degrees) != 2: raise ValueError( - "If degrees is a sequence, it must be of len 2.") + "If degrees is a sequence, it must be of len 2." + ) self.degrees = degrees super(RandomRotation, self).__init__(keys) @@ -1509,8 +1531,9 @@ class RandomRotation(BaseTransform): angle = self._get_param(self.degrees) - return F.rotate(img, angle, self.interpolation, self.expand, - self.center, self.fill) + return F.rotate( + img, angle, self.interpolation, self.expand, self.center, self.fill + ) class RandomPerspective(BaseTransform): @@ -1524,13 +1547,13 @@ class RandomPerspective(BaseTransform): interpolation (str, optional): Interpolation method. If omitted, or if the image has only one channel, it is set to PIL.Image.NEAREST or cv2.INTER_NEAREST. - When use pil backend, support method are as following: - - "nearest": Image.NEAREST, - - "bilinear": Image.BILINEAR, + When use pil backend, support method are as following: + - "nearest": Image.NEAREST, + - "bilinear": Image.BILINEAR, - "bicubic": Image.BICUBIC - When use cv2 backend, support method are as following: - - "nearest": cv2.INTER_NEAREST, - - "bilinear": cv2.INTER_LINEAR, + When use cv2 backend, support method are as following: + - "nearest": cv2.INTER_NEAREST, + - "bilinear": cv2.INTER_LINEAR, - "bicubic": cv2.INTER_CUBIC fill (int|list|tuple, optional): Pixel fill value for the area outside the transformed image. If given a number, the value is used for all bands respectively. @@ -1544,7 +1567,7 @@ class RandomPerspective(BaseTransform): A callable object of RandomPerspective. Examples: - + .. code-block:: python import paddle @@ -1558,15 +1581,19 @@ class RandomPerspective(BaseTransform): print(fake_img.shape) """ - def __init__(self, - prob=0.5, - distortion_scale=0.5, - interpolation='nearest', - fill=0, - keys=None): + def __init__( + self, + prob=0.5, + distortion_scale=0.5, + interpolation='nearest', + fill=0, + keys=None, + ): super(RandomPerspective, self).__init__(keys) assert 0 <= prob <= 1, "probability must be between 0 and 1" - assert 0 <= distortion_scale <= 1, "distortion_scale must be between 0 and 1" + assert ( + 0 <= distortion_scale <= 1 + ), "distortion_scale must be between 0 and 1" assert interpolation in ['nearest', 'bilinear', 'bicubic'] assert isinstance(fill, (numbers.Number, str, list, tuple)) @@ -1584,35 +1611,43 @@ class RandomPerspective(BaseTransform): half_height = height // 2 half_width = width // 2 topleft = [ - int(random.uniform(0, - int(distortion_scale * half_width) + 1)), - int(random.uniform(0, - int(distortion_scale * half_height) + 1)), + int(random.uniform(0, int(distortion_scale * half_width) + 1)), + int(random.uniform(0, int(distortion_scale * half_height) + 1)), ] topright = [ int( - random.uniform(width - int(distortion_scale * half_width) - 1, - width)), - int(random.uniform(0, - int(distortion_scale * half_height) + 1)), + random.uniform( + width - int(distortion_scale * half_width) - 1, width + ) + ), + int(random.uniform(0, int(distortion_scale * half_height) + 1)), ] botright = [ int( - random.uniform(width - int(distortion_scale * half_width) - 1, - width)), + random.uniform( + width - int(distortion_scale * half_width) - 1, width + ) + ), int( - random.uniform(height - int(distortion_scale * half_height) - 1, - height)), + random.uniform( + height - int(distortion_scale * half_height) - 1, height + ) + ), ] botleft = [ - int(random.uniform(0, - int(distortion_scale * half_width) + 1)), + int(random.uniform(0, int(distortion_scale * half_width) + 1)), int( - random.uniform(height - int(distortion_scale * half_height) - 1, - height)), + random.uniform( + height - int(distortion_scale * half_height) - 1, height + ) + ), + ] + startpoints = [ + [0, 0], + [width - 1, 0], + [width - 1, height - 1], + [0, height - 1], ] - startpoints = [[0, 0], [width - 1, 0], [width - 1, height - 1], - [0, height - 1]] endpoints = [topleft, topright, botright, botleft] return startpoints, endpoints @@ -1629,10 +1664,12 @@ class RandomPerspective(BaseTransform): width, height = _get_image_size(img) if random.random() < self.prob: - startpoints, endpoints = self.get_params(width, height, - self.distortion_scale) - return F.perspective(img, startpoints, endpoints, - self.interpolation, self.fill) + startpoints, endpoints = self.get_params( + width, height, self.distortion_scale + ) + return F.perspective( + img, startpoints, endpoints, self.interpolation, self.fill + ) return img @@ -1645,7 +1682,7 @@ class Grayscale(BaseTransform): Shape: - img(PIL.Image|np.ndarray|Paddle.Tensor): The input image with shape (H x W x C). - - output(PIL.Image|np.ndarray|Paddle.Tensor): Grayscale version of the input image. + - output(PIL.Image|np.ndarray|Paddle.Tensor): Grayscale version of the input image. - If output_channels == 1 : returned image is single channel - If output_channels == 3 : returned image is 3 channel with r == g == b @@ -1653,7 +1690,7 @@ class Grayscale(BaseTransform): A callable object of Grayscale. Examples: - + .. code-block:: python import numpy as np @@ -1688,19 +1725,19 @@ class RandomErasing(BaseTransform): Args: prob (float, optional): Probability of the input data being erased. Default: 0.5. - scale (sequence, optional): The proportional range of the erased area to the input image. + scale (sequence, optional): The proportional range of the erased area to the input image. Default: (0.02, 0.33). ratio (sequence, optional): Aspect ratio range of the erased area. Default: (0.3, 3.3). value (int|float|sequence|str, optional): The value each pixel in erased area will be replaced with. - If value is a single number, all pixels will be erased with this value. - If value is a sequence with length 3, the R, G, B channels will be ereased - respectively. If value is set to "random", each pixel will be erased with + If value is a single number, all pixels will be erased with this value. + If value is a sequence with length 3, the R, G, B channels will be ereased + respectively. If value is set to "random", each pixel will be erased with random values. Default: 0. inplace (bool, optional): Whether this transform is inplace. Default: False. keys (list[str]|tuple[str], optional): Same as ``BaseTransform``. Default: None. - + Shape: - - img(paddle.Tensor | np.array | PIL.Image): The input image. For Tensor input, the shape should be (C, H, W). + - img(paddle.Tensor | np.array | PIL.Image): The input image. For Tensor input, the shape should be (C, H, W). For np.array input, the shape should be (H, W, C). - output(paddle.Tensor | np.array | PIL.Image): A random erased image. @@ -1708,11 +1745,11 @@ class RandomErasing(BaseTransform): A callable object of RandomErasing. Examples: - + .. code-block:: python import paddle - + fake_img = paddle.randn((3, 10, 10)).astype(paddle.float32) transform = paddle.vision.transforms.RandomErasing() result = transform(fake_img) @@ -1720,27 +1757,34 @@ class RandomErasing(BaseTransform): print(result) """ - def __init__(self, - prob=0.5, - scale=(0.02, 0.33), - ratio=(0.3, 3.3), - value=0, - inplace=False, - keys=None): + def __init__( + self, + prob=0.5, + scale=(0.02, 0.33), + ratio=(0.3, 3.3), + value=0, + inplace=False, + keys=None, + ): super(RandomErasing, self).__init__(keys) - assert isinstance(scale, - (tuple, list)), "scale should be a tuple or list" - assert (scale[0] >= 0 and scale[1] <= 1 and scale[0] <= scale[1] - ), "scale should be of kind (min, max) and in range [0, 1]" - assert isinstance(ratio, - (tuple, list)), "ratio should be a tuple or list" - assert (ratio[0] >= 0 - and ratio[0] <= ratio[1]), "ratio should be of kind (min, max)" - assert (prob >= 0 - and prob <= 1), "The probability should be in range [0, 1]" assert isinstance( - value, (numbers.Number, str, tuple, - list)), "value should be a number, tuple, list or str" + scale, (tuple, list) + ), "scale should be a tuple or list" + assert ( + scale[0] >= 0 and scale[1] <= 1 and scale[0] <= scale[1] + ), "scale should be of kind (min, max) and in range [0, 1]" + assert isinstance( + ratio, (tuple, list) + ), "ratio should be a tuple or list" + assert ( + ratio[0] >= 0 and ratio[0] <= ratio[1] + ), "ratio should be of kind (min, max)" + assert ( + prob >= 0 and prob <= 1 + ), "The probability should be in range [0, 1]" + assert isinstance( + value, (numbers.Number, str, tuple, list) + ), "value should be a number, tuple, list or str" if isinstance(value, str) and value != "random": raise ValueError("value must be 'random' when type is str") @@ -1755,10 +1799,10 @@ class RandomErasing(BaseTransform): Args: img (paddle.Tensor | np.array | PIL.Image): Image to be erased. - scale (sequence, optional): The proportional range of the erased area to the input image. + scale (sequence, optional): The proportional range of the erased area to the input image. ratio (sequence, optional): Aspect ratio range of the erased area. value (sequence | None): The value each pixel in erased area will be replaced with. - If value is a sequence with length 3, the R, G, B channels will be ereased + If value is a sequence with length 3, the R, G, B channels will be ereased respectively. If value is None, each pixel will be erased with random values. Returns: @@ -1784,7 +1828,8 @@ class RandomErasing(BaseTransform): if F._is_tensor_image(img): if value is None: v = paddle.normal(shape=[c, erase_h, erase_w]).astype( - img.dtype) + img.dtype + ) else: v = paddle.to_tensor(value, dtype=img.dtype)[:, None, None] else: @@ -1820,6 +1865,7 @@ class RandomErasing(BaseTransform): "Value should be a single number or a sequence with length equals to image's channel." ) top, left, erase_h, erase_w, v = self._get_param( - img, self.scale, self.ratio, value) + img, self.scale, self.ratio, value + ) return F.erase(img, top, left, erase_h, erase_w, v, self.inplace) return img