未验证 提交 17d5516f 编写于 作者: H hg-1099255210 提交者: GitHub

Fix doc 175-185 (#51040)

上级 8fc46209
...@@ -27,6 +27,10 @@ def with_mateclass(meta, *bases): ...@@ -27,6 +27,10 @@ def with_mateclass(meta, *bases):
class PyLayerContext: class PyLayerContext:
"""
``PyLayerContext`` can assist the :ref:`api_paddle_autograd_PyLayer` in implementing certain functionalities.
"""
def save_for_backward(self, *tensors): def save_for_backward(self, *tensors):
""" """
Saves given tensors that backward need. Use ``saved_tensor`` in the `backward` to get the saved tensors. Saves given tensors that backward need. Use ``saved_tensor`` in the `backward` to get the saved tensors.
...@@ -242,10 +246,32 @@ class PyLayerMeta(type): ...@@ -242,10 +246,32 @@ class PyLayerMeta(type):
class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)): class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)):
"""
Paddle implements Python custom operators on the PaddlePaddle framework by creating a subclass of
``PyLayer``, which must comply with the following rules:
1. The subclass must contain static ``forward`` and ``backward`` functions, with the first argument being
:ref:`api_paddle_autograd_PyLayerContext`. If a returned value in ``backward`` corresponds to a ``Tensor`` that
requires gradients in ``forward``, the returned value must be a ``Tensor``.
2. Except for the first argument, other arguments of ``backward`` are gradients of the output ``Tensors``
of ``forward``. Therefore, the number of input ``Tensor`` in ``backward`` must be the same as the number
of output ``Tensor`` in ``forward``. If you need to use input ``Tensor`` from ``forward`` in ``backward``,
you can save these ``Tensors`` by inputting them into :ref:`api_paddle_autograd_PyLayerContext`'s
``save_for_backward`` method and use them in ``backward`` later.
3. The output of ``backward`` can be ``Tensor`` or ``list/tuple(Tensor)``, which are gradients of the
output ``Tensor`` of ``forward``. Therefore, the number of output ``Tensor`` in ``backward`` is the same
as the number of input ``Tensor`` in ``forward``.
After building the custom operator, apply it by running the ``apply`` method.
"""
@staticmethod @staticmethod
def forward(ctx, *args, **kwargs): def forward(ctx, *args, **kwargs):
""" """
It is to be overloaded by subclasses. It must accept a object of `PyLayerContext` as It is to be overloaded by subclasses. It must accept a object of :ref:`api_paddle_autograd_PyLayerContext` as
the first argument, followed by any number of arguments (tensors or other types). the first argument, followed by any number of arguments (tensors or other types).
`None` can not be included in the returned result. `None` can not be included in the returned result.
...@@ -285,9 +311,9 @@ class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)): ...@@ -285,9 +311,9 @@ class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)):
def backward(ctx, *args): def backward(ctx, *args):
""" """
This is a function to calculate the gradient. It is to be overloaded by subclasses. This is a function to calculate the gradient. It is to be overloaded by subclasses.
It must accept a object of `PyLayerContext` as the first argument, and the rest It must accept a object of :ref:`api_paddle_autograd_PyLayerContext` as the first
arguments are the gradient of forward's output tensors. Output tensors of backward argument, and the rest arguments are the gradient of forward's output tensors.
are the gradient of forward's input tensors. Output tensors of backward are the gradient of forward's input tensors.
Args: Args:
*args(tuple): The gradient of forward's output tensor(s). *args(tuple): The gradient of forward's output tensor(s).
......
...@@ -27,7 +27,10 @@ class Bilinear(Initializer): ...@@ -27,7 +27,10 @@ class Bilinear(Initializer):
""" """
This initializer can be used in transposed convolution operator to This initializer can be used in transposed convolution operator to
act as upsampling. Users can upsample a feature map with shape of act as upsampling. Users can upsample a feature map with shape of
(B, C, H, W) by any integer factor. The usage is: (B, C, H, W) by any integer factor.
Returns:
Bilinear initializer instance objects.
Examples: Examples:
......
...@@ -357,8 +357,7 @@ def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None): ...@@ -357,8 +357,7 @@ def allclose(x, y, rtol=1e-05, atol=1e-08, equal_nan=False, name=None):
.. math:: .. math::
\left| x - y \right| \leq atol + rtol \times \left| y \right| \left| x - y \right| \leq atol + rtol \times \left| y \right|
elementwise, for all elements of :math:`x` and :math:`y`. The behaviour of this elementwise, for all elements of :math:`x` and :math:`y`. This is analogous to :math:`numpy.allclose`, namely that it returns :math:`True` if
operator is analogous to :math:`numpy.allclose`, namely that it returns :math:`True` if
two tensors are elementwise equal within a tolerance. two tensors are elementwise equal within a tolerance.
Args: Args:
......
...@@ -2813,25 +2813,25 @@ def scatter(x, index, updates, overwrite=True, name=None): ...@@ -2813,25 +2813,25 @@ def scatter(x, index, updates, overwrite=True, name=None):
.. code-block:: python .. code-block:: python
import numpy as np import paddle
#input: #input:
x = np.array([[1, 1], [2, 2], [3, 3]]) x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32')
index = np.array([2, 1, 0, 1]) index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')
# shape of updates should be the same as x # shape of updates should be the same as x
# shape of updates with dim > 1 should be the same as input # shape of updates with dim > 1 should be the same as input
updates = np.array([[1, 1], [2, 2], [3, 3], [4, 4]]) updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32')
overwrite = False overwrite = False
# calculation: # calculation:
if not overwrite: if not overwrite:
for i in range(len(index)): for i in range(len(index)):
x[index[i]] = np.zeros((2)) x[index[i]] = paddle.zeros([2])
for i in range(len(index)): for i in range(len(index)):
if (overwrite): if (overwrite):
x[index[i]] = updates[i] x[index[i]] = updates[i]
else: else:
x[index[i]] += updates[i] x[index[i]] += updates[i]
# output: # output:
out = np.array([[3, 3], [6, 6], [1, 1]]) out = paddle.to_tensor([[3, 3], [6, 6], [1, 1]])
out.shape # [3, 2] out.shape # [3, 2]
**NOTICE**: The order in which updates are applied is nondeterministic, **NOTICE**: The order in which updates are applied is nondeterministic,
...@@ -2841,10 +2841,10 @@ def scatter(x, index, updates, overwrite=True, name=None): ...@@ -2841,10 +2841,10 @@ def scatter(x, index, updates, overwrite=True, name=None):
x (Tensor): The input N-D Tensor with ndim>=1. Data type can be float32, float64. x (Tensor): The input N-D Tensor with ndim>=1. Data type can be float32, float64.
index (Tensor): The index is a 1-D or 0-D Tensor. Data type can be int32, int64. The length of index cannot exceed updates's length, and the value in index cannot exceed input's length. index (Tensor): The index is a 1-D or 0-D Tensor. Data type can be int32, int64. The length of index cannot exceed updates's length, and the value in index cannot exceed input's length.
updates (Tensor): Update input with updates parameter based on index. When the index is a 1-D tensor, the updates shape should be the same as input, and dim value with dim > 1 should be the same as input. When the index is a 0-D tensor, the updates should be a (N-1)-D tensor, the ith dim of the updates should be queal with the (i+1)th dim of the input. updates (Tensor): Update input with updates parameter based on index. When the index is a 1-D tensor, the updates shape should be the same as input, and dim value with dim > 1 should be the same as input. When the index is a 0-D tensor, the updates should be a (N-1)-D tensor, the ith dim of the updates should be queal with the (i+1)th dim of the input.
overwrite (bool): The mode that updating the output when there are same indices. overwrite (bool, optional): The mode that updating the output when there are same indices.
If True, use the overwrite mode to update the output of the same index, If True, use the overwrite mode to update the output of the same index,
if False, use the accumulate mode to update the output of the same index.Default value is True. if False, use the accumulate mode to update the output of the same index. Default value is True.
name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` . name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` .
......
...@@ -750,6 +750,9 @@ def floor_divide(x, y, name=None): ...@@ -750,6 +750,9 @@ def floor_divide(x, y, name=None):
.. math:: .. math::
out = trunc(x / y) out = trunc(x / y)
- :math:`x`: Multidimensional Tensor.
- :math:`y`: Multidimensional Tensor.
Note: Note:
``paddle.floor_divide`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ . ``paddle.floor_divide`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册