简体中文 | English
The docstring of the function consists of five modules:
Class docstring also consists of five modules:
The specifications for each module are described in detail below.
The goal is for the user to understand it quickly. The module can be disassembled into 3 parts, function description + calculation formula + annotation.
Example:
"""
Add two tensors element-wise. The equation is:
out = x + y
Note: This function supports broadcasting. If you want know more about broadcasting, please
refer to :ref:`user_guide_broadcasting` .
"""
Explain clearly the type, meaning and default value (if any) for each parameter.
Note:
optional
, for example: name (str|None, optinoal)
;|
to separate;list[{type name}]
and tuple[{type name}]
. For example, list[int]
represents a list containing an element of type int. tuple[int|float]
equivalent to tuple[int]| tuple[float]
;list[{type name}]
and tuple[{type name}]
, the default assumption is that the list or tuple parameters are homogeneous (that is, all elements contained in the list or tuple have the same type). If the list or tuple parameters are heterogeneous, it needs to be explained in the literal description.int
, Tensor
, etc., there is no need to add a space before and after the |
. However, if it is multiple complex types such as list[int]
and tuple[float]
, a space should be added before and after the |
.Example:
"""
Args:
x (Tensor|np.ndarray): Input tensor or numpy array.
points (list[int] | tuple[int|float]): List or tuple of data points.
name (str|None, optional): Name for the operation. If None, the operation will not be named.
Default: None.
"""
For a function return value, first describe the type of the return value (surrounded by ()
, with the same syntax as the parameter type description), and then explain the meaning of the return value. There is no need to specify the type of the object obtained by instantiating the class.
Example 1:
"""
Returns:
(tuple): When label is None, it returns (im, im_info); otherwise it returns (im, im_info, label).
"""
Example 2:
"""
Returns:
(N-D Tensor): A location into which the result is stored.
"""
Example 3 (In the class definition):
"""
Returns:
A callable object of Activation.
"""
You need to give the exception type and the conditions under which it is thrown.
Example:
"""
Raises:
ValueError: When memory() is called outside block().
TypeError: When init is set and is not a Variable.
"""
Provide as many examples as possible for various usage scenarios of the function or class, and give the expected results of executing the code in the comments.
Requirement: Users can run the script by copying the sample code. Note that the necessary import
statements need to be added.
Single example:
"""
Examples:
import paddle
import numpy as np
paddle.enable_imperative()
np_x = np.array([2, 3, 4]).astype('float64')
np_y = np.array([1, 5, 2]).astype('float64')
x = paddle.imperative.to_variable(np_x)
y = paddle.imperative.to_variable(np_y)
z = paddle.add(x, y)
np_z = z.numpy()
# [3., 8., 6. ]
z = paddle.add(x, y, alpha=10)
np_z = z.numpy()
# [12., 53., 24. ]
"""
Multi-examples:
"""
Examples 1:
from paddleseg.cvlibs.manager import ComponentManager
model_manager = ComponentManager()
class AlexNet: ...
class ResNet: ...
model_manager.add_component(AlexNet)
model_manager.add_component(ResNet)
# Alternatively, pass a sequence:
model_manager.add_component([AlexNet, ResNet])
print(model_manager.components_dict)
# {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>}
Examples 2:
# Use it as a Python decorator.
from paddleseg.cvlibs.manager import ComponentManager
model_manager = ComponentManager()
@model_manager.add_component
class AlexNet: ...
@model_manager.add_component
class ResNet: ...
print(model_manager.components_dict)
# {'AlexNet': <class '__main__.AlexNet'>, 'ResNet': <class '__main__.ResNet'>}
"""
Examples:
title and the sample code content.class Activation(nn.Layer):
"""
The wrapper of activations.
Args:
act (str, optional): Activation name in lowercase. It must be one of {'elu', 'gelu',
'hardshrink', 'tanh', 'hardtanh', 'prelu', 'relu', 'relu6', 'selu', 'leakyrelu', 'sigmoid',
'softmax', 'softplus', 'softshrink', 'softsign', 'tanhshrink', 'logsigmoid', 'logsoftmax',
'hsigmoid'}. Default: None, means identical transformation.
Returns:
A callable object of Activation.
Raises:
KeyError: When parameter `act` is not in the optional range.
Examples:
from paddleseg.models.common.activation import Activation
relu = Activation("relu")
print(relu)
# <class 'paddle.nn.layer.activation.ReLU'>
sigmoid = Activation("sigmoid")
print(sigmoid)
# <class 'paddle.nn.layer.activation.Sigmoid'>
not_exit_one = Activation("not_exit_one")
# KeyError: "not_exit_one does not exist in the current dict_keys(['elu', 'gelu', 'hardshrink',
# 'tanh', 'hardtanh', 'prelu', 'relu', 'relu6', 'selu', 'leakyrelu', 'sigmoid', 'softmax',
# 'softplus', 'softshrink', 'softsign', 'tanhshrink', 'logsigmoid', 'logsoftmax', 'hsigmoid'])"
"""
...