api_doc_std_cn.md 5.6 KB
Newer Older
R
ranqiu 已提交
1 2
# API注释撰写标准

W
weixing02 已提交
3 4 5 6
- [API注释撰写标准](#api)
    - [API注释模块](#api)
    - [格式及示例](#)
    - [完整示例](#)
R
ranqiu 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43


## API注释模块

API文档须包含以下几个模块(排列顺序为文档撰写顺序):

- Python API Definition

  API的代码定义。

- Function Description

  API的功能描述。描述该API的含义、作用或对输入所做的操作,及参考文献和对应链接(如果有),必要时给出公式,并解释公式中关键变量的含义。

- Args Description

  API参数介绍。按代码定义中的参数顺序逐个介绍,介绍内容包含数据类型、默认值(如果有)、含义等。

- Returns

  API返回值介绍。介绍返回值含义,必要时给出对应的形状。若返回值为包含多个参数的tuple,则按顺序逐个介绍各参数。

- Raises(如果有)

  可能抛出的异常或错误及可能的产生原因,当可能抛出多种异常或错误时应分条列出。

- Note(如果有)

  注意事项。当有多条注意事项时,应分条列出。

- Examples

  API的使用示例。


## 格式及示例

R
ranqiu 已提交
44
API文档须使用reStructuredText格式撰写,该格式详情请参考[链接](http://sphinx-doc-zh.readthedocs.io/en/latest/rest.html)。API文档各模块的内容格式及示例如下(以下以fc为例进行说明):
R
ranqiu 已提交
45 46 47 48

- Python API Definition

  - 格式:
W
weixing 已提交
49

R
ranqiu 已提交
50
      [Python API Definition]
W
weixing 已提交
51

R
ranqiu 已提交
52
  - 示例
W
weixing 已提交
53

R
ranqiu 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66
      ```
      fc(input,
         size,
         num_flatten_dims=1,
         param_attr=None,
         bias_attr=None,
         act=None,
         name=None,
         main_program=None,
         startup_program=None)
      ```

- Function Description
W
weixing 已提交
67

R
ranqiu 已提交
68 69 70 71 72
  - 格式

      本模块应包含以下内容(排列顺序为文档撰写顺序):

      [Function Description]
W
weixing 已提交
73

R
ranqiu 已提交
74
      [Formula]
W
weixing 已提交
75

R
ranqiu 已提交
76
      [Symbols' Descriptions if necessary]
W
weixing 已提交
77

R
ranqiu 已提交
78
      [References if necessary]
W
weixing 已提交
79

R
ranqiu 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  - 示例

      [Function Description]

       ```
       **Fully Connected Layer**

       The fully connected layer can take multiple tensors as its inputs. It
       creates a variable called weights for each input tensor, which represents
       a fully connected weight matrix from each input unit to each output unit.
       The fully connected layer multiplies each input tensor with its coresponding
       weight to produce an output Tensor. If multiple input tensors are given,
       the results of multiple multiplications will be sumed up. If bias_attr is
       not None, a bias variable will be created and added to the output. Finally,
       if activation is not None, it will be applied to the output as well.
       ```

      [Formula]

      ```
      This process can be formulated as follows:

      .. math::

           Out = Act({\sum_{i=0}^{N-1}X_iW_i + b})
      ```

      [Symbols' Descriptions if necessary]

      ```
      In the above equation:

      * :math:`N`: Number of the input.
      * :math:`X_i`: The input tensor.
      * :math:`W`: The weights created by this layer.
      * :math:`b`: The bias parameter created by this layer (if needed).
      * :math:`Act`: The activation function.
      * :math:`Out`: The output tensor.
      ```

      [References if necessary]

      因fc没有必要列出的参考文献,故该内容省略。其他情况下需明确给出对应的参考文献和对应连接,以 layer_norm 为例:
W
weixing 已提交
123

R
ranqiu 已提交
124 125 126
      ```
      Refer to `Layer Normalization <https://arxiv.org/pdf/1607.06450v1.pdf>`_ for more details.
      ```
W
weixing 已提交
127

R
ranqiu 已提交
128 129

- Args Description
W
weixing 已提交
130

R
ranqiu 已提交
131
  - 格式
W
weixing 已提交
132

R
ranqiu 已提交
133
      \[Arg's Name\][(Data Type, Default Value)][Description]
W
weixing 已提交
134

R
ranqiu 已提交
135 136 137 138 139 140
  - 示例

      fc的部分参数注释如下:

      ```
      Args:
R
ranqiu 已提交
141 142
          input (Variable|list of Variable): The input tensor(s) of this layer, and the dimension of
              the input tensor(s) is at least 2.
R
ranqiu 已提交
143 144 145 146 147 148
          param_attr (ParamAttr|list of ParamAttr, default None): The parameter attribute for learnable
              parameters/weights of this layer.
          name (str, default None): The name of this layer.
      ```

- Returns
W
weixing 已提交
149

R
ranqiu 已提交
150
  - 格式
W
weixing 已提交
151

R
ranqiu 已提交
152
      [Name][Shape]
W
weixing 已提交
153

R
ranqiu 已提交
154
  - 示例
W
weixing 已提交
155

R
ranqiu 已提交
156 157 158 159
      ```
      Returns:
          A tensor variable storing the transformation result.
      ```
W
weixing 已提交
160

R
ranqiu 已提交
161
      当返回值为包含多个参数的tuple时,应按顺序逐个介绍各参数,以dynamic_lstm为例:
W
weixing 已提交
162

R
ranqiu 已提交
163 164 165 166 167 168
      ```
      Returns:
          A tuple containing:
            The hidden state of LSTM whose shape is (T X D).
            The cell state of LSTM whose shape is (T X D).
      ```
W
weixing 已提交
169

R
ranqiu 已提交
170 171 172
- Raises

  - 格式
W
weixing 已提交
173

R
ranqiu 已提交
174 175 176
      [Exception Type][Condition]

  - 示例
W
weixing 已提交
177

R
ranqiu 已提交
178 179 180 181 182 183 184 185
      ```
      Raises:
          ValueError: If the rank of the input is less than 2.
      ```

- Note

  - 格式
W
weixing 已提交
186

R
ranqiu 已提交
187 188 189 190
     [Note]

  - 示例

R
ranqiu 已提交
191
      fc没有注意事项,故该模块省略不写。如有注意事项应明确给出,当有多条注意事项,须分条列出,以scaled\_dot\_product\_attention为例:
R
ranqiu 已提交
192 193 194 195 196 197 198 199 200 201

      ```
      Note:
          1. When num_heads > 1, three linear projections are learned respectively
             to map input queries, keys and values into queries', keys' and values'.
             queries', keys' and values' have the same shapes with queries, keys
             and values.
          2. When num_heads == 1, scaled_dot_product_attention has no learnable
             parameters.
      ```
W
weixing 已提交
202

R
ranqiu 已提交
203 204 205 206 207
- Examples

  - 格式

      \[Python Code Snipper]
W
weixing 已提交
208

R
ranqiu 已提交
209
  - 示例
W
weixing 已提交
210

R
ranqiu 已提交
211 212 213 214 215 216 217 218 219 220
      ```
      Examples:
          .. code-block:: python

            data = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
            fc = fluid.layers.fc(input=data, size=1000, act="tanh")
      ```

## 完整示例

W
weixing02 已提交
221
fc 的完整注释见[示例](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/fluid/dev/src/fc.py)