nn_cn.rst 350.0 KB
Newer Older
C
Cheerego 已提交
1 2 3 4 5 6 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
=======
nn
=======

.. _cn_api_fluid_layers_adaptive_pool2d:

adaptive_pool2d
-------------------------------

.. py:function:: paddle.fluid.layers.adaptive_pool2d(input, pool_size, pool_type='max', require_index=False, name=None)

pooling2d操作根据输入 ``input`` , ``pool_size`` , ``pool_type`` 参数计算输出。 输入(X)和输出(Out)采用NCHW格式,其中N是批大小batch size,C是通道数,H是feature(特征)的高度,W是feature(特征)的宽度。 参数 ``pool_size`` 由两个元素构成, 这两个元素分别代表高度和宽度。 输出(Out)的H和W维与 ``pool_size`` 大小相同。


对于平均adaptive pool2d:

..  math::

       hstart &= floor(i * H_{in} / H_{out})

       hend &= ceil((i + 1) * H_{in} / H_{out})

       wstart &= floor(j * W_{in} / W_{out})

       wend &= ceil((j + 1) * W_{in} / W_{out})

       Output(i ,j) &= \frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)}

参数:
  - **input** (Variable) - 池化操作的输入张量。 输入张量的格式为NCHW,其中N是batch大小,C是通道数,H是特征的高度,W是特征的宽度。
  - **pool_size** (int | list | tuple) - 池化核大小。 如果池化核大小是元组或列表,则它必须包含两个整数(pool_size_Height,pool_size_Width)。
  - **pool_type** (string)- 池化类型,可输入“max”代表max-pooling,或者“avg”代表average-pooling。
  - **require_index** (bool) - 如果为true,则输出中带有最大池化点所在的索引。 如果pool_type为avg,该项不可被设置为true。
  - **name** (str | None) - 此层的名称(可选)。 如果设置为None,则将自动命名该层。


返回: 池化结果

返回类型: Variable


抛出异常:

  - ``ValueError`` – ``pool_type`` 不是 ‘max’ 或 ‘avg’
  - ``ValueError`` – 当 ``pool_type`` 是 ‘avg’ 时,错误地设置 ‘require_index’ 为true .
  - ``ValueError`` – ``pool_size`` 应为一个长度为2的列表或元组

.. code-block:: python

    # 假设输入形为[N, C, H, W], `pool_size` 为 [m, n],
    # 输出形为 [N, C, m, n], adaptive pool 将输入的 H 和 W 维度
    # 平均分割为 m * n 个栅格(grid) ,然后为每个栅格进行池化得到输出
    # adaptive average pool 进行如下操作
    #
    #     for i in range(m):
    #         for j in range(n):
    #             hstart = floor(i * H / m)
    #             hend = ceil((i + 1) * H / m)
    #             wstart = floor(i * W / n)
    #             wend = ceil((i + 1) * W / n)
    #             output[:, :, i, j] = avg(input[:, :, hstart: hend, wstart: wend])
    #
X
xsrobin 已提交
63
    import paddle.fluid as fluid
C
Cheerego 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    data = fluid.layers.data(
        name='data', shape=[3, 32, 32], dtype='float32')
    pool_out = fluid.layers.adaptive_pool2d(
                      input=data,
                      pool_size=[3, 3],
                      pool_type='avg')




.. _cn_api_fluid_layers_adaptive_pool3d:

adaptive_pool3d
-------------------------------

.. py:function:: paddle.fluid.layers.adaptive_pool3d(input, pool_size, pool_type='max', require_index=False, name=None)

pooling3d操作根据输入 ``input`` ,``pool_size`` , ``pool_type`` 参数计算输出。 输入(X)和输出(输出)采用NCDHW格式,其中N是批大小batch size,C是通道数,D是特征(feature)的深度,H是特征的高度,W是特征的宽度。 参数 ``pool_size`` 由三个元素组成。 这三个元素分别代表深度,高度和宽度。输出(Out)的D,H,W维与 ``pool_size`` 相同。


对于平均adaptive pool3d:

..  math::

      dstart &= floor(i * D_{in} / D_{out})

      dend &= ceil((i + 1) * D_{in} / D_{out})

      hstart &= floor(j * H_{in} / H_{out})

      hend &= ceil((j + 1) * H_{in} / H_{out})

      wstart &= floor(k * W_{in} / W_{out})

      wend &= ceil((k + 1) * W_{in} / W_{out})

      Output(i ,j, k) &= \frac{sum(Input[dstart:dend, hstart:hend, wstart:wend])}{(dend - dstart) * (hend - hstart) * (wend - wstart)}



参数:
  - **input** (Variable) - 池化操作的输入张量。 输入张量的格式为NCDHW,其中N是batch大小,C是通道数,D为特征的深度,H是特征的高度,W是特征的宽度。
  - **pool_size** (int | list | tuple) - 池化核大小。 如果池化核大小是元组或列表,则它必须包含三个整数(Depth, Height, Width)。
  - **pool_type** (string)- 池化类型,可输入“max”代表max-pooling,或者“avg”代表average-pooling。
  - **require_index** (bool) - 如果为true,则输出中带有最大池化点所在的索引。 如果pool_type为avg,该项不可被设置为true。
  - **name** (str | None) - 此层的名称(可选)。 如果设置为None,则将自动命名该层。


返回: 池化结果

返回类型: Variable


抛出异常:

  - ``ValueError`` – ``pool_type`` 不是 ‘max’ 或 ‘avg’
  - ``ValueError`` – 当 ``pool_type`` 是 ‘avg’ 时,错误地设置 ‘require_index’ 为true .
  - ``ValueError`` – ``pool_size`` 应为一个长度为3的列表或元组

.. code-block:: python

    # 假设输入形为[N, C, D, H, W], `pool_size` 为 [l, m, n],
    # 输出形为 [N, C, l, m, n], adaptive pool 将输入的D, H 和 W 维度
    # 平均分割为 l * m * n 个栅格(grid) ,然后为每个栅格进行池化得到输出
    # adaptive average pool 进行如下操作
    #
    #     for i in range(l):
    #         for j in range(m):
    #             for k in range(n):
    #                 dstart = floor(i * D / l)
    #                 dend = ceil((i + 1) * D / l)
    #                 hstart = floor(j * H / m)
    #                 hend = ceil((j + 1) * H / m)
    #                 wstart = floor(k * W / n)
    #                 wend = ceil((k + 1) * W / n)
    #                 output[:, :, i, j, k] =
    #                     avg(input[:, :, dstart:dend, hstart: hend, wstart: wend])
    #
    
    import paddle.fluid as fluid

    data = fluid.layers.data(
    name='data', shape=[3, 32, 32, 32], dtype='float32')
    pool_out, mask = fluid.layers.adaptive_pool3d(
                      input=data,
                      pool_size=[3, 3, 3],
                      pool_type='avg')




.. _cn_api_fluid_layers_add_position_encoding:

add_position_encoding
-------------------------------

.. py:function:: paddle.fluid.layers.add_position_encoding(input, alpha, beta, name=None)

**添加位置编码层**

接受形状为[N×M×P]的三维输入张量,并返回一个形为[N×M×P]的输出张量,且输出张量具有位置编码值。

可参考论文: `Attention Is All You Need <http://arxiv.org/pdf/1706.03762.pdf>`_

.. math::

  PE(pos, 2i) &= \sin{(pos / 10000^{2i / P})}\\
  PE(pos, 2i + 1) &= \cos{(pos / 10000^{2i / P})}\\
  Out(:, pos, i) &= \alpha * input(:, pos, i) + \beta * PE(pos, i)

其中:
    - PE(pos, 2i): 偶数位置上数字的增量
    - PE(pos, 2i + 1): 奇数位置上数字的增量

参数:
    - **input**  (Variable) – 形状为[N x M x P]的三维输入张量
    - **alpha**  (float) – 输入张量的倍数
    - **beta**  (float) – 位置编码张量Positional Encoding Tensor的倍数
    - **name**  (string) – 位置编码层的名称


返回:  具有位置编码的三维形状张量[N×M×P]

返回类型: Variable

**代码示例:**

.. code-block:: python

  import paddle.fluid as fluid
     
  tensor = fluid.layers.data(
        name='tensor',
        shape=[32, 64, 512],
        dtype='float32',
        append_batch_size=False)
  position_tensor = fluid.layers.add_position_encoding(
        input=tensor, alpha=1.0, beta=1.0)











.. _cn_api_fluid_layers_affine_channel:

affine_channel
-------------------------------

.. py:function:: paddle.fluid.layers.affine_channel(x, scale=None, bias=None, data_layout='NCHW', name=None,act=None)

对输入的每个 channel 应用单独的仿射变换。用于将空间批处理范数替换为其等价的固定变换。

输入也可以是二维张量,并在二维应用仿射变换。

参数:
  - **x** (Variable):特征图输入可以是一个具有NCHW阶或NHWC阶的4D张量。它也可以是二维张量和应用于第二维度的仿射变换。
  - **scale** (Variable): 形状为(C)的一维输入,第C个元素为输入的第C通道仿射变换的尺度因子。
  - **bias** (Variable):形状为(C)的一维输入,第C个元素是输入的第C个通道的仿射变换的偏置。
  - **data_layout** (string, default NCHW): NCHW 或 NHWC,如果输入是一个2D张量,可以忽略该参数
  - **name** (str, default None): 此层的名称
  - **act** (str, default None): 应用于该层输出的激活函数

返回: out (Variable): 与x具有相同形状和数据布局的张量。

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
    data = fluid.layers.data(name='data', shape=[3, 32, 32],
                             dtype='float32')
    input_scale = fluid.layers.create_parameter(shape=[3],
                             dtype="float32")
    input_bias = fluid.layers.create_parameter(shape=[3],
                             dtype="float32")
    out = fluid.layers.affine_channel(data,scale=input_scale,
                             bias=input_bias)





.. _cn_api_fluid_layers_affine_grid:

affine_grid
-------------------------------

.. py:function:: paddle.fluid.layers.affine_grid(theta, out_shape, name=None)

它使用仿射变换的参数生成(x,y)坐标的网格,这些参数对应于一组点,在这些点上,输入特征映射应该被采样以生成转换后的输出特征映射。



.. code-block:: text

        * 例 1:
          给定:
              theta = [[[x_11, x_12, x_13]
                        [x_14, x_15, x_16]]
                       [[x_21, x_22, x_23]
                        [x_24, x_25, x_26]]]
              out_shape = [2, 3, 5, 5]

          Step 1:

              根据out_shape生成标准化坐标

              归一化坐标的值在-1和1之间

              归一化坐标的形状为[2,H, W],如下所示:

              C = [[[-1.  -1.  -1.  -1.  -1. ]
                    [-0.5 -0.5 -0.5 -0.5 -0.5]
                    [ 0.   0.   0.   0.   0. ]
                    [ 0.5  0.5  0.5  0.5  0.5]
                    [ 1.   1.   1.   1.   1. ]]
                   [[-1.  -0.5  0.   0.5  1. ]
                    [-1.  -0.5  0.   0.5  1. ]
                    [-1.  -0.5  0.   0.5  1. ]
                    [-1.  -0.5  0.   0.5  1. ]
                    [-1.  -0.5  0.   0.5  1. ]]]

              C[0]是高轴坐标,C[1]是宽轴坐标。

          Step2:

              将C转换并重组成形为[H * W, 2]的张量,并追加到最后一个维度

              我们得到:

              C_ = [[-1.  -1.   1. ]
                    [-0.5 -1.   1. ]
                    [ 0.  -1.   1. ]
                    [ 0.5 -1.   1. ]
                    [ 1.  -1.   1. ]
                    [-1.  -0.5  1. ]
                    [-0.5 -0.5  1. ]
                    [ 0.  -0.5  1. ]
                    [ 0.5 -0.5  1. ]
                    [ 1.  -0.5  1. ]
                    [-1.   0.   1. ]
                    [-0.5  0.   1. ]
                    [ 0.   0.   1. ]
                    [ 0.5  0.   1. ]
                    [ 1.   0.   1. ]
                    [-1.   0.5  1. ]
                    [-0.5  0.5  1. ]
                    [ 0.   0.5  1. ]
                    [ 0.5  0.5  1. ]
                    [ 1.   0.5  1. ]
                    [-1.   1.   1. ]
                    [-0.5  1.   1. ]
                    [ 0.   1.   1. ]
                    [ 0.5  1.   1. ]
                    [ 1.   1.   1. ]]
          Step3:
              按下列公式计算输出
.. math::

  Output[i] = C\_ * Theta[i]^T

参数:
  - **theta** (Variable): 一类具有形状为[N, 2, 3]的仿射变换参数
  - **out_shape** (Variable | list | tuple):具有格式[N, C, H, W]的目标输出的shape,out_shape可以是变量、列表或元组。
  - **name** (str|None): 此层的名称(可选)。如果没有设置,将自动命名。

返回: Variable: 形为[N, H, W, 2]的输出。

抛出异常: ValueError: 如果输入了不支持的参数类型

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
    theta = fluid.layers.data(name="x", shape=[2, 3], dtype="float32")
    out_shape = fluid.layers.data(name="y", shape=[-1], dtype="float32")
    data = fluid.layers.affine_grid(theta, out_shape)
    # or
    data = fluid.layers.affine_grid(theta, [5, 3, 28, 28])









.. _cn_api_fluid_layers_autoincreased_step_counter:

autoincreased_step_counter
-------------------------------

.. py:function:: paddle.fluid.layers.autoincreased_step_counter(counter_name=None, begin=1, step=1)

创建一个自增变量,每个mini-batch返回主函数运行次数,变量自动加1,默认初始值为1.

参数:
    - **counter_name** (str)-计数名称,默认为 ``@STEP_COUNTER@``
    - **begin** (int)-开始计数
    - **step** (int)-执行之间增加的步数

返回:全局运行步数

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
381
    import paddle.fluid as fluid
C
Cheerego 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    global_step = fluid.layers.autoincreased_step_counter(
        counter_name='@LR_DECAY_COUNTER@', begin=0, step=1)









.. _cn_api_fluid_layers_batch_norm:

batch_norm
-------------------------------

.. py:function:: paddle.fluid.layers.batch_norm(input, act=None, is_test=False, momentum=0.9, epsilon=1e-05, param_attr=None, bias_attr=None, data_layout='NCHW', in_place=False, name=None, moving_mean_name=None, moving_variance_name=None, do_model_average_for_mean_and_var=False, fuse_with_relu=False, use_global_stats=False)

批正则化层(Batch Normalization Layer)

可用作conv2d和全链接操作的正则化函数。该层需要的数据格式如下:

1.NHWC[batch,in_height,in_width,in_channels]
2.NCHW[batch,in_channels,in_height,in_width]

更多详情请参考 : `Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift <https://arxiv.org/pdf/1502.03167.pdf>`_

``input`` 是mini-batch的输入特征。

.. math::
    \mu_{\beta}        &\gets \frac{1}{m} \sum_{i=1}^{m} x_i                                 \quad &// mini-batch-mean \\
    \sigma_{\beta}^{2} &\gets \frac{1}{m} \sum_{i=1}^{m}(x_i - \mu_{\beta})^2               \quad &// mini-batch-variance \\
    \hat{x_i}          &\gets \frac{x_i - \mu_\beta} {\sqrt{\sigma_{\beta}^{2} + \epsilon}}  \quad &// normalize \\
    y_i &\gets \gamma \hat{x_i} + \beta                                                      \quad &// scale-and-shift

当use_global_stats = True时, :math:`\mu_{\beta}` 和 :math:`\sigma_{\beta}^{2}` 不是一个minibatch的统计数据。 它们是全局(或运行)统计数据。 (它通常来自预先训练好的模型。)训练和测试(或预测)具有相同的行为:

.. math::

    \hat{x_i} &\gets \frac{x_i - \mu_\beta} {\sqrt{\
    \sigma_{\beta}^{2} + \epsilon}}  \\
    y_i &\gets \gamma \hat{x_i} + \beta



参数:
    - **input** (Variable) - 输入变量的排序,可以为 2, 3, 4, 5
    - **act** (string,默认None)- 激活函数类型,linear|relu|prelu|...
    - **is_test** (bool,默认False) - 指示它是否在测试阶段。
    - **momentum** (float,默认0.9)- 此值用于计算 moving_mean and moving_var. 更新公式为:  :math:`\(moving\_mean = moving\_mean * momentum + new\_mean * (1. - momentum)\)` :math:`\(moving\_var = moving\_var * momentum + new\_var * (1. - momentum)\)` , 默认值0.9.
    - **epsilon** (float,默认1e-05)- 加在分母上为了数值稳定的值。默认值为1e-5。
    - **param_attr** (ParamAttr|None) - batch_norm参数范围的属性,如果设为None或者是ParamAttr的一个属性,batch_norm创建ParamAttr为param_attr。如果没有设置param_attr的初始化函数,参数初始化为Xavier。默认:None
    - **bias_attr** (ParamAttr|None) - batch_norm bias参数的属性,如果设为None或者是ParamAttr的一个属性,batch_norm创建ParamAttr为bias_attr。如果没有设置bias_attr的初始化函数,参数初始化为0。默认:None
    - **data_layout** (string,默认NCHW) - NCHW|NHWC
    - **in_place** (bool,默认False)- 得出batch norm可复用记忆的输入和输出
    - **name** (string,默认None)- 该层名称(可选)。若设为None,则自动为该层命名
    - **moving_mean_name** (string,默认None)- moving_mean的名称,存储全局Mean。如果将其设置为None, ``batch_norm``将随机命名全局平均值;否则, `batch_norm`将命名全局平均值为``moving_mean_name``
    - **moving_variance_name** (string,默认None)- moving_variance的名称,存储全局变量。如果将其设置为None,``batch_norm``将随机命名全局方差;否则, `batch_norm`将命名全局方差为``moving_mean_name``
    - **do_model_average_for_mean_and_var** (bool,默认False)- 是否为mean和variance做模型均值
    - **fuse_with_relu** (bool)- 如果为True,batch norm后该操作符执行relu
    - **use_global_stats** (bool, Default False) – 是否使用全局均值和方差。 在预测或测试模式下,将use_global_stats设置为true或将is_test设置为true,并且行为是等效的。 在训练模式中,当设置use_global_stats为True时,在训练期间也使用全局均值和方差。

返回: 张量,在输入中运用批正则后的结果

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python
    
X
xsrobin 已提交
452
    import paddle.fluid as fluid
C
Cheerego 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
    x = fluid.layers.data(name='x', shape=[3, 7, 3, 7], dtype='float32', append_batch_size=False)
    hidden1 = fluid.layers.fc(input=x, size=200, param_attr='fc1.w')
    hidden2 = fluid.layers.batch_norm(input=hidden1)











.. _cn_api_fluid_layers_beam_search:

beam_search
-------------------------------

.. py:function:: paddle.fluid.layers.beam_search(pre_ids, pre_scores, ids, scores, beam_size, end_id, level=0, is_accumulated=True, name=None, return_parent_idx=False)

在机器翻译任务中,束搜索(Beam search)是选择候选词的一种经典算法

更多细节参考 `Beam Search <https://en.wikipedia.org/wiki/Beam_search>`_

该层在一时间步中按束进行搜索。具体而言,根据候选词使用于源句子所得的 ``scores`` , 从候选词 ``ids`` 中选择当前步骤的 top-K (最佳K)候选词的id,其中 ``K`` 是 ``beam_size`` , ``ids`` , ``scores`` 是计算单元的预测结果。如果没有提供 ``ids`` ,则将会根据 ``scores`` 计算得出。 另外, ``pre_id`` 和 ``pre_scores`` 是上一步中 ``beam_search`` 的输出,用于特殊处理翻译的结束边界。

注意,如果 ``is_accumulated`` 为 True,传入的 ``scores`` 应该是累积分数。反之,``scores`` 会被认为为直接得分(straightforward scores), 并且会被转化为log值并且在此运算中会被累积到 ``pre_scores`` 中。在计算累积分数之前应该使用额外的 operators 进行长度惩罚。

有关束搜索用法演示,请参阅以下示例:

     fluid/tests/book/test_machine_translation.py



参数:
  - **pre_ids** (Variable) -  LodTensor变量,它是上一步 ``beam_search`` 的输出。在第一步中。它应该是LodTensor,shape为 :math:`(batch\_size,1)` , :math:`lod [[0,1,...,batch\_size],[0,1,...,batch\_size]]`
  - **pre_scores** (Variable) -  LodTensor变量,它是上一步中beam_search的输出
  - **ids** (Variable) - 包含候选ID的LodTensor变量。shape为 :math:`(batch\_size×beam\_ize,K)` ,其中 ``K`` 应该是 ``beam_size``
  - **scores** (Variable) - 与 ``ids`` 及其shape对应的累积分数的LodTensor变量, 与 ``ids`` 的shape相同。
  - **beam_size** (int) - 束搜索中的束宽度。
  - **end_id** (int) - 结束标记的id。
  - **level** (int,default 0) - **可忽略,当前不能更改** 。它表示lod的源级别,解释如下。 ``ids`` 的 lod 级别应为2.第一级是源级别, 描述每个源句子(beam)的前缀(分支)的数量,第二级是描述这些候选者属于前缀的句子级别的方式。链接前缀和所选候选者的路径信息保存在lod中。
  - **is_accumulated** (bool,默认为True) - 输入分数是否为累计分数。
  - **name** (str | None) - 该层的名称(可选)。如果设置为None,则自动命名该层。
  - **return_parent_idx** (bool) - 是否返回一个额外的Tensor变量,在输出的pre_ids中保留selected_ids的双亲indice,可用于在下一个时间步收集单元状态。


返回:LodTensor元组。包含所选的id和与其相应的分数。 如果return_parent_idx为True,则包含一个保留selected_ids的双亲indice的额外Tensor变量。

返回类型:Variable

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid

    # 假设 `probs` 包含计算神经元所得的预测结果
    # `pre_ids` 和 `pre_scores` 为beam_search之前时间步的输出
    beam_size = 4
    end_id = 1
    pre_ids = fluid.layers.data(
        name='pre_id', shape=[1], lod_level=2, dtype='int64')
    pre_scores = fluid.layers.data(
        name='pre_scores', shape=[1], lod_level=2, dtype='float32')
    probs = fluid.layers.data(
        name='probs', shape=[10000], dtype='float32')
    topk_scores, topk_indices = fluid.layers.topk(probs, k=beam_size)
    accu_scores = fluid.layers.elementwise_add(
                                          x=fluid.layers.log(x=topk_scores)),
                                          y=fluid.layers.reshape(
                                              pre_scores, shape=[-1]),
                                          axis=0)
    selected_ids, selected_scores = fluid.layers.beam_search(
                                          pre_ids=pre_ids,
                                          pre_scores=pre_scores,
                                          ids=topk_indices,
                                          scores=accu_scores,
                                          beam_size=beam_size,
                                          end_id=end_id)











.. _cn_api_fluid_layers_beam_search_decode:

beam_search_decode
-------------------------------

.. py:function:: paddle.fluid.layers.beam_search_decode(ids, scores, beam_size, end_id, name=None)

束搜索层(Beam Search Decode Layer)通过回溯LoDTensorArray ids,为每个源语句构建完整假设,LoDTensorArray ``ids`` 的lod可用于恢复束搜索树中的路径。请参阅下面的demo中的束搜索使用示例:

    ::

        fluid/tests/book/test_machine_translation.py

参数:
        - **id** (Variable) - LodTensorArray,包含所有回溯步骤重中所需的ids。
        - **score** (Variable) - LodTensorArra,包含所有回溯步骤对应的score。
        - **beam_size** (int) - 束搜索中波束的宽度。
        - **end_id** (int) - 结束token的id。
        - **name** (str|None) - 该层的名称(可选)。如果设置为None,该层将被自动命名。

返回: LodTensor 对(pair), 由生成的id序列和相应的score序列组成。两个LodTensor的shape和lod是相同的。lod的level=2,这两个level分别表示每个源句有多少个假设,每个假设有多少个id。

返回类型: 变量(variable)


**代码示例**

.. code-block:: python

       import paddle.fluid as fluid

       # 假设 `ids` 和 `scores` 为 LodTensorArray变量,它们保留了
       # 选择出的所有时间步的id和score
       ids = fluid.layers.create_array(dtype='int64')
       scores = fluid.layers.create_array(dtype='float32')
       finished_ids, finished_scores = fluid.layers.beam_search_decode(
                ids, scores, beam_size=5, end_id=0)









.. _cn_api_fluid_layers_bilinear_tensor_product:

bilinear_tensor_product
-------------------------------

.. py:function:: paddle.fluid.layers.bilinear_tensor_product(x, y, size, act=None, name=None, param_attr=None, bias_attr=None)

该层对两个输入执行双线性张量积。

例如:

.. math::
       out_{i} = x * W_{i} * {y^\mathrm{T}}, i=0,1,...,size-1

在这个公式中:
  - :math:`x`: 第一个输入,包含M个元素,形状为[batch_size, M]
  - :math:`y`: 第二个输入,包含N个元素,形状为[batch_size, N]
  - :math:`W_{i}`: 第i个被学习的权重,形状是[M, N]
  - :math:`out_{i}`: out的第i个元素,形状是[batch_size, size]
  - :math:`y^\mathrm{T}`: :math:`y_{2}` 的转置

参数:
    - **x** (Variable): 2-D 输入张量,形状为 [batch_size, M]
    - **y** (Variable): 2-D 输入张量,形状为 [batch_size, N]
    - **size** (int): 此层的维度,
    - **act** (str, default None): 应用到该层输出的激活函数
    - **name** (str, default None): 该层的名称
    - **param_attr** (ParamAttr, default None): 可学习参数/权重(w) 的参数属性
    - **bias_attr** (ParamAttr, default None): 偏差的参数属性,如果设置为False,则不会向输出单元添加偏差。如果设置为零,偏差初始化为零。默认值:None

返回: Variable: 一个形为[batch_size, size]的2-D张量

**代码示例:**

.. code-block:: python

X
xsrobin 已提交
627
  import paddle.fluid as fluid
C
Cheerego 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
  layer1 = fluid.layers.data("t1", shape=[-1, 5], dtype="float32")
  layer2 = fluid.layers.data("t2", shape=[-1, 4], dtype="float32")
  tensor = fluid.layers.bilinear_tensor_product(x=layer1, y=layer2, size=1000)




.. _cn_api_fluid_layers_bpr_loss:

bpr_loss
-------------------------------

.. py:function:: paddle.fluid.layers.bpr_loss(input, label, name=None)


贝叶斯个性化排序损失计算(Bayesian Personalized Ranking Loss Operator )

该算子属于pairwise的排序类型,其标签是期望物品。在某次会话中某一给定点的损失值由下式计算而得:

.. math::

  \[Y[i] = 1/(N[i] - 1) * \sum_j{\log(\sigma(X[i, Label[i]]-X[i, j]))}\]

更多细节请参考 `Session Based Recommendations with Recurrent Neural Networks`_

参数:
  - **input** (Variable|list) - 一个形为[N x D]的2-D tensor , 其中 N 为批大小batch size ,D 为种类的数量。该输入为logits而非概率。
  - **label** (Variable|list) - 2-D tensor<int64> 类型的真实值, 形为[N x 1]
  - **name** (str|None) - (可选)该层的命名。 如果为None, 则自动为该层命名。 默认为None.

返回: 形为[N x 1]的2D张量,即bpr损失

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
     
    neg_size = 10
    label = fluid.layers.data(
              name="label", shape=[1], dtype="int64")
    predict = fluid.layers.data(
              name="predict", shape=[neg_size + 1], dtype="float32")
    cost = fluid.layers.bpr_loss(input=predict, label=label)





.. _cn_api_fluid_layers_brelu:

brelu
-------------------------------

.. py:function:: paddle.fluid.layers.brelu(x, t_min=0.0, t_max=24.0, name=None)


BRelu 激活函数

.. math::   out=max(min(x,tmin),tmax)

参数:
    - **x** (Variable) - BReluoperator的输入
    - **t_min** (FLOAT|0.0) - BRelu的最小值
    - **t_max** (FLOAT|24.0) - BRelu的最大值
    - **name** (str|None) - 该层的名称(可选)。如果设置为None,该层将被自动命名


**代码示例:**

.. code-block:: python

X
xsrobin 已提交
700
    import paddle.fluid as fluid
C
Cheerego 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    x = fluid.layers.data(name="x", shape=[2,3,16,16], dtype=”float32”)
    y = fluid.layers.brelu(x, t_min=1.0, t_max=20.0)






.. _cn_api_fluid_layers_chunk_eval:

chunk_eval
-------------------------------

.. py:function:: paddle.fluid.layers.chunk_eval(input, label, chunk_scheme, num_chunk_types, excluded_chunk_types=None)

块估计(Chunk Evaluator)

该功能计算并输出块检测(chunk detection)的准确率、召回率和F1值。

chunking的一些基础请参考 `Chunking with Support Vector Machines <https://aclanthology.info/pdf/N/N01/N01-1025.pdf>`_

ChunkEvalOp计算块检测(chunk detection)的准确率、召回率和F1值,并支持IOB,IOE,IOBES和IO标注方案。以下是这些标注方案的命名实体(NER)标注例子:

::


    ====== ====== ======  =====  ==  ============   =====  ===== =====  ==  =========
           Li     Ming    works  at  Agricultural   Bank   of    China  in  Beijing.
    ====== ====== ======  =====  ==  ============   =====  ===== =====  ==  =========
    IO     I-PER  I-PER   O      O   I-ORG          I-ORG  I-ORG I-ORG  O   I-LOC
    IOB    B-PER  I-PER   O      O   B-ORG          I-ORG  I-ORG I-ORG  O   B-LOC
    IOE    I-PER  E-PER   O      O   I-ORG          I-ORG  I-ORG E-ORG  O   E-LOC
    IOBES  B-PER  E-PER   O      O   I-ORG          I-ORG  I-ORG E-ORG  O   S-LOC
    ====== ====== ======  =====  ==  ============   =====  ===== =====  ==  =========

有三种块类别(命名实体类型),包括PER(人名),ORG(机构名)和LOC(地名),标签形式为标注类型(tag type)-块类型(chunk type)。

由于计算实际上用的是标签id而不是标签,需要额外注意将标签映射到相应的id,这样CheckEvalOp才可运行。关键在于id必须在列出的等式中有效。

::


    tag_type = label % num_tag_type
    chunk_type = label / num_tag_type

num_tag_type是标注规则中的标签类型数,num_chunk_type是块类型数,tag_type从下面的表格中获取值。

::


    Scheme Begin Inside End   Single
    plain   0     -      -     -
    IOB     0     1      -     -
    IOE     -     0      1     -
    IOBES   0     1      2     3

仍以NER为例,假设标注规则是IOB块类型为ORG,PER和LOC。为了满足以上等式,标签图如下:

::


    B-ORG  0
    I-ORG  1
    B-PER  2
    I-PER  3
    B-LOC  4
    I-LOC  5
    O      6

不难证明等式的块类型数为3,IOB规则中的标签类型数为2.例如I-LOC的标签id为5,I-LOC的标签类型id为1,I-LOC的块类型id为2,与等式的结果一致。

参数:
    - **input** (Variable) - 网络的输出预测
    - **label** (Variable) - 测试数据集的标签
    - **chunk_scheme** (str) - 标注规则,表示如何解码块。必须数IOB,IOE,IOBES或者plain。详情见描述
    - **num_chunk_types** (int) - 块类型数。详情见描述
    - **excluded_chunk_types** (list) - 列表包含块类型id,表示不在计数内的块类型。详情见描述

返回:元组(tuple),包含precision, recall, f1_score, num_infer_chunks, num_label_chunks, num_correct_chunks

返回类型:tuple(元组)

**代码示例**:

.. code-block:: python:

    import paddle.fluid as fluid
     
    dict_size = 10000
    label_dict_len = 7
    sequence = fluid.layers.data(
        name='id', shape=[1], lod_level=1, dtype='int64')
    embedding = fluid.layers.embedding(
        input=sequence, size=[dict_size, 512])
    hidden = fluid.layers.fc(input=embedding, size=512)
    label = fluid.layers.data(
        name='label', shape=[1], lod_level=1, dtype='int32')
    crf = fluid.layers.linear_chain_crf(
        input=hidden, label=label, param_attr=fluid.ParamAttr(name="crfw"))
    crf_decode = fluid.layers.crf_decoding(
        input=hidden, param_attr=fluid.ParamAttr(name="crfw"))
    fluid.layers.chunk_eval(
        input=crf_decode,
        label=label,
        chunk_scheme="IOB",
        num_chunk_types=(label_dict_len - 1) / 2)









.. _cn_api_fluid_layers_clip:

clip
-------------------------------

.. py:function:: paddle.fluid.layers.clip(x, min, max, name=None)

clip算子

clip算子限制给定输入的值在一个区间内。间隔使用参数"min"和"max"来指定:公式为

.. math::
        Out=min(max(X,min),max)

参数:
        - **x** (Variable)- (Tensor)clip运算的输入,维数必须在[1,9]之间。
        - **min** (FLOAT)- (float)最小值,小于该值的元素由min代替。
        - **max** (FLOAT)- (float)最大值,大于该值的元素由max替换。
        - **name** (basestring | None)- 输出的名称。

返回:        (Tensor)clip操作后的输出和输入(X)具有形状(shape)

返回类型:        输出(Variable)。

**代码示例:**

.. code-block:: python
    
    import paddle.fluid as fluid
    input = fluid.layers.data(
        name='data', shape=[1], dtype='float32')
    reward = fluid.layers.clip(x=input, min=-1.0, max=1.0)






.. _cn_api_fluid_layers_clip_by_norm:

clip_by_norm
-------------------------------

.. py:function:: paddle.fluid.layers.clip_by_norm(x, max_norm, name=None)

ClipByNorm算子

此算子将输入 ``X`` 的L2范数限制在 ``max_norm`` 内。如果 ``X`` 的L2范数小于或等于 ``max_norm``  ,则输出(Out)将与 ``X`` 相同。如果X的L2范数大于 ``max_norm`` ,则 ``X`` 将被线性缩放,使得输出(Out)的L2范数等于 ``max_norm`` ,如下面的公式所示:

.. math::
         Out = \frac{max\_norm * X}{norm(X)}

其中, :math:`norm(X)` 代表 ``x`` 的L2范数。


参数:
        - **x** (Variable)- (Tensor) clip_by_norm运算的输入,维数必须在[1,9]之间。
        - **max_norm** (float)- 最大范数值。
        - **name** (basestring | None)- 输出的名称。

返回:        (Tensor)clip_by_norm操作后的输出和输入(X)具有形状(shape).

返回类型:       Variable

**代码示例:**

.. code-block:: python

X
xsrobin 已提交
884
    import paddle.fluid as fluid
C
Cheerego 已提交
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    input = fluid.layers.data(
        name='data', shape=[1], dtype='float32')
    reward = fluid.layers.clip_by_norm(x=input, max_norm=1.0)







.. _cn_api_fluid_layers_continuous_value_model:

continuous_value_model
-------------------------------

.. py:function:: paddle.fluid.layers.continuous_value_model(input, cvm, use_cvm=True)

**continuous_value_model层**

现在,continuous value model(cvm)仅考虑CTR项目中的展示和点击值。我们假设输入是一个含cvm_feature的词向量,其形状为[N * D](D为2 + 嵌入维度)。如果use_cvm=True,它会计算log(cvm_feature),且输出的形状为[N * D]。如果use_cvm=False,它会从输入中移除cvm_feature,且输出的形状为[N * (D - 2)]。
    
该层接受一个名为input的张量,嵌入后成为ID层(lod level为1), cvm为一个show_click info。

参数:
    - **input** (Variable)-一个N x D的二维LodTensor, N为batch size, D为2 + 嵌入维度, lod level = 1。
    - **cvm** (Variable)-一个N x 2的二维Tensor, N为batch size,2为展示和点击值。
    - **use_cvm** (bool)-分使用/不使用cvm两种情况。如果使用cvm,输出维度和输入相等;如果不使用cvm,输出维度为输入-2(移除展示和点击值)。(cvm op是一个自定义的op,其输入是一个含embed_with_cvm默认值的序列,因此我们需要一个名为cvm的op来决定是否使用cvm。)

返回:变量,一个N x D的二维LodTensor,如果使用cvm,D等于输入的维度,否则D等于输入的维度-2。

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
921
    import paddle.fluid as fluid
C
Cheerego 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    input = fluid.layers.data(name="input", shape=[-1, 1], lod_level=1, append_batch_size=False, dtype="int64")#, stop_gradient=False)
    label = fluid.layers.data(name="label", shape=[-1, 1], append_batch_size=False, dtype="int64")
    embed = fluid.layers.embedding(
                            input=input,
                            size=[100, 11],
                            dtype='float32')
    ones = fluid.layers.fill_constant_batch_size_like(input=label, shape=[-1, 1], dtype="int64", value=1)
    show_clk = fluid.layers.cast(fluid.layers.concat([ones, label], axis=1), dtype='float32')
    show_clk.stop_gradient = True
    input_with_cvm = fluid.layers.continuous_value_model(embed, show_clk, True)







.. _cn_api_fluid_layers_conv2d:

conv2d
-------------------------------

.. py:function:: paddle.fluid.layers.conv2d(input, num_filters, filter_size, stride=1, padding=0, dilation=1, groups=None, param_attr=None, bias_attr=None, use_cudnn=True, act=None, name=None)

卷积二维层(convolution2D layer)根据输入、滤波器(filter)、步长(stride)、填充(padding)、dilations、一组参数计算输出。输入和输出是NCHW格式,N是批尺寸,C是通道数,H是特征高度,W是特征宽度。滤波器是MCHW格式,M是输出图像通道数,C是输入图像通道数,H是滤波器高度,W是滤波器宽度。如果组数大于1,C等于输入图像通道数除以组数的结果。详情请参考UFLDL's : `卷积 <http://ufldl.stanford.edu/tutorial/supervised/FeatureExtractionUsingConvolution/>`_ 。如果提供了bias属性和激活函数类型,bias会添加到卷积(convolution)的结果中相应的激活函数会作用在最终结果上。

对每个输入X,有等式:

.. math::

    Out = \sigma \left ( W * X + b \right )

其中:
    - :math:`X` :输入值,NCHW格式的张量(Tensor)
    - :math:`W` :滤波器值,MCHW格式的张量(Tensor)
    - :math:`*` : 卷积操作
    - :math:`b` :Bias值,二维张量(Tensor),shape为 ``[M,1]``
    - :math:`\sigma` :激活函数
    - :math:`Out` :输出值,``Out`` 和 ``X`` 的shape可能不同

**示例**

- 输入:

  输入shape::math:`( N,C_{in},H_{in},W_{in} )`

  滤波器shape: :math:`( C_{out},C_{in},H_{f},W_{f} )`

- 输出:

  输出shape: :math:`( N,C_{out},H_{out},W_{out} )`

其中

.. math::

    H_{out} = \frac{\left ( H_{in}+2*paddings[0]-\left ( dilations[0]*\left ( H_{f}-1 \right )+1 \right ) \right )}{strides[0]}+1

    W_{out} = \frac{\left ( W_{in}+2*paddings[1]-\left ( dilations[1]*\left ( W_{f}-1 \right )+1 \right ) \right )}{strides[1]}+1

参数:
    - **input** (Variable) - 格式为[N,C,H,W]格式的输入图像
    - **num_fliters** (int) - 滤波器数。和输出图像通道相同
    - **filter_size** (int|tuple|None) - 滤波器大小。如果filter_size是一个元组,则必须包含两个整型数,(filter_size,filter_size_W)。否则,滤波器为square
    - **stride** (int|tuple) - 步长(stride)大小。如果步长(stride)为元组,则必须包含两个整型数,(stride_H,stride_W)。否则,stride_H = stride_W = stride。默认:stride = 1
    - **padding** (int|tuple) - 填充(padding)大小。如果填充(padding)为元组,则必须包含两个整型数,(padding_H,padding_W)。否则,padding_H = padding_W = padding。默认:padding = 0
    - **dilation** (int|tuple) - 膨胀(dilation)大小。如果膨胀(dialation)为元组,则必须包含两个整型数,(dilation_H,dilation_W)。否则,dilation_H = dilation_W = dilation。默认:dilation = 1
    - **groups** (int) - 卷积二维层(Conv2D Layer)的组数。根据Alex Krizhevsky的深度卷积神经网络(CNN)论文中的成组卷积:当group=2,滤波器的前一半仅和输入通道的前一半连接。滤波器的后一半仅和输入通道的后一半连接。默认:groups = 1
    - **param_attr** (ParamAttr|None) - conv2d的可学习参数/权重的参数属性。如果设为None或者ParamAttr的一个属性,conv2d创建ParamAttr为param_attr。如果param_attr的初始化函数未设置,参数则初始化为 :math:`Normal(0.0,std)` ,并且std为 :math:`\frac{2.0}{filter\_elem\_num}^{0.5}` 。默认为None
    - **bias_attr** (ParamAttr|bool|None) - conv2d bias的参数属性。如果设为False,则没有bias加到输出。如果设为None或者ParamAttr的一个属性,conv2d创建ParamAttr为bias_attr。如果bias_attr的初始化函数未设置,bias初始化为0.默认为None
    - **use_cudnn** (bool) - 是否用cudnn核,仅当下载cudnn库才有效。默认:True
    - **act** (str) - 激活函数类型,如果设为None,则未添加激活函数。默认:None
    - **name** (str|None) - 该层名称(可选)。若设为None,则自动为该层命名。

返回:张量,存储卷积和非线性激活结果

返回类型:变量(Variable)

抛出异常:
  - ``ValueError`` - 如果输入shape和filter_size,stride,padding和group不匹配。

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
1007
    import paddle.fluid as fluid
C
Cheerego 已提交
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
    data = fluid.layers.data(name='data', shape=[3, 32, 32], dtype='float32')
    conv2d = fluid.layers.conv2d(input=data, num_filters=2, filter_size=3, act="relu")











.. _cn_api_fluid_layers_conv2d_transpose:

conv2d_transpose
-------------------------------

.. py:function:: paddle.fluid.layers.conv2d_transpose(input, num_filters, output_size=None, filter_size=None, padding=0, stride=1, dilation=1, groups=None, param_attr=None, bias_attr=None, use_cudnn=True, act=None, name=None)

2-D卷积转置层(Convlution2D transpose layer)

该层根据 输入(input)、滤波器(filter)和卷积核膨胀(dilations)、步长(stride)、填充(padding)来计算输出。输入(Input)和输出(Output)为NCHW格式,其中 ``N`` 为batch大小, ``C`` 为通道数(channel),``H`` 为特征高度, ``W`` 为特征宽度。参数(膨胀、步长、填充)分别都包含两个元素。这两个元素分别表示高度和宽度。欲了解卷积转置层细节,请参考下面的说明和 参考文献_ 。如果参数 ``bias_attr`` 和 ``act`` 不为 ``None``,则在卷积的输出中加入偏置,并对最终结果应用相应的激活函数。

.. _参考文献: http://www.matthewzeiler.com/wp-content/uploads/2017/07/cvpr2010.pdf

输入 :math:`X` 和输出 :math:`Out` 函数关系如下:

.. math::
                        Out=\sigma (W*X+b)\\

其中:
    -  :math:`X` : 输入张量,具有 ``NCHW`` 格式

    -  :math:`W` : 滤波器张量,具有 ``NCHW`` 格式

    -  :math:`*` : 卷积操作

    -  :math:`b` : 偏置(bias),二维张量,shape为 ``[M,1]``

    -  :math:`σ` : 激活函数

    -  :math:`Out` : 输出值,Out和 ``X`` 的 ``shape`` 可能不一样

**样例**:

输入:

.. math::

    输入张量的shape :  (N,C_{in}, H_{in}, W_{in})

    滤波器(filter)shape : (C_{in}, C_{out}, H_f, W_f)

输出:

.. math::
    输出张量的 shape : (N,C_{out}, H_{out}, W_{out})

其中

.. math::

        & H'_{out} = (H_{in}-1)*strides[0]-2*paddings[0]+dilations[0]*(H_f-1)+1\\
        & W'_{out} = (W_{in}-1)*strides[1]-2*paddings[1]+dilations[1]*(W_f-1)+1 \\
        & H_{out}\in[H'_{out},H'_{out} + strides[0])\\
        & W_{out}\in[W'_{out},W'_{out} + strides[1])\\



参数:
  - **input** (Variable)- 输入张量,格式为[N, C, H, W]
  - **num_filters** (int) - 滤波器(卷积核)的个数,与输出的图片的通道数( channel )相同
  - **output_size** (int|tuple|None) - 输出图片的大小。如果output_size是一个元组(tuple),则该元形式为(image_H,image_W),这两个值必须为整型。如果output_size=None,则内部会使用filter_size、padding和stride来计算output_size。如果output_size和filter_size是同时指定的,那么它们应满足上面的公式。
  - **filter_size** (int|tuple|None) - 滤波器大小。如果filter_size是一个tuple,则形式为(filter_size_H, filter_size_W)。否则,滤波器将是一个方阵。如果filter_size=None,则内部会计算输出大小。
  - **padding** (int|tuple) - 填充大小。如果padding是一个元组,它必须包含两个整数(padding_H、padding_W)。否则,padding_H = padding_W = padding。默认:padding = 0。
  - **stride** (int|tuple) - 步长大小。如果stride是一个元组,那么元组的形式为(stride_H、stride_W)。否则,stride_H = stride_W = stride。默认:stride = 1。
  - **dilation** (int|元组) - 膨胀(dilation)大小。如果dilation是一个元组,那么元组的形式为(dilation_H, dilation_W)。否则,dilation_H = dilation_W = dilation_W。默认:dilation= 1。
  - **groups** (int) - Conv2d转置层的groups个数。从Alex Krizhevsky的CNN Deep论文中的群卷积中受到启发,当group=2时,前半部分滤波器只连接到输入通道的前半部分,而后半部分滤波器只连接到输入通道的后半部分。默认值:group = 1。
  - **param_attr** (ParamAttr|None) - conv2d_transfer中可学习参数/权重的属性。如果param_attr值为None或ParamAttr的一个属性,conv2d_transfer使用ParamAttrs作为param_attr的值。如果没有设置的param_attr初始化器,那么使用Xavier初始化。默认值:None。
  - **bias_attr** (ParamAttr|bool|None) - conv2d_tran_bias中的bias属性。如果设置为False,则不会向输出单元添加偏置。如果param_attr值为None或ParamAttr的一个属性,将conv2d_transfer使用ParamAttrs作为,bias_attr。如果没有设置bias_attr的初始化器,bias将初始化为零。默认值:None。
  - **use_cudnn** (bool) - 是否使用cudnn内核,只有已安装cudnn库时才有效。默认值:True。
  - **act** (str) -  激活函数类型,如果设置为None,则不使用激活函数。默认值:None。
  - **name** (str|None) -  该layer的名称(可选)。如果设置为None, 将自动命名该layer。默认值:True。


返回: 存储卷积转置结果的张量。

返回类型: 变量(variable)

抛出异常:
    -  ``ValueError`` : 如果输入的shape、filter_size、stride、padding和groups不匹配,抛出ValueError

**代码示例**

..  code-block:: python

X
xsrobin 已提交
1105
    import paddle.fluid as fluid
C
Cheerego 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
    data = fluid.layers.data(name='data', shape=[3, 32, 32], dtype='float32')
    conv2d_transpose = fluid.layers.conv2d_transpose(input=data, num_filters=2, filter_size=3)











.. _cn_api_fluid_layers_conv3d:

conv3d
-------------------------------

.. py:function:: paddle.fluid.layers.conv3d(input, num_filters, filter_size, stride=1, padding=0, dilation=1, groups=None, param_attr=None, bias_attr=None, use_cudnn=True, act=None, name=None)

3D卷积层(convolution3D layer)根据输入、滤波器(filter)、步长(stride)、填充(padding)、膨胀(dilations)、组数参数计算得到输出。输入和输出是NCHW格式,N是批尺寸,C是通道数,H是特征高度,W是特征宽度。卷积三维(Convlution3D)和卷积二维(Convlution2D)相似,但多了一维深度(depth)。如果提供了bias属性和激活函数类型,bias会添加到卷积(convolution)的结果中相应的激活函数会作用在最终结果上。

对每个输入X,有等式:

.. math::


    Out = \sigma \left ( W * X + b \right )

其中:
    - :math:`X` :输入值,NCDHW格式的张量(Tensor)
    - :math:`W` :滤波器值,MCDHW格式的张量(Tensor)
    - :math:`*` : 卷积操作
    - :math:`b` :Bias值,二维张量(Tensor),形为 ``[M,1]``
    - :math:`\sigma` :激活函数
    - :math:`Out` :输出值, 和 ``X`` 的形状可能不同

**示例**

- 输入:
    输入shape: :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`

    滤波器shape: :math:`(C_{out}, C_{in}, D_f, H_f, W_f)`
- 输出:
    输出shape: :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})`

其中

.. math::


    D_{out}&= \frac{(D_{in} + 2 * paddings[0] - (dilations[0] * (D_f - 1) + 1))}{strides[0]} + 1 \\
    H_{out}&= \frac{(H_{in} + 2 * paddings[1] - (dilations[1] * (H_f - 1) + 1))}{strides[1]} + 1 \\
    W_{out}&= \frac{(W_{in} + 2 * paddings[2] - (dilations[2] * (W_f - 1) + 1))}{strides[2]} + 1

参数:
    - **input** (Variable) - 格式为[N,C,D,H,W]格式的输入图像
    - **num_fliters** (int) - 滤波器数。和输出图像通道相同
    - **filter_size** (int|tuple|None) - 滤波器大小。如果filter_size是一个元组,则必须包含三个整型数,(filter_size_D, filter_size_H, filter_size_W)。否则,滤波器为棱长为int的立方体形。
    - **stride** (int|tuple) - 步长(stride)大小。如果步长(stride)为元组,则必须包含三个整型数, (stride_D, stride_H, stride_W)。否则,stride_D = stride_H = stride_W = stride。默认:stride = 1
    - **padding** (int|tuple) - 填充(padding)大小。如果填充(padding)为元组,则必须包含三个整型数,(padding_D, padding_H, padding_W)。否则, padding_D = padding_H = padding_W = padding。默认:padding = 0
    - **dilation** (int|tuple) - 膨胀(dilation)大小。如果膨胀(dialation)为元组,则必须包含两个整型数, (dilation_D, dilation_H, dilation_W)。否则,dilation_D = dilation_H = dilation_W = dilation。默认:dilation = 1
    - **groups** (int) - 卷积二维层(Conv2D Layer)的组数。根据Alex Krizhevsky的深度卷积神经网络(CNN)论文中的成组卷积:当group=2,滤波器的前一半仅和输入通道的前一半连接。滤波器的后一半仅和输入通道的后一半连接。默认:groups = 1
    - **param_attr** (ParamAttr|None) - conv2d的可学习参数/权重的参数属性。如果设为None或者ParamAttr的一个属性,conv2d创建ParamAttr为param_attr。如果param_attr的初始化函数未设置,参数则初始化为 :math:`Normal(0.0,std)`,并且std为 :math:`\left ( \frac{2.0}{filter\_elem\_num} \right )^{0.5}` 。默认为None
    - **bias_attr** (ParamAttr|bool|None) - conv2d bias的参数属性。如果设为False,则没有bias加到输出。如果设为None或者ParamAttr的一个属性,conv2d创建ParamAttr为bias_attr。如果bias_attr的初始化函数未设置,bias初始化为0.默认为None
    - **use_cudnn** (bool) - 是否用cudnn核,仅当下载cudnn库才有效。默认:True
    - **act** (str) - 激活函数类型,如果设为None,则未添加激活函数。默认:None
    - **name** (str|None) - 该层名称(可选)。若设为None,则自动为该层命名。

返回:张量,存储卷积和非线性激活结果

返回类型:变量(Variable)

抛出异常:
  - ``ValueError`` - 如果 ``input`` 的形和 ``filter_size`` , ``stride`` , ``padding`` 和 ``group`` 不匹配。

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
1186
    import paddle.fluid as fluid
C
Cheerego 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
    data = fluid.layers.data(name='data', shape=[3, 12, 32, 32], dtype='float32')
    conv3d = fluid.layers.conv3d(input=data, num_filters=2, filter_size=3, act="relu")









.. _cn_api_fluid_layers_conv3d_transpose:

conv3d_transpose
-------------------------------

.. py:function:: paddle.fluid.layers.conv3d_transpose(input, num_filters, output_size=None, filter_size=None, padding=0, stride=1, dilation=1, groups=None, param_attr=None, bias_attr=None, use_cudnn=True, act=None, name=None)

3-D卷积转置层(Convlution3D transpose layer)

该层根据 输入(input)、滤波器(filter)和卷积核膨胀(dilations)、步长(stride)、填充来计算输出。输入(Input)和输出(Output)为NCDHW格式。其中 ``N`` 为batch大小, ``C`` 为通道数(channel), ``D``  为特征深度, ``H`` 为特征高度, ``W`` 为特征宽度。参数(膨胀、步长、填充)分别包含两个元素。这两个元素分别表示高度和宽度。欲了解卷积转置层细节,请参考下面的说明和 参考文献_ 。如果参数 ``bias_attr`` 和 ``act`` 不为None,则在卷积的输出中加入偏置,并对最终结果应用相应的激活函数

.. _参考文献: http://www.matthewzeiler.com/wp-content/uploads/2017/07/cvpr2010.pdf

输入X和输出Out函数关系X,有等式如下:

.. math::
                        \\Out=\sigma (W*X+b)\\

其中:
    -  :math:`X` : 输入张量,具有 ``NCDHW`` 格式

    -  :math:`W` : 滤波器张量,,具有 ``NCDHW`` 格式

    -  :math:`*` : 卷积操作

    -  :math:`b` : 偏置(bias),二维张量,shape为 ``[M,1]``

    -  :math:`σ` : 激活函数

    -  :math:`Out` : 输出值, ``Out`` 和 ``X`` 的 shape可能不一样


**样例**

输入:

.. math::

    Input shape: (N,C_{in},D_{in},H_{in},W_{in})

    Filter shape: (C_{in},C_{out},D_f,H_f,W_f)



输出:

.. math::

    Output shape: (N,C_{out},D_{out},H_{out},W_{out})


其中:

.. math::



    D_{out}=(D_{in}-1)*strides[0]-2*paddings[0]+dilations[0]*(D_f-1)+1

    H_{out}=(H_{in}-1)*strides[1]-2*paddings[1]+dilations[1]*(H_f-1)+1

    W_{out}=(W_{in}-1)*strides[2]-2*paddings[2]+dilations[2]*(W_f-1)+1



参数:
  - **input** (Variable)- 输入张量,格式为[N, C, D, H, W]
  - **num_filters** (int) - 滤波器(卷积核)的个数,与输出的图片的通道数(channel)相同
  - **output_size** (int|tuple|None) - 输出图片的大小。如果 ``output_size`` 是一个元组(tuple),则该元形式为(image_H,image_W),这两个值必须为整型。如果 ``output_size=None`` ,则内部会使用filter_size、padding和stride来计算output_size。如果 ``output_size`` 和 ``filter_size`` 是同时指定的,那么它们应满足上面的公式。
  - **filter_size** (int|tuple|None) - 滤波器大小。如果 ``filter_size`` 是一个tuple,则形式为(filter_size_H, filter_size_W)。否则,滤波器将是一个方阵。如果 ``filter_size=None`` ,则内部会计算输出大小。
  - **padding** (int|tuple) - 填充大小。如果 ``padding`` 是一个元组,它必须包含两个整数(padding_H、padding_W)。否则,padding_H = padding_W = padding。默认:padding = 0。
  - **stride** (int|tuple) - 步长大小。如果 ``stride`` 是一个元组,那么元组的形式为(stride_H、stride_W)。否则,stride_H = stride_W = stride。默认:stride = 1。
  - **dilation** (int|元组) - 膨胀大小。如果 ``dilation`` 是一个元组,那么元组的形式为(dilation_H, dilation_W)。否则,dilation_H = dilation_W = dilation_W。默认:dilation= 1。
  - **groups** (int) - Conv2d转置层的groups个数。从Alex Krizhevsky的CNN Deep论文中的群卷积中受到启发,当group=2时,前半部分滤波器只连接到输入通道的前半部分,而后半部分滤波器只连接到输入通道的后半部分。默认值:group = 1。
  - **param_attr** (ParamAttr|None) - conv2d_transfer中可学习参数/权重的属性。如果param_attr值为None或ParamAttr的一个属性,conv2d_transfer使用ParamAttrs作为param_attr的值。如果没有设置的param_attr初始化器,那么使用Xavier初始化。默认值:None。
  - **bias_attr** (ParamAttr|bool|None) - conv2d_tran_bias中的bias属性。如果设置为False,则不会向输出单元添加偏置。如果param_attr值为None或ParamAttr的一个属性,将conv2d_transfer使用ParamAttrs作为,bias_attr。如果没有设置bias_attr的初始化器,bias将初始化为零。默认值:None。
  - **use_cudnn** (bool) - 是否使用cudnn内核,只有已安装cudnn库时才有效。默认值:True。
  - **act** (str) -  激活函数类型,如果设置为None,则不使用激活函数。默认值:None。
  - **name** (str|None) - 该layer的名称(可选)。如果设置为None, 将自动命名该layer。默认值:True。


返回: 存储卷积转置结果的张量。

返回类型: 变量(variable)

抛出异常:
    -  ``ValueError``  : 如果输入的shape、filter_size、stride、padding和groups不匹配,抛出ValueError


**代码示例**

..  code-block:: python

X
xsrobin 已提交
1291
    import paddle.fluid as fluid
C
Cheerego 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
    data = fluid.layers.data(name='data', shape=[3, 12, 32, 32], dtype='float32')
    conv3d_transpose = fluid.layers.conv3d_transpose(input=data, num_filters=2, filter_size=3)










.. _cn_api_fluid_layers_cos_sim:

cos_sim
-------------------------------

.. py:function:: paddle.fluid.layers.cos_sim(X, Y)

余弦相似度算子(Cosine Similarity Operator)

.. math::

        Out = \frac{X^{T}*Y}{\sqrt{X^{T}*X}*\sqrt{Y^{T}*Y}}

输入X和Y必须具有相同的shape,除非输入Y的第一维为1(不同于输入X),在计算它们的余弦相似度之前,Y的第一维会被broadcasted,以匹配输入X的shape。

输入X和Y都携带或者都不携带LoD(Level of Detail)信息。但输出仅采用输入X的LoD信息。

参数:
    - **X** (Variable) - cos_sim操作函数的一个输入
    - **Y** (Variable) - cos_sim操作函数的第二个输入

返回:cosine(X,Y)的输出

返回类型:变量(Variable)

**代码示例**

..  code-block:: python

X
xsrobin 已提交
1333
     import paddle.fluid as fluid
C
Cheerego 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
     x = fluid.layers.data(name='x', shape=[3, 7], dtype='float32', append_batch_size=False)
     y = fluid.layers.data(name='y', shape=[1, 7], dtype='float32', append_batch_size=False)
     out = fluid.layers.cos_sim(x, y)





.. _cn_api_fluid_layers_crf_decoding:

crf_decoding
-------------------------------

.. py:function::  paddle.fluid.layers.crf_decoding(input, param_attr, label=None)

该函数读取由 ``linear_chain_crf`` 学习的emission feature weights(发射状态特征的权重)和 transition feature weights(转移特征的权重)。
本函数实现了Viterbi算法,可以动态地寻找隐藏状态最可能的序列,该序列也被称为Viterbi路径(Viterbi path),从而得出的标注(tags)序列。

这个运算的结果会随着 ``Label`` 参数的有无而改变:

      1. ``Label`` 非None的情况,在实际训练中时常发生。此时本函数会协同 ``chunk_eval`` 工作。本函数会返回一行形为[N X 1]的向量,其中值为0的部分代表该label不适合作为对应结点的标注,值为1的部分则反之。此类型的输出可以直接作为 ``chunk_eval`` 算子的输入

      2. 当没有 ``Label`` 时,该函数会执行标准decoding过程

(没有 ``Label`` 时)该运算返回一个形为 [N X 1]的向量,其中元素取值范围为 0 ~ 最大标注个数-1,分别为预测出的标注(tag)所在的索引。

参数:
    - **input** (Variable)(LoDTensor,默认类型为 LoDTensor<float>) — 一个形为 [N x D] 的LoDTensor,其中 N 是mini-batch的大小,D是标注(tag) 的总数。 该输入是 ``linear_chain_crf`` 的 unscaled emission weight matrix (未标准化的发射权重矩阵)
    - **param_attr** (ParamAttr) — 参与训练的参数的属性
    - **label** (Variable)(LoDTensor,默认类型为 LoDTensor<int64_t>) —  形为[N x 1]的正确标注(ground truth)。 该项可选择传入。 有关该参数的更多信息,请详见上述描述

返回:(LoDTensor, LoDTensor<int64_t>)decoding结果。具体内容根据 ``Label`` 参数是否提供而定。请参照函数介绍来详细了解。

返回类型: Variable


**代码示例**

..  code-block:: python

X
xsrobin 已提交
1374
    import paddle.fluid as fluid
C
Cheerego 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
    images = fluid.layers.data(name='pixel', shape=[784], dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int32')
    hidden = fluid.layers.fc(input=images, size=2)
    crf = fluid.layers.linear_chain_crf(input=hidden, label=label,
            param_attr=fluid.ParamAttr(name="crfw"))
    crf_decode = fluid.layers.crf_decoding(input=hidden,
            param_attr=fluid.ParamAttr(name="crfw"))












.. _cn_api_fluid_layers_crop:

crop
-------------------------------

.. py:function:: paddle.fluid.layers.crop(x, shape=None, offsets=None, name=None)

根据偏移量(offsets)和形状(shape),裁剪输入张量。

**样例**:

::

    * Case 1:
        Given
            X = [[0, 1, 2, 0, 0]
                 [0, 3, 4, 0, 0]
                 [0, 0, 0, 0, 0]],
        and
            shape = [2, 2],
            offsets = [0, 1],
        output is:
            Out = [[1, 2],
                   [3, 4]].
    * Case 2:
        Given
            X = [[0, 1, 2, 5, 0]
                 [0, 3, 4, 6, 0]
                 [0, 0, 0, 0, 0]],
        and shape is tensor
            shape = [[0, 0, 0]
                     [0, 0, 0]]
        and
            offsets = [0, 1],

        output is:
            Out = [[1, 2, 5],
                   [3, 4, 6]].


参数:
  - **x** (Variable): 输入张量。
  - **shape** (Variable|list/tuple of integer) - 输出张量的形状由参数shape指定,它可以是一个变量/整数的列表/整数元组。如果是张量变量,它的秩必须与x相同。该方式适可用于每次迭代时候需要改变输出形状的情况。如果是整数列表/tupe,则其长度必须与x的秩相同
  - **offsets** (Variable|list/tuple of integer|None) - 指定每个维度上的裁剪的偏移量。它可以是一个Variable,或者一个整数list/tupe。如果是一个tensor variable,它的rank必须与x相同,这种方法适用于每次迭代的偏移量(offset)都可能改变的情况。如果是一个整数list/tupe,则长度必须与x的rank的相同,如果shape=None,则每个维度的偏移量为0。
  - **name** (str|None) - 该层的名称(可选)。如果设置为None,该层将被自动命名。

返回: 裁剪张量。

返回类型: 变量(Variable)

抛出异常: 如果形状不是列表、元组或变量,抛出ValueError


**代码示例**:

..  code-block:: python
    
    import paddle.fluid as fluid
    x = fluid.layers.data(name="x", shape=[3, 5], dtype="float32")
    y = fluid.layers.data(name="y", shape=[2, 3], dtype="float32")
    crop = fluid.layers.crop(x, shape=y)


    ## or
    z = fluid.layers.data(name="z", shape=[3, 5], dtype="float32")
    crop = fluid.layers.crop(z, shape=[2, 3])










.. _cn_api_fluid_layers_cross_entropy:

cross_entropy
-------------------------------

.. py:function:: paddle.fluid.layers.cross_entropy(input, label, soft_label=False, ignore_index=-100)

该函数定义了输入和标签之间的cross entropy(交叉熵)层。该函数支持standard cross-entropy computation(标准交叉熵损失计算)
以及soft-label cross-entropy computation(软标签交叉熵损失计算)

  1. One-hot cross-entropy算法

     soft_label = False, Label[i, 0] 指明样本i的类别所具的索引:
                            .. math::
                                     \\Y[i]=-log(X[i,Label[i]])\\

  2. Soft-label cross-entropy算法

     soft_label = True, Label[i, j] 表明样本i对应类别j的soft label(软标签):
                            .. math::
                                     \\Y[i]= \sum_{j}-Label[i,j]*log(X[i,j])\\

     **请确保采用此算法时识别为各软标签的概率总和为1**

  3. One-hot cross-entropy with vecterized label(使用向量化标签的One-hot)算法

     作为 *2* 的特殊情况,当软类标签内部只有一个非零概率元素,且它的值为1,那么 *2* 算法降级为一种仅有one-hot标签的one-hot交叉熵





参数:
    - **input** (Variable|list) – 一个形为[N x D]的二维tensor,其中N是batch大小,D是类别(class)数目。 这是由之前的operator计算出的概率,绝大多数情况下是由softmax operator得出的结果
    - **label** (Variable|list) – 一个二维tensor组成的正确标记的数据集(ground truth)。 当 ``soft_label`` 为False时,label为形为[N x 1]的tensor<int64>。 ``soft_label`` 为True时, label是形为 [N x D]的 tensor<float/double>
    - **soft_label** (bool) – 标志位,指明是否需要把给定的标签列表认定为软标签。默认为False。
    - **ignore_index** (int) – 指定一个被无视的目标值,并且这个值不影响输入梯度。仅在 ``soft_label`` 为False时生效。 默认值: kIgnoreIndex

返回: 一个形为[N x 1]的二维tensor,承载了交叉熵损失

弹出异常: ``ValueError``

                        1. 当 ``input`` 的第一维和 ``label`` 的第一维不相等时,弹出异常
                        2. 当 ``soft_label`` 值为True, 且 ``input`` 的第二维和 ``label`` 的第二维不相等时,弹出异常
                        3. 当 ``soft_label`` 值为False,且 ``label`` 的第二维不是1时,弹出异常



**代码示例**

..  code-block:: python

X
xsrobin 已提交
1522
        import paddle.fluid as fluid
C
Cheerego 已提交
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
        classdim = 7
        x = fluid.layers.data(name='x', shape=[3, 7], dtype='float32', append_batch_size=False)
        label = fluid.layers.data(name='label', shape=[3, 1], dtype='float32', append_batch_size=False)
        predict = fluid.layers.fc(input=x, size=classdim, act='softmax')
        cost = fluid.layers.cross_entropy(input=predict, label=label)













.. _cn_api_fluid_layers_ctc_greedy_decoder:

ctc_greedy_decoder
-------------------------------

.. py:function:: paddle.fluid.layers.ctc_greedy_decoder(input, blank, name=None)

此op用于贪婪策略解码序列,步骤如下:
    1. 获取输入中的每一行的最大值索引,也就是numpy.argmax(input, axis=0)。
    2. 对于step1结果中的每个序列,在两个空格之间合并重复部分(即合并重复的上一步中的到的索引值)并删除所有空格。


简单举一个例子,

::

        已知:

        input.data = [[0.6, 0.1, 0.3, 0.1],
                      [0.3, 0.2, 0.4, 0.1],
                      [0.1, 0.5, 0.1, 0.3],
                      [0.5, 0.1, 0.3, 0.1],

                      [0.5, 0.1, 0.3, 0.1],
                      [0.2, 0.2, 0.2, 0.4],
                      [0.2, 0.2, 0.1, 0.5],
                      [0.5, 0.1, 0.3, 0.1]]

        input.lod = [[4, 4]]

        计算过程:

        1. 将argmax的运算结果应用于输入的第一个序列,即 input.data[0:4] 。
           则得出的结果为[[0], [2], [1], [0]]
        2. 合并重复的索引值部分,删除空格,即为0的值。
           则第一个输入序列对应的输出为:[[2], [1]]

        最后

        output.data = [[2],
                       [1],
                       [3]]

        output.lod = [[2, 1]]


参数:
        - **input** (Variable) — (LoDTensor<float>),变长序列的概率,它是一个具有LoD信息的二维张量。它的形状是[Lp, num_classes + 1],其中Lp是所有输入序列长度的和,num_classes是真正的类别。(不包括空白标签)。
        - **blank** (int) — Connectionist Temporal Classification (CTC) loss空白标签索引,  属于半开区间[0,num_classes + 1)。
        - **name** (str) — 此层的名称。可选。

返回: CTC贪婪解码结果是一个形为(Lp,1)的二维张量,其中Lp是所有输出序列的长度之和。如果结果中的所有序列都为空,则输出LoDTensor 为[-1],其中LoD[[]] 形为[1,1]。

返回类型: 变量(Variable)


**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[8], dtype='float32')

    cost = fluid.layers.ctc_greedy_decoder(input=x, blank=0)





.. _cn_api_fluid_layers_data_norm:

data_norm
-------------------------------

.. py:function:: paddle.fluid.layers.data_norm(input, act=None, epsilon=1e-05, param_attr=None, data_layout='NCHW', in_place=False, name=None, moving_mean_name=None, moving_variance_name=None, do_model_average_for_mean_and_var=False)

**数据正则化层**

可用作conv2d和fully_connected操作的正则化函数。 此层所需的数据格式为以下之一:

1. NHWC [batch, in_height, in_width, in_channels]
2. NCHW [batch, in_channels, in_height, in_width]

:math:`input` 为一个mini-batch上的特征:

.. math::
        \mu_{\beta} &\gets \frac{1}{m} \sum_{i=1}^{m} x_i \qquad &//\
        \ mini-batch\ mean \\
        \sigma_{\beta}^{2} &\gets \frac{1}{m} \sum_{i=1}^{m}(x_i - \
        \mu_{\beta})^2 \qquad &//\ mini-batch\ variance \\
        \hat{x_i} &\gets \frac{x_i - \mu_\beta} {\sqrt{\
        \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\
        y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift

参数:
  - **input** (variable) - 输入变量,它是一个LoDTensor。
  - **act** (string,默认None) - 激活函数类型,线性| relu | prelu | ...
  - **epsilon** (float,默认1e-05) -
  - **param_attr** (ParamAttr) - 参数比例的参数属性。
  - **data_layout** (string,默认NCHW) -  NCHW | NHWC
  - **in_place** (bool,默认值False) - 使data_norm的输入和输出复用同一块内存。
  - **name** (string,默认None) - 此层的名称(可选)。 如果设置为None,则将自动命名该层。
  - **moving_mean_name** (string,Default None) - 存储全局Mean的moving_mean的名称。
  - **moving_variance_name** (string,默认None) - 存储全局Variance的moving_variance的名称。
  - **do_model_average_for_mean_and_var** (bool,默认值为false) - 是否为mean和variance进行模型平均。

返回: 张量变量,是对输入数据进行正则化后的结果。

返回类型: Variable

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid

    hidden1 = fluid.layers.data(name="hidden1", shape=[200])
    hidden2 = fluid.layers.data_norm(name="hidden2", input=hidden1)







.. _cn_api_fluid_layers_deformable_conv:

deformable_conv
-------------------------------

.. py:function:: paddle.fluid.layers.deformable_conv(input, offset, mask, num_filters, filter_size, stride=1, padding=0, dilation=1, groups=None, deformable_groups=None, im2col_step=None, param_attr=None, bias_attr=None, name=None)

可变形卷积层

在4-D输入上计算2-D可变形卷积。给定输入图像x,输出特征图y,可变形卷积操作如下所示:
\[y(p) = \sum_{k=1}^{K}{w_k * x(p + p_k + \Delta p_k) * \Delta m_k}\]
其中\(\Delta p_k\) 和 \(\Delta m_k\) 分别为第k个位置的可学习偏移和调制标量。
参考可变形卷积网络v2:可变形程度越高,结果越好。

**示例**
     
输入:
    输入形状: \((N, C_{in}, H_{in}, W_{in})\)
    卷积核形状: \((C_{out}, C_{in}, H_f, W_f)\)
    偏移形状: \((N, 2 * deformable\_groups * H_f * H_w, H_{in}, W_{in})\)
    掩膜形状: \((N, deformable\_groups * H_f * H_w, H_{in}, W_{in})\)
     
输出:
    输出形状: \((N, C_{out}, H_{out}, W_{out})\)

其中
    \[\begin{split}H_{out}&= \frac{(H_{in} + 2 * paddings[0] - (dilations[0] * (H_f - 1) + 1))}{strides[0]} + 1 \\
    W_{out}&= \frac{(W_{in} + 2 * paddings[1] - (dilations[1] * (W_f - 1) + 1))}{strides[1]} + 1\end{split}\]
     

参数:
    - **input** (Variable) - 形式为[N, C, H, W]的输入图像。
    - **offset** (Variable) – 可变形卷积层的输入坐标偏移。
    - **Mask** (Variable) – 可变形卷积层的输入掩膜。
    - **num_filters** (int) – 卷积核数。和输出图像通道数相同。
    - **filter_size** (int|tuple|None) – 卷积核大小。如果filter_size为元组,则必须包含两个整数(filter_size_H, filter_size_W)。否则卷积核将为方形。
    - **stride** (int|tuple) – 步长大小。如果stride为元组,则必须包含两个整数(stride_H, stride_W)。否则stride_H = stride_W = stride。默认stride = 1。
    - **padding** (int|tuple) – padding大小。如果padding为元组,则必须包含两个整数(padding_H, padding_W)。否则padding_H = padding_W = padding。默认padding = 0。
    - **dilation** (int|tuple) – dilation大小。如果dilation为元组,则必须包含两个整数(dilation_H, dilation_W)。否则dilation_H = dilation_W = dilation。默认dilation = 1。
    - **groups** (int) – 可变形卷积层的群组数。依据Alex Krizhevsky的Deep CNN论文中的分组卷积,有:当group=2时,前一半卷积核只和前一半输入通道有关,而后一半卷积核只和后一半输入通道有关。默认groups=1。
    - **deformable_groups** (int) – 可变形群组分区数。默认deformable_groups = 1。
    - **im2col_step** (int) – 每个im2col计算的最大图像数。总batch大小应该可以被该值整除或小于该值。如果您面临内存问题,可以尝试在此处使用一个更小的值。默认im2col_step = 64。
    - **param_attr** (ParamAttr|None) – 可变形卷积的可学习参数/权重的参数属性。如果将其设置为None或ParamAttr的一个属性,可变形卷积将创建ParamAttr作为param_attr。如果没有设置此param_attr的Initializer,该参数将被\(Normal(0.0, std)\)初始化,且其中的\(std\) 为 \((\frac{2.0 }{filter\_elem\_num})^{0.5}\)。默认值None。
    - **bias_attr** (ParamAttr|bool|None) – 可变形卷积层的偏置的参数属性。如果设为False,则输出单元不会加偏置。如果设为None或者ParamAttr的一个属性,conv2d会创建ParamAttr作为bias_attr。如果不设置bias_attr的Initializer,偏置会被初始化为0。默认值None。
    - **name** (str|None) – 该层的名字(可选项)。如果设为None,该层将会被自动命名。默认值None。
 
返回:储存可变形卷积结果的张量变量。
     
返回类型:变量(Variable)
     
抛出:ValueError – 如果input, filter_size, stride, padding和groups的大小不匹配。

**代码示例**

..  code-block:: python

X
xsrobin 已提交
1722
    import paddle.fluid as fluid
C
Cheerego 已提交
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    data = fluid.layers.data(name='data', shape=[3, 32, 32], dtype='float32')
    offset = fluid.layers.data(name='offset', shape=[18, 32, 32], dtype='float32')
    mask = fluid.layers.data(name='mask', shape=[9, 32, 32], dtype='float32')
    out = fluid.layers.deformable_conv(input=data, offset=offset, mask=mask, num_filters=2, filter_size=3, padding=1)






.. _cn_api_fluid_layers_deformable_roi_pooling:

deformable_roi_pooling
-------------------------------

.. py:function:: paddle.fluid.layers.deformable_roi_pooling(input, rois, trans, no_trans=False, spatial_scale=1.0, group_size=[1, 1], pooled_height=1, pooled_width=1, part_size=None, sample_per_part=1, trans_std=0.1, position_sensitive=False, name=None)

可变形PSROI池层

参数:
    - **input** (Variable) - 可变形PSROI池层的输入。输入张量的形状为[N,C,H,W]。其中N是批量大小,C是输入通道的数量,H是特征的高度,W是特征的宽度。
    - **rois** (Variable)- 将池化的ROIs(感兴趣区域)。应为一个形状为(num_rois, 4)的2-D LoDTensor,且lod level为1。给出[[x1, y1, x2, y2], ...],(x1, y1)为左上角坐标,(x2, y2)为右下角坐标。
    - **trans** (Variable)- 池化时ROIs上的特征偏移。格式为NCHW,其中N是ROIs的数量,C是通道的数量,指示x和y方向上的偏移距离,H是池化的高度,W是池化的宽度。
    - **no_trans** (bool)- roi池化阶段是否加入偏移以获取新值。取True或False。默认为False。
    - **spatial_scale** (float) - 输入特征图的高度(或宽度)与原始图像高度(或宽度)的比率。等于卷积图层中总步长的倒数,默认为1.0。
    - **group_size** (list|tuple)- 输入通道划分成的组数(例如,输入通道的数量是k1 * k2 *(C + 1),其中k1和k2是组宽度和高度,C + 1是输出通道的数量。如( 4,6)中4是组的高度,6是组的宽度)。默认为[1,1]。
    - **pooled_height** (integer)- 池化后输出的高度。
    - **pooled_width** (integer)- 池化后输出的宽度。
    - **part_size** (list|tuple)- 偏移高度和宽度,如(4, 6)代表高度为4、宽度为6,默认为None,此时默认值[pooled_height, pooled_width]。
    - **sample_per_part** (integer)- 每个bin中的样本数量,默认为1。
    - **trans_std** (float)- 偏移系数,默认为0.1。
    - **position_sensitive** (bool)- 是否选择可变形psroi池化模式,默认为False。
    - **name** (str)- 层名,默认为None。

返回: 存储可变形psroi池层的张量变量

返回类型:  变量(Variable)

**代码示例**

..  code-block:: python

X
xsrobin 已提交
1765
    import paddle.fluid as fluid
C
Cheerego 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
    input = fluid.layers.data(name="input",
                              shape=[2, 192, 64, 64],
                              dtype='float32',
                              append_batch_size=False)
    rois = fluid.layers.data(name="rois",
                             shape=[4],
                             dtype='float32',
                             lod_level=1)
    trans = fluid.layers.data(name="trans",
                              shape=[2, 384, 64, 64],
                              dtype='float32',
                              append_batch_size=False)
    x = fluid.layers.nn.deformable_roi_pooling(input=input,
                                                 rois=rois,
                                                 trans=trans,
                                                 no_trans=False,
                                                 spatial_scale=1.0,
                                                 group_size=(1, 1),
                                                 pooled_height=8,
                                                 pooled_width=8,
                                                 part_size=(8, 8),
                                                 sample_per_part=4,
                                                 trans_std=0.1,
                                                 position_sensitive=False)

.. _cn_api_fluid_layers_dice_loss:

dice_loss
-------------------------------

.. py:function:: paddle.fluid.layers.dice_loss(input, label, epsilon=1e-05)

dice_loss是比较两批数据相似度,通常用于二值图像分割,即标签为二值。

dice_loss定义为:

.. math::
        dice\_loss &= 1- \frac{2 * intersection\_area}{total\_rea}\\
                   &= \frac{(total\_area−intersection\_area)−intersection\_area}{total\_area}\\
                   &= \frac{union\_area−intersection\_area}{total\_area}

参数:
    - **input** (Variable) - rank>=2的预测。第一个维度是batch大小,最后一个维度是类编号。
    - **label** (Variable)- 与输入tensor rank相同的正确的标注数据(groud truth)。第一个维度是batch大小,最后一个维度是1。
    - **epsilon** (float) - 将会加到分子和分母上。如果输入和标签都为空,则确保dice为1。默认值:0.00001

返回: dice_loss shape为[1]。

返回类型:  dice_loss(Variable)

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='data', shape = [3, 224, 224, 2], dtype='float32')
    label = fluid.layers.data(name='label', shape=[3, 224, 224, 1], dtype='float32')
    predictions = fluid.layers.softmax(x)
    loss = fluid.layers.dice_loss(input=predictions, label=label)











.. _cn_api_fluid_layers_dropout:

dropout
-------------------------------

.. py:function:: paddle.fluid.layers.dropout(x,dropout_prob,is_test=False,seed=None,name=None,dropout_implementation='downgrade_in_infer')

dropout操作

丢弃或者保持x的每个元素独立。Dropout是一种正则化技术,通过在训练过程中阻止神经元节点间的联合适应性来减少过拟合。根据给定的丢弃概率dropout操作符随机将一些神经元输出设置为0,其他的仍保持不变。

dropout op可以从Program中删除,提高执行效率。

参数:
    - **x** (Variable)-输入张量
    - **dropout_prob** (float)-设置为0的单元的概率
    - **is_test** (bool)-显示是否进行测试用语的标记
    - **seed** (int)-Python整型,用于创建随机种子。如果该参数设为None,则使用随机种子。注:如果给定一个整型种子,始终丢弃相同的输出单元。训练过程中勿用固定不变的种子。
    - **name** (str|None)-该层名称(可选)。如果设置为None,则自动为该层命名
    - **dropout_implementation** (string) -

      [‘downgrade_in_infer’(default)|’upscale_in_train’] 其中:

      1. downgrade_in_infer(default), 在预测时减小输出结果

         - train: out = input * mask

         - inference: out = input * (1.0 - dropout_prob)

         (mask是一个张量,维度和输入维度相同,值为0或1,值为0的比例即为 ``dropout_prob`` )

      2. upscale_in_train, 增加训练时的结果

         - train: out = input * mask / ( 1.0 - dropout_prob )

         - inference: out = input

         (mask是一个张量,维度和输入维度相同,值为0或1,值为0的比例即为 ``dropout_prob`` )

dropout操作符可以从程序中移除,程序变得高效。

返回:与输入X,shape相同的张量

返回类型:变量

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
1885
    import paddle.fluid as fluid
C
Cheerego 已提交
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070
    x = fluid.layers.data(name="data", shape=[32, 32], dtype="float32")
    droped = fluid.layers.dropout(x, dropout_prob=0.5)









.. _cn_api_fluid_layers_dynamic_gru:

dynamic_gru
-------------------------------

.. py:function::  paddle.fluid.layers.dynamic_gru(input, size, param_attr=None, bias_attr=None, is_reverse=False, gate_activation='sigmoid', candidate_activation='tanh', h_0=None, origin_mode=False)



**实现了Gated Recurrent Unit层。**

如果origin_mode为False,那么gru运算公式来自论文 `Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling <https://arxiv.org/abs/1412.3555>`_ 。


公式如下:

.. math::
  u_{t}=act_g(W_{ux}x_{t}+W_{uh}h_{t-1}+b_{u})
.. math::
  r_{t}=act_g(W_{rx}x_{t}+W_{rh}h_{t-1}+b_{r})
.. math::
  \widetilde{h_{t}}=act_{c}(W_{cx}x_{t}+W_{ch}(r_{t}\odot h_{t-1})+b_c)
.. math::
  h_t=(1-u_t)\odot h_{t-1}+u_t\odot \widetilde{h_t}




如果origin_mode为True,那么运算公式来自于 `Learning Phrase Representations using RNN Encoder Decoder for Statistical Machine Translation <https://arxiv.org/pdf/1406.1078.pdf>`_



公式如下:

.. math::
    u_t & = act_g(W_{ux}x_{t} + W_{uh}h_{t-1} + b_u)\\
    r_t & = act_g(W_{rx}x_{t} + W_{rh}h_{t-1} + b_r)\\
    \tilde{h_t} & = act_c(W_{cx}x_{t} + W_{ch}(r_t \odot h_{t-1}) + b_c)\\
    h_t & = u_t \odot h_{t-1} + (1-u_t) \odot \tilde{h_t}




其中, :math:`\odot` 为按元素将向量相乘。 :math:`act_g` 是更新门(update gate)和重置门(reset gate)的激励函数(activation), 常为 :math:`sigmoid` 函数。 :math:`act_c` 是candidate hidden state(候选隐藏状态)的激励函数,常为 :math:`tanh` 。

注意 :math:`W_{ux}x_{t},W_{rx}x_{t},W_{cx}x_{t}` 这些在 input  :math:`x_t` 上的操作不包括在该运算中。用户可以选择性地在GRU层之前使用FC层来进行这一操作。



参数:
  - **input** (Variable) – dynamic_gru层的输入, 支持variable time length input sequence(可变时长输入序列)。 本变量底层的tensor是一个(T×3D)矩阵, 其中T是该mini-batch中总时间步数, D是隐藏状态的规模(hidden size)。
  - **size** (int) – GRU cell的维度
  - **param_attr** (ParamAttr|None)  –  可学习的隐藏层权重矩阵的参数属性。
    注意:
                                    - 该矩阵为一个(T X 3D)矩阵。其中D为隐藏状态的规模(hidden size)
                                    - 该矩阵的所有元素由两部分组成。一是update gate和reset gate的权重,形为(D X 2D),二是候选隐藏状态(candidate hidden state)的权重,形为 (D X D)
    如果该函数参数被设为None或者 ``ParamAttr`` 类的属性之一,则会生成一个 ``ParamAttr`` 类的对象作为param_attr。如果param_attr未被初始化(即其构造函数未被设置),Xavier会负责初始化它。 默认值为None。
  - **bias_attr** (ParamAttr|bool|None) - GRU层bias的参数属性。该(1 X 3D)形的bias变量将会连结(concatenate)在update gate(更新门)、reset gate(重置门)、candidate calculations(候选隐藏状态计算)后。如果值为False,将没有bias会应用到上述三个过程中。如果该函数参数被设为None或者 ``ParamAttr`` 类的属性之一, ``dynamic_gru`` 会生成一个 ``ParamAttr`` 类的对象作为param_attr。如果bias_attr未被初始化(即其构造函数未被设置),则它会被初始化为0。默认值为None。
  - **is_reverse** (bool) –是否计算反GRU(reversed GRU),默认为False
  - **gate_activation** (str) – update gate 和 reset gate的激励函数(activation)。 可选择[“sigmoid”, “tanh”, “relu”, “identity”]其一, 默认为 “sigmoid”
  - **candidate_activation** (str) – candidate hidden state(候选隐藏状态)计算所需的激励函数(activation)。 可从[“sigmoid”, “tanh”, “relu”, “identity”]中选择, 默认为 “tanh”
  - **h_0** (Variable) – 该函数参数为初始隐藏状态。若未赋值,则默认为0。它是一个 (N x D) tensor, 其中 N 为输入mini-batch的总时间步数, D 为 隐藏状态规模(hidden size)


返回: GRU的隐藏状态(hidden state)。形为(T X D),序列长度和输入相同。

返回类型: 变量(variable)


**代码示例**

..  code-block:: python

    import paddle.fluid as fluid

    dict_dim, emb_dim = 128, 64
    data = fluid.layers.data(name='sequence', shape=[1],
                             dtype='int32', lod_level=1)
    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])
    hidden_dim = 512
    x = fluid.layers.fc(input=emb, size=hidden_dim * 3)
    hidden = fluid.layers.dynamic_gru(input=x, size=hidden_dim)
















.. _cn_api_fluid_layers_dynamic_lstm:

dynamic_lstm
-------------------------------

.. py:function::  paddle.fluid.layers.dynamic_lstm(input, size, h_0=None, c_0=None, param_attr=None, bias_attr=None, use_peepholes=True, is_reverse=False, gate_activation='sigmoid', cell_activation='tanh', candidate_activation='tanh', dtype='float32', name=None)

LSTM,即Long-Short Term Memory(长短期记忆)运算。

默认实现方式为diagonal/peephole连接(https://arxiv.org/pdf/1402.1128.pdf),公式如下:


.. math::
      i_t=\sigma (W_{ix}x_{t}+W_{ih}h_{t-1}+W_{ic}c_{t-1}+b_i)
.. math::
      f_t=\sigma (W_{fx}x_{t}+W_{fh}h_{t-1}+W_{fc}c_{t-1}+b_f)
.. math::
      \widetilde{c_t}=act_g(W_{ct}x_{t}+W_{ch}h_{t-1}+b_{c})
.. math::
      o_t=\sigma (W_{ox}x_{t}+W_{oh}h_{t-1}+W_{oc}c_{t}+b_o)
.. math::
      c_t=f_t\odot c_{t-1}+i_t\odot \widetilde{c_t}
.. math::
      h_t=o_t\odot act_h(c_t)

W 代表了权重矩阵(weight matrix),例如 :math:`W_{xi}` 是从输入门(input gate)到输入的权重矩阵, :math:`W_{ic}` ,:math:`W_{fc}` ,  :math:`W_{oc}` 是对角权重矩阵(diagonal weight matrix),用于peephole连接。在此实现方式中,我们使用向量来代表这些对角权重矩阵。

其中:
      - :math:`b` 表示bias向量( :math:`b_i` 是输入门的bias向量)
      - :math:`σ` 是非线性激励函数(non-linear activations),比如逻辑sigmoid函数
      - :math:`i` ,:math:`f` ,:math:`o` 和 :math:`c` 分别为输入门(input gate),遗忘门(forget gate),输出门(output gate),以及神经元激励向量(cell activation vector)这些向量和神经元输出激励向量(cell output activation vector) :math:`h` 有相同的大小。
      - :math:`⊙` 意为按元素将两向量相乘
      - :math:`act_g` , :math:`act_h` 分别为神经元(cell)输入、输出的激励函数(activation)。常常使用tanh函数。
      - :math:`\widetilde{c_t}` 也被称为候选隐藏状态(candidate hidden state)。可根据当前输入和之前的隐藏状态计算而得

将 ``use_peepholes`` 设为False来禁用 peephole 连接方法。 公式等详细信息请参考 http://www.bioinf.jku.at/publications/older/2604.pdf 。

注意, :math:`W_{xi}x_t, W_{xf}x_t, W_{xc}x_t,W_{xo}x_t` 这些在输入 :math:`x_t` 上的操作不包括在此运算中。用户可以在LSTM operator之前选择使用全连接运算。




参数:
  - **input** (Variable) (LoDTensor) - LodTensor类型,支持variable time length input sequence(时长可变的输入序列)。 该LoDTensor中底层的tensor是一个形为(T X 4D)的矩阵,其中T为此mini-batch上的总共时间步数。D为隐藏层的大小、规模(hidden size)
  - **size** (int) – 4 * 隐藏层大小
  - **h_0** (Variable) – 最初的隐藏状态(hidden state),可选项。默认值为0。它是一个(N x D)张量,其中N是batch大小,D是隐藏层大小。
  - **c_0** (Variable) – 最初的神经元状态(cell state), 可选项。 默认值0。它是一个(N x D)张量, 其中N是batch大小。h_0和c_0仅可以同时为None,不能只其中一个为None。
  - **param_attr** (ParamAttr|None) – 可学习的隐藏层权重的参数属性。
    注意:
                      - Weights = :math:`\{W_{ch}, W_{ih},  W_{fh},  W_{oh} \}`
                      - 形为(D x 4D), 其中D是hidden size(隐藏层规模)

    如果它被设为None或者 ``ParamAttr`` 属性之一, dynamic_lstm会创建 ``ParamAttr`` 对象作为param_attr。如果没有对param_attr初始化(即构造函数没有被设置), Xavier会负责初始化参数。默认为None。
  - **bias_attr** (ParamAttr|None) – 可学习的bias权重的属性, 包含两部分,input-hidden bias weights(输入隐藏层的bias权重)和 peephole connections weights(peephole连接权重)。如果 ``use_peepholes`` 值为 ``True`` , 则意为使用peephole连接的权重。
    另外:
      - use_peepholes = False - Biases = :math:`\{ b_c,b_i,b_f,b_o \}` - 形为(1 x 4D)。
      - use_peepholes = True - Biases = :math:`\{ b_c,b_i,b_f,b_o,W_{ic},W_{fc},W_{oc} \}` - 形为 (1 x 7D)。

    如果它被设为None或 ``ParamAttr`` 的属性之一, ``dynamic_lstm`` 会创建一个 ``ParamAttr`` 对象作为bias_attr。 如果没有对bias_attr初始化(即构造函数没有被设置),bias会被初始化为0。默认值为None。
  - **use_peepholes** (bool) – (默认: True) 是否使用diagonal/peephole连接方式
  - **is_reverse** (bool) – (默认: False) 是否计算反LSTM(reversed LSTM)
  - **gate_activation** (str) – (默认: "sigmoid")应用于input gate(输入门),forget gate(遗忘门)和 output gate(输出门)的激励函数(activation),默认为sigmoid
  - **cell_activation** (str) – (默认: tanh)用于神经元输出的激励函数(activation), 默认为tanh
  - **candidate_activation** (str) – (默认: tanh)candidate hidden state(候选隐藏状态)的激励函数(activation), 默认为tanh
  - **dtype** (str) – 即 Data type(数据类型)。 可以选择 [“float32”, “float64”],默认为“float32”
  - **name** (str|None) – 该层的命名,可选项。如果值为None, 将会自动对该层命名

返回:隐藏状态(hidden state),LSTM的神经元状态。两者都是(T x D)形,且LoD保持与输入一致

返回类型: 元组(tuple)


**代码示例**

..  code-block:: python

X
xsrobin 已提交
2071
  import paddle.fluid as fluid
C
Cheerego 已提交
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
  emb_dim = 256
  vocab_size = 10000
  hidden_dim = 512

  data = fluid.layers.data(name='x', shape=[1],
                 dtype='int32', lod_level=1)
  emb = fluid.layers.embedding(input=data, size=[vocab_size, emb_dim], is_sparse=True)
     
  forward_proj = fluid.layers.fc(input=emb, size=hidden_dim * 4,
                                 bias_attr=False)
  forward, _ = fluid.layers.dynamic_lstm(
      input=forward_proj, size=hidden_dim * 4, use_peepholes=False)













.. _cn_api_fluid_layers_dynamic_lstmp:

dynamic_lstmp
-------------------------------
.. py:function:: paddle.fluid.layers.dynamic_lstmp(input, size, proj_size, param_attr=None, bias_attr=None, use_peepholes=True, is_reverse=False, gate_activation='sigmoid', cell_activation='tanh', candidate_activation='tanh', proj_activation='tanh', dtype='float32', name=None, h_0=None, c_0=None, cell_clip=None, proj_clip=None)

动态LSTMP层(Dynamic LSTMP Layer)

LSTMP层(具有循环映射的LSTM)在LSTM层后有一个分离的映射层,从原始隐藏状态映射到较低维的状态,用来减少参数总数,减少LSTM计算复杂度,特别是输出单元相对较大的情况下。 `Long Short-Term Memory Recurrent Neural Network Architectures for Large Scale Acoustic Modeling <https://research.google.com/pubs/archive/43905.pdf>`_

公式如下:

.. math::

        i_t & = \sigma(W_{ix}x_{t} + W_{ir}r_{t-1} + W_{ic}c_{t-1} + b_i)\\
        f_t & = \sigma(W_{fx}x_{t} + W_{fr}r_{t-1} + W_{fc}c_{t-1} + b_f)\\
        \tilde{c_t} & = act_g(W_{cx}x_t + W_{cr}r_{t-1} + b_c)\\
        o_t & = \sigma(W_{ox}x_{t} + W_{or}r_{t-1} + W_{oc}c_t + b_o)\\
        c_t & = f_t \odot c_{t-1} + i_t \odot \tilde{c_t}\\
        h_t & = o_t \odot act_h(c_t)\\
        r_t & = \overline{act_h}(W_{rh}h_t)\\


在以上公式中:
    - :math:`W` : 代表权重矩阵(例如 :math:`W_{xi}` 是输入门道输入的权重矩阵)
    - :math:`W_{ic}` , :math:`W_{fc}` , :math:`W_{oc}`  : peephole connections的对角权重矩阵。在我们的实现中,外面用向量代表这些对角权重矩阵
    - :math:`b` : 代表偏差向量(例如 :math:`b_{i}` 是输入偏差向量)
    - :math:`\delta` : 激活函数,比如逻辑回归函数
    - :math:`i,f,o` 和 :math:`c` :分别代表输入门,遗忘门,输出门和cell激活函数向量,四者的大小和cell输出激活函数向量 :math:`h` 的四者大小相等
    - :math:`h` : 隐藏状态
    - :math:`r` : 隐藏状态的循环映射
    - :math:`\tilde{c_t}` : 候选隐藏状态
    - :math:`\odot` : 向量的元素状态生成
    - :math:`act_g` 和 :math:`act_h` : cell输入和cell输出激活函数,通常使用 :math:`tanh`
    - :math:`\overline{act_h}` : 映射输出的激活函数,通常用 :math:`identity` 或等同的 :math:`act_h`

将 ``use_peepholes`` 设置为False,断开窥视孔连接(peephole connection)。在此省略公式,详情请参照论文 `LONG SHORT-TERM MEMORY <http://www.bioinf.jku.at/publications/older/2604.pdf>`_ 。

注意输入 :math:`x_{t}` 中的 :math:`W_{xi}x_{t},W_{xf}x_{t},W_{xc}x_{t},W_{xo}x_{t}` 不在此操作符中。用户选择在LSTMP层之前使用全链接层。

参数:
    - **input** (Variable) - dynamic_lstmp层的输入,支持输入序列长度为变量的倍数。该变量的张量为一个矩阵,维度为(T X 4D),T为mini-batch的总时间步长,D是隐藏大小。
    - **size** (int) - 4*隐藏状态大小(hidden size)
    - **proj_size** (int) - 投影输出的大小
    - **param_attr** (ParamAttr|None) -   可学习hidden-hidden权重和投影权重的参数属性。
      说明:
        - Hidden-hidden (隐藏状态到隐藏状态)权重 = :math:`\{ W_{ch},W_{ih},W_{fh},W_{oh} \}`
        - hidden-hidden权重的权重矩阵为(P*4D),P是投影大小,D是隐藏大小。
        - 投影(Projection)权重 = :math:`\{ W_{rh} \}`
        - 投影权重的shape为(D\*P)

      如果设为None或者ParamAttr的一个属性,dynamic_lstm将创建ParamAttr为param_attr。如果param_attr的初始函数未设置,参数则初始化为Xavier。默认:None。
    - **bias_attr** (ParamAttr|None) - 可学习bias权重的bias属性,包含输入隐藏的bias权重和窥视孔连接权重(peephole connection),前提是use_peepholes设为True。

      说明:
        1.use_peepholes = False
            - Biases = { :math:`b_{c},b_{i},b_{f},b_{o}`}.
            - 维度为(1*4D)

        2.use_peepholes = True
            - Biases = { :math:`b_{c},b_{i},b_{f},b_{o},W_{ic},W_{fc},W_{oc}`}
            - 维度为(1*7D)

        如果设置为None或者ParamAttr的一个属性,dynamic_lstm将创建ParamAttr为bias_attr。bias_attr的初始函数未设置,bias则初始化为0.默认:None。

    - **use_peepholes** (bool) - 是否开启诊断/窥视孔链接,默认为True。
    - **is_reverse** (bool) - 是否计算反向LSTM,默认为False。
    - **gate_activation** (bool) - 输入门(input gate)、遗忘门(forget gate)和输出门(output gate)的激活函数。Choices = [“sigmoid”,“tanh”,“relu”,“identity”],默认“sigmoid”。
    - **cell_activation** (str) - cell输出的激活函数。Choices = [“sigmoid”,“tanh”,“relu”,“identity”],默认“tanh”。
    - **candidate_activation** (str) - 候选隐藏状态(candidate hidden state)的激活状态。Choices = [“sigmoid”,“tanh”,“relu”,“identity”],默认“tanh”。
    - **proj_activation** (str) - 投影输出的激活函数。Choices = [“sigmoid”,“tanh”,“relu”,“identity”],默认“tanh”。
    - **dtype** (str) - 数据类型。Choices = [“float32”,“float64”],默认“float32”。
    - **name** (str|None) - 该层名称(可选)。若设为None,则自动为该层命名。
    - **h_0** (Variable) - 初始隐藏状态是可选输入,默认为0。这是一个具有形状的张量(N x D),其中N是批大小,D是投影大小。
    - **c_0** (Variable) - 初始cell状态是可选输入,默认为0。这是一个具有形状(N x D)的张量,其中N是批大小。h_0和c_0可以为空,但只能同时为空。
    - **cell_clip** (float) - 如果提供该参数,则在单元输出激活之前,单元状态将被此值剪裁。
    - **proj_clip** (float) - 如果 num_proj > 0 并且 proj_clip 被提供,那么将投影值沿元素方向剪切到[-proj_clip,proj_clip]内

返回:含有两个输出变量的元组,隐藏状态(hidden state)的投影和LSTMP的cell状态。投影的shape为(T*P),cell state的shape为(T*D),两者的LoD和输入相同。

返回类型:元组(tuple)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
2183
    import paddle.fluid as fluid
C
Cheerego 已提交
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
    dict_dim, emb_dim = 128, 64
    data = fluid.layers.data(name='sequence', shape=[1],
                         dtype='int32', lod_level=1)
    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])
    hidden_dim, proj_dim = 512, 256
    fc_out = fluid.layers.fc(input=emb, size=hidden_dim * 4,
                         act=None, bias_attr=None)
    proj_out, _ = fluid.layers.dynamic_lstmp(input=fc_out,
                                         size=hidden_dim * 4,
                                         proj_size=proj_dim,
                                         use_peepholes=False,
                                         is_reverse=True,
                                         cell_activation="tanh",
                                         proj_activation="tanh")











.. _cn_api_fluid_layers_edit_distance:


edit_distance
-------------------------------

.. py:function:: paddle.fluid.layers.edit_distance(input,label,normalized=True,ignored_tokens=None)

编辑距离算子

计算一批给定字符串及其参照字符串间的编辑距离。编辑距离也称Levenshtein距离,通过计算从一个字符串变成另一个字符串所需的最少操作步骤来衡量两个字符串的相异度。这里的操作包括插入、删除和替换。

比如给定假设字符串A=“kitten”和参照字符串B=“sitting”,从A变换成B编辑距离为3,至少需要两次替换和一次插入:

“kitten”->“sitten”->“sittn”->“sitting”

输入为LoDTensor,包含假设字符串(带有表示批尺寸的总数)和分离信息(具体为LoD信息)。并且批尺寸大小的参照字符串和输入LoDTensor的顺序保持一致。

输出包含批尺寸大小的结果,代表一对字符串中每个字符串的编辑距离。如果Attr(normalized)为真,编辑距离则处以参照字符串的长度。

参数:
    - **input** (Variable)-假设字符串的索引
    - **label** (Variable)-参照字符串的索引
    - **normalized** (bool,默认为True)-表示是否用参照字符串的长度进行归一化
    - **ignored_tokens** (list<int>,默认为None)-计算编辑距离前需要移除的token
    - **name** (str)-该层名称,可选

返回:[batch_size,1]中序列到序列到编辑距离

返回类型:变量

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[1], dtype='int64')
    y = fluid.layers.data(name='y', shape=[1], dtype='int64')
    cost, _ = fluid.layers.edit_distance(input=x,label=y)

    cpu = fluid.core.CPUPlace()
    exe = fluid.Executor(cpu)
    exe.run(fluid.default_startup_program())

    import numpy
    x_ = numpy.random.randint(5, size=(2, 1)).astype('int64')
    y_ = numpy.random.randint(5, size=(2, 1)).astype('int64')
    
    print(x_)
    print(y_)
    
    x = fluid.create_lod_tensor(x_, [[2]], cpu)
    y = fluid.create_lod_tensor(y_, [[2]], cpu)
    
    outs = exe.run(feed={'x':x, 'y':y}, fetch_list=[cost.name])
    
    print(outs)









.. _cn_api_fluid_layers_elementwise_add:

elementwise_add
-------------------------------

.. py:function:: paddle.fluid.layers.elementwise_add(x, y, axis=-1, act=None, name=None)

逐元素相加算子

等式为:

.. math::
        Out = X + Y

- :math:`X` :任意维度的张量(Tensor).
- :math:`Y` :一个维度必须小于等于X维度的张量(Tensor)。
对于这个运算算子有2种情况:
        1. :math:`Y` 的形状(shape)与 :math:`X` 相同。
        2. :math:`Y` 的形状(shape)是 :math:`X` 的连续子序列。

对于情况2:
        1. 用 :math:`Y` 匹配 :math:`X` 的形状(shape),则 ``axis`` 为 :math:`Y` 传到 :math:`X` 上的起始维度索引。
        2. 如果 ``axis`` 为-1(默认值),则 :math:`axis= rank(X)-rank(Y)` 。
        3. 考虑到子序列, :math:`Y` 的大小为1的尾部维度将被忽略,例如shape(Y)=(2,1)=>(2)。

例如:

..  code-block:: text

        shape(X) = (2, 3, 4, 5), shape(Y) = (,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
        shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
        shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
        shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0

输入 :math:`X` 和 :math:`Y` 可以携带不同的LoD信息。但输出仅与输入 :math:`X` 共享LoD信息。

参数:
        - **x** (Tensor)- 第一个输入张量(Tensor)。
        - **y** (Tensor)- 第二个输入张量(Tensor)。
        - **axis** (INT)- (int,默认-1)。将Y传到X上的起始维度索引。
        - **act** (basestring | None)- 激活函数名称,应用于输出。
        - **name** (basestring | None)- 输出的名称。

返回:        元素运算的输出。

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    # 例1: shape(x) = (2, 3, 4, 5), shape(y) = (2, 3, 4, 5)
    x0 = fluid.layers.data(name="x0", shape=[2, 3, 4, 5], dtype='float32')
    y0 = fluid.layers.data(name="y0", shape=[2, 3, 4, 5], dtype='float32')
    z0 = fluid.layers.elementwise_add(x0, y0)
     
    # 例2: shape(X) = (2, 3, 4, 5), shape(Y) = (5)
    x1 = fluid.layers.data(name="x1", shape=[2, 3, 4, 5], dtype='float32')
    y1 = fluid.layers.data(name="y1", shape=[5], dtype='float32')
    z1 = fluid.layers.elementwise_add(x1, y1)
     
    # 例3: shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
    x2 = fluid.layers.data(name="x2", shape=[2, 3, 4, 5], dtype='float32')
    y2 = fluid.layers.data(name="y2", shape=[4, 5], dtype='float32')
    z2 = fluid.layers.elementwise_add(x2, y2, axis=2)
     
    # 例4: shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
    x3 = fluid.layers.data(name="x3", shape=[2, 3, 4, 5], dtype='float32')
    y3 = fluid.layers.data(name="y3", shape=[3, 4], dtype='float32')
    z3 = fluid.layers.elementwise_add(x3, y3, axis=1)
     
    # 例5: shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
    x4 = fluid.layers.data(name="x4", shape=[2, 3, 4, 5], dtype='float32')
    y4 = fluid.layers.data(name="y4", shape=[2], dtype='float32')
    z4 = fluid.layers.elementwise_add(x4, y4, axis=0)
     
    # 例6: shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
    x5 = fluid.layers.data(name="x5", shape=[2, 3, 4, 5], dtype='float32')
    y5 = fluid.layers.data(name="y5", shape=[2], dtype='float32')
    z5 = fluid.layers.elementwise_add(x5, y5, axis=0)





.. _cn_api_fluid_layers_elementwise_div:

elementwise_div
-------------------------------

.. py:function:: paddle.fluid.layers.elementwise_div(x, y, axis=-1, act=None, name=None)

逐元素相除算子

等式是:

.. math::
        Out = X / Y

- :math:`X` :任何维度的张量(Tensor)。
- :math:`Y` :维度必须小于或等于X维度的张量(Tensor)。

此运算算子有两种情况:
        1. :math:`Y` 的形状(shape)与 :math:`X` 相同。
        2. :math:`Y` 的形状(shape)是 :math:`X` 的连续子序列。

对于情况2:
        1. 用 :math:`Y` 匹配 :math:`X` 的形状(shape),其中 ``axis`` 将是 :math:`Y` 传到 :math:`X` 上的起始维度索引。
        2. 如果 ``axis`` 为-1(默认值),则 :math:`axis = rank(X)-rank(Y)` 。
        3. 考虑到子序列, :math:`Y` 的大小为1的尾部维度将被忽略,例如shape(Y)=(2,1)=>(2)。

例如:

..  code-block:: text

        shape(X) = (2, 3, 4, 5), shape(Y) = (,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
        shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
        shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
        shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0

输入 :math:`X` 和 :math:`Y` 可以携带不同的LoD信息。但输出仅与输入 :math:`X` 共享LoD信息。

参数:
        - **x** (Tensor)- 第一个输入张量(Tensor)。
        - **y** (Tensor)- 第二个输入张量(Tensor)。
        - **axis** (INT)- (int,默认-1)。将Y传到X上的起始维度索引。
        - **act** (basestring | None)- 激活函数名称,应用于输出。
        - **name** (basestring | None)- 输出的名称。

返回:        元素运算的输出。

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    # 例1: shape(x) = (2, 3, 4, 5), shape(y) = (2, 3, 4, 5)
    x0 = fluid.layers.data(name="x0", shape=[2, 3, 4, 5], dtype='float32')
    y0 = fluid.layers.data(name="y0", shape=[2, 3, 4, 5], dtype='float32')
    z0 = fluid.layers.elementwise_div(x0, y0)
     
    # 例2: shape(X) = (2, 3, 4, 5), shape(Y) = (5)
    x1 = fluid.layers.data(name="x1", shape=[2, 3, 4, 5], dtype='float32')
    y1 = fluid.layers.data(name="y1", shape=[5], dtype='float32')
    z1 = fluid.layers.elementwise_div(x1, y1)
     
    # 例3: shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
    x2 = fluid.layers.data(name="x2", shape=[2, 3, 4, 5], dtype='float32')
    y2 = fluid.layers.data(name="y2", shape=[4, 5], dtype='float32')
    z2 = fluid.layers.elementwise_div(x2, y2, axis=2)
     
    # 例4: shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
    x3 = fluid.layers.data(name="x3", shape=[2, 3, 4, 5], dtype='float32')
    y3 = fluid.layers.data(name="y3", shape=[3, 4], dtype='float32')
    z3 = fluid.layers.elementwise_div(x3, y3, axis=1)
     
    # 例5: shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
    x4 = fluid.layers.data(name="x4", shape=[2, 3, 4, 5], dtype='float32')
    y4 = fluid.layers.data(name="y4", shape=[2], dtype='float32')
    z4 = fluid.layers.elementwise_div(x4, y4, axis=0)
     
    # 例6: shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
    x5 = fluid.layers.data(name="x5", shape=[2, 3, 4, 5], dtype='float32')
    y5 = fluid.layers.data(name="y5", shape=[2], dtype='float32')
    z5 = fluid.layers.elementwise_div(x5, y5, axis=0)














.. _cn_api_fluid_layers_elementwise_floordiv:
    
elementwise_floordiv
-------------------------------

.. py:function:: paddle.fluid.layers.elementwise_floordiv(x, y, axis=-1, act=None, name=None)

FloorDiv运算。

等式是:

.. math::
        Out = X // Y

- :math:`X` :任何维度的张量(Tensor)。
- :math:`Y` :维度必须小于或等于X维度的张量(Tensor)。

此运算分两种情况:
        1. :math:`Y` 的形状(shape)与 :math:`X` 相同。
        2. :math:`Y` 的形状(shape)是 :math:`X` 的连续子序列。

对于情况2:
        1. 用 :math:`Y` 匹配 :math:`X` 的形状(shape),其中 ``axis`` 将是 :math:`Y` 传到 :math:`X` 上的起始维度索引。
        2. 如果 ``axis`` 为-1(默认值),则 :math:`axis = rank(X)-rank(Y)` 。
        3. 考虑到子序列, :math:`Y` 的大小为1的尾部维度将被忽略,例如shape(Y)=(2,1)=>(2)。

例如:

..  code-block:: text

        shape(X) = (2, 3, 4, 5), shape(Y) = (,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
        shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
        shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
        shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0

输入 :math:`X` 和 :math:`Y` 可以携带不同的LoD信息。但输出仅与输入 :math:`X` 共享LoD信息。

参数:
        - **x** (Tensor)- 第一个输入张量(Tensor)。
        - **y** (Tensor)- 第二个输入张量(Tensor)。
        - **axis** (INT)- (int,默认-1)。将Y传到X上的起始维度索引。
        - **act** (basestring | None)- 激活函数名称,应用于输出。
        - **name** (basestring | None)- 输出的名称。

返回:        元素运算的输出。


**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    # example 1: shape(x) = (2, 3, 4, 5), shape(y) = (2, 3, 4, 5)
    x0 = fluid.layers.data(name="x0", shape=[2, 3, 4, 5], dtype='float32')
    y0 = fluid.layers.data(name="y0", shape=[2, 3, 4, 5], dtype='float32')
    z0 = fluid.layers.elementwise_floordiv(x0, y0)

    # example 2: shape(X) = (2, 3, 4, 5), shape(Y) = (5)
    x1 = fluid.layers.data(name="x1", shape=[2, 3, 4, 5], dtype='float32')
    y1 = fluid.layers.data(name="y1", shape=[5], dtype='float32')
    z1 = fluid.layers.elementwise_floordiv(x1, y1)

    # example 3: shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
    x2 = fluid.layers.data(name="x2", shape=[2, 3, 4, 5], dtype='float32')
    y2 = fluid.layers.data(name="y2", shape=[4, 5], dtype='float32')
    z2 = fluid.layers.elementwise_floordiv(x2, y2, axis=2)

    # example 4: shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
    x3 = fluid.layers.data(name="x3", shape=[2, 3, 4, 5], dtype='float32')
    y3 = fluid.layers.data(name="y3", shape=[3, 4], dtype='float32')
    z3 = fluid.layers.elementwise_floordiv(x3, y3, axis=1)

    # example 5: shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
    x4 = fluid.layers.data(name="x4", shape=[2, 3, 4, 5], dtype='float32')
    y4 = fluid.layers.data(name="y4", shape=[2], dtype='float32')
    z4 = fluid.layers.elementwise_floordiv(x4, y4, axis=0)

    # example 6: shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
    x5 = fluid.layers.data(name="x5", shape=[2, 3, 4, 5], dtype='float32')
    y5 = fluid.layers.data(name="y5", shape=[2], dtype='float32')
    z5 = fluid.layers.elementwise_floordiv(x5, y5, axis=0)






.. _cn_api_fluid_layers_elementwise_max:

elementwise_max
-------------------------------

.. py:function:: paddle.fluid.layers.elementwise_max(x, y, axis=-1, act=None, name=None)
最大元素算子

等式是:

.. math::
        Out = max(X, Y)

- :math:`X` :任何维度的张量(Tensor)。
- :math:`Y` :维度必须小于或等于X维度的张量(Tensor)。

此运算算子有两种情况:
        1. :math:`Y` 的形状(shape)与 :math:`X` 相同。
        2. :math:`Y` 的形状(shape)是 :math:`X` 的连续子序列。

对于情况2:
        1. 用 :math:`Y` 匹配 :math:`X` 的形状(shape),其中 ``axis`` 将是 :math:`Y` 传到 :math:`X` 上的起始维度索引。
        2. 如果 ``axis`` 为-1(默认值),则 :math:`axis = rank(X)-rank(Y)` 。
        3. 考虑到子序列, :math:`Y` 的大小为1的尾部维度将被忽略,例如shape(Y)=(2,1)=>(2)。

例如:

..  code-block:: text

        shape(X) = (2, 3, 4, 5), shape(Y) = (,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
        shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
        shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
        shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0

输入X和Y可以携带不同的LoD信息。但输出仅与输入X共享LoD信息。

参数:
        - **x** (Tensor)- 第一个输入张量(Tensor)。
        - **y** (Tensor)- 第二个输入张量(Tensor)。
        - **axis** (INT)- (int,默认-1)。将Y传到X上的起始维度索引。
        - **act** (basestring | None)- 激活函数名称,应用于输出。
        - **name** (basestring | None)- 输出的名称。

返回:        元素运算的输出。

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    # 例1: shape(x) = (2, 3, 4, 5), shape(y) = (2, 3, 4, 5)
    x0 = fluid.layers.data(name="x0", shape=[2, 3, 4, 5], dtype='float32')
    y0 = fluid.layers.data(name="y0", shape=[2, 3, 4, 5], dtype='float32')
    z0 = fluid.layers.elementwise_max(x0, y0)
     
    # 例2: shape(X) = (2, 3, 4, 5), shape(Y) = (5)
    x1 = fluid.layers.data(name="x1", shape=[2, 3, 4, 5], dtype='float32')
    y1 = fluid.layers.data(name="y1", shape=[5], dtype='float32')
    z1 = fluid.layers.elementwise_max(x1, y1)
     
    # 例3: shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
    x2 = fluid.layers.data(name="x2", shape=[2, 3, 4, 5], dtype='float32')
    y2 = fluid.layers.data(name="y2", shape=[4, 5], dtype='float32')
    z2 = fluid.layers.elementwise_max(x2, y2, axis=2)
     
    # 例4: shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
    x3 = fluid.layers.data(name="x3", shape=[2, 3, 4, 5], dtype='float32')
    y3 = fluid.layers.data(name="y3", shape=[3, 4], dtype='float32')
    z3 = fluid.layers.elementwise_max(x3, y3, axis=1)
     
    # 例5: shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
    x4 = fluid.layers.data(name="x4", shape=[2, 3, 4, 5], dtype='float32')
    y4 = fluid.layers.data(name="y4", shape=[2], dtype='float32')
    z4 = fluid.layers.elementwise_max(x4, y4, axis=0)
     
    # 例6: shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
    x5 = fluid.layers.data(name="x5", shape=[2, 3, 4, 5], dtype='float32')
    y5 = fluid.layers.data(name="y5", shape=[2], dtype='float32')
    z5 = fluid.layers.elementwise_max(x5, y5, axis=0)








.. _cn_api_fluid_layers_elementwise_min:

elementwise_min
-------------------------------

.. py:function:: paddle.fluid.layers.elementwise_min(x, y, axis=-1, act=None, name=None)

最小元素算子

等式是:

.. math::
        Out = min(X, Y)

- :math:`X` :任何维度的张量(Tensor)。
- :math:`Y` :维度必须小于或等于X维度的张量(Tensor)。

此运算算子有两种情况:
        1. :math:`Y` 的形状(shape)与 :math:`X` 相同。
        2. :math:`Y` 的形状(shape)是 :math:`X` 的连续子序列。

对于情况2:
        1. 用 :math:`Y` 匹配 :math:`X` 的形状(shape),其中 ``axis`` 将是 :math:`Y` 传到 :math:`X` 上的起始维度索引。
        2. 如果 ``axis`` 为-1(默认值),则 :math:`axis = rank(X)-rank(Y)` 。
        3. 考虑到子序列, :math:`Y` 的大小为1的尾部维度将被忽略,例如shape(Y)=(2,1)=>(2)。

例如:

..  code-block:: text

        shape(X) = (2, 3, 4, 5), shape(Y) = (,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
        shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
        shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
        shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0

输入X和Y可以携带不同的LoD信息。但输出仅与输入X共享LoD信息。

参数:
        - **x** (Tensor)- 第一个输入张量(Tensor)。
        - **y** (Tensor)- 第二个输入张量(Tensor)。
        - **axis** (INT)- (int,默认-1)。将Y传到X上的起始维度索引。
        - **act** (basestring | None)- 激活函数名称,应用于输出。
        - **name** (basestring | None)- 输出的名称。

返回:        元素运算的输出。

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    # 例1: shape(x) = (2, 3, 4, 5), shape(y) = (2, 3, 4, 5)
    x0 = fluid.layers.data(name="x0", shape=[2, 3, 4, 5], dtype='float32')
    y0 = fluid.layers.data(name="y0", shape=[2, 3, 4, 5], dtype='float32')
    z0 = fluid.layers.elementwise_min(x0, y0)
     
    # 例2: shape(X) = (2, 3, 4, 5), shape(Y) = (5)
    x1 = fluid.layers.data(name="x1", shape=[2, 3, 4, 5], dtype='float32')
    y1 = fluid.layers.data(name="y1", shape=[5], dtype='float32')
    z1 = fluid.layers.elementwise_min(x1, y1)
     
    # 例3: shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
    x2 = fluid.layers.data(name="x2", shape=[2, 3, 4, 5], dtype='float32')
    y2 = fluid.layers.data(name="y2", shape=[4, 5], dtype='float32')
    z2 = fluid.layers.elementwise_min(x2, y2, axis=2)
     
    # 例4: shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
    x3 = fluid.layers.data(name="x3", shape=[2, 3, 4, 5], dtype='float32')
    y3 = fluid.layers.data(name="y3", shape=[3, 4], dtype='float32')
    z3 = fluid.layers.elementwise_min(x3, y3, axis=1)
     
    # 例5: shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
    x4 = fluid.layers.data(name="x4", shape=[2, 3, 4, 5], dtype='float32')
    y4 = fluid.layers.data(name="y4", shape=[2], dtype='float32')
    z4 = fluid.layers.elementwise_min(x4, y4, axis=0)
     
    # 例6: shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
    x5 = fluid.layers.data(name="x5", shape=[2, 3, 4, 5], dtype='float32')
    y5 = fluid.layers.data(name="y5", shape=[2], dtype='float32')
    z5 = fluid.layers.elementwise_min(x5, y5, axis=0)
     





.. _cn_api_fluid_layers_elementwise_mod:
    
elementwise_mod
-------------------------------

.. py:function:: paddle.fluid.layers.elementwise_mod(x, y, axis=-1, act=None, name=None)

按元素的取模运算。

等式是:

.. math::
        Out = X\%Y

- :math:`X` :任何维度的张量(Tensor)。
- :math:`Y` :维度必须小于或等于X维度的张量(Tensor)。

此运算算子有两种情况:
        1. :math:`Y` 的形状(shape)与 :math:`X` 相同。
        2. :math:`Y` 的形状(shape)是 :math:`X` 的连续子序列。

对于情况2:
        1. 用 :math:`Y` 匹配 :math:`X` 的形状(shape),其中 ``axis`` 将是 :math:`Y` 传到 :math:`X` 上的起始维度索引。
        2. 如果 ``axis`` 为-1(默认值),则 :math:`axis = rank(X)-rank(Y)` 。
        3. 考虑到子序列, :math:`Y` 的大小为1的尾随维度将被忽略,例如shape(Y)=(2,1)=>(2)。

例如:

..  code-block:: text

        shape(X) = (2, 3, 4, 5), shape(Y) = (,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
        shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
        shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
        shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0

输入X和Y可以携带不同的LoD信息。但输出仅与输入X共享LoD信息。

参数:
        - **x** (Tensor)- 第一个输入张量(Tensor)。
        - **y** (Tensor)- 第二个输入张量(Tensor)。
        - **axis** (INT)- (int,默认-1)。将Y传到X上的起始维度索引。
        - **act** (basestring | None)- 激活函数名称,应用于输出。
        - **name** (basestring | None)- 输出的名称。

返回:        元素运算的输出。
    
    
**代码示例**

..  code-block:: python
  
    import paddle.fluid as fluid
    # example 1: shape(x) = (2, 3, 4, 5), shape(y) = (2, 3, 4, 5)
    x0 = fluid.layers.data(name="x0", shape=[2, 3, 4, 5], dtype='float32')
    y0 = fluid.layers.data(name="y0", shape=[2, 3, 4, 5], dtype='float32')
    z0 = fluid.layers.elementwise_mod(x0, y0)

    # example 2: shape(X) = (2, 3, 4, 5), shape(Y) = (5)
    x1 = fluid.layers.data(name="x1", shape=[2, 3, 4, 5], dtype='float32')
    y1 = fluid.layers.data(name="y1", shape=[5], dtype='float32')
    z1 = fluid.layers.elementwise_mod(x1, y1)

    # example 3: shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
    x2 = fluid.layers.data(name="x2", shape=[2, 3, 4, 5], dtype='float32')
    y2 = fluid.layers.data(name="y2", shape=[4, 5], dtype='float32')
    z2 = fluid.layers.elementwise_mod(x2, y2, axis=2)

    # example 4: shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
    x3 = fluid.layers.data(name="x3", shape=[2, 3, 4, 5], dtype='float32')
    y3 = fluid.layers.data(name="y3", shape=[3, 4], dtype='float32')
    z3 = fluid.layers.elementwise_mod(x3, y3, axis=1)

    # example 5: shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
    x4 = fluid.layers.data(name="x4", shape=[2, 3, 4, 5], dtype='float32')
    y4 = fluid.layers.data(name="y4", shape=[2], dtype='float32')
    z4 = fluid.layers.elementwise_mod(x4, y4, axis=0)

    # example 6: shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
    x5 = fluid.layers.data(name="x5", shape=[2, 3, 4, 5], dtype='float32')
    y5 = fluid.layers.data(name="y5", shape=[2], dtype='float32')
    z5 = fluid.layers.elementwise_mod(x5, y5, axis=0)



.. _cn_api_fluid_layers_elementwise_mul:

elementwise_mul
-------------------------------

.. py:function:: paddle.fluid.layers.elementwise_mul(x, y, axis=-1, act=None, name=None)

逐元素相乘算子

等式是:

.. math::
        Out = X \odot Y

- :math:`X` :任何维度的张量(Tensor)。
- :math:`Y` :维度必须小于或等于X维度的张量(Tensor)。

此运算算子有两种情况:
        1. :math:`Y` 的形状(shape)与 :math:`X` 相同。
        2. :math:`Y` 的形状(shape)是 :math:`X` 的连续子序列。

对于情况2:
        1. 用 :math:`Y` 匹配 :math:`X` 的形状(shape),其中 ``axis`` 将是 :math:`Y` 传到 :math:`X` 上的起始维度索引。
        2. 如果 ``axis`` 为-1(默认值),则 :math:`axis = rank(X)-rank(Y)` 。
        3. 考虑到子序列, :math:`Y` 的大小为1的尾随维度将被忽略,例如shape(Y)=(2,1)=>(2)。

例如:

..  code-block:: text

        shape(X) = (2, 3, 4, 5), shape(Y) = (,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
        shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
        shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
        shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0

输入X和Y可以携带不同的LoD信息。但输出仅与输入X共享LoD信息。

参数:
        - **x** (Tensor)- 第一个输入张量(Tensor)。
        - **y** (Tensor)- 第二个输入张量(Tensor)。
        - **axis** (INT)- (int,默认-1)。将Y传到X上的起始维度索引。
        - **act** (basestring | None)- 激活函数名称,应用于输出。
        - **name** (basestring | None)- 输出的名称。

返回:        元素运算的输出。

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    # 例1: shape(x) = (2, 3, 4, 5), shape(y) = (2, 3, 4, 5)
    x0 = fluid.layers.data(name="x0", shape=[2, 3, 4, 5], dtype='float32')
    y0 = fluid.layers.data(name="y0", shape=[2, 3, 4, 5], dtype='float32')
    z0 = fluid.layers.elementwise_mul(x0, y0)
     
    # 例2: shape(X) = (2, 3, 4, 5), shape(Y) = (5)
    x1 = fluid.layers.data(name="x1", shape=[2, 3, 4, 5], dtype='float32')
    y1 = fluid.layers.data(name="y1", shape=[5], dtype='float32')
    z1 = fluid.layers.elementwise_mul(x1, y1)
     
    # 例3: shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
    x2 = fluid.layers.data(name="x2", shape=[2, 3, 4, 5], dtype='float32')
    y2 = fluid.layers.data(name="y2", shape=[4, 5], dtype='float32')
    z2 = fluid.layers.elementwise_mul(x2, y2, axis=2)
     
    # 例4: shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
    x3 = fluid.layers.data(name="x3", shape=[2, 3, 4, 5], dtype='float32')
    y3 = fluid.layers.data(name="y3", shape=[3, 4], dtype='float32')
    z3 = fluid.layers.elementwise_mul(x3, y3, axis=1)
     
    # 例5: shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
    x4 = fluid.layers.data(name="x4", shape=[2, 3, 4, 5], dtype='float32')
    y4 = fluid.layers.data(name="y4", shape=[2], dtype='float32')
    z4 = fluid.layers.elementwise_mul(x4, y4, axis=0)
     
    # 例6: shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
    x5 = fluid.layers.data(name="x5", shape=[2, 3, 4, 5], dtype='float32')
    y5 = fluid.layers.data(name="y5", shape=[2], dtype='float32')
    z5 = fluid.layers.elementwise_mul(x5, y5, axis=0)







.. _cn_api_fluid_layers_elementwise_pow:

elementwise_pow
-------------------------------

.. py:function:: paddle.fluid.layers.elementwise_pow(x, y, axis=-1, act=None, name=None)

逐元素幂运算算子

等式是:

.. math::
        Out = X ^ Y

- :math:`X` :任何维度的张量(Tensor)。
- :math:`Y` :维度必须小于或等于X维度的张量(Tensor)。

此运算算子有两种情况:
        1. :math:`Y` 的形状(shape)与 :math:`X` 相同。
        2. :math:`Y` 的形状(shape)是 :math:`X` 的连续子序列。

对于情况2:
        1. 用 :math:`Y` 匹配 :math:`X` 的形状(shape),其中 ``axis`` 将是 :math:`Y` 传到 :math:`X` 上的起始维度索引。
        2. 如果 ``axis`` 为-1(默认值),则 :math:`axis = rank(X)-rank(Y)` 。
        3. 考虑到子序列, :math:`Y` 的大小为1的尾随维度将被忽略,例如shape(Y)=(2,1)=>(2)。

**代码示例**

..  code-block:: text

        shape(X) = (2, 3, 4, 5), shape(Y) = (,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
        shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
        shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
        shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0

输入X和Y可以携带不同的LoD信息。但输出仅与输入X共享LoD信息。

参数:
        - **x** (Tensor)- 第一个输入张量(Tensor)。
        - **y** (Tensor)- 第二个输入张量(Tensor)。
        - **axis** (INT)- (int,默认-1)。将Y传到X上的起始维度索引。
        - **act** (basestring | None)- 激活函数名称,应用于输出。
        - **name** (basestring | None)- 输出的名称。

返回:        元素运算的输出。

**代码示例**

..  code-block:: python

    # 例1: shape(x) = (2, 3, 4, 5), shape(y) = (2, 3, 4, 5)
X
xsrobin 已提交
2951
    import paddle.fluid as fluid
C
Cheerego 已提交
2952 2953 2954 2955 2956
    x0 = fluid.layers.data(name="x0", shape=[2, 3, 4, 5], dtype='float32')
    y0 = fluid.layers.data(name="y0", shape=[2, 3, 4, 5], dtype='float32')
    z0 = fluid.layers.elementwise_pow(x0, y0)
     
    # 例2: shape(X) = (2, 3, 4, 5), shape(Y) = (5)
X
xsrobin 已提交
2957
    import paddle.fluid as fluid
C
Cheerego 已提交
2958 2959 2960 2961 2962
    x1 = fluid.layers.data(name="x1", shape=[2, 3, 4, 5], dtype='float32')
    y1 = fluid.layers.data(name="y1", shape=[5], dtype='float32')
    z1 = fluid.layers.elementwise_pow(x1, y1)
     
    # 例3: shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
X
xsrobin 已提交
2963
    import paddle.fluid as fluid
C
Cheerego 已提交
2964 2965 2966 2967 2968
    x2 = fluid.layers.data(name="x2", shape=[2, 3, 4, 5], dtype='float32')
    y2 = fluid.layers.data(name="y2", shape=[4, 5], dtype='float32')
    z2 = fluid.layers.elementwise_pow(x2, y2, axis=2)
     
    # 例4: shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
X
xsrobin 已提交
2969
    import paddle.fluid as fluid
C
Cheerego 已提交
2970 2971 2972 2973 2974
    x3 = fluid.layers.data(name="x3", shape=[2, 3, 4, 5], dtype='float32')
    y3 = fluid.layers.data(name="y3", shape=[3, 4], dtype='float32')
    z3 = fluid.layers.elementwise_pow(x3, y3, axis=1)
     
    # 例5: shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
X
xsrobin 已提交
2975
    import paddle.fluid as fluid
C
Cheerego 已提交
2976 2977 2978 2979 2980
    x4 = fluid.layers.data(name="x4", shape=[2, 3, 4, 5], dtype='float32')
    y4 = fluid.layers.data(name="y4", shape=[2], dtype='float32')
    z4 = fluid.layers.elementwise_pow(x4, y4, axis=0)
     
    # 例6: shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
X
xsrobin 已提交
2981
    import paddle.fluid as fluid
C
Cheerego 已提交
2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106
    x5 = fluid.layers.data(name="x5", shape=[2, 3, 4, 5], dtype='float32')
    y5 = fluid.layers.data(name="y5", shape=[2], dtype='float32')
    z5 = fluid.layers.elementwise_pow(x5, y5, axis=0)








.. _cn_api_fluid_layers_elementwise_sub:

elementwise_sub
-------------------------------

.. py:function:: paddle.fluid.layers.elementwise_sub(x, y, axis=-1, act=None, name=None)

逐元素相减算子

等式是:

.. math::
       Out = X - Y

- **X** :任何维度的张量(Tensor)。
- **Y** :维度必须小于或等于**X**维度的张量(Tensor)。

此运算算子有两种情况:
        1. :math:`Y` 的形状(shape)与 :math:`X` 相同。
        2. :math:`Y` 的形状(shape)是 :math:`X` 的连续子序列。

对于情况2:
        1. 用 :math:`Y` 匹配 :math:`X` 的形状(shape),其中 ``axis`` 将是 :math:`Y` 传到 :math:`X` 上的起始维度索引。
        2. 如果 ``axis`` 为-1(默认值),则 :math:`axis = rank(X)-rank(Y)` 。
        3. 考虑到子序列, :math:`Y` 的大小为1的尾随维度将被忽略,例如shape(Y)=(2,1)=>(2)。

例如:

..  code-block:: text

        shape(X) = (2, 3, 4, 5), shape(Y) = (,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
        shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
        shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
        shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
        shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0

输入X和Y可以携带不同的LoD信息。但输出仅与输入X共享LoD信息。

参数:
        - **x** (Tensor)- 第一个输入张量(Tensor)。
        - **y** (Tensor)- 第二个输入张量(Tensor)。
        - **axis** (INT)- (int,默认-1)。将Y传到X上的起始维度索引。
        - **act** (basestring | None)- 激活函数名称,应用于输出。
        - **name** (basestring | None)- 输出的名称。

返回:        元素运算的输出。

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    # 例1: shape(x) = (2, 3, 4, 5), shape(y) = (2, 3, 4, 5)
    x0 = fluid.layers.data(name="x0", shape=[2, 3, 4, 5], dtype='float32')
    y0 = fluid.layers.data(name="y0", shape=[2, 3, 4, 5], dtype='float32')
    z0 = fluid.layers.elementwise_sub(x0, y0)
     
    # 例2: shape(X) = (2, 3, 4, 5), shape(Y) = (5)
    x1 = fluid.layers.data(name="x1", shape=[2, 3, 4, 5], dtype='float32')
    y1 = fluid.layers.data(name="y1", shape=[5], dtype='float32')
    z1 = fluid.layers.elementwise_sub(x1, y1)
     
    # 例3: shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
    x2 = fluid.layers.data(name="x2", shape=[2, 3, 4, 5], dtype='float32')
    y2 = fluid.layers.data(name="y2", shape=[4, 5], dtype='float32')
    z2 = fluid.layers.elementwise_sub(x2, y2, axis=2)
     
    # 例4: shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
    x3 = fluid.layers.data(name="x3", shape=[2, 3, 4, 5], dtype='float32')
    y3 = fluid.layers.data(name="y3", shape=[3, 4], dtype='float32')
    z3 = fluid.layers.elementwise_sub(x3, y3, axis=1)
     
    # 例5: shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
    x4 = fluid.layers.data(name="x4", shape=[2, 3, 4, 5], dtype='float32')
    y4 = fluid.layers.data(name="y4", shape=[2], dtype='float32')
    z4 = fluid.layers.elementwise_sub(x4, y4, axis=0)
     
    # 例6: shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
    x5 = fluid.layers.data(name="x5", shape=[2, 3, 4, 5], dtype='float32')
    y5 = fluid.layers.data(name="y5", shape=[2], dtype='float32')
    z5 = fluid.layers.elementwise_sub(x5, y5, axis=0)





.. _cn_api_fluid_layers_elu:

elu
-------------------------------

.. py:function:: paddle.fluid.layers.elu(x, alpha=1.0, name=None)

ELU激活层(ELU Activation Operator)

根据 https://arxiv.org/abs/1511.07289 对输入张量中每个元素应用以下计算。

.. math::
        \\out=max(0,x)+min(0,α∗(ex−1))\\

参数:
    - x(Variable)- ELU operator的输入
    - alpha(FAOAT|1.0)- ELU的alpha值
    - name (str|None) -这个层的名称(可选)。如果设置为None,该层将被自动命名。

返回: ELU操作符的输出

返回类型: 输出(Variable)

**代码示例**

.. code-block:: python

X
xsrobin 已提交
3107
    import paddle.fluid as fluid
C
Cheerego 已提交
3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196
    x = fluid.layers.data(name="x", shape=[3,10,32,32], dtype="float32")
    y = fluid.layers.elu(x, alpha=0.2)







.. _cn_api_fluid_layers_embedding:

embedding
-------------------------------

.. py:function:: paddle.fluid.layers.embedding(input, size, is_sparse=False, is_distributed=False, padding_idx=None, param_attr=None, dtype='float32')

嵌入层(Embedding Layer)

该层用于查找由输入提供的id在查找表中的嵌入矩阵。查找的结果是input里每个ID对应的嵌入矩阵。
所有的输入变量都作为局部变量传入LayerHelper构造器

参数:
    - **input** (Variable)-包含IDs的张量
    - **size** (tuple|list)-查找表参数的维度。应当有两个参数,一个代表嵌入矩阵字典的大小,一个代表每个嵌入向量的大小。
    - **is_sparse** (bool)-代表是否用稀疏更新的标志
    - **is_distributed** (bool)-是否从远程参数服务端运行查找表
    - **padding_idx** (int|long|None)-如果为 ``None`` ,对查找结果无影响。如果padding_idx不为空,表示一旦查找表中找到input中对应的 ``padding_idz``,则用0填充输出结果。如果 :math:`padding_{i}dx<0` ,在查找表中使用的 ``padding_idx`` 值为 :math:`size[0]+dim` 。
    - **param_attr** (ParamAttr)-该层参数
    - **dtype** (np.dtype|core.VarDesc.VarType|str)-数据类型:float32,float_16,int等。

返回:张量,存储已有输入的嵌入矩阵。

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid
    data = fluid.layers.data(name='sequence', shape=[1], dtype='int64', lod_level=1)
    emb = fluid.layers.embedding(input=data, size=[128, 64])









.. _cn_api_fluid_layers_expand:

expand
-------------------------------

.. py:function:: paddle.fluid.layers.expand(x, expand_times, name=None)

expand运算会按给定的次数对输入各维度进行复制(tile)运算。 您应该通过提供属性 ``expand_times`` 来为每个维度设置次数。 X的秩应该在[1,6]中。请注意, ``expand_times`` 的大小必须与X的秩相同。以下是一个用例:

::

        输入(X) 是一个形状为[2, 3, 1]的三维张量(Tensor):

                [
                   [[1], [2], [3]],
                   [[4], [5], [6]]
                ]

        属性(expand_times):  [1, 2, 2]

        输出(Out) 是一个形状为[2, 6, 2]的三维张量(Tensor):

                [
                    [[1, 1], [2, 2], [3, 3], [1, 1], [2, 2], [3, 3]],
                    [[4, 4], [5, 5], [6, 6], [4, 4], [5, 5], [6, 6]]
                ]

参数:
        - **x** (Variable)- 一个秩在[1, 6]范围中的张量(Tensor).
        - **expand_times** (list|tuple) - 每一个维度要扩展的次数.

返回:     expand变量是LoDTensor。expand运算后,输出(Out)的每个维度的大小等于输入(X)的相应维度的大小乘以 ``expand_times`` 给出的相应值。

返回类型:   变量(Variable)

**代码示例**

..  code-block:: python

X
xsrobin 已提交
3197
        import paddle.fluid as fluid
C
Cheerego 已提交
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282
        x = fluid.layers.data(name='x', shape=[10], dtype='float32')
        out = fluid.layers.expand(x=x, expand_times=[1, 2, 2])










.. _cn_api_fluid_layers_fc:

fc
-------------------------------

.. py:function::  paddle.fluid.layers.fc(input, size, num_flatten_dims=1, param_attr=None, bias_attr=None, act=None, is_test=False, name=None)


**全连接层**

该函数在神经网络中建立一个全连接层。 它可以将一个或多个tensor( ``input`` 可以是一个list或者Variable,详见参数说明)作为自己的输入,并为每个输入的tensor创立一个变量,称为“权”(weights),等价于一个从每个输入单元到每个输出单元的全连接权矩阵。FC层用每个tensor和它对应的权相乘得到形状为[M, size]输出tensor,M是批大小。如果有多个输入tensor,那么形状为[M, size]的多个输出张量的结果将会被加起来。如果 ``bias_attr`` 非空,则会新创建一个偏向变量(bias variable),并把它加入到输出结果的运算中。最后,如果 ``act`` 非空,它也会加入最终输出的计算中。

当输入为单个张量:

.. math::

        \\Out = Act({XW + b})\\



当输入为多个张量:

.. math::

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


上述等式中:
  - :math:`N` :输入的数目,如果输入是变量列表,N等于len(input)
  - :math:`X_i` :第i个输入的tensor
  - :math:`W_i` :对应第i个输入张量的第i个权重矩阵
  - :math:`b` :该层创立的bias参数
  - :math:`Act` :activation function(激励函数)
  - :math:`Out` :输出tensor

::

            Given:
                data_1.data = [[[0.1, 0.2],
                               [0.3, 0.4]]]
                data_1.shape = (1, 2, 2) # 1 is batch_size

                data_2 = [[[0.1, 0.2, 0.3]]]
                data_2.shape = (1, 1, 3)

                out = fluid.layers.fc(input=[data_1, data_2], size=2)

            Then:
                out.data = [[0.18669507, 0.1893476]]
                out.shape = (1, 2)


参数:
  - **input** (Variable|list of Variable) – 该层的输入tensor(s)(张量),其维度至少是2
  - **size** (int) – 该层输出单元的数目
  - **num_flatten_dims** (int, default 1) – fc层可以接受一个维度大于2的tensor。此时, 它首先会被扁平化(flattened)为一个二维矩阵。 参数``num_flatten_dims`` 决定了输入tensor的flattened方式: 前 ``num_flatten_dims`` (包含边界,从1开始数) 个维度会被扁平化为最终矩阵的第一维 (维度即为矩阵的高), 剩下的 rank(X) - num_flatten_dims 维被扁平化为最终矩阵的第二维 (即矩阵的宽)。 例如, 假设X是一个五维tensor,其形可描述为(2, 3, 4, 5, 6), 且num_flatten_dims = 3。那么扁平化的矩阵形状将会如此: (2 x 3 x 4, 5 x 6) = (24, 30)
  - **param_attr** (ParamAttr|list of ParamAttr, default None) – 该层可学习的参数/权的参数属性
  - **bias_attr** (ParamAttr|list of ParamAttr, default None) – 该层bias变量的参数属性。如果值为False, 则bias变量不参与输出单元运算。 如果值为None,bias变量被初始化为0。默认为 None。
  - **act** (str, default None) – 应用于输出的Activation(激励函数)
  - **is_test** (bool) – 表明当前执行是否处于测试阶段的标志
  - **name** (str, default None) – 该层的命名


返回:转换结果

返回类型: Variable

弹出异常:``ValueError`` - 如果输入tensor的维度小于2

**代码示例**

..  code-block:: python

X
xsrobin 已提交
3283
         import paddle.fluid as fluid
C
Cheerego 已提交
3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353
         # 当输入为单个张量时

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

        # 当输入为多个张量时
        data_1 = fluid.layers.data(name="data_1", shape=[32, 32], dtype="float32")
        data_2 = fluid.layers.data(name="data_2", shape=[24, 36], dtype="float32")
        fc = fluid.layers.fc(input=[data_1, data_2], size=1000, act="tanh")













.. _cn_api_fluid_layers_flatten:

flatten
-------------------------------

.. py:function::  paddle.fluid.layers.flatten(x, axis=1, name=None)

将输入张量压扁成二维矩阵

例如:

.. code-block:: text

    Case 1:

      给定
        X.shape = (3, 100, 100, 4)

        axis = 2
      得到:
        Out.shape = (3 * 100, 4 * 100)

    Case 2:

      给定
        X.shape = (3, 100, 100, 4)

        axis = 0
      得到:
        Out.shape = (1, 3 * 100 * 100 * 4)

参数:
  - **x** (Variable) - 一个秩>=axis 的张量
  - **axis** (int) - flatten的划分轴,[0, axis) 轴数据被flatten到输出矩阵的0轴,[axis, R)被flatten到输出矩阵的1轴,其中R是输入张量的秩。axis的值必须在[0,R]范围内。当 axis= 0 时,输出张量的形状为 (1,d_0 \* d_1 \*… d_n) ,其输入张量的形状为(d_0, d_1,… d_n)。
  - **name** (str|None) - 此层的名称(可选)。如果没有设置,层将自动命名。

返回: 一个二维张量,它包含输入张量的内容,但维数发生变化。输入的[0, axis)维将沿给定轴flatten到输出的前一个维度,剩余的输入维数flatten到输出的后一个维度。

返回类型: Variable

抛出异常:
  - ValueError: 如果 x 不是一个变量
  - ValueError: 如果axis的范围不在 [0, rank(x)]

**代码示例**

.. code-block:: python

X
xsrobin 已提交
3354
    import paddle.fluid as fluid
C
Cheerego 已提交
3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442
    x = fluid.layers.data(name="x", shape=[4, 4, 3], dtype="float32")
    out = fluid.layers.flatten(x=x, axis=2)



.. _cn_api_fluid_layers_fsp_matrix:

fsp_matrix
-------------------------------

.. py:function:: paddle.fluid.layers.fsp_matrix(x, y)

**FSP matrix op**

此运算用于计算两个特征映射的求解过程(FSP)矩阵。给定形状为[x_channel,h,w]的特征映射x和形状为[y_channel,h,w]的特征映射y,我们可以分两步得到x和y的fsp矩阵:

1.用形状[X_channel,H*W]将X重塑为矩阵,并用形状[H*W,y_channel]将Y重塑和转置为矩阵。

2.乘以x和y得到形状为[x_channel,y_channel]的fsp矩阵。

输出是一批fsp矩阵。

参数:
    - **x** (Variable): 一个形状为[batch_size, x_channel, height, width]的特征映射
    - **y** (Variable):具有形状[batch_size, y_channel, height, width]的特征映射。Y轴通道可以与输入(X)的X轴通道不同,而其他尺寸必须与输入(X)相同。

返回:形状为[batch_size, x_channel, y_channel]的fsp op的输出。x_channel 是x的通道,y_channel是y的通道。

返回类型:fsp matrix (Variable)

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    data = fluid.layers.data(name='data', shape=[3, 32, 32])
    feature_map_0 = fluid.layers.conv2d(data, num_filters=2,
                                        filter_size=3)
    feature_map_1 = fluid.layers.conv2d(feature_map_0, num_filters=2,
                                        filter_size=1)
    loss = fluid.layers.fsp_matrix(feature_map_0, feature_map_1)






.. _cn_api_fluid_layers_gather:

gather
-------------------------------

.. py:function:: paddle.fluid.layers.gather(input, index, overwrite=True)

收集层(gather layer)

根据索引index获取X的最外层维度的条目,并将它们串连在一起。

.. math::
                        Out=X[Index]

::

        X = [[1, 2],
             [3, 4],
             [5, 6]]

        Index = [1, 2]

        Then:

        Out = [[3, 4],
               [5, 6]]


参数:
         - **input** (Variable) - input的秩rank >= 1。
        - **index** (Variable) - index的秩rank = 1。
        - **overwrite** (bool) - 具有相同索引时更新grad的模式。如果为True,则使用覆盖模式更新相同索引的grad,如果为False,则使用accumulate模式更新相同索引的grad。Default值为True。

返回:和输入的秩相同的输出张量。

返回类型:output (Variable)

**代码示例**

..  code-block:: python
  
X
xsrobin 已提交
3443
  import paddle.fluid as fluid
C
Cheerego 已提交
3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481
  x = fluid.layers.data(name='x', shape=[-1, 5], dtype='float32')
  index = fluid.layers.data(name='index', shape=[-1, 1], dtype='int32')
  output = fluid.layers.gather(x, index)









.. _cn_api_fluid_layers_gaussian_random:

gaussian_random
-------------------------------

.. py:function:: paddle.fluid.layers.gaussian_random(shape, mean=0.0, std=1.0, seed=0, dtype='float32')

gaussian_random算子。

用于使用高斯随机生成器初始化张量(Tensor)。

参数:
        - **shape** (tuple | list)- (vector <int>)随机张量的维数
        - **mean** (Float)- (默认值0.0)随机张量的均值
        - **std** (Float)- (默认值为1.0)随机张量的std
        - **seed** (Int)- (默认值为 0)生成器随机生成种子。0表示使用系统范围的种子。注意如果seed不为0,则此算子每次将始终生成相同的随机数
        - **dtype** (np.dtype | core.VarDesc.VarType | str)- 输出的数据类型。

返回:        输出高斯随机运算矩阵

返回类型:        输出(Variable)

**代码示例:**

.. code-block:: python

X
xsrobin 已提交
3482
    import paddle.fluid as fluid
C
Cheerego 已提交
3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521
    import paddle.fluid.layers as layers
    out = fluid.layers.gaussian_random(shape=[20, 30])








.. _cn_api_fluid_layers_gaussian_random_batch_size_like:

gaussian_random_batch_size_like
-------------------------------

.. py:function:: paddle.fluid.layers.gaussian_random_batch_size_like(input, shape, input_dim_idx=0, output_dim_idx=0, mean=0.0, std=1.0, seed=0, dtype='float32')

用于使用高斯随机发生器初始化张量。分布的defalut均值为0.并且分布的defalut标准差(std)为1.用户可以通过输入参数设置mean和std。

参数:
        - **input** (Variable)- 其input_dim_idx'th维度指定batch_size的张量(Tensor)。
        - **shape** (元组|列表)- 输出的形状。
        - **input_dim_idx** (Int)- 默认值0.输入批量大小维度的索引。
        - **output_dim_idx** (Int)- 默认值0.输出批量大小维度的索引。
        - **mean** (Float)- (默认值0.0)高斯分布的平均值(或中心值)。
        - **std** (Float)- (默认值 1.0)高斯分布的标准差(std或spread)。
        - **seed** (Int)- (默认为0)用于随机数引擎的随机种子。0表示使用系统生成的种子。请注意,如果seed不为0,则此算子将始终每次生成相同的随机数。
        - **dtype** (np.dtype | core.VarDesc.VarType | str)- 输出数据的类型为float32,float_16,int等。

返回:        指定形状的张量将使用指定值填充。

返回类型:        输出(Variable)。



**代码示例:**

.. code-block:: python

X
xsrobin 已提交
3522
    import paddle.fluid as fluid
C
Cheerego 已提交
3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670
    input = fluid.layers.data(name="input", shape=[13, 11], dtype='float32')

    out = fluid.layers.gaussian_random_batch_size_like(
        input, shape=[-1, 11], mean=1.0, std=2.0)






.. _cn_api_fluid_layers_get_tensor_from_selected_rows:

get_tensor_from_selected_rows
-------------------------------

.. py:function::  paddle.fluid.layers.get_tensor_from_selected_rows(x, name=None)

:code:`Get Tensor From Selected Rows` 用于从选中行(Selected Rows)中获取张量

参数:
  - **x** (Variable) - 输入,类型是SelectedRows
  - **name** (basestring|None) - 输出的名称

返回: 输出类型为LoDTensor

返回类型: out(Variable)

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
    b = fluid.default_main_program().global_block()
    input = b.create_var(name="X", dtype="float32", persistable=True, type=fluid.core.VarDesc.VarType.SELECTED_ROWS)
    out = fluid.layers.get_tensor_from_selected_rows(input)









.. _cn_api_fluid_layers_grid_sampler:

grid_sampler
-------------------------------

.. py:function::  paddle.fluid.layers.grid_sampler(x, grid, name=None)

该操作使用基于flow field网格的双线性插值对输入X进行采样,通常由affine_grid生成。

形状为(N、H、W、2)的网格是由两个形状均为(N、H、W)的坐标(grid_x grid_y)连接而成的。

其中,grid_x是输入数据x的第四个维度(宽度维度)的索引,grid_y是第三维度(高维)的索引,最终得到4个最接近的角点的双线性插值值。

step 1:

  得到(x, y)网格坐标,缩放到[0,h -1/W-1]

  grid_x = 0.5 * (grid[:, :, :, 0] + 1) * (W - 1) grid_y = 0.5 * (grid[:, :, :, 1] + 1) * (H - 1)

step 2:

  在每个[H, W]区域用网格(X, y)作为输入数据X的索引,并将双线性插值点值由4个最近的点表示。

.. code-block:: text

      wn ------- y_n ------- en
      |           |           |
      |          d_n          |
      |           |           |
     x_w --d_w-- grid--d_e-- x_e
      |           |           |
      |          d_s          |
      |           |           |
      ws ------- y_s ------- wn

    x_w = floor(x)              // west side x coord
    x_e = x_w + 1               // east side x coord
    y_n = floor(y)              // north side y coord
    y_s = y_s + 1               // south side y coord
    d_w = grid_x - x_w          // distance to west side
    d_e = x_e - grid_x          // distance to east side
    d_n = grid_y - y_n          // distance to north side
    d_s = y_s - grid_y          // distance to south side
    wn = X[:, :, y_n, x_w]      // north-west point value
    en = X[:, :, y_n, x_e]      // north-east point value
    ws = X[:, :, y_s, x_w]      // south-east point value
    es = X[:, :, y_s, x_w]      // north-east point value


    output = wn * d_e * d_s + en * d_w * d_s
           + ws * d_e * d_n + es * d_w * d_n

参数:
  - **x** (Variable): 输入数据,形状为[N, C, H, W]
  - **grid** (Variable): 输入网格张量,形状为[N, H, W, 2]
  - **name** (str, default None): 该层的名称

返回: **out** (Variable): 输入X基于输入网格的bilnear插值计算结果,形状为[N, C, H, W]

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid

    x = fluid.layers.data(name='x', shape=[3, 10, 32, 32], dtype='float32')
    theta = fluid.layers.data(name='theta', shape=[3, 2, 3], dtype='float32')
    grid = fluid.layers.affine_grid(theta=theta, out_shape=[3, 10, 32, 32]})
    out = fluid.layers.grid_sampler(x=x, grid=grid)










.. _cn_api_fluid_layers_group_norm:

group_norm
-------------------------------

.. py:function::  paddle.fluid.layers.group_norm(input, groups, epsilon=1e-05, param_attr=None, bias_attr=None, act=None, data_layout='NCHW', name=None)

参考论文: `Group Normalization <https://arxiv.org/abs/1803.08494>`_

参数:
  - **input** (Variable):输入张量变量
  - **groups** (int):从 channel 中分离出来的 group 的数目
  - **epsilon** (float):为防止方差除零,增加一个很小的值
  - **param_attr** (ParamAttr|None):可学习标度的参数属性 :math:`g`,如果设置为False,则不会向输出单元添加标度。如果设置为0,偏差初始化为1。默认值:None
  - **bias_attr** (ParamAttr|None):可学习偏置的参数属性 :math:`b ` , 如果设置为False,则不会向输出单元添加偏置量。如果设置为零,偏置初始化为零。默认值:None。
  - **act** (str):将激活应用于输出的 group normalizaiton
  - **data_layout** (string|NCHW): 只支持NCHW。
  - **name** (str):这一层的名称(可选)

返回: Variable: 一个张量变量,它是对输入进行 group normalization 后的结果。

**代码示例:**

.. code-block:: python

X
xsrobin 已提交
3671
    import paddle.fluid as fluid
C
Cheerego 已提交
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210
    data = fluid.layers.data(name='data', shape=[8, 32, 32],
                             dtype='float32')
    x = fluid.layers.group_norm(input=data, groups=4)










.. _cn_api_fluid_layers_gru_unit:

gru_unit
-------------------------------

.. py:function:: paddle.fluid.layers.gru_unit(input, hidden, size, param_attr=None, bias_attr=None, activation='tanh', gate_activation='sigmoid', origin_mode=False)

GRU单元层。GRU执行步骤基于如下等式:


如果origin_mode为True,则该运算公式来自论文
`Empirical Evaluation of Gated Recurrent Neural Networks on Sequence Modeling  <https://arxiv.org/pdf/1412.3555.pdf>`_ 。

公式如下:

.. math::
    u_t=actGate(xu_t+W_{u}h_{t-1}+b_u)
.. math::
    r_t=actGate(xr_t+W_{r}h_{t-1}+b_r)
.. math::
    m_t=actNode(xm_t+W_{c}dot(r_t,h_{t-1})+b_m)
.. math::
    h_t=dot((1-u_t),m_t)+dot(u_t,h_{t-1})


如果origin_mode为False,则该运算公式来自论文
`Learning Phrase Representations using RNN Encoder Decoder for Statistical Machine Translation <https://arxiv.org/pdf/1406.1078.pdf>`_ 。

.. math::
    u_t & = act_g(W_{ux}x_{t} + W_{uh}h_{t-1} + b_u)\\
    r_t & = act_g(W_{rx}x_{t} + W_{rh}h_{t-1} + b_r)\\
    \tilde{h_t} & = act_c(W_{cx}x_{t} + W_{ch}(r_t \odot h_{t-1}) + b_c)\\
    h_t & = u_t \odot h_{t-1} + (1-u_t) \odot \tilde{h_t}


GRU单元的输入包括 :math:`z_t` , :math:`h_{t-1}` 。在上述等式中, :math:`z_t` 会被分割成三部分: :math:`xu_t` 、 :math:`xr_t` 和 :math:`xm_t`  。
这意味着要为一批输入实现一个全GRU层,我们需要采用一个全连接层,才能得到 :math:`z_t=W_{fc}x_t` 。
:math:`u_t` 和 :math:`r_t` 分别代表了GRU神经元的update gates(更新门)和reset gates(重置门)。
和LSTM不同,GRU少了一个门(它没有LSTM的forget gate)。但是它有一个叫做中间候选隐藏状态(intermediate candidate hidden output)的输出,
记为 :math:`m_t` 。 该层有三个输出: :math:`h_t, dot(r_t,h_{t-1})` 以及 :math:`u_t,r_t,m_t` 的连结(concatenation)。




参数:
  - **input** (Variable) – 经FC层变换后的当前步骤的输入值
  - **hidden** (Variable) –  从上一步而来的gru unit 隐藏状态值(hidden value)
  - **size** (integer) – 输入数据的维度
  - **param_attr** (ParamAttr|None) – 可学习的隐藏层权重矩阵的参数属性。
    注意:
      - 该权重矩阵形为 :math:`(T×3D)` , :math:`D` 是隐藏状态的规模(hidden size)
      - 该权重矩阵的所有元素由两部分组成, 一是update gate和reset gate的权重,形为 :math:`(D×2D)` ;二是候选隐藏状态(candidate hidden state)的权重矩阵,形为 :math:`(D×D)`
    如果该函数参数值为None或者 ``ParamAttr`` 类中的属性之一,gru_unit则会创建一个 ``ParamAttr`` 类的对象作为 param_attr。如果param_attr没有被初始化,那么会由Xavier来初始化它。默认值为None
  - **bias_attr** (ParamAttr|bool|None) - GRU的bias变量的参数属性。形为 :math:`(1x3D)` 的bias连结(concatenate)在update gates(更新门),reset gates(重置门)以及candidate calculations(候选隐藏状态计算)中的bias。如果值为False,那么上述三者将没有bias参与运算。若值为None或者 ``ParamAttr`` 类中的属性之一,gru_unit则会创建一个 ``ParamAttr`` 类的对象作为 bias_attr。如果bias_attr没有被初始化,那它会被默认初始化为0。默认值为None。
  - **activation** (string) –  神经元 “actNode” 的激励函数(activation)类型。默认类型为‘tanh’
  - **gate_activation** (string) – 门 “actGate” 的激励函数(activation)类型。 默认类型为 ‘sigmoid’


返回:  hidden value(隐藏状态的值),reset-hidden value(重置隐藏状态值),gate values(门值)

返回类型:  元组(tuple)


**代码示例**

..  code-block:: python

    import paddle.fluid as fluid

    dict_dim, emb_dim = 128, 64
    data = fluid.layers.data(name='step_data', shape=[1], dtype='int32')
    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])
    hidden_dim = 512
    x = fluid.layers.fc(input=emb, size=hidden_dim * 3)
    pre_hidden = fluid.layers.data(
        name='pre_hidden', shape=[hidden_dim], dtype='float32')
    hidden = fluid.layers.gru_unit(
        input=x, hidden=pre_hidden, size=hidden_dim * 3)














.. _cn_api_fluid_layers_hard_sigmoid:

hard_sigmoid
-------------------------------

.. py:function:: paddle.fluid.layers.hard_sigmoid(x, slope=0.2, offset=0.5, name=None)

HardSigmoid激活算子。

sigmoid的分段线性逼近(https://arxiv.org/abs/1603.00391),比sigmoid快得多。

.. math::

      \\out=\max(0,\min(1,slope∗x+shift))\\

斜率是正数。偏移量可正可负的。斜率和位移的默认值是根据上面的参考设置的。建议使用默认值。

参数:
    - **x** (Variable) - HardSigmoid operator的输入
    - **slope** (FLOAT|0.2) -斜率
    - **offset** (FLOAT|0.5)  - 偏移量
    - **name** (str|None) - 这个层的名称(可选)。如果设置为None,该层将被自动命名。


**代码示例:**


.. code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name="x", shape=[3,10,32,32], dtype="float32")
    y = fluid.layers.hard_sigmoid(x, slope=0.3, offset=0.8)






.. _cn_api_fluid_layers_hash:

hash
-------------------------------

.. py:function::  paddle.fluid.layers.hash(input, hash_size, num_hash=1, name=None)

将输入 hash 到一个整数,该数的值小于给定的 hash size

我们使用的哈希算法是 xxHash - `Extremely fast hash algorithm <https://github.com/Cyan4973/xxHash/tree/v0.6.5>`_

提供一简单的例子:

.. code-block:: text

  给出:

    # shape [2, 2]
    input.data = [
        [[1, 2],
        [3, 4]],
    ]

    input.lod = [[0, 2]]

    hash_size = 10000

    num_hash = 4

  然后:

    哈希操作将这个二维input的所有数字作为哈希算法每次的输入。

    每个输入都将被哈希4次,最终得到一个长度为4的数组。

    数组中的每个值的范围从0到9999。



    # shape [2, 4]
    output.data = [
        [[9662, 9217, 1129, 8487],
        [8310, 1327, 1654, 4567]],
    ]

    output.lod = [[0, 2]]

参数:
  - **input** (Variable) - 输入变量是一个 one-hot 词。输入变量的维数必须是2。
  - **hash_size** (int) - 哈希算法的空间大小。输出值将保持在 :math:`[0, hash\_size - 1]` 范围内。
  - **num_hash** (int) - 哈希次数,默认为1。
  - **name** (str, default None) - 该层的名称

返回:哈希的结果变量,是一个lodtensor。

返回类型: Variable

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
    import paddle.fluid.layers as layers
    import numpy as np

    titles = fluid.layers.data(name='titles', shape=[1], dtype='int32', lod_level=1)
    hash_r = fluid.layers.hash(name='hash_x', input=titles, num_hash=1, hash_size=1000)

    place = fluid.core.CPUPlace()
    exece = fluid.Executor(place)
    exece.run(fluid.default_startup_program())

    # 初始化Tensor
    tensor = fluid.core.LoDTensor()
    tensor.set(np.random.randint(0, 10, (3, 1)).astype("int32"), place)
    # 设置LoD
    tensor.set_recursive_sequence_lengths([[1, 1, 1]])

    out = exece.run(feed={'titles': tensor}, fetch_list=[hash_r], return_numpy=False)









.. _cn_api_fluid_layers_hsigmoid:

hsigmoid
-------------------------------

.. py:function:: paddle.fluid.layers.hsigmoid(input, label, num_classes, param_attr=None, bias_attr=None, name=None, path_table=None, path_code=None, is_custom=False, is_sparse=False)

层次sigmod( hierarchical sigmoid )加速语言模型的训练过程。这个operator将类别组织成一个完全二叉树,也可以使用 ``is_custom`` 参数来传入自定义的树结构来实现层次化。

树中每个叶节点表示一个类(一个单词),每个内部节点进行一个二分类。对于每个单词,都有一个从根到它的叶子节点的唯一路径,hsigmoid计算路径上每个内部节点的损失(cost),并将它们相加得到总损失(cost)。

hsigmoid可以把时间复杂度 :math:`O(N)` 优化到 :math:`O(logN)` ,其中 :math:`N` 表示单词字典的大小。

使用默认树结构,请参考 `Hierarchical Probabilistic Neural Network Language Model <http://www.iro.umontreal.ca/~lisa/pointeurs/hierarchical-nnlm-aistats05.pdf>`_ 。

若要使用自定义树结构,请设置 ``is_custom`` 值为True。但在此之前,请完成以下几步:

1.使用自定义词典来建立二叉树,每个叶结点都应该是词典中的单词

2.建立一个dict类型数据结构,来存储 **单词id -> 该单词叶结点至根结点路径** 的映射,称之为路径表 ``path_table`` 参数

3.建立一个dict类型数据结构,来存储 **单词id -> 该单词叶结点至根结点路径的编码(code)** 的映射。 编码code是指每次二分类的标签,1为真,0为假

4.现在我们的每个单词都已经有自己的路径和路径编码,当对于同一批输入进行操作时,你可以同时传入一批路径和路径编码进行运算。

参数:
    - **input** (Variable) - 输入张量,shape为 ``[N×D]`` ,其中 ``N`` 是minibatch的大小,D是特征大小。
    - **label** (Variable) - 训练数据的标签。该tensor的shape为 ``[N×1]``
    - **num_classes** (int) - 类别的数量不能少于2。若使用默认树结构,该参数必须用户设置。当 ``is_custom=False`` 时,该项绝不能为None。反之,如果 ``is_custom=True`` ,它取值应为非叶节点的个数,来指明二分类实用的类别数目。
    - **param_attr** (ParamAttr|None) - 可学习参数/ hsigmoid权重的参数属性。如果将其设置为ParamAttr的一个属性或None,则将ParamAttr设置为param_attr。如果没有设置param_attr的初始化器,那么使用用Xavier初始化。默认值:没None。
    - **bias_attr** (ParamAttr|bool|None) - hsigmoid偏置的参数属性。如果设置为False,则不会向输出添加偏置。如果将其设置ParamAttr的一个属性或None,则将ParamAttr设置为bias_attr。如果没有设置bias_attr的初始化器,偏置将初始化为零。默认值:None。
    - **name** (str|None) - 该layer的名称(可选)。如果设置为None,该层将被自动命名。默认值:None。
    - **path_table** (Variable|None) – 存储每一批样本从词到根节点的路径。路径应为从叶至根方向。 ``path_table`` 和 ``path_code`` 应具有相同的形, 对于每个样本 i ,path_table[i]为一个类似np.array的结构,该数组内的每个元素都是其双亲结点权重矩阵的索引
    - **path_code** (Variable|None) – 存储每批样本的路径编码,仍然是按从叶至根方向。各样本路径编码批都由其各祖先结点的路径编码组成
    - **is_custom** (bool|False) – 使用用户自定义二叉树取代默认二叉树结构,如果该项为真, 请务必设置 ``path_table`` , ``path_code`` , ``num_classes`` , 否则就需要设置 num_classes
    - **is_sparse** (bool|False) – 使用稀疏更新方式,而非密集更新。如果为真, W的梯度和输入梯度将会变得稀疏

返回:  (LoDTensor) 层次sigmod( hierarchical sigmoid) 。shape[N, 1]

返回类型:  Out


**代码示例**

..  code-block:: python

      import paddle.fluid as fluid
      x = fluid.layers.data(name='x', shape=[2], dtype='float32')
      y = fluid.layers.data(name='y', shape=[1], dtype='int64')
      out = fluid.layers.hsigmoid(input=x, label=y, num_classes=6)




.. _cn_api_fluid_layers_huber_loss:

huber_loss
-------------------------------

.. py:function:: paddle.fluid.layers.huber_loss(input, label, delta)

Huber损失是更具鲁棒性的损失函数。 huber损失可以评估输入对标签的合适度。 与MSE损失不同,Huber损失可更为稳健地处理异常值。

当输入和标签之间的距离大于delta时:

.. math::
        huber\_loss = delta * (label - input) - 0.5 * delta * delta

当输入和标签之间的距离小于delta时:

.. math::
        huber\_loss = 0.5 * (label - input) * (label - input)


参数:
  - **input** (Variable) - 此输入是前一个算子计算得到的概率。 第一个维度是批大小batch_size,最后一个维度是1。
  - **label** (Variable) - 第一个维度为批量大小batch_size且最后一个维度为1的真实值
  - **delta** (float) -  huber loss的参数,用于控制异常值的范围

返回: 形为[batch_size, 1]的huber loss.

返回类型:   huber_loss (Variable)



**代码示例**

..  code-block:: python

    import paddle.fluid as fluid

    x = fluid.layers.data(name='x', shape=[13], dtype='float32')
    predict = fluid.layers.fc(input=x, size=1)
    label = fluid.layers.data(
        name='label', shape=[1], dtype='float32')
    loss = fluid.layers.huber_loss(
        input=predict, label=label, delta=1.0)





.. _cn_api_fluid_layers_im2sequence:

im2sequence
-------------------------------

.. py:function:: paddle.fluid.layers.im2sequence(input, filter_size=1, stride=1, padding=0, input_image_size=None, out_stride=1, name=None)

从输入张量中提取图像张量,与im2col相似,shape={input.batch_size * output_height * output_width, filter_size_H * filter_size_W * input.通道}。这个op使用filter / kernel扫描图像并将这些图像转换成序列。一个图片展开后的timestep的个数为output_height * output_width,其中output_height和output_width由下式计算:


.. math::
                        output\_size=1+\frac{(2∗padding+img\_size−block\_size+stride-1)}{stride}

每个timestep的维度为 :math:`block\_y * block\_x * input.channels` 。

参数:
  - **input** (Variable)- 输入张量,格式为[N, C, H, W]
  - **filter_size** (int|tuple|None) - 滤波器大小。如果filter_size是一个tuple,它必须包含两个整数(filter_size_H, filter_size_W)。否则,过滤器将是一个方阵。
  - **stride** (int|tuple) - 步长大小。如果stride是一个元组,它必须包含两个整数(stride_H、stride_W)。否则,stride_H = stride_W = stride。默认:stride = 1。
  - **padding** (int|tuple) - 填充大小。如果padding是一个元组,它可以包含两个整数(padding_H, padding_W),这意味着padding_up = padding_down = padding_H和padding_left = padding_right = padding_W。或者它可以使用(padding_up, padding_left, padding_down, padding_right)来指示四个方向的填充。否则,标量填充意味着padding_up = padding_down = padding_left = padding_right = padding Default: padding = 0。
  - **input_image_size** (Variable) - 输入包含图像的实际大小。它的维度为[batchsize,2]。该参数可有可无,是用于batch上的预测。
  - **out_stride** (int|tuple) - 通过CNN缩放图像。它可有可无,只有当input_image_size不为空时才有效。如果out_stride是tuple,它必须包含(out_stride_H, out_stride_W),否则,out_stride_H = out_stride_W = out_stride。
  - **name** (int) - 该layer的名称,可以忽略。

返回: LoDTensor shape为{batch_size * output_height * output_width, filter_size_H * filter_size_W * input.channels}。如果将输出看作一个矩阵,这个矩阵的每一行都是一个序列的step。

返回类型: output

::

  Given:

    x = [[[[ 6.  2.  1.]
      [ 8.  3.  5.]
      [ 0.  2.  6.]]

        [[ 2.  4.  4.]
         [ 6.  3.  0.]
         [ 6.  4.  7.]]]

       [[[ 6.  7.  1.]
         [ 5.  7.  9.]
         [ 2.  4.  8.]]

        [[ 1.  2.  1.]
         [ 1.  3.  5.]
         [ 9.  0.  8.]]]]

    x.dims = {2, 2, 3, 3}

    And:

    filter = [2, 2]
    stride = [1, 1]
    padding = [0, 0]

    Then:

    output.data = [[ 6.  2.  8.  3.  2.  4.  6.  3.]
                   [ 2.  1.  3.  5.  4.  4.  3.  0.]
                   [ 8.  3.  0.  2.  6.  3.  6.  4.]
                   [ 3.  5.  2.  6.  3.  0.  4.  7.]
                   [ 6.  7.  5.  7.  1.  2.  1.  3.]
                   [ 7.  1.  7.  9.  2.  1.  3.  5.]
                   [ 5.  7.  2.  4.  1.  3.  9.  0.]
                   [ 7.  9.  4.  8.  3.  5.  0.  8.]]

    output.dims = {8, 8}

    output.lod = [[4, 4]]


**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    data = fluid.layers.data(name='data', shape=[3, 32, 32],
                             dtype='float32')
    output = fluid.layers.im2sequence(
        input=data, stride=[1, 1], filter_size=[2, 2])










.. _cn_api_fluid_layers_image_resize:

image_resize
-------------------------------

.. py:function:: paddle.fluid.layers.image_resize(input, out_shape=None, scale=None, name=None, resample='BILINEAR', actual_shape=None, align_corners=True, align_mode=1)

调整一个batch中图片的大小。

输入张量的shape为(num_batch, channels, in_h, in_w),并且调整大小只适用于最后两个维度(高度和宽度)。

支持重新取样方法:

    BILINEAR:双线性插值

    NEAREST:最近邻插值


最近邻插值是在输入张量的第3维(高度)和第4维(宽度)上进行最近邻插值。

双线性插值是线性插值的扩展,用于在直线2D网格上插值两个变量(例如,该操作中的H方向和W方向)的函数。 关键思想是首先在一个方向上执行线性插值,然后在另一个方向上再次执行线性插值。

Align_corners和align_mode是可选参数,插值的计算方法可以由它们选择。

示例:

::

      For scale:

        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)


      Nearest neighbor interpolation:

      if:
          align_corners = False

          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:

          H_out = \left \lfloor {H_{in} * scale_{}factor}} \right \rfloor
          W_out = \left \lfloor {W_{in} * scale_{}factor}} \right \rfloor

      else:
          align_corners = True

          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:

          H_out = round(H_{in} * scale_{factor})
          W_out = round(W_{in} * scale_{factor})

      Bilinear interpolation:

      if:
          align_corners = False , align_mode = 0

          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:

          H_out = (H_{in}+0.5) * scale_{factor} - 0.5
          W_out = (W_{in}+0.5) * scale_{factor} - 0.5


      else:

          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:

          H_out = H_{in} * scale_{factor}
          W_out = W_{in} * scale_{factor}


有关最近邻插值的详细信息,请参阅维基百科:
https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation。

有关双线性插值的详细信息,请参阅维基百科:
https://en.wikipedia.org/wiki/Bilinear_interpolation。

参数:
    - **input** (Variable) - 图片调整层的输入张量,这是一个shape=4的张量(num_batch, channels, in_h, in_w)
    - **out_shape** (list|tuple|Variable|None) - 图片调整层的输出,shape为(out_h, out_w)。默认值:None
    - **scale** (float|None)-输入的高度或宽度的乘数因子 。 out_shape和scale至少要设置一个。out_shape的优先级高于scale。默认值:None
    - **name** (str|None) - 该层的名称(可选)。如果设置为None,该层将被自动命名
    - **resample** (str) - 重采样方法。目前只支持“双线性”。默认值:双线性插值
    - **actual_shape** (Variable) - 可选输入,用于动态指定输出形状。如果指定actual_shape,图像将根据给定的形状调整大小,而不是根据指定形状的 :code:`out_shape` 和 :code:`scale` 进行调整。也就是说, :code:`actual_shape` 具有最高的优先级。如果希望动态指定输出形状,建议使用 :code:`actual_shape` 而不是 :code:`out_shape` 。在使用actual_shape指定输出形状时,还需要设置out_shape和scale之一,否则在图形构建阶段会出现错误。默认值:None
    - **align_corners** (bool)- 一个可选的bool型参数,如果为True,则将输入和输出张量的4个角落像素的中心对齐,并保留角点像素的值。 默认值:True
    - **align_mode** (int)- 双线性插值的可选项。 可以是 '0' 代表src_idx = scale *(dst_indx + 0.5)-0.5;可以为'1' ,代表src_idx = scale * dst_index。

返回: 4维tensor,shape为 (num_batches, channls, out_h, out_w).

返回类型: 变量(variable)

抛出异常:
    - :code:`TypeError` - out_shape应该是一个列表、元组或变量。
    - :code:`TypeError` - actual_shape应该是变量或None。
    - :code:`ValueError` - image_resize的"resample"只能是"BILINEAR"或"NEAREST"。
    - :code:`ValueError` - out_shape 和 scale 不可同时为 None。
    - :code:`ValueError` - out_shape 的长度必须为 2。
    - :code:`ValueError` - scale应大于0。
    - :code:`TypeError`  - align_corners 应为bool型。
    - :code:`ValueError` - align_mode 只能取 ‘0’ 或 ‘1’。


**代码示例**

..  code-block:: python

X
xsrobin 已提交
4211
  import paddle.fluid as fluid
C
Cheerego 已提交
4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247
  input = fluid.layers.data(name="input", shape=[3,6,9], dtype="float32")
  out = fluid.layers.image_resize(input, out_shape=[12, 12], resample="NEAREST")












.. _cn_api_fluid_layers_image_resize_short:

image_resize_short
-------------------------------

.. py:function:: paddle.fluid.layers.image_resize_short(input, out_short_len, resample='BILINEAR')

调整一批图片的大小。输入图像的短边将被调整为给定的out_short_len 。输入图像的长边按比例调整大小,最终图像的长宽比保持不变。

参数:
        - **input** (Variable) -  图像调整图层的输入张量,这是一个4维的形状张量(num_batch, channels, in_h, in_w)。
        - **out_short_len** (int) -  输出图像的短边长度。
        - **resample** (str) - resample方法,默认为双线性插值。

返回: 4维张量,shape为(num_batch, channls, out_h, out_w)

返回类型: 变量(variable)

**代码示例**

..  code-block:: python

X
xsrobin 已提交
4248
    import paddle.fluid as fluid
C
Cheerego 已提交
4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290
    input = fluid.layers.data(name="input", shape=[3,6,9], dtype="float32")
    out = fluid.layers.image_resize_short(input, out_short_len=3)

.. _cn_api_fluid_layers_kldiv_loss:

kldiv_loss
-------------------------------

.. py:function:: paddle.fluid.layers.kldiv_loss(x, target, reduction='mean', name=None)

此运算符计算输入(x)和输入(Target)之间的Kullback-Leibler发散损失。

kL发散损失计算如下:

..  math::

    l(x, y) = y * (log(y) - x)

:math:`x` 为输入(x),:math:`y` 输入(Target)。

当 ``reduction``  为 ``none`` 时,输出损失与输入(x)形状相同,各点的损失单独计算,不应用reduction 。

当 ``reduction``  为 ``mean`` 时,输出损失为[1]的形状,损失值为所有损失的平均值。

当 ``reduction``  为 ``sum`` 时,输出损失为[1]的形状,损失值为所有损失的总和。

当 ``reduction``  为 ``batchmean`` 时,输出损失为[1]的形状,损失值为所有损失的总和除以批量大小。

参数:
    - **x** (Variable) - KL发散损失算子的输入张量。这是一个形状为[N, \*]的张量,其中N是批大小,\*表示任何数量的附加维度
    - **target** (Variable) - KL发散损失算子的张量。这是一个具有输入(x)形状的张量
    - **reduction** (Variable)-要应用于输出的reduction类型,可用类型为‘none’ | ‘batchmean’ | ‘mean’ | ‘sum’,‘none’表示无reduction,‘batchmean’ 表示输出的总和除以批大小,‘mean’ 表示所有输出的平均值,‘sum’表示输出的总和。
    - **name** (str, default None) - 该层的名称

返回:KL发散损失

返回类型:kldiv_loss (Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
4291
    import paddle.fluid as fluid
C
Cheerego 已提交
4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332
    x = fluid.layers.data(name='x', shape=[4,2,2], dtype='float32')
    target = fluid.layers.data(name='target', shape=[4,2,2], dtype='float32')
    loss = fluid.layers.kldiv_loss(x=x, target=target, reduction='batchmean')







.. _cn_api_fluid_layers_l2_normalize:

l2_normalize
-------------------------------

.. py:function:: paddle.fluid.layers.l2_normalize(x,axis,epsilon=1e-12,name=None)

L2正则(L2 normalize Layer)

该层用欧几里得距离之和对维轴的x归一化。对于1-D张量(系数矩阵的维度固定为0),该层计算公式如下:

.. math::

    y=\frac{x}{\sqrt{\sum x^{2}+epsion}}

对于x多维的情况,该函数分别对维度轴上的每个1-D切片单独归一化

参数:
    - **x** (Variable|list)- l2正则层(l2_normalize layer)的输入
    - **axis** (int)-运用归一化的轴。如果轴小于0,归一化的维是rank(X)+axis。-1是最后维
    - **epsilon** (float)-epsilon用于避免分母为0,默认值为1e-12
    - **name** (str|None)-该层名称(可选)。如果设为空,则自动为该层命名

    返回:输出张量,同x的维度一致

    返回类型:变量

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
4333
    import paddle.fluid as fluid
C
Cheerego 已提交
4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382
    data = fluid.layers.data(name="data",
                         shape=(3, 17, 13),
                         dtype="float32")
    normed = fluid.layers.l2_normalize(x=data, axis=1)









.. _cn_api_fluid_layers_label_smooth:

label_smooth
-------------------------------

.. py:function:: paddle.fluid.layers.label_smooth(label, prior_dist=None, epsilon=0.1, dtype='float32', name=None)

标签平滑是一种对分类器层进行正则化的机制,称为标签平滑正则化(LSR)。


由于直接优化正确标签的对数似然可能会导致过拟合,降低模型的适应能力,因此提出了标签平滑的方法来降低模型置信度。
标签平滑使用标签 :math:`y` 自身和一些固定模式随机分布变量 :math:`\mu` 。对 :math:`k` 标签,我们有:

.. math::
            \tilde{y_k} = (1 - \epsilon) * y_k + \epsilon * \mu_k,

其中 :math:`1-\epsilon` 和 :math:`\epsilon` 分别是权重, :math:`\tilde{y_k}` 是平滑后的标签。 通常μ 使用均匀分布


查看更多关于标签平滑的细节 https://arxiv.org/abs/1512.00567

参数:
  - **label** (Variable) - 包含标签数据的输入变量。 标签数据应使用 one-hot 表示。
  - **prior_dist** (Variable) - 用于平滑标签的先验分布。 如果未提供,则使用均匀分布。 prior_dist的shape应为 :math:`(1,class\_num)` 。
  - **epsilon** (float) - 用于混合原始真实分布和固定分布的权重。
  - **dtype** (np.dtype | core.VarDesc.VarType | str) - 数据类型:float32,float_64,int等。
  - **name** (str | None) - 此层的名称(可选)。 如果设置为None,则将自动命名图层。

返回:张量变量, 包含平滑后的标签

返回类型: Variable

**代码示例**

..  code-block:: python

X
xsrobin 已提交
4383
    import paddle.fluid as fluid
C
Cheerego 已提交
4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441
    import paddle.fluid.layers as layers

    label = fluid.layers.data(name="label", shape=[1], dtype="float32")
    one_hot_label = fluid.layers.one_hot(input=label, depth=10)
    smooth_label = fluid.layers.label_smooth(
    label=one_hot_label, epsilon=0.1, dtype="float32")









.. _cn_api_fluid_layers_layer_norm:

layer_norm
-------------------------------

.. py:function:: paddle.fluid.layers.layer_norm(input, scale=True, shift=True, begin_norm_axis=1, epsilon=1e-05, param_attr=None, bias_attr=None, act=None, name=None)

假设特征向量存在于维度 ``begin_norm_axis ... rank (input)`` 上,计算大小为 ``H`` 的特征向量a在该维度上的矩统计量,然后使用相应的统计量对每个特征向量进行归一化。 之后,如果设置了 ``scale`` 和 ``shift`` ,则在标准化的张量上应用可学习的增益和偏差以进行缩放和移位。

请参考 `Layer Normalization <https://arxiv.org/pdf/1607.06450v1.pdf>`_

公式如下

.. math::
            \\\mu=\frac{1}{H}\sum_{i=1}^{H}a_i\\
.. math::
            \\\sigma=\sqrt{\frac{1}{H}\sum_i^H{(a_i-\mu)^2}}\\
.. math::
             \\h=f(\frac{g}{\sigma}(a-\mu) + b)\\

- :math:`\alpha` : 该层神经元输入总和的向量表示
- :math:`H` : 层中隐藏的神经元个数
- :math:`g` : 可训练的缩放因子参数
- :math:`b` : 可训练的bias参数


参数:
  - **input** (Variable) - 输入张量变量。
  - **scale** (bool) - 是否在归一化后学习自适应增益g。默认为True。
  - **shift** (bool) - 是否在归一化后学习自适应偏差b。默认为True。
  - **begin_norm_axis** (int) - ``begin_norm_axis`` 到 ``rank(input)`` 的维度执行规范化。默认1。
  - **epsilon** (float) - 添加到方差的很小的值,以防止除零。默认1e-05。
  - **param_attr** (ParamAttr | None) - 可学习增益g的参数属性。如果  ``scale`` 为False,则省略 ``param_attr`` 。如果 ``scale`` 为True且 ``param_attr`` 为None,则默认 ``ParamAttr`` 将作为比例。如果添加了 ``param_attr``, 则将其初始化为1。默认None。
  - **bias_attr** (ParamAttr | None) - 可学习偏差的参数属性b。如果 ``shift`` 为False,则省略 ``bias_attr`` 。如果 ``shift`` 为True且 ``param_attr`` 为None,则默认 ``ParamAttr`` 将作为偏差。如果添加了 ``bias_attr`` ,则将其初始化为0。默认None。
  - **act** (str) - 激活函数。默认 None
  - **name** (str) - 该层的名称, 可选的。默认为None,将自动生成唯一名称。

返回: 标准化后的结果

**代码示例**

..  code-block:: python

X
xsrobin 已提交
4442
   import paddle.fluid as fluid
C
Cheerego 已提交
4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474
   data = fluid.layers.data(name='data', shape=[3, 32, 32],
                                           dtype='float32')
   x = fluid.layers.layer_norm(input=data, begin_norm_axis=1)









.. _cn_api_fluid_layers_leaky_relu:

leaky_relu
-------------------------------

.. py:function:: paddle.fluid.layers.leaky_relu(x, alpha=0.02, name=None)

LeakyRelu 激活函数

.. math::   out=max(x,α∗x)

参数:
    - **x** (Variable) - LeakyRelu Operator的输入
    - **alpha** (FLOAT|0.02) - 负斜率,值很小。
    - **name** (str|None) - 此层的名称(可选)。如果设置为None,该层将被自动命名。

**代码示例:**

.. code-block:: python

X
xsrobin 已提交
4475
    import paddle.fluid as fluid
C
Cheerego 已提交
4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647
    x = fluid.layers.data(name="x", shape=[2,3,16,16], dtype="float32")
    y = fluid.layers.leaky_relu(x, alpha=0.01)







.. _cn_api_fluid_layers_linear_chain_crf:

linear_chain_crf
-------------------------------

.. py:function:: paddle.fluid.layers.linear_chain_crf(input, label, param_attr=None)

线性链条件随机场(Linear Chain CRF)

条件随机场定义间接概率图,节点代表随机变量,边代表两个变量之间的依赖。CRF学习条件概率 :math:`P\left ( Y|X \right )` , :math:`X = \left ( x_{1},x_{2},...,x_{n} \right )` 是结构性输入,:math:`Y = \left ( y_{1},y_{2},...,y_{n} \right )` 为输入标签。

线性链条件随机场(Linear Chain CRF)是特殊的条件随机场(CRF),有利于序列标注任务。序列标注任务不为输入设定许多条件依赖。唯一的限制是输入和输出必须是线性序列。因此类似CRF的图是一个简单的链或者线,也就是线性链随机场(linear chain CRF)。

该操作符实现了线性链条件随机场(linear chain CRF)的前向——反向算法。详情请参照 http://www.cs.columbia.edu/~mcollins/fb.pdf 和 http://cseweb.ucsd.edu/~elkan/250Bwinter2012/loglinearCRFs.pdf。


长度为L的序列s的概率定义如下:

.. math::

    P(s) = (1/Z) exp(a_{s_1} + b_{s_L} + sum_{l=1}^L x_{s_l} + sum_{l=2}^L w_{s_{l-1},s_l})


其中Z是正则化值,所有可能序列的P(s)之和为1,x是线性链条件随机场(linear chain CRF)的发射(emission)特征权重。

线性链条件随机场最终输出mini-batch每个训练样本的条件概率的对数


  1.这里 :math:`x` 代表Emission

  2.Transition的第一维度值,代表起始权重,这里用 :math:`a` 表示

  3.Transition的下一维值,代表末尾权重,这里用 :math:`b` 表示

  4.Transition剩下的值,代表转移权重,这里用 :math:`w` 表示

  5.Label用 :math:`s` 表示




**注意:**

    1.条件随机场(CRF)的特征函数由发射特征(emission feature)和转移特征(transition feature)组成。发射特征(emission feature)权重在调用函数前计算,而不在函数里计算。

    2.由于该函数对所有可能序列的进行全局正则化,发射特征(emission feature)权重应是未缩放的。因此如果该函数带有发射特征(emission feature),并且发射特征是任意非线性激活函数的输出,则请勿调用该函数。

    3.Emission的第二维度必须和标记数字(tag number)相同

参数:
    - **input** (Variable,LoDTensor,默认float类型LoDTensor) - 一个二维LoDTensor,shape为[N*D],N是mini-batch的大小,D是总标记数。线性链条件随机场的未缩放发射权重矩阵
    - **input** (Tensor,默认float类型LoDTensor) - 一个二维张量,shape为[(D+2)*D]。linear_chain_crf操作符的可学习参数。更多详情见operator注释
    - **label** (Variable,LoDTensor,默认int64类型LoDTensor) - shape为[N*10的LoDTensor,N是mini-batch的总元素数
    - **param_attr** (ParamAttr) - 可学习参数的属性

返回:
    output(Variable,Tensor,默认float类型Tensor):shape为[N*D]的二维张量。Emission的指数。这是前向计算中的中间计算结果,在后向计算中还会复用

    output(Variable,Tensor,默认float类型Tensor):shape为[(D+2)*D]的二维张量。Transition的指数。这是前向计算中的中间计算结果,在后向计算中还会复用

    output(Variable,Tensor,默认float类型Tensor):mini-batch每个训练样本的条件概率的对数。这是一个shape为[S*1]的二维张量,S是mini-batch的序列数。注:S等同于mini-batch的序列数。输出不再是LoDTensor

返回类型:output(Variable)

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
    emission = fluid.layers.data(name='emission', shape=[1000], dtype='float32')
    target = fluid.layers.data(name='target', shape=[1], dtype='int32')
    crf_cost = fluid.layers.linear_chain_crf(
        input=emission,
        label=target,
        param_attr=fluid.ParamAttr(
            name='crfw',
            learning_rate=0.2))











.. _cn_api_fluid_layers_lod_reset:

lod_reset
-------------------------------

.. py:function:: paddle.fluid.layers.lod_reset(x, y=None, target_lod=None)


设定x的LoD为y或者target_lod。如果提供y,首先将y.lod指定为目标LoD,否则y.data将指定为目标LoD。如果未提供y,目标LoD则指定为target_lod。如果目标LoD指定为Y.data或target_lod,只提供一层LoD。

::


    * 例1:

    给定一级LoDTensor x:
        x.lod =  [[ 2,           3,                   1 ]]
        x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        x.dims = [6, 1]

    target_lod: [4, 2]

    得到一级LoDTensor:
        out.lod =  [[4,                          2]]
        out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        out.dims = [6, 1]

    * 例2:

    给定一级LoDTensor x:
        x.lod =  [[2,            3,                   1]]
        x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        x.dims = [6, 1]

    y是张量(Tensor):
        y.data = [[2, 4]]
        y.dims = [1, 3]

    得到一级LoDTensor:
        out.lod =  [[2,            4]]
        out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        out.dims = [6, 1]

    * 例3:

    给定一级LoDTensor x:
        x.lod =  [[2,            3,                   1]]
        x.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        x.dims = [6, 1]

    y是二级LoDTensor:
        y.lod =  [[2, 2], [2, 2, 1, 1]]
        y.data = [[1.1], [2.1], [3.1], [4.1], [5.1], [6.1]]
        y.dims = [6, 1]

    得到一个二级LoDTensor:
        out.lod =  [[2, 2], [2, 2, 1, 1]]
        out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
        out.dims = [6, 1]

参数:
    - **x** (Variable)-输入变量,可以为Tensor或者LodTensor
    - **y** (Variable|None)-若提供,输出的LoD则衍生自y
    - **target_lod** (list|tuple|None)-一层LoD,y未提供时作为目标LoD

返回:输出变量,该层指定为LoD

返回类型:变量

抛出异常:``TypeError`` - 如果y和target_lod都为空

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
4648
    import paddle.fluid as fluid
C
Cheerego 已提交
4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687
    x = fluid.layers.data(name='x', shape=[10])
    y = fluid.layers.data(name='y', shape=[10, 20], lod_level=2)
    out = fluid.layers.lod_reset(x=x, y=y)









.. _cn_api_fluid_layers_log:

log
-------------------------------

.. py:function:: paddle.fluid.layers.log(x, name=None)


给定输入张量,计算其每个元素的自然对数

.. math::
                  \\Out=ln(x)\\


参数:
  - **x** (Variable) – 输入张量
  - **name** (str|None, default None) – 该layer的名称,如果为None,自动命名

返回:给定输入张量计算自然对数

返回类型: 变量(variable)


**代码示例**

..  code-block:: python

X
xsrobin 已提交
4688
  import paddle.fluid as fluid
C
Cheerego 已提交
4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732
  x = fluid.layers.data(name="x", shape=[3, 4], dtype="float32")
  output = fluid.layers.log(x)











.. _cn_api_fluid_layers_log_loss:

log_loss
-------------------------------

.. py:function:: paddle.fluid.layers.log_loss(input, label, epsilon=0.0001, name=None)

**负log loss层**

该层对输入的预测结果和目的标签进行计算,返回负log loss损失值。

.. math::

    Out = -label * \log{(input + \epsilon)} - (1 - label) * \log{(1 - input + \epsilon)}


参数:
  - **input** (Variable|list) – 形为[N x 1]的二维张量, 其中N为batch大小。 该输入是由先前运算得来的概率集。
  - **label** (Variable|list) – 形为[N x 1]的二维张量,承载着正确标记的数据, 其中N为batch大小。
  - **epsilon** (float) – epsilon
  - **name** (string) – log_loss层的名称

返回: 形为[N x 1]的二维张量,承载着负log_loss值

返回类型: 变量(Variable)


**代码示例**

..  code-block:: python

X
xsrobin 已提交
4733
    import paddle.fluid as fluid
C
Cheerego 已提交
4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')
    prob = fluid.layers.data(name='prob', shape=[10], dtype='float32')
    cost = fluid.layers.log_loss(input=prob, label=label)











.. _cn_api_fluid_layers_logical_and:

logical_and
-------------------------------

.. py:function:: paddle.fluid.layers.logical_and(x, y, out=None, name=None)

logical_and算子

它在X和Y上以元素方式操作,并返回Out。X、Y和Out是N维布尔张量(Tensor)。Out的每个元素的计算公式为:

.. math::
       Out = X \&\& Y

参数:
        - **x** (Variable)- (LoDTensor)logical_and算子的左操作数
        - **y** (Variable)- (LoDTensor)logical_and算子的右操作数
        - **out** (Tensor)- 输出逻辑运算的张量。
        - **name** (basestring | None)- 输出的名称。

返回:        (LoDTensor)n-dim bool张量。每个元素的计算公式: :math:`Out = X \&\& Y`

返回类型:        输出(Variable)。


**代码示例:**

.. code-block:: python

X
xsrobin 已提交
4777
    import paddle.fluid as fluid
C
Cheerego 已提交
4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819
    left = fluid.layers.data(
        name='left', shape=[1], dtype='int32')
    right = fluid.layers.data(
        name='right', shape=[1], dtype='int32')
    result = fluid.layers.logical_and(x=left, y=right)









.. _cn_api_fluid_layers_logical_not:

logical_not
-------------------------------

.. py:function:: paddle.fluid.layers.logical_not(x, out=None, name=None)

logical_not算子

它在X上以元素方式操作,并返回Out。X和Out是N维布尔张量(Tensor)。Out的每个元素的计算公式为:

.. math::
        Out = !X

参数:
        - **x** (Variable)- (LoDTensor)logical_not算子的操作数
        - **out** (Tensor)- 输出逻辑运算的张量。
        - **name** (basestring | None)- 输出的名称。

返回:        (LoDTensor)n维布尔张量。

返回类型:        输出(Variable)。


**代码示例:**

.. code-block:: python

X
xsrobin 已提交
4820
    import paddle.fluid as fluid
C
Cheerego 已提交
4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861
    left = fluid.layers.data(
        name='left', shape=[1], dtype='int32')
    result = fluid.layers.logical_not(x=left)







.. _cn_api_fluid_layers_logical_or:

logical_or
-------------------------------

.. py:function:: paddle.fluid.layers.logical_or(x, y, out=None, name=None)

logical_or算子

它在X和Y上以元素方式操作,并返回Out。X、Y和Out是N维布尔张量(Tensor)。Out的每个元素的计算公式为:

.. math::
        Out = X || Y

参数:
        - **x** (Variable)- (LoDTensor)logical_or算子的左操作数
        - **y** (Variable)- (LoDTensor)logical_or算子的右操作数
        - **out** (Tensor)- 输出逻辑运算的张量。
        - **name** (basestring | None)- 输出的名称。

返回:        (LoDTensor)n维布尔张量。每个元素的计算公式: :math:`Out = X || Y`

返回类型:        输出(Variable)。



**代码示例:**

.. code-block:: python


X
xsrobin 已提交
4862
    import paddle.fluid as fluid
C
Cheerego 已提交
4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903
    left = fluid.layers.data(
        name='left', shape=[1], dtype='int32')
    right = fluid.layers.data(
        name='right', shape=[1], dtype='int32')
    result = fluid.layers.logical_or(x=left, y=right)






.. _cn_api_fluid_layers_logical_xor:

logical_xor
-------------------------------

.. py:function:: paddle.fluid.layers.logical_xor(x, y, out=None, name=None)

logical_xor算子

它在X和Y上以元素方式操作,并返回Out。X、Y和Out是N维布尔张量(Tensor)。Out的每个元素的计算公式为:

.. math::
        Out = (X || Y) \&\& !(X \&\& Y)

参数:
        - **x** (Variable)- (LoDTensor)logical_xor算子的左操作数
        - **y** (Variable)- (LoDTensor)logical_xor算子的右操作数
        - **out** (Tensor)- 输出逻辑运算的张量。
        - **name** (basestring | None)- 输出的名称。

返回:        (LoDTensor)n维布尔张量。

返回类型:        输出(Variable)。



**代码示例:**

.. code-block:: python

X
xsrobin 已提交
4904
    import paddle.fluid as fluid
C
Cheerego 已提交
4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957
    left = fluid.layers.data(
        name='left', shape=[1], dtype='int32')
    right = fluid.layers.data(
        name='right', shape=[1], dtype='int32')
    result = fluid.layers.logical_xor(x=left, y=right)






.. _cn_api_fluid_layers_lrn:

lrn
-------------------------------

.. py:function:: paddle.fluid.layers.lrn(input, n=5, k=1.0, alpha=0.0001, beta=0.75, name=None)

局部响应正则层(Local Response Normalization Layer)

该层对局部输入区域正则化,执行一种侧向抑制(lateral inhibition)。

公式如下:

.. math::

    Output(i,x,y) = Input(i,x,y)/\left ( k+\alpha \sum_{j=max(0,i-n/2)}^{min(C-1,i+n/2)}(Input(j,x,y))^2 \right )^\beta

在以上公式中:
  - :math:`n` :累加的通道数
  - :math:`k` :位移(避免除数为0)
  - :math:`\alpha` : 缩放参数
  - :math:`\beta` : 指数参数

参考 : `ImageNet Classification with Deep Convolutional Neural Networks <https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf>`_

参数:
    - **input** (Variable)- 该层输入张量,输入张量维度必须为4
    - **n** (int,默认5) - 累加的通道数
    - **k** (float,默认1.0)- 位移(通常为正数,避免除数为0)
    - **alpha** (float,默认1e-4)- 缩放参数
    - **beta** (float,默认0.75)- 指数
    - **name** (str,默认None)- 操作符名

抛出异常:
  - ``ValueError`` - 如果输入张量的阶不为4

返回:张量,存储转置结果

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
4958
    import paddle.fluid as fluid
C
Cheerego 已提交
4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030
    data = fluid.layers.data(
        name="data", shape=[3, 112, 112], dtype="float32")
    lrn = fluid.layers.lrn(input=data)











.. _cn_api_fluid_layers_lstm:

lstm
-------------------------------

.. py:function::  paddle.fluid.layers.lstm(input, init_h, init_c, max_len, hidden_size, num_layers, dropout_prob=0.0, is_bidirec=False, is_test=False, name=None, default_initializer=None, seed=-1)

如果您的设备是GPU,本op将使用cudnn LSTM实现

一个没有 peephole 连接的四门长短期记忆网络。在前向传播中,给定迭代的输出ht和单元输出ct可由递归输入ht-1、单元输入ct-1和上一层输入xt计算,给定矩阵W、R和bias bW, bR由下式计算:

.. math::

  i_t &= \sigma(W_{ix}x_{t} + W_{ih}h_{t-1} + bx_i + bh_i)\\
  f_t &= \sigma(W_{fx}x_{t} + W_{fh}h_{t-1} + bx_f + bh_f)\\
  o_t &= \sigma(W_{ox}x_{t} + W_{oh}h_{t-1} + bx_o + bh_o)\\
  \tilde{c_t} &= tanh(W_{cx}x_t + W_{ch}h_{t-1} + bx_c + bh_c)\\
  c_t &= f_t \odot c_{t-1} + i_t \odot \tilde{c_t}\\
  h_t &= o_t \odot tanh(c_t)

公式中:
  - W 项表示权重矩阵(e.g. :math:`W_{ix}` 是从输入门到输入的权重矩阵)
  - b 项表示偏差向量( :math:`b_{xi}` 和 :math:`b_{hi}` 是输入门的偏差向量)
  - sigmoid 是 logistic sigmoid 函数
  - i、f、o、c 分别为输入门、遗忘门、输出门和激活向量,它们的大小与 cell 输出激活向量h相同。
  - :math:`\odot` 是向量的元素乘积
  - tanh是激活函数
  - :math:`\tilde{c_t}` 也称为候选隐藏状态,它是根据当前输入和之前的隐藏状态来计算的

sigmoid的计算公式为: :math:`sigmoid(x) = 1 / (1 + e^{-x})` 。


参数:
  - **input** (Variable) - LSTM 输入张量,形状必须为(seq_len x,batch_size,x,input_size)
  - **init_h** (Variable) – LSTM的初始隐藏状态,是一个有形状的张量(num_layers,x,batch_size,x,hidden_size)如果is_bidirec = True,形状应该是(num_layers*2,x, batch_size, x, hidden_size)
  - **init_c** (Variable) - LSTM的初始状态。这是一个有形状的张量(num_layers, x, batch_size, x, hidden_size)如果is_bidirec = True,形状应该是(num_layers*2, x, batch_size, x, hidden_size)
  - **max_len** (int) – LSTM的最大长度。输入张量的第一个 dim 不能大于max_len
  - **hidden_size** (int) - LSTM的隐藏大小
  - **num_layers** (int) –  LSTM的总层数
  - **dropout_prob** (float|0.0) – dropout prob,dropout 只在 rnn 层之间工作,而不是在时间步骤之间。dropout 不作用于最后的 rnn 层的 rnn 输出中
  - **is_bidirec** (bool) – 是否是双向的
  - **is_test** (bool) – 是否在测试阶段
  - **name** (str|None) - 此层的名称(可选)。如果没有设置,该层将被自动命名。
  - **default_initializer** (Initialize|None) – 在哪里使用初始化器初始化权重,如果没有设置,将进行默认初始化。
  - **seed** (int) – LSTM中dropout的Seed,如果是-1,dropout将使用随机Seed

返回:   三个张量, rnn_out, last_h, last_c:

- rnn_out为LSTM hidden的输出结果。形为(seq_len x batch_size x hidden_size)如果is_bidirec设置为True,则形为(seq_len x batch_sze hidden_size * 2)
- last_h(Tensor):  LSTM最后一步的隐藏状态,形状为(num_layers x batch_size x hidden_size);如果is_bidirec设置为True,形状为(num_layers*2 x batch_size x hidden_size)
- last_c(Tensor): LSTM最后一步的cell状态,形状为(num_layers x batch_size x hidden_size);如果is_bidirec设置为True,形状为(num_layers*2 x batch_size x hidden_size)

返回类型:   rnn_out(Tensor),last_h(Tensor),last_c(Tensor)

**代码示例:**

.. code-block:: python

X
xsrobin 已提交
5031
  import paddle.fluid as fluid
C
Cheerego 已提交
5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166
  emb_dim = 256
  vocab_size = 10000
  data = fluid.layers.data(name='x', shape=[-1, 100, 1],
                 dtype='int32')
  emb = fluid.layers.embedding(input=data, size=[vocab_size, emb_dim], is_sparse=True)
  batch_size = 20
  max_len = 100
  dropout_prob = 0.2
  input_size = 100
  hidden_size = 150
  num_layers = 1
  init_h = layers.fill_constant( [num_layers, batch_size, hidden_size], 'float32', 0.0 )
  init_c = layers.fill_constant( [num_layers, batch_size, hidden_size], 'float32', 0.0 )

  rnn_out, last_h, last_c = fluid.layers.lstm(emb, init_h, init_c, max_len, hidden_size, num_layers, dropout_prob=dropout_prob)












.. _cn_api_fluid_layers_lstm_unit:

lstm_unit
-------------------------------

.. py:function:: paddle.fluid.layers.lstm_unit(x_t, hidden_t_prev, cell_t_prev, forget_bias=0.0, param_attr=None, bias_attr=None, name=None)

Lstm unit layer

lstm步的等式:

.. math::

    i_{t} &= \sigma \left ( W_{x_{i}}x_{t}+W_{h_{i}}h_{t-1}+b_{i} \right ) \\
    f_{t} &= \sigma \left ( W_{x_{f}}x_{t}+W_{h_{f}}h_{t-1}+b_{f} \right ) \\
    c_{t} &= f_{t}c_{t-1}+i_{t}tanh\left ( W_{x_{c}}x_{t} +W_{h_{c}}h_{t-1}+b_{c}\right ) \\
    o_{t} &= \sigma \left ( W_{x_{o}}x_{t}+W_{h_{o}}h_{t-1}+b_{o} \right ) \\
    h_{t} &= o_{t}tanh \left ( c_{t} \right )

lstm单元的输入包括 :math:`x_{t}` , :math:`h_{t-1}` 和 :math:`c_{t-1}` 。:math:`h_{t-1}` 和 :math:`c_{t-1}` 的第二维应当相同。在此实现过程中,线性转换和非线性转换分离。以 :math:`i_{t}` 为例。线性转换运用到fc层,等式为:

.. math::

    L_{i_{t}} = W_{x_{i}}x_{t} + W_{h_{i}}h_{t-1} + b_{i}

非线性转换运用到lstm_unit运算,方程如下:

.. math::

    i_{t} = \sigma \left ( L_{i_{t}} \right )

该层有 :math:`h_{t}` 和 :math:`c_{t}` 两个输出。

参数:
    - **x_t** (Variable) - 当前步的输入值,二维张量,shape为 M x N ,M是批尺寸,N是输入尺寸
    - **hidden_t_prev** (Variable) - lstm单元的隐藏状态值,二维张量,shape为 M x S,M是批尺寸,N是lstm单元的大小
    - **cell_t_prev** (Variable) - lstm单元的cell值,二维张量,shape为 M x S ,M是批尺寸,N是lstm单元的大小
    - **forget_bias** (Variable) - lstm单元的遗忘bias
    - **param_attr** (ParamAttr|None) - 可学习hidden-hidden权重的擦参数属性。如果设为None或者ParamAttr的一个属性,lstm_unit创建ParamAttr为param_attr。如果param_attr的初始化函数未设置,参数初始化为Xavier。默认:None
    - **bias_attr** (ParamAttr|None) - 可学习bias权重的bias属性。如果设为False,输出单元中则不添加bias。如果设为None或者ParamAttr的一个属性,lstm_unit创建ParamAttr为bias_attr。如果bias_attr的初始化函数未设置,bias初始化为0.默认:None
    - **name** (str|None) - 该层名称(可选)。若设为None,则自动为该层命名

返回:lstm单元的hidden(隐藏状态)值和cell值

返回类型:tuple(元组)

抛出异常:
  - ``ValueError`` - ``x_t``,``hidden_t_prev`` 和 ``cell_t_prev`` 的阶不为2,或者 ``x_t`` ,``hidden_t_prev`` 和 ``cell_t_prev`` 的第一维不一致,或者 ``hidden_t_prev`` 和 ``cell_t_prev`` 的第二维不一致

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid
     
    dict_dim, emb_dim, hidden_dim = 128, 64, 512
    data = fluid.layers.data(name='step_data', shape=[1], dtype='int32')
    x = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])
    pre_hidden = fluid.layers.data(name='pre_hidden', shape=[hidden_dim], dtype='float32')
    pre_cell = fluid.layers.data(name='pre_cell', shape=[hidden_dim], dtype='float32')
    hidden = fluid.layers.lstm_unit(
        x_t=x,
        hidden_t_prev=prev_hidden,
        cell_t_prev=prev_cell)











.. _cn_api_fluid_layers_margin_rank_loss:

margin_rank_loss
-------------------------------

.. py:function:: paddle.fluid.layers.margin_rank_loss(label, left, right, margin=0.1, name=None)

margin rank loss(差距排序损失)层。在排序问题中,它可以比较传进来的 ``left`` 得分和 ``right`` 得分。

可用如下等式定义:

.. math::
    rank\_loss = max(0, -label * (left - right) + margin)


参数:
  - **label** (Variable) – 表明是否左元素排名要高于右元素
  - **left** (Variable) – 左元素排序得分
  - **right** (Variable) – 右元素排序得分
  - **margin** (float) – 指定固定的得分差
  - **name** (str|None) – 可选项,该层的命名。如果为None, 该层将会被自动命名

返回: 排序损失

返回类型: 变量(Variable)

抛出异常:
  - ``ValueError`` - ``label`` , ``left`` , ``right`` 有一者不为Variable类型时,抛出此异常

**代码示例**

..  code-block:: python

X
xsrobin 已提交
5167
    import paddle.fluid as fluid
C
Cheerego 已提交
5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243
    label = fluid.layers.data(name="label", shape=[-1, 1], dtype="float32")
    left = fluid.layers.data(name="left", shape=[-1, 1], dtype="float32")
    right = fluid.layers.data(name="right", shape=[-1, 1], dtype="float32")
    out = fluid.layers.margin_rank_loss(label, left, right)











.. _cn_api_fluid_layers_matmul:



matmul
-------------------------------

.. py:function:: paddle.fluid.layers.matmul(x, y, transpose_x=False, transpose_y=False, alpha=1.0, name=None)

对两个张量进行矩阵相乘

当前输入的张量可以为任意阶,但当任意一个输入的阶数大于3时,两个输入的阶必须相等。
实际的操作取决于x,y的维度和 ``transpose_x`` , ``transpose_y`` 的标记值。具体如下:

- 如果transpose值为真,则对应 ``tensor`` 的最后两维将被转置。如:x是一个shape=[D]的一阶张量,那么x在非转置形式中为[1,D],在转置形式中为[D,1],而y则相反,在非转置形式中作为[D,1],在转置形式中作为[1,D]。

- 转置后,这两个`tensors`将为 2-D 或 n-D ,并依据下列规则进行矩阵相乘:
  - 如果两个都是2-D,则同普通矩阵一样进行矩阵相乘
  - 如果任意一个是n-D,则将其视为驻留在最后两个维度的矩阵堆栈,并在两个张量上应用支持广播的批处理矩阵乘法。

**注意,如果原始张量x或y的秩为1且没有转置,则在矩阵乘法之后,前置或附加维度1将被移除。**


参数:
    - **x** (Variable)-输入变量,类型为Tensor或LoDTensor
    - **y** (Variable)-输入变量,类型为Tensor或LoDTensor
    - **transpose_x** (bool)-相乘前是否转置x
    - **transpose_y** (bool)-相乘前是否转置y
    - **alpha** (float)-输出比例。默认为1.0
    - **name** (str|None)-该层名称(可选)。如果设置为空,则自动为该层命名

返回:张量乘积变量

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

    # 以下是解释输入和输出维度的示例
    # x: [B, ..., M, K], y: [B, ..., K, N]
    # fluid.layers.matmul(x, y)  # out: [B, ..., M, N]

    # x: [B, M, K], y: [B, K, N]
    # fluid.layers.matmul(x, y)  # out: [B, M, N]

    # x: [B, M, K], y: [K, N]
    # fluid.layers.matmul(x, y)  # out: [B, M, N]

    # x: [M, K], y: [K, N]
    # fluid.layers.matmul(x, y)  # out: [M, N]

    # x: [B, M, K], y: [K]
    # fluid.layers.matmul(x, y)  # out: [B, M]

    # x: [K], y: [K]
    # fluid.layers.matmul(x, y)  # out: [1]

    # x: [M], y: [N]
    # fluid.layers.matmul(x, y, True, True)  # out: [M, N]

X
xsrobin 已提交
5244
    import paddle.fluid as fluid
C
Cheerego 已提交
5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290
    x = fluid.layers.data(name='x', shape=[2, 3], dtype='float32')
    y = fluid.layers.data(name='y', shape=[3, 2], dtype='float32')
    out = fluid.layers.matmul(x, y, True, True)







.. _cn_api_fluid_layers_maxout:

maxout
-------------------------------

.. py:function:: paddle.fluid.layers.maxout(x, groups, name=None)

假设输入形状为(N, Ci, H, W),输出形状为(N, Co, H, W),则 :math:`Co=Ci/groups` 运算公式如下:

.. math::

  y_{si+j} &= \max_k x_{gsi + sk + j} \\
  g &= groups \\
  s &= \frac{input.size}{num\_channels} \\
  0 \le &i < \frac{num\_channels}{groups} \\
  0 \le &j < s \\
  0 \le &k < groups


请参阅论文:
  - Maxout Networks:  http://www.jmlr.org/proceedings/papers/v28/goodfellow13.pdf
  - Multi-digit Number Recognition from Street View Imagery using Deep Convolutional Neural Networks: https://arxiv.org/pdf/1312.6082v4.pdf

参数:
    - **x** (Variable) - (tensor) maxout算子的输入张量。输入张量的格式为NCHW。其中N为 batch size ,C为通道数,H和W为feature的高和宽
    - **groups** (INT)- 指定将输入张量的channel通道维度进行分组的数目。输出的通道数量为通道数除以组数。
    - **name** (basestring|None) - 输出的名称

返回:Tensor,maxout算子的输出张量。输出张量的格式也是NCHW。其中N为 batch size,C为通道数,H和W为特征的高和宽。

返回类型:out(Variable)

**代码示例**:
    
.. code-block:: python

X
xsrobin 已提交
5291
    import paddle.fluid as fluid
C
Cheerego 已提交
5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327
    input = fluid.layers.data(
        name='data',
        shape=[256, 32, 32],
        dtype='float32')
    out = fluid.layers.maxout(input, groups=2)










.. _cn_api_fluid_layers_mean:

mean
-------------------------------

.. py:function:: paddle.fluid.layers.mean(x, name=None)

mean算子计算X中所有元素的平均值

参数:
        - **x** (Variable)- (Tensor) 均值运算的输入。
        - **name** (basestring | None)- 输出的名称。

返回:       均值运算输出张量(Tensor)

返回类型:        Variable

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
5328
    import paddle.fluid as fluid
C
Cheerego 已提交
5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425
    input = fluid.layers.data(
        name='data', shape=[2, 3], dtype='float32')
    mean = fluid.layers.mean(input)









.. _cn_api_fluid_layers_mean_iou:

mean_iou
-------------------------------

.. py:function:: paddle.fluid.layers.mean_iou(input, label, num_classes)

均值IOU(Mean  Intersection-Over-Union)是语义图像分割中的常用的评价指标之一,它首先计算每个语义类的IOU,然后计算类之间的平均值。定义如下:

.. math::

    IOU = \frac{true\_positive}{true\_positive+false\_positive+false\_negative}

在一个confusion矩阵中累积得到预测值,然后从中计算均值-IOU。

参数:
    - **input** (Variable) - 类型为int32或int64的语义标签的预测结果张量。
    - **label** (Variable) - int32或int64类型的真实label张量。它的shape应该与输入相同。
    - **num_classes** (int) - 标签可能的类别数目。

返回: 返回三个变量:

- mean_iou: 张量,形为[1], 代表均值IOU。
- out_wrong: 张量,形为[num_classes]。每个类别中错误的个数。
- out_correct:张量,形为[num_classes]。每个类别中的正确的个数。

返回类型:   mean_iou (Variable),out_wrong(Variable),out_correct(Variable)

**代码示例**

..  code-block:: python

   import paddle.fluid as fluid
   predict = fluid.layers.data(name='predict', shape=[3, 32, 32])
   label = fluid.layers.data(name='label', shape=[1])
   iou, wrongs, corrects = fluid.layers.mean_iou(predict, label, num_classes)









.. _cn_api_fluid_layers_merge_selected_rows:

merge_selected_rows
-------------------------------

.. py:function:: paddle.fluid.layers.merge_selected_rows(x, name=None)

**实现合并选中行(row)操作**

该运算用于合并(值相加)输入张量中重复的行。输出行没有重复的行,并且按值从小到大顺序重新对行排序。

::

    例如:

          输入:
               X.rows = [0, 5, 5, 4, 19]
               X.height = 20
               X.value = [[1, 1] [2, 2] [3, 3] [4, 4] [6, 6]]


          输出:
               Out.row is [0, 4, 5, 19]
               Out.height is 20
               Out.value is: [[1, 1] [4, 4] [5, 5] [6, 6]]



参数:
  - x (Variable) – 输入类型为SelectedRows, 选中行有可能重复
  - name (basestring|None) – 输出变量的命名

返回: 输出类型为SelectedRows,并且选中行不会重复

返回类型: 变量(Variable)

**代码示例**

..  code-block:: python

X
xsrobin 已提交
5426
  import paddle.fluid as fluid
C
Cheerego 已提交
5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611
  b = fluid.default_main_program().global_block()
  var = b.create_var(
        name="X", dtype="float32", persistable=True,
        type=fluid.core.VarDesc.VarType.SELECTED_ROWS)
  y = fluid.layers.merge_selected_rows(var)









.. _cn_api_fluid_layers_mul:

mul
-------------------------------

.. py:function:: paddle.fluid.layers.mul(x, y, x_num_col_dims=1, y_num_col_dims=1, name=None)

mul算子
此运算是用于对输入X和Y执行矩阵乘法。
等式是:

.. math::
        Out = X * Y

输入X和Y都可以携带LoD(详细程度)信息。但输出仅与输入X共享LoD信息。

参数:
        - **x** (Variable)- (Tensor) 乘法运算的第一个输入张量。
        - **y** (Variable)- (Tensor) 乘法运算的第二个输入张量。
        - **x_num_col_dims** (int)- 默认值1, 可以将具有两个以上维度的张量作为输入。如果输入X是具有多于两个维度的张量,则输入X将先展平为二维矩阵。展平规则是:前 ``num_col_dims`` 将被展平成最终矩阵的第一个维度(矩阵的高度),其余的 rank(X) - num_col_dims 维度被展平成最终矩阵的第二个维度(矩阵的宽度)。结果是展平矩阵的高度等于X的前 ``x_num_col_dims`` 维数的乘积,展平矩阵的宽度等于X的最后一个秩(x)- ``num_col_dims`` 个剩余维度的维数的乘积。例如,假设X是一个五维张量,形状为(2,3,4,5,6)。 则扁平化后的张量具有的形即为 (2x3x4,5x6)=(24,30)。
        - **y_num_col_dims** (int)- 默认值1, 可以将具有两个以上维度的张量作为输入。如果输入Y是具有多于两个维度的张量,则Y将首先展平为二维矩阵。 ``y_num_col_dims`` 属性确定Y的展平方式。有关更多详细信息,请参阅 ``x_num_col_dims`` 的注释。
        - **name** (basestring | None)- 输出的名称。

返回:       乘法运算输出张量(Tensor).

返回类型:    输出(Variable)。

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    dataX = fluid.layers.data(name="dataX", append_batch_size = False, shape=[2, 5], dtype="float32")
    dataY = fluid.layers.data(name="dataY", append_batch_size = False, shape=[5, 3], dtype="float32")
    output = fluid.layers.mul(dataX, dataY,
                              x_num_col_dims = 1,
                              y_num_col_dims = 1)






.. _cn_api_fluid_layers_multiplex:

multiplex
-------------------------------

.. py:function:: paddle.fluid.layers.multiplex(inputs, index)

引用给定的索引变量,该层从输入变量中选择行构造Multiplex变量。

假设有 :math:`m` 个输入变量,:math:`I_{i}` 代表第i个输入变量,而且 :math:`i` is in :math:`[0,m)` 。

所有输入变量都是具有相同形状的张量 :math:`[d_0,d_1, ... ,d_R]` 。

请注意,输入张量的秩应至少为2。每个输入变量将被视为形状为 :math:`[M,N]` 的二维矩阵,其中 :math:`M` 表示 :math:`d0` ,N表示 :math:`d_1 * d_2 * ... * d_R` 。

设 :math:`I_{i}[j]` 为第i个输入变量的第j行。 给定的索引变量是具有形状[M,1]的2-D张量。 设 :math:`ID[i]` 为索引变量的第i个索引值。 然后输出变量将是一个形状为 :math:`[d_0,d_1, ... ,d_R]` 的张量。

如果将输出张量视为具有形状[M,N]的2-D矩阵,并且令O[i]为矩阵的第i行,则O[i]等于 :math:`I_{ID}[i][i]`

- Ids: 索引张量
- X[0 : N - 1]: 输出的候选张量度(N >= 2).
- 对于从 0 到 batchSize-1 的每个索引i,输出是第(Ids [i])  张量的第i行

对于第i行的输出张量:

.. math::
            \\y[i]=x_k[i]\\

其中 :math:`y` 为输出张量, :math:`x_k` 为第k个输入张量,并且 :math:`k=Ids[i]` 。

示例:

.. code-block:: text

        例1:

        假设:

        X = [[[0,0,3,4], [0,1,3,4], [0,2,4,4], [0,3,3,4]],
             [[1,0,3,4], [1,1,7,8], [1,2,4,2], [1,3,3,4]],
             [[2,0,3,4], [2,1,7,8], [2,2,4,2], [2,3,3,4]],
             [[3,0,3,4], [3,1,7,8], [3,2,4,2], [3,3,3,4]]]

        index = [3,0,1,2]

        out:[[3 0 3 4]    // X[3,0] (3 = index[i], 0 = i); i=0
             [0 1 3 4]    // X[0,1] (0 = index[i], 1 = i); i=1
             [1 2 4 2]    // X[1,2] (0 = index[i], 2 = i); i=2
             [2 3 3 4]]   // X[2,3] (0 = index[i], 3 = i); i=3

        例2:

        假设:

        X = [[[0,0,3,4], [0,1,3,4], [0,2,4,4], [0,3,3,4]],
             [[1,0,3,4], [1,1,7,8], [1,2,4,2], [1,3,3,4]]]

        index = [1,0]

        out:[[1 0 3 4]    // X[1,0] (3 = index[0], 0 = i); i=1
             [0 1 3 4]    // X[0,1] (0 = index[1], 1 = i); i=2
             [0 2 4 4]    // X[0,2] (0 = 0, 2 = i); i=3
             [0 3 3 4]]   // X[0,3] (0 = 0, 3 = i); i=4





参数:
  - **inputs** (list) - 要从中收集的变量列表。所有变量的形状相同,秩至少为2
  - **index** (Variable) -  Tensor <int32>,索引变量为二维张量,形状[M, 1],其中M为批大小。

返回:multiplex 张量

**代码示例**

..  code-block:: python

   import paddle.fluid as fluid

   x1 = fluid.layers.data(name='x1', shape=[4], dtype='float32')
   x2 = fluid.layers.data(name='x2', shape=[4], dtype='float32')
   index = fluid.layers.data(name='index', shape=[1], dtype='int32')
   out = fluid.layers.multiplex(inputs=[x1, x2], index=index)









.. _cn_api_fluid_layers_nce:

nce
-------------------------------

.. py:function:: paddle.fluid.layers.nce(input, label, num_total_classes, sample_weight=None, param_attr=None, bias_attr=None, num_neg_samples=None, name=None, sampler='uniform', custom_dist=None, seed=0, is_sparse=False)

计算并返回噪音对比估计( noise-contrastive estimation training loss)。
`请参考 See Noise-contrastive estimation: A new estimation principle for unnormalized statistical models
<http://www.jmlr.org/proceedings/papers/v9/gutmann10a/gutmann10a.pdf>`_
该operator默认使用均匀分布进行抽样。

参数:
    - **input** (Variable) -  输入变量
    - **label** (Variable) -  标签
    - **num_total_classes** (int) - 所有样本中的类别的总数
    - **sample_weight** (Variable|None) - 存储每个样本权重,shape为[batch_size, 1]存储每个样本的权重。每个样本的默认权重为1.0
    - **param_attr** (ParamAttr|None) - :math:`可学习参数/nce权重` 的参数属性。如果它没有被设置为ParamAttr的一个属性,nce将创建ParamAttr为param_attr。如没有设置param_attr的初始化器,那么参数将用Xavier初始化。默认值:None
    - **bias_attr** (ParamAttr|bool|None) -  nce偏置的参数属性。如果设置为False,则不会向输出添加偏置(bias)。如果值为None或ParamAttr的一个属性,则bias_attr=ParamAtt。如果没有设置bias_attr的初始化器,偏置将被初始化为零。默认值:None
    - **num_neg_samples** (int) - 负样例的数量。默认值是10
    - **name** (str|None) - 该layer的名称(可选)。如果设置为None,该层将被自动命名
    - **sampler** (str) – 取样器,用于从负类别中进行取样。可以是 ‘uniform’, ‘log_uniform’ 或 ‘custom_dist’。 默认 ‘uniform’
    - **custom_dist** (float[]) – 一个 float[] 并且它的长度为 ``num_total_classes`` 。  如果取样器类别为‘custom_dist’,则使用此参数。 custom_dist[i] 是第i个类别被取样的概率。默认为 None
    - **seed** (int) – 取样器使用的seed。默认为0
    - **is_sparse** (bool) – 标志位,指明是否使用稀疏更新,  :math:`weight@GRAD` 和 :math:`bias@GRAD` 会变为 SelectedRows

返回: nce loss

返回类型: 变量(Variable)


**代码示例**

..  code-block:: python

X
xsrobin 已提交
5612
    import paddle.fluid as fluid
C
Cheerego 已提交
5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676
    import numpy as np

    window_size = 5
    words = []
    for i in xrange(window_size):
        words.append(fluid.layers.data(
            name='word_{0}'.format(i), shape=[1], dtype='int64'))

    dict_size = 10000
    label_word = int(window_size / 2) + 1

    embs = []
    for i in xrange(window_size):
        if i == label_word:
            continue

        emb = fluid.layers.embedding(input=words[i], size=[dict_size, 32],
                           param_attr='embed', is_sparse=True)
        embs.append(emb)

    embs = fluid.layers.concat(input=embs, axis=1)
    loss = fluid.layers.nce(input=embs, label=words[label_word],
              num_total_classes=dict_size, param_attr='nce.w_0',
              bias_attr='nce.b_0')

    # 或使用自定义分布
    dist = np.array([0.05,0.5,0.1,0.3,0.05])
    loss = fluid.layers.nce(input=embs, label=words[label_word],
              num_total_classes=5, param_attr='nce.w_1',
              bias_attr='nce.b_1',
              num_neg_samples=3,
              sampler="custom_dist",
              custom_dist=dist)




.. _cn_api_fluid_layers_npair_loss:

npair_loss
-------------------------------

.. py:function:: paddle.fluid.layers.npair_loss(anchor, positive, labels, l2_reg=0.002)

**Npair Loss Layer**

参考阅读 `Improved Deep Metric Learning with Multi class N pair Loss Objective <http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf>`_

NPair损失需要成对的数据。NPair损失分为两部分:第一部分是嵌入向量上的L2正则化器;第二部分是以anchor的相似矩阵和正的相似矩阵为逻辑的交叉熵损失。

参数:
    - **anchor** (Variable) -  嵌入锚定图像的向量。尺寸=[batch_size, embedding_dims]
    - **positive** (Variable) -  嵌入正图像的向量。尺寸=[batch_size, embedding_dims]
    - **labels** (Variable) - 1维张量,尺寸=[batch_size]
    - **l2_reg** (float32) - 嵌入向量的L2正则化项,默认值:0.002

返回: npair loss,尺寸=[1]

返回类型:npair loss(Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
5677
    import paddle.fluid as fluid
C
Cheerego 已提交
5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712
    anchor = fluid.layers.data(
              name = 'anchor', shape = [18, 6], dtype = 'float32', append_batch_size=False)
    positive = fluid.layers.data(
              name = 'positive', shape = [18, 6], dtype = 'float32', append_batch_size=False)
    labels = fluid.layers.data(
              name = 'labels', shape = [18], dtype = 'float32', append_batch_size=False)

    npair_loss = fluid.layers.npair_loss(anchor, positive, labels, l2_reg = 0.002)






.. _cn_api_fluid_layers_one_hot:

one_hot
-------------------------------

.. py:function:: paddle.fluid.layers.one_hot(input, depth)

该层创建输入指数的one-hot表示

参数:
    - **input** (Variable)-输入指数,最后维度必须为1
    - **depth** (scalar)-整数,定义one-hot维度的深度

返回:输入的one-hot表示

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
5713
    import paddle.fluid as fluid
C
Cheerego 已提交
5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945
    label = fluid.layers.data(name="label", shape=[1], dtype="int64")
    one_hot_label = fluid.layers.one_hot(input=label, depth=10)









.. _cn_api_fluid_layers_pad:

pad
-------------------------------

.. py:function:: paddle.fluid.layers.pad(x, paddings, pad_value=0.0, name=None)

在张量上加上一个由 ``pad_value`` 给出的常数值,填充宽度由 ``paddings`` 指定。
其中,维度 ``i`` 中 ``x`` 内容前填充的值个数用 ``paddings[i]`` 表示,维度 ``i`` 中 ``x`` 内容后填充的值个数用 ``paddings[i+1]`` 表示。

一个例子:

::

        Given:

         x = [[1, 2], [3, 4]]

        paddings = [0, 1, 1, 2]

        pad_value = 0

        Return:

        out = [[0, 1, 2, 0, 0]
               [0, 3, 4, 0, 0]
               [0, 0, 0, 0, 0]]


参数:
    - **x** (Variable) — —输入张量变量。
    - **paddings** (list) — 一个整数列表。按顺序填充在每个维度上填充元素。 ``padding`` 长度必须是 ``rank(x)×2``
    - **pad_value** (float) — 用来填充的常量值。
    - **name** (str|None) — 这个层的名称(可选)。如果设置为None,该层将被自动命名。

返回: 填充后的张量变量

返回类型: 变量(Variable)


**代码示例**

..  code-block:: python

    # x 为一个秩为2的张量
    import paddle.fluid as fluid
    x = fluid.layers.data(name='data', shape=[224], dtype='float32')
    out = fluid.layers.pad(
        x=x, paddings=[0, 1, 1, 2], pad_value=0.)










.. _cn_api_fluid_layers_pad2d:

pad2d
-------------------------------

.. py:function::  paddle.fluid.layers.pad2d(input, paddings=[0, 0, 0, 0], mode='constant', pad_value=0.0, data_format='NCHW', name=None)

依照 paddings 和 mode 属性对图像进行2维 ``pad``,如果mode是 ``reflection``,则paddings[0]和paddings[1]必须不大于height-1。宽度维数具有相同的条件。

例如:

.. code-block:: text

  假设X是输入图像:

      X = [[1, 2, 3],
           [4, 5, 6]]

     Case 0:
        paddings = [0, 1, 2, 3],
        mode = 'constant'
        pad_value = 0
        Out = [[0, 0, 1, 2, 3, 0, 0, 0]
               [0, 0, 4, 5, 6, 0, 0, 0]
               [0, 0, 0, 0, 0, 0, 0, 0]]

     Case 1:
        paddings = [0, 1, 2, 1],
        mode = 'reflect'
        Out = [[3, 2, 1, 2, 3, 2]
               [6, 5, 4, 5, 6, 5]
               [3, 2, 1, 2, 3, 2]]

     Case 2:
        paddings = [0, 1, 2, 1],
        mode = 'edge'
        Out = [[1, 1, 1, 2, 3, 3]
               [4, 4, 4, 5, 6, 6]
               [4, 4, 4, 5, 6, 6]]

参数:
  - **input** (Variable) - 具有[N, C, H, W]格式或[N, H, W, C]格式的输入图像。
  - **paddings** (tuple|list|Variable) - 填充区域的大小。如果填充是一个元组,它必须包含四个整数,
    (padding_top, padding_bottom, padding_left, padding_right)。默认:padding =[0,0,0,0]。
  - **mode** (str) - 三种模式:constant(默认)、reflect、edge。默认值:常数
  - **pad_value** (float32) - 以常量模式填充填充区域的值。默认值:0
  - **data_format** (str)  - 可选字符串,选项有: ``NHWC`` , ``NCHW``。指定输入数据的数据格式。默认值:``NCHW``
  - **name** (str|None) - 此层的名称(可选)。如果没有设置,该层将被自动命名。

返回: tensor变量,按照 padding值 和 mode 进行填充

返回类型:variable

**代码示例:**

.. code-block:: python

  import paddle.fluid as fluid
  data = fluid.layers.data(name='data', shape=[3, 32, 32], dtype='float32')
  result = fluid.layers.pad2d(input=data, paddings=[1,2,3,4], mode='reflect')











.. _cn_api_fluid_layers_pad_constant_like:

pad_constant_like
-------------------------------

.. py:function:: paddle.fluid.layers.pad_constant_like(x, y, pad_value=0.0, name=None)

使用 ``pad_value`` 填充 ``Y`` ,填充到每个axis(轴)值的数量由X和Y的形不同而指定。((0,shape_x_0 - shape_y_0),...(0,shape_x_n - shape_y_n ))是每个axis唯一pad宽度。输入应该是k维张量(k> 0且k <7)。

**实例如下**

::

    Given:
        X = [[[[ 0,  1,  2],
               [ 3,  4,  5]],
              [[ 6,  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]]]]
        X.shape = (2, 3, 2, 3)

        Y = [[[[35, 36, 37]],
              [[38, 39, 40]],
              [[41, 42, 43]]]]
        Y.shape = (1, 3, 1, 3)

参数:
          - **x** (Variable)- 输入Tensor变量。
          - **y** (Variable)- 输出Tensor变量。
          - **pad_value** (float) - 用于填充的常量值。
          - **name** (str | None) - 这一层的名称(可选)。如果设置为None,则将自动命名这一层。

返回:填充张量(Tensor)变量

返回类型:  变量(Variable)

**示例代码**

..  code-block:: python

    # x是秩为4的tensor, x.shape = (2, 3, 2, 3)
    # y是秩为4的tensor, y.shape = (1, 3, 1, 3)
    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[2,3,2,3], dtype='float32')
    y = fluid.layers.data(name='y', shape=[1,3,1,3], dtype='float32')
    out = fluid.layers.pad_constant_like(x=x, y=y, pad_value=0.)
    # out是秩为4的tensor, out.shape = [2, 3 ,2 , 3]




.. _cn_api_fluid_layers_pixel_shuffle:

pixel_shuffle
-------------------------------

.. py:function:: paddle.fluid.layers.pixel_shuffle(x, upscale_factor)

pixel shuffle 层(像素重组层)

该层将一个形为[N, C, H, W]的张量重新排列成形为 [N, C/r**2, H*r, W*r] 的张量。这样做有利于实现步长(stride)为1/r的高效sub-pixel(亚像素)卷积。详见Shi等人在2016年发表的论文 `Real Time Single Image and Video Super Resolution Using an Efficient Sub Pixel Convolutional Neural Network <https://arxiv.org/abs/1609.05158v2>`_ 。

.. code-block:: text

    给定一个形为  x.shape = [1, 9, 4, 4]  的4-D张量
    设定:upscale_factor=3
    那么输出张量的形为:[1, 1, 12, 12]

参数:
          - **x** (Variable)- 输入Tensor变量。
          - **upscale_factor** (int)- 增大空间分辨率的增大因子


返回:根据新的维度信息进行重组的张量

返回类型:  Variable

抛出异常: ``ValueError``  - 如果upscale_factor的平方不能整除输入的通道维(C)大小。


**示例代码**

..  code-block:: python

X
xsrobin 已提交
5946
    import paddle.fluid as fluid
C
Cheerego 已提交
5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035
    input = fluid.layers.data(name="input", shape=[9,4,4])
    output = fluid.layers.pixel_shuffle(x=input, upscale_factor=3)





.. _cn_api_fluid_layers_pool2d:

pool2d
-------------------------------

.. py:function:: paddle.fluid.layers.pool2d(input, pool_size=-1, pool_type='max', pool_stride=1, pool_padding=0, global_pooling=False, use_cudnn=True, ceil_mode=False, name=None, exclusive=True)

pooling2d操作符根据 ``input`` , 池化类型 ``pool_type`` , 池化核大小 ``pool_size`` , 步长 ``pool_stride`` ,填充 ``pool_padding`` 这些参数得到输出。

输入X和输出Out是NCHW格式,N为batch尺寸,C是通道数,H是特征高度,W是特征宽度。

参数(ksize,strides,paddings)含有两个元素。这两个元素分别代表高度和宽度。输入X的大小和输出Out的大小可能不一致。

例如:

输入:
    X shape::math:`\left ( N,C,H_{in},W_{in} \right )`

输出:
    Out shape::math:`\left ( N,C,H_{out},W_{out} \right )`

如果 ``ceil_mode`` = false:

.. math::
    H_{out} = \frac{(H_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1

.. math::
    W_{out} = \frac{(W_{in} - ksize[1] + 2 * paddings[1])}{strides[1]} + 1

如果 ``ceil_mode`` = true:

.. math::
    H_{out} = \frac{(H_{in} - ksize[0] + 2 * paddings[0] + strides[0] - 1)}{strides[0]} + 1

.. math::
    W_{out} = \frac{(W_{in} - ksize[1] + 2 * paddings[1] + strides[1] - 1)}{strides[1]} + 1

如果 ``exclusive`` = false:

.. math::
    hstart &= i * strides[0] - paddings[0] \\
    hend   &= hstart + ksize[0] \\
    wstart &= j * strides[1] - paddings[1] \\
    wend   &= wstart + ksize[1] \\
    Output(i ,j) &= \frac{sum(Input[hstart:hend, wstart:wend])}{ksize[0] * ksize[1]}

如果 ``exclusive`` = true:

.. math::
    hstart &= max(0, i * strides[0] - paddings[0])\\
    hend &= min(H, hstart + ksize[0]) \\
    wstart &= max(0, j * strides[1] - paddings[1]) \\
    wend & = min(W, wstart + ksize[1]) \\
    Output(i ,j) & = \frac{sum(Input[hstart:hend, wstart:wend])}{(hend - hstart) * (wend - wstart)}



参数:
    - **input** (Variable) - 池化操作的输入张量。输入张量格式为NCHW,N为批尺寸,C是通道数,H是特征高度,W是特征宽度
    - **pool_size** (int|list|tuple)  - 池化核的大小。如果它是一个元组或列表,它必须包含两个整数值, (pool_size_Height, pool_size_Width)。若为一个整数,则它的平方值将作为池化核大小,比如若pool_size=2, 则池化核大小为2x2。
    - **pool_type** (string) - 池化类型,可以是“max”对应max-pooling,“avg”对应average-pooling
    - **pool_stride** (int|list|tuple)  - 池化层的步长。如果它是一个元组或列表,它将包含两个整数,(pool_stride_Height, pool_stride_Width)。否则它是一个整数的平方值。
    - **pool_padding** (int|list|tuple) - 填充大小。如果它是一个元组或列表,它必须包含两个整数值,(pool_padding_on_Height, pool_padding_on_Width)。否则它是一个整数的平方值。
    - **global_pooling** (bool,默认false)- 是否用全局池化。如果global_pooling = true, ``pool_size`` 和 ``pool_padding`` 将被忽略。
    - **use_cudnn** (bool,默认false)- 只在cudnn核中用,需要下载cudnn
    - **ceil_mode** (bool,默认false)- 是否用ceil函数计算输出高度和宽度。默认False。如果设为False,则使用floor函数
    - **name** (str|None) - 该层名称(可选)。若设为None,则自动为该层命名。
    - **exclusive** (bool) - 是否在平均池化模式忽略填充值。默认为True。

返回:池化结果

返回类型:变量(Variable)

抛出异常:
    - ``ValueError`` - 如果 ``pool_type`` 既不是“max”也不是“avg”
    - ``ValueError`` - 如果 ``global_pooling`` 为False并且‘pool_size’为-1
    - ``ValueError`` - 如果 ``use_cudnn`` 不是bool值

**代码示例**

.. code-block:: python

X
xsrobin 已提交
6036
    import paddle.fluid as fluid
C
Cheerego 已提交
6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132
    data = fluid.layers.data(
        name='data', shape=[3, 32, 32], dtype='float32')
    pool2d = fluid.layers.pool2d(
                  input=data,
                  pool_size=2,
                  pool_type='max',
                  pool_stride=1,
                  global_pooling=False)









.. _cn_api_fluid_layers_pool3d:

pool3d
-------------------------------

.. py:function:: paddle.fluid.layers.pool3d(input, pool_size=-1, pool_type='max', pool_stride=1, pool_padding=0, global_pooling=False, use_cudnn=True, ceil_mode=False, name=None, exclusive=True)

函数使用上述输入参数的池化配置,为三维空间添加池化操作

pooling3d操作根据input,pool_type,pool_size,strides和paddings参数计算输出。 输入(X)和输出(输出)采用NCDHW格式,其中N是批量大小,C是通道数,D,H和W分别是特征的深度,高度和宽度。 参数(ksize,strides,paddings)是三个元素。 这三个元素分别代表深度,高度和宽度。 输入(X)大小和输出(Out)大小可能不同。


例如,

输入X形为 :math:`(N, C, D_{in}, H_{in}, W_{in})` ,输出形为 :math:`(N, C, D_{out}, H_{out}, W_{out})`

当ceil_mode = false时,

.. math::

    D_{out} &= \frac{(D_{in} - ksize[0] + 2 * paddings[0])}{strides[0]} + 1\\
    H_{out} &= \frac{(H_{in} - ksize[1] + 2 * paddings[1])}{strides[2]} + 1\\
    W_{out} &= \frac{(W_{in} - ksize[2] + 2 * paddings[2])}{strides[2]} + 1

当ceil_mode = true时,

.. math::

    D_{out} &= \frac{(D_{in} - ksize[0] + 2 * paddings[0] + strides[0] -1)}{strides[0]} + 1\\
    H_{out} &= \frac{(H_{in} - ksize[1] + 2 * paddings[1] + strides[1] -1)}{strides[1]} + 1\\
    W_{out} &= \frac{(W_{in} - ksize[2] + 2 * paddings[2] + strides[2] -1)}{strides[2]} + 1

当exclusive = false时,

.. math::

    dstart &= i * strides[0] - paddings[0]\\
    dend &= dstart + ksize[0]\\
    hstart &= j * strides[1] - paddings[1]\\
    hend &= hstart + ksize[1]\\
    wstart &= k * strides[2] - paddings[2]\\
    wend &= wstart + ksize[2]\\
    Output(i ,j, k) &= \frac{sum(Input[dstart:dend, hstart:hend, wstart:wend])}{ksize[0] * ksize[1] * ksize[2]}



当exclusive = true时,

.. math::

    dstart &= max(0, i * strides[0] - paddings[0])\\
    dend &= min(D, dstart + ksize[0])\\
    hstart &= max(0, j * strides[1] - paddings[1])\\
    hend &= min(H, hstart + ksize[1])\\
    wstart &= max(0, k * strides[2] - paddings[2])\\
    wend &= min(W, wstart + ksize[2])\\
    Output(i ,j, k) &= \frac{sum(Input[dstart:dend, hstart:hend, wstart:wend])}{(dend - dstart) * (hend - hstart) * (wend - wstart)}


参数:
    - **input** (Vairable) - 池化运算的输入张量。输入张量的格式为NCDHW, N是批尺寸,C是通道数,D是特征深度,H是特征高度,W是特征宽度。
    - **pool_size** (int|list|tuple) - 池化窗口的大小。如果为元组类型,那么它应该是由三个整数组成:深度,高度,宽度。如果是int类型,它应该是一个整数的立方。
    - **pool_type** (str) - 池化类型, "max" 对应max-pooling, "avg" 对应average-pooling。
    - **pool_stride** (int|list|tuple) - 池化跨越步长。如果为元组类型,那么它应该是由三个整数组成:深度,高度,宽度。如果是int类型,它应该是一个整数的立方。
    - **pool_padding** (int|list|tuple) - 填充大小。如果为元组类型,那么它应该是由三个整数组成:深度,高度,宽度。如果是int类型,它应该是一个整数的立方。
    - **global_pooling** (bool) - 是否使用全局池化。如果global_pooling = true, ``pool_size`` 和 ``pool_padding`` 将被忽略。
    - **use_cudnn** (bool) - 是否用cudnn核,只有在cudnn库安装时有效。
    - **ceil_mode** (bool) - 是否用ceil函数计算输出高度和宽度。默认False。如果设为False,则使用floor函数。
    - **name** (str) - 该层名称(可选)。若为空,则自动为该层命名。
    - **exclusive** (bool) - 是否在平均池化模式忽略填充值。默认为True。

返回:pool3d层的输出

返回类型:变量(Variable)

**代码示例**

.. code-block:: python

X
xsrobin 已提交
6133
    import paddle.fluid as fluid
C
Cheerego 已提交
6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368
    data = fluid.layers.data(
        name='data', shape=[3, 32, 32, 32], dtype='float32')
    pool3d = fluid.layers.pool3d(
                      input=data,
                      pool_size=2,
                      pool_type='max',
                      pool_stride=1,
                      global_pooling=False)










.. _cn_api_fluid_layers_pow:

pow
-------------------------------

.. py:function:: paddle.fluid.layers.pow(x, factor=1.0, name=None)

指数激活算子(Pow Activation Operator.)

.. math::

    out = x^{factor}

参数
    - **x** (Variable) - Pow operator的输入
    - **factor** (FLOAT|1.0) - Pow的指数因子
    - **name** (str|None) -这个层的名称(可选)。如果设置为None,该层将被自动命名。

返回: 输出Pow操作符

返回类型: 输出(Variable)


**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name="x", shape=[3,10,32,32], dtype="float32")
    y = fluid.layers.pow(x, factor=2.0)






.. _cn_api_fluid_layers_prelu:

prelu
-------------------------------

.. py:function:: paddle.fluid.layers.prelu(x, mode, param_attr=None, name=None)

等式:

.. math::
    y = max(0, x) + \alpha min(0, x)

共提供三种激活方式:

.. code-block:: text

    all: 所有元素使用同一个alpha值
    channel: 在同一个通道中的元素使用同一个alpha值
    element: 每一个元素有一个独立的alpha值


参数:
          - **x** (Variable)- 输入为Tensor。
          - **mode** (string) - 权重共享模式。
          - **param_attr** (ParamAttr|None) - 可学习权重 :math:`[\alpha]` 的参数属性,可由ParamAttr创建。
          - **name** (str | None)- 这一层的名称(可选)。如果设置为None,则将自动命名这一层。

返回: 输出Tensor与输入shape相同。

返回类型:  变量(Variable)

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
    from paddle.fluid.param_attr import ParamAttr
    x = fluid.layers.data(name="x", shape=[5,10,10], dtype="float32")
    mode = 'channel'
    output = fluid.layers.prelu(
             x,mode,param_attr=ParamAttr(name='alpha'))




.. _cn_api_fluid_layers_psroi_pool:

psroi_pool
-------------------------------

.. py:function:: paddle.fluid.layers.psroi_pool(input, rois, output_channels, spatial_scale, pooled_height, pooled_width, name=None)

PSROIPool运算

区分位置的感兴趣区域池化方法(Position sensitive region of interest pooling,也称为PSROIPooling)是对输入的 "感兴趣区域"(RoI)执行按位置的average池化,并将N个按位置评分图(score map)和一个由num_rois个感兴趣区域所组成的列表作为输入。

用于R-FCN的PSROIPooling。 有关更多详细信息,请参阅 https://arxiv.org/abs/1605.06409。

参数:
    - **input** (Variable) - (Tensor),PSROIPoolOp的输入。 输入张量的格式是NCHW。 其中N是批大小batch_size,C是输入通道的数量,H是输入特征图的高度,W是特征图宽度
    - **rois** (Variable) - 要进行池化的RoI(感兴趣区域)。应为一个形状为(num_rois, 4)的二维LoDTensor,其lod level为1。给出[[x1, y1, x2, y2], ...],(x1, y1)为左上角坐标,(x2, y2)为右下角坐标。
    - **output_channels** (integer) - (int),输出特征图的通道数。 对于共C个种类的对象分类任务,output_channels应该是(C + 1),该情况仅适用于分类任务。
    - **spatial_scale** (float) - (float,default 1.0),乘法空间比例因子,用于将ROI坐标从其输入比例转换为池化使用的比例。默认值:1.0
    - **pooled_height** (integer) - (int,默认值1),池化输出的高度。默认值:1
    - **pooled_width** (integer) - (int,默认值1),池化输出的宽度。默认值:1
    - **name** (str,default None) - 此层的名称。

返回: (Tensor),PSROIPoolOp的输出是形为 (num_rois,output_channels,pooled_h,pooled_w) 的4-D Tensor。

返回类型:  变量(Variable)

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[490, 28, 28], dtype='float32')
    rois = fluid.layers.data(name='rois', shape=[4], lod_level=1, dtype='float32')
    pool_out = fluid.layers.psroi_pool(x, rois, 10, 1.0, 7, 7)





.. _cn_api_fluid_layers_py_func:

py_func
-------------------------------

.. py:function:: paddle.fluid.layers.py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None)

PyFunc运算。

用户可以使用 ``py_func`` 在Python端注册operator。 ``func`` 的输入 ``x`` 是LoDTensor,输出可以是numpy数组或LoDTensor。 Paddle将在前向部分调用注册的 ``func`` ,并在反向部分调用 ``backward_func`` (如果 ``backward_func`` 不是None)。

在调用此函数之前,应正确设置 ``out`` 的数据类型和形状。 但是,``out`` 和 ``x`` 对应梯度的数据类型和形状将自动推断而出。

``backward_func`` 的输入顺序为:前向输入x,前向输出 ``out`` 和反向输入 ``out`` 的梯度。 如果 ``out`` 的某些变量没有梯度,则输入张量在Python端将为None。

如果in的某些变量没有梯度,则用户应返回None。

此功能还可用于调试正在运行的网络,可以通过添加没有输出的py_func运算,并在func中打印输入x。

参数:
    - **func** (callable) - 前向Python函数。
    - **x** (Variable|list(Variable)|tuple(Variable)) -  func的输入。
    - **out** (Variable|list(Variable)|tuple(Variable)) -  func的输出。 Paddle无法自动推断out的形状和数据类型。 应事先创建 ``out`` 。
    - **backward_func** (callable|None) - 反向Python函数。 None意味着没有反向计算。 默认None。
    - **skip_vars_in_backward_input** (Variable|list(Variable)|tuple(Variable)) -  backward_func输入中不需要的变量。 这些变量必须是x和out中的一个。 如果设置,这些变量将不是backward_func的输入,仅在backward_func不是None时有用。 默认None。

返回: 传入的 ``out``

返回类型: out (Variable|list(Variable)|tuple(Variable))

**代码示例**:

..  code-block:: python

    import paddle.fluid as fluid
    import six

    def create_tmp_var(name, dtype, shape):
        return fluid.default_main_program().current_block().create_var(
            name=name, dtype=dtype, shape=shape)

    # Paddle C++ op提供的tanh激活函数
    # 此处仅采用tanh作为示例展示py_func的使用方法
    def tanh(x):
        return np.tanh(x)

    # 跳过前向输入x
    def tanh_grad(y, dy):
        return np.array(dy) * (1 - np.square(np.array(y)))

    def debug_func(x):
        print(x)

    def simple_net(img, label):
        hidden = img
        for idx in six.moves.range(4):
            hidden = fluid.layers.fc(hidden, size=200)
            new_hidden = create_tmp_var(name='hidden_{}'.format(idx),
                dtype=hidden.dtype, shape=hidden.shape)

            # 用户自定义的前向反向计算
            hidden = fluid.layers.py_func(func=tanh, x=hidden,
                out=new_hidden, backward_func=tanh_grad,
                skip_vars_in_backward_input=hidden)

            # 用户自定义的调试层,可以打印出变量细则
            fluid.layers.py_func(func=debug_func, x=hidden, out=None)

        prediction = fluid.layers.fc(hidden, size=10, act='softmax')
        loss = fluid.layers.cross_entropy(input=prediction, label=label)
        return fluid.layers.mean(loss)






.. _cn_api_fluid_layers_random_crop:

random_crop
-------------------------------

.. py:function:: paddle.fluid.layers.random_crop(x, shape, seed=None)

该operator对batch中每个实例进行随机裁剪。这意味着每个实例的裁剪位置不同,裁剪位置由均匀分布随机生成器决定。所有裁剪的实例都具有相同的shape,由参数shape决定。

参数:
    - **x(Variable)** - 一组随机裁剪的实例
    - **shape(int)** - 裁剪实例的形状
    - **seed(int|变量|None)** - 默认情况下,随机种子从randint(-65536,-65536)中取得

返回: 裁剪后的batch

**代码示例**:

..  code-block:: python

X
xsrobin 已提交
6369
   import paddle.fluid as fluid
C
Cheerego 已提交
6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398
   img = fluid.layers.data("img", [3, 256, 256])
   cropped_img = fluid.layers.random_crop(img, shape=[3, 224, 224])





.. _cn_api_fluid_layers_rank:

rank
-------------------------------

.. py:function::  paddle.fluid.layers.rank(input)

排序层

返回张量的维数,一个数据类型为int32的0-D Tensor。

参数:
    - **input** (Variable):输入变量

返回:输入变量的秩

返回类型: 变量(Variable)

**代码示例**

.. code-block:: python

X
xsrobin 已提交
6399
       import paddle.fluid as fluid
C
Cheerego 已提交
6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443
       input = layers.data(
            name="input", shape=[3, 100, 100], dtype="float32")
       rank = layers.rank(input) # 4


.. _cn_api_fluid_layers_rank_loss:

rank_loss
-------------------------------


.. py:function::  paddle.fluid.layers.rank_loss(label, left, right, name=None)

`RankNet <http://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf>`_ 是一个成对的
排序模型,训练样本由一对文档组成:A和B。标签P表示a的排名是否高于B:

P 的取值可为: {0, 1} 或 {0, 0.5, 1}, 其中,0.5表示输入的两文档排序相同。

排序的损失函数有三个输入:left(o_i)、right(o_j) 和 label (P\_{i,j})。输入分别表示RankNet对文档A、B的输出得分和标签p的值。由下式计算输入的排序损失C\_{i,j}:

.. math::

   C_{i,j} &= -\tilde{P_{ij}} * o_{i,j} + \log(1 + e^{o_{i,j}}) \\
      o_{i,j} &=  o_i - o_j  \\
      \tilde{P_{i,j}} &= \left \{0, 0.5, 1 \right \} \ or \ \left \{0, 1 \right \}

排序损失层的输入带有batch_size (batch_size >= 1)

参数:
  - **label** (Variable):A的排名是否高于B
  - **left** (Variable):RankNet对doc A的输出分数
  - **right** (Variable):RankNet对doc B的输出分数
  - **name** (str|None):此层的名称(可选)。如果没有设置,层将自动命名。

返回:rank loss的值

返回类型: list

抛出异常: ``ValueError`` - label, left, 和right至少有一者不是variable变量类型。

**代码示例**

.. code-block:: python

X
xsrobin 已提交
6444
    import paddle.fluid as fluid
C
Cheerego 已提交
6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479
    label = fluid.layers.data(name="label", shape=[-1, 1], dtype="float32")
    left = fluid.layers.data(name="left", shape=[-1, 1], dtype="float32")
    right = fluid.layers.data(name="right", shape=[-1, 1], dtype="float32")
    out = fluid.layers.rank_loss(label, left, right)



.. _cn_api_fluid_layers_reduce_all:

reduce_all
-------------------------------

.. py:function:: paddle.fluid.layers.reduce_all(input, dim=None, keep_dim=False, name=None)

计算给定维度上张量(Tensor)元素的与逻辑。

参数:
          - **input** (Variable):输入变量为Tensor或LoDTensor。
          - **dim** (list | int | None):与逻辑运算的维度。如果为None,则计算所有元素的与逻辑并返回包含单个元素的Tensor变量,否则必须在  :math:`[−rank(input),rank(input)]` 范围内。如果 :math:`dim [i] <0` ,则维度将减小为 :math:`rank+dim[i]` 。
          - **keep_dim** (bool | False):是否在输出Tensor中保留减小的维度。除非 ``keep_dim`` 为true,否则结果张量的维度将比输入张量小。
          - **name** (str | None):这一层的名称(可选)。如果设置为None,则将自动命名这一层。

返回:  减少维度之后的Tensor变量。

返回类型:  变量(Variable)

**代码示例**

..  code-block:: python
     
     
        # x是一个布尔型Tensor,元素如下:
        #    [[True, False]
        #     [True, True]]
        # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
X
xsrobin 已提交
6480 6481 6482
        import paddle.fluid as fluid
        import paddle.fluid.layers as layers
        import numpy as np
C
Cheerego 已提交
6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516
        fluid.layers.reduce_all(x)  # False
        fluid.layers.reduce_all(x, dim=0)  # [True, False]
        fluid.layers.reduce_all(x, dim=-1)  # [False, True]
        fluid.layers.reduce_all(x, dim=1,
                                 keep_dim=True)  # [[False], [True]]
     
.. _cn_api_fluid_layers_reduce_any:

reduce_any
-------------------------------

.. py:function:: paddle.fluid.layers.reduce_any(input, dim=None, keep_dim=False, name=None)

计算给定维度上张量(Tensor)元素的或逻辑。     

参数:
          - **input** (Variable):输入变量为Tensor或LoDTensor。
          - **dim** (list | int | None):或逻辑运算的维度。如果为None,则计算所有元素的或逻辑并返回仅包含单个元素的Tensor变量,否则必须在  :math:`[−rank(input),rank(input)]` 范围内。如果 :math:`dim [i] <0` ,则维度将减小为 :math:`rank+dim[i]` 。
          - **keep_dim** (bool | False):是否在输出Tensor中保留减小的维度。除非 ``keep_dim`` 为true,否则结果张量的维度将比输入张量小。
          - **name** (str | None):这一层的名称(可选)。如果设置为None,则将自动命名这一层。

返回:  减少维度之后的Tensor变量。

返回类型:  变量(Variable)

**代码示例**

..  code-block:: python
     
     
        # x是一个布尔型Tensor,元素如下:
        #    [[True, False]
        #     [False, False]]
        # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
X
xsrobin 已提交
6517 6518 6519
        import paddle.fluid as fluid
        import paddle.fluid.layers as layers
        import numpy as np
C
Cheerego 已提交
6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811
        fluid.layers.reduce_any(x)  # True
        fluid.layers.reduce_any(x, dim=0)  # [True, False]
        fluid.layers.reduce_any(x, dim=-1)  # [True, False]
        fluid.layers.reduce_any(x, dim=1,
                                 keep_dim=True)  # [[True], [False]]






.. _cn_api_fluid_layers_reduce_max:

reduce_max
-------------------------------

.. py:function:: paddle.fluid.layers.reduce_max(input, dim=None, keep_dim=False, name=None)

计算给定维度上张量(Tensor)元素最大值。

参数:
          - **input** (Variable):输入变量为Tensor或LoDTensor。
          - **dim** (list | int | None):函数运算的维度。如果为None,则计算所有元素中的最大值并返回单个元素的Tensor变量,否则必须在  :math:`[−rank(input),rank(input)]` 范围内。如果 :math:`dim [i] <0` ,则维度将减小为 :math:`rank+dim[i]` 。
          - **keep_dim** (bool | False):是否在输出Tensor中保留减小的维度。除非 ``keep_dim`` 为true,否则结果张量将比输入少一个维度。
          - **name** (str | None):这一层的名称(可选)。如果设置为None,则将自动命名这一层。

返回:  运算、减少维度之后的Tensor变量。

返回类型:  变量(Variable)

**代码示例**

..  code-block:: python

      import paddle.fluid as fluid
      # x是一个Tensor,元素如下:
      #    [[0.2, 0.3, 0.5, 0.9]
      #     [0.1, 0.2, 0.6, 0.7]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
      x = fluid.layers.data(name='x', shape=[4, 2], dtype='float32')
      fluid.layers.reduce_max(x)  # [0.9]
      fluid.layers.reduce_max(x, dim=0)  # [0.2, 0.3, 0.6, 0.9]
      fluid.layers.reduce_max(x, dim=-1)  # [0.9, 0.7]
      fluid.layers.reduce_max(x, dim=1, keep_dim=True)  # [[0.9], [0.7]]

      # y是一个shape为[2, 2, 2]的Tensor,元素如下:
      #      [[[1.0, 2.0], [3.0, 4.0]],
      #      [[5.0, 6.0], [7.0, 8.0]]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
      y = fluid.layers.data(name='y', shape=[2, 2, 2], dtype='float32')
      fluid.layers.reduce_max(y, dim=[1, 2]) # [4.0, 8.0]
      fluid.layers.reduce_max(y, dim=[0, 1]) # [7.0, 8.0]










.. _cn_api_fluid_layers_reduce_mean:

reduce_mean
-------------------------------

.. py:function:: paddle.fluid.layers.reduce_mean(input, dim=None, keep_dim=False, name=None)

计算给定维度上张量(Tensor)元素平均值。

参数:
          - **input** (Variable):输入变量为Tensor或LoDTensor。
          - **dim** (list | int | None):函数运算的维度。如果为None,则对输入的所有元素求平均值并返回单个元素的Tensor变量,否则必须在  :math:`[−rank(input),rank(input)]` 范围内。如果 :math:`dim [i] <0` ,则维度将减小为 :math:`rank+dim[i]` 。
          - **keep_dim** (bool | False):是否在输出Tensor中保留减小的维度。除非 ``keep_dim`` 为true,否则结果张量将比输入少一个维度。
          - **name** (str | None):这一层的名称(可选)。如果设置为None,则将自动命名这一层。

返回:  运算、减少维度之后的Tensor变量。

返回类型:  变量(Variable)

**代码示例**

..  code-block:: python

      import paddle.fluid as fluid
      # x是一个Tensor,元素如下:
      #    [[0.2, 0.3, 0.5, 0.9]
      #     [0.1, 0.2, 0.6, 0.7]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
      x = fluid.layers.data(name='x', shape=[4, 2], dtype='float32')
      fluid.layers.reduce_mean(x)  # [0.4375]
      fluid.layers.reduce_mean(x, dim=0)  # [0.15, 0.25, 0.55, 0.8]
      fluid.layers.reduce_mean(x, dim=-1)  # [0.475, 0.4]
      fluid.layers.reduce_mean(x, dim=1, keep_dim=True)  # [[0.475], [0.4]]

      # y是一个shape为[2, 2, 2]的Tensor元素如下:
      #      [[[1.0, 2.0], [3.0, 4.0]],
      #      [[5.0, 6.0], [7.0, 8.0]]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。。
      y = fluid.layers.data(name='y', shape=[2, 2, 2], dtype='float32')
      fluid.layers.reduce_mean(y, dim=[1, 2]) # [2.5, 6.5]
      fluid.layers.reduce_mean(y, dim=[0, 1]) # [4.0, 5.0]










.. _cn_api_fluid_layers_reduce_min:

reduce_min
-------------------------------

.. py:function:: paddle.fluid.layers.reduce_min(input, dim=None, keep_dim=False, name=None)

计算给定维度上张量元素的最小值。

参数:
          - **input** (Variable):输入变量为Tensor或LoDTensor。
          - **dim** (list | int | None):函数运算的维度。如果为None,则对输入的所有元素做差并返回单个元素的Tensor变量,否则必须在  :math:`[−rank(input),rank(input)]` 范围内。如果 :math:`dim [i] <0` ,则维度将减小为 :math:`rank+dim[i]` 。
          - **keep_dim** (bool | False):是否在输出Tensor中保留减小的维度。除非 ``keep_dim`` 为true,否则结果张量将比输入少一个维度。
          - **name** (str | None):这一层的名称(可选)。如果设置为None,则将自动命名这一层。

返回:  运算、减少维度之后的Tensor变量。

返回类型:  变量(Variable)

**代码示例**

..  code-block:: python

      import paddle.fluid as fluid
      # x是一个Tensor,元素如下:
      #    [[0.2, 0.3, 0.5, 0.9]
      #     [0.1, 0.2, 0.6, 0.7]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
      x = fluid.layers.data(name='x', shape=[4, 2], dtype='float32')
      fluid.layers.reduce_min(x)  # [0.1]
      fluid.layers.reduce_min(x, dim=0)  # [0.1, 0.2, 0.5, 0.7]
      fluid.layers.reduce_min(x, dim=-1)  # [0.2, 0.1]
      fluid.layers.reduce_min(x, dim=1, keep_dim=True)  # [[0.2], [0.1]]

      # y是一个shape为[2, 2, 2]的Tensor元素如下:
      #      [[[1.0, 2.0], [3.0, 4.0]],
      #      [[5.0, 6.0], [7.0, 8.0]]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
      y = fluid.layers.data(name='y', shape=[2, 2, 2], dtype='float32')
      fluid.layers.reduce_min(y, dim=[1, 2]) # [1.0, 5.0]
      fluid.layers.reduce_min(y, dim=[0, 1]) # [1.0, 2.0]










.. _cn_api_fluid_layers_reduce_prod:

reduce_prod
-------------------------------

.. py:function:: paddle.fluid.layers.reduce_prod(input, dim=None, keep_dim=False, name=None)

计算给定维度上张量(Tensor)元素乘积。

参数:
          - **input** (Variable):输入变量为Tensor或LoDTensor。
          - **dim** (list | int | None):函数运算的维度。如果为None,则将输入的所有元素相乘并返回单个元素的Tensor变量,否则必须在  :math:`[−rank(input),rank(input)]` 范围内。如果 :math:`dim [i] <0` ,则维度将减小为 :math:`rank+dim[i]` 。
          - **keep_dim** (bool | False):是否在输出Tensor中保留减小的维度。除非 ``keep_dim`` 为true,否则结果张量将比输入少一个维度。
          - **name** (str | None):这一层的名称(可选)。如果设置为None,则将自动命名这一层。

返回:  运算、减少维度之后的Tensor变量。

返回类型:  变量(Variable)

**代码示例**

..  code-block:: python

      import paddle.fluid as fluid
      # x是一个Tensor,元素如下:
      #    [[0.2, 0.3, 0.5, 0.9]
      #     [0.1, 0.2, 0.6, 0.7]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
      x = fluid.layers.data(name='x', shape=[4, 2], dtype='float32')
      fluid.layers.reduce_prod(x)  # [0.0002268]
      fluid.layers.reduce_prod(x, dim=0)  # [0.02, 0.06, 0.3, 0.63]
      fluid.layers.reduce_prod(x, dim=-1)  # [0.027, 0.0084]
      fluid.layers.reduce_prod(x, dim=1,
                               keep_dim=True)  # [[0.027], [0.0084]]

      # y 是一个shape为[2, 2, 2]的Tensor元素如下:
      #      [[[1.0, 2.0], [3.0, 4.0]],
      #      [[5.0, 6.0], [7.0, 8.0]]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
      y = fluid.layers.data(name='y', shape=[2, 2, 2], dtype='float32')
      fluid.layers.reduce_prod(y, dim=[1, 2]) # [24.0, 1680.0]
      fluid.layers.reduce_prod(y, dim=[0, 1]) # [105.0, 384.0]










.. _cn_api_fluid_layers_reduce_sum:

reduce_sum
-------------------------------

.. py:function:: paddle.fluid.layers.reduce_sum(input, dim=None, keep_dim=False, name=None)

计算给定维度上张量(Tensor)元素之和。

参数:
          - **input** (Variable)- 输入变量为Tensor或LoDTensor。
          - **dim** (list | int | None)- 求和运算的维度。如果为None,则对输入的所有元素求和并返回单个元素的Tensor变量,否则必须在  :math:`[−rank(input),rank(input)]` 范围内。如果 :math:`dim [i] <0` ,则维度将减小为 :math:`rank+dim[i]` 。
          - **keep_dim** (bool | False)- 是否在输出Tensor中保留减小的维度。除非 ``keep_dim`` 为true,否则结果张量将比输入少一个维度。
          - **name** (str | None)- 这一层的名称(可选)。如果设置为None,则将自动命名这一层。

返回:  运算、减少维度之后的Tensor变量。

返回类型:  变量(Variable)

**代码示例**

..  code-block:: python

      import paddle.fluid as fluid
      # x是一个Tensor,元素如下:
      #    [[0.2, 0.3, 0.5, 0.9]
      #     [0.1, 0.2, 0.6, 0.7]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
      x = fluid.layers.data(name='x', shape=[4, 2], dtype='float32')
      fluid.layers.reduce_sum(x)  # [3.5]
      fluid.layers.reduce_sum(x, dim=0)  # [0.3, 0.5, 1.1, 1.6]
      fluid.layers.reduce_sum(x, dim=-1)  # [1.9, 1.6]
      fluid.layers.reduce_sum(x, dim=1, keep_dim=True)  # [[1.9], [1.6]]

      # y 是一个shape为[2, 2, 2]的Tensor元素如下:
      #      [[[1, 2], [3, 4]],
      #      [[5, 6], [7, 8]]]
      # 接下来的示例中,我们在每处函数调用后面都标注出了它的结果张量。
      y = fluid.layers.data(name='y', shape=[2, 2, 2], dtype='float32')
      fluid.layers.reduce_sum(y, dim=[1, 2]) # [10, 26]
      fluid.layers.reduce_sum(y, dim=[0, 1]) # [16, 20]










.. _cn_api_fluid_layers_relu:

relu
-------------------------------

.. py:function:: paddle.fluid.layers.relu(x, name=None)

Relu接受一个输入数据(张量),输出一个张量。将线性函数y = max(0, x)应用到张量中的每个元素上。

.. math::
              \\Out=\max(0,x)\\


参数:
  - **x** (Variable):输入张量。
  - **name** (str|None,默认None) :如果设置为None,该层将自动命名。

返回: 与输入形状相同的输出张量。

返回类型: 变量(Variable)

**代码示例**:

..  code-block:: python
      
X
xsrobin 已提交
6812
    import paddle.fluid as fluid
C
Cheerego 已提交
6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852
    x = fluid.layers.data(name="x", shape=[3, 4], dtype="float32")
    output = fluid.layers.relu(x)










.. _cn_api_fluid_layers_relu6:

relu6
-------------------------------

.. py:function:: paddle.fluid.layers.relu6(x, threshold=6.0, name=None)

relu6激活算子(Relu6 Activation Operator)

.. math::

    \\out=min(max(0, x), 6)\\


参数:
    - **x** (Variable) - Relu6 operator的输入
    - **threshold** (FLOAT|6.0) - Relu6的阈值
    - **name** (str|None) -这个层的名称(可选)。如果设置为None,该层将被自动命名。

返回: Relu6操作符的输出

返回类型: 输出(Variable)


**代码示例:**

.. code-block:: python

X
xsrobin 已提交
6853
    import paddle.fluid as fluid
C
Cheerego 已提交
6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906
    x = fluid.layers.data(name="x", shape=[3,10,32,32], dtype="float32")
    y = fluid.layers.relu6(x, threshold=6.0)







.. _cn_api_fluid_layers_reshape:

reshape
-------------------------------

.. py:function::  paddle.fluid.layers.reshape(x, shape, actual_shape=None, act=None, inplace=False, name=None)

保持输入张量数据不变的情况下,改变张量的形状。

目标形状可由 ``shape`` 或 ``actual_shape`` 给出。``shape`` 是一个整数列表,而 ``actual_shape`` 是一个张量变量。
当两个属性同时被指定时,``actual_shape`` 的优先级高于 ``shape`` ,但在编译时仍然应该正确地设置 ``shape`` 以保证形状推断。

在指定目标shape时存在一些技巧:

.. code-block:: text

  1. -1表示这个维度的值是从x的元素总数和剩余维度推断出来的。因此,有且只有一个维度可以被设置为-1。
  2. 0表示实际的维数是从x的对应维数中复制出来的,因此shape中0的索引值不能超过秩(x)。


这里有一些例子来解释它们:

.. code-block:: text

  1. 给定一个形状为[2,4,6]的三维张量x,目标形状为[6,8], ``reshape`` 将x变换为形状为[6,8]的二维张量,且x的数据保持不变。
  2. 给定一个形状为[2,4,6]的三维张量x,指定的目标形状为[2,3,-1,2], ``reshape``将x变换为形状为[2,3,4,2]的4- d张量,不改变x的数据。在这种情况下,目标形状的一个维度被设置为-1,这个维度的值是从x的元素总数和剩余维度推断出来的。
  3. 给定一个形状为[2,4,6]的三维张量x,目标形状为[- 1,0,3,2],整形算子将x变换为形状为[2,4,3,2]的四维张量,使x的数据保持不变。在这种情况下,0意味着实际的维值将从x的对应维数中复制,-1位置的维度由x的元素总数和剩余维度计算得来。

参数:
  - **x** (variable) - 输入张量
  - **shape** (list) - 新的形状。新形状最多只能有一个维度为-1。
  - **actual_shape** (variable) - 一个可选的输入。如果提供,则根据 ``actual_shape`` 进行 reshape,而不是指定 ``shape`` 。也就是说,actual_shape具有比shape更高的优先级。
  - **act** (str) - 对reshpe后的tensor变量执行非线性激活
  - **inplace** (bool) - 如果 ``inplace`` 为True,则 ``layers.reshape`` 的输入和输出是同一个变量,否则, ``layers.reshape`` 的输入和输出是不同的变量。请注意,如果x作为多个层的输入,则 ``inplace`` 必须为False。
  - **name** (str) -  可选变量,此层的名称

返回:如果 ``act`` 为 ``None``,返回reshape后的tensor变量。如果 ``inplace`` 为 ``False`` ,将返回一个新的Tensor变量,否则,将改变x自身。如果 ``act`` 不是 ``None`` ,则返回激活的张量变量。

抛出异常:``TypeError`` - 如果 actual_shape 既不是变量也不是None

**代码示例**

.. code-block:: python

X
xsrobin 已提交
6907
  import paddle.fluid as fluid
C
Cheerego 已提交
6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990
  data = fluid.layers.data(
      name='data', shape=[2, 4, 6], dtype='float32')
  reshaped = fluid.layers.reshape(
      x=data, shape=[-1, 0, 3, 2], inplace=True)










.. _cn_api_fluid_layers_resize_bilinear:

resize_bilinear
-------------------------------

.. py:function:: paddle.fluid.layers.resize_bilinear(input, out_shape=None, scale=None, name=None, actual_shape=None, align_corners=True, align_mode=1)


根据指定的out_shape执行双线性插值调整输入大小,输出形状按优先级由actual_shape、out_shape和scale指定。

双线性插值是对线性插值的扩展,即二维变量方向上(如h方向和w方向)插值。关键思想是先在一个方向上执行线性插值,然后再在另一个方向上执行线性插值。

详情请参阅 `维基百科 <https://en.wikipedia.org/wiki/Bilinear_interpolation>`_ 。

align_corners和align_mode是可选参数,插值的计算方法可以由它们选择。


::

    Example:

      For scale:

        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)

    Bilinear interpolation:

      if align_corners = False , align_mode = 0

          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:

          H_out = (H_{in}+0.5) * scale_{factor} - 0.5
          W_out = (W_{in}+0.5) * scale_{factor} - 0.5


      else:

          input : (N,C,H_in,W_in)
          output: (N,C,H_out,W_out) where:

          H_out = H_{in} * scale_{factor}
          W_out = W_{in} * scale_{factor}



参数:
    - **input** (Variable) - 双线性插值的输入张量,是一个shape为(N x C x h x w)的4d张量。
    - **out_shape** (list|tuple|Variable|None) - 调整双线性层的输出形状,形式为(out_h, out_w)。默认值:None。
    - **scale** (float|None) - 用于输入高度或宽度的乘数因子。out_shape和scale至少要设置一个。out_shape的优先级高于scale。默认值:None。
    - **name** (str|None) - 输出变量名。
    - **actual_shape** (Variable) - 可选输入,用于动态指定输出形状。如果指定actual_shape,图像将根据给定的形状调整大小,而不是根据指定形状的 :code:`out_shape` 和 :code:`scale` 进行调整。也就是说, :code:`actual_shape` 具有最高的优先级。如果希望动态指定输出形状,建议使用 :code:`actual_shape` 而不是 :code:`out_shape` 。在使用actual_shape指定输出形状时,还需要设置out_shape和scale之一,否则在图形构建阶段会出现错误。默认值:None
    - **align_corners** (bool)- 一个可选的bool型参数,如果为True,则将输入和输出张量的4个角落像素的中心对齐,并保留角点像素的值。 默认值:True
    - **align_mode** (int)- 双线性插值的可选项。 可以是'0'代表src_idx = scale *(dst_indx + 0.5)-0.5;可以为'1' ,代表src_idx = scale * dst_index。


返回: 插值运算的输出张量,其各维度是(N x C x out_h x out_w)


**代码示例**

.. code-block:: python
  
X
xsrobin 已提交
6991
  import paddle.fluid as fluid
C
Cheerego 已提交
6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063
  input = fluid.layers.data(name="input", shape=[3,6,9], dtype="float32")
  out = fluid.layers.resize_bilinear(input, out_shape=[12, 12])








.. _cn_api_fluid_layers_resize_nearest:

resize_nearest
-------------------------------

.. py:function:: paddle.fluid.layers.resize_nearest(input, out_shape=None, scale=None, name=None, actual_shape=None, align_corners=True)

该层对输入进行放缩,在第三维(高度方向)和第四维(宽度方向)进行最邻近插值(nearest neighbor interpolation)操作。
输出形状按优先级顺序依据 ``actual_shape`` , ``out_shape`` 和 ``scale`` 而定。

::

    Example:

          For scale:

            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)


          Nearest neighbor interpolation:

          if align_corners = False

              input : (N,C,H_in,W_in)
              output: (N,C,H_out,W_out) where:

              H_out = \left \lfloor {H_{in} * scale_{}factor}} \right \rfloor
              W_out = \left \lfloor {W_{in} * scale_{}factor}} \right \rfloor

          else:
              align_corners = True

              input : (N,C,H_in,W_in)
              output: (N,C,H_out,W_out) where:

              H_out = round(H_{in} * scale_{factor})
              W_out = round(W_{in} * scale_{factor})

最邻近插值的详细介绍请参照: `Wiki Nearest-neighbor interpolation <https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation>`_



参数:
  - **input** (Variable) – 插值运算的输入张量, 是一个形为 (N,C,H,W) 的四维张量
  - **out_shape** (list|tuple|Variable|None) – 调整最近邻层的输出形状,形式为(out_h, out_w)。默认值:None。
  - **scale** (float|None) – 输入高、宽的乘法器。 ``out_shape`` 和 ``scale`` 二者至少设置其一。 ``out_shape`` 具有比 ``scale`` 更高的优先级。 默认: None
  - **name** (str|None) – 输出变量的命名
  - **actual_shape** (Variable) – 可选输入, 动态设置输出张量的形状。 如果提供该值, 图片放缩会依据此形状进行, 而非依据 ``out_shape`` 和 ``scale`` 。 即为, ``actual_shape`` 具有最高的优先级。 如果想动态指明输出形状,推荐使用 ``actual_shape`` 取代 ``out_shape`` 。 当使用 ``actual_shape`` 来指明输出形状, ``out_shape`` 和 ``scale`` 也应该进行设置, 否则在图形生成阶段将会报错。默认: None
  - **align_corners** (bool)- 一个可选的bool型参数,如果为True,则将输入和输出张量的4个角落像素的中心对齐,并保留角点像素的值。 默认值:True

返回:插值运算的输出张量,是一个形为 [N,C,H,W] 的四维张量

**代码示例**

..  code-block:: python
    
X
xsrobin 已提交
7064
    import paddle.fluid as fluid
C
Cheerego 已提交
7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108
    input = fluid.layers.data(name="input", shape=[3,6,9], dtype="float32")
    out = fluid.layers.resize_nearest(input, out_shape=[12, 12])










.. _cn_api_fluid_layers_roi_align:

roi_align
-------------------------------

.. py:function:: paddle.fluid.layers.roi_align(input, rois, pooled_height=1, pooled_width=1, spatial_scale=1.0, sampling_ratio=-1, name=None)

**实现RoIAlign操作。**

Region of Interests align(直译:有意义、有价值选区对齐) 用于实现双线性插值,它可以将不均匀大小的输入
变为固定大小的特征图(feature map)。

该运算通过 ``pooled_width`` 和 ``pooled_height`` 将每个推荐区域划分为等大小分块。位置保持不变。

在每个RoI框中,四个常取样位置会通过双线性插值直接计算。输出为这四个位置的平均值从而解决不对齐问题。

参数:
  - **input** (Variable) – (Tensor) 该运算的的输入张量,形为(N,C,H,W)。其中 N 为batch大小, C 为输入通道的个数, H 特征高度, W 特征宽度
  - **rois** (Variable) – 待池化的ROIs (Regions of Interest)
  - **pooled_height** (integer) – (默认为1), 池化后的输出高度
  - **pooled_width** (integer) – (默认为1), 池化后的输出宽度
  - **spatial_scale** (float) – (默认为1.0),乘法性质空间标尺因子,池化时,将RoI坐标变换至运算采用的标度
  - **sampling_ratio** (intger) – (默认为-1),插值格中采样点的数目。 如果它 <=0, 它们将自适应 ``roi_width`` 和 ``pooled_w`` , 在高度上也是同样的道理。

返回:一个形为 (num_rois, channels, pooled_h, pooled_w) 的四维张量

返回类型:Variable

**代码示例**

..  code-block:: python

X
xsrobin 已提交
7109
    import paddle.fluid as fluid
C
Cheerego 已提交
7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306
    x = fluid.layers.data(
            name='data', shape=[256, 32, 32], dtype='float32')
    rois = fluid.layers.data(
            name='rois', shape=[4], dtype='float32')
    align_out = fluid.layers.roi_align(input=x,
                                       rois=rois,
                                       pooled_height=7,
                                       pooled_width=7,
                                       spatial_scale=0.5,
                                       sampling_ratio=-1)











.. _cn_api_fluid_layers_roi_pool:

roi_pool
-------------------------------

.. py:function:: paddle.fluid.layers.roi_pool(input, rois, pooled_height=1, pooled_width=1, spatial_scale=1.0)


roi池化是对非均匀大小的输入执行最大池化,以获得固定大小的特征映射(例如7*7)。

该operator有三个步骤:

    1. 用pooled_width和pooled_height将每个区域划分为大小相等的部分
    2. 在每个部分中找到最大的值
    3. 将这些最大值复制到输出缓冲区

Faster-RCNN.使用了roi池化。roi关于roi池化请参考 https://stackoverflow.com/questions/43430056/what-is-roi-layer-in-fast-rcnn

参数:
    - **input** (Variable) - 张量,ROIPoolOp的输入。输入张量的格式是NCHW。其中N为batch大小,C为输入通道数,H为特征高度,W为特征宽度
    - **roi** (Variable) -  roi区域。
    - **pooled_height** (integer) - (int,默认1),池化输出的高度。默认:1
    - **pooled_width** (integer) -  (int,默认1) 池化输出的宽度。默认:1
    - **spatial_scale** (float) - (float,默认1.0),用于将ROI coords从输入比例转换为池化时使用的比例。默认1.0

返回: (张量),ROIPoolOp的输出是一个shape为(num_rois, channel, pooled_h, pooled_w)的4d张量。

返回类型: 变量(Variable)


**代码示例**

..  code-block:: python

  import paddle.fluid as fluid
     
  x = fluid.layers.data(
            name='x', shape=[8, 112, 112], dtype='float32')
  rois = fluid.layers.data(
            name='roi', shape=[4], lod_level=1, dtype='float32')
  pool_out = fluid.layers.roi_pool(
            input=x,
            rois=rois,
            pooled_height=7,
            pooled_width=7,
            spatial_scale=1.0)









.. _cn_api_fluid_layers_row_conv:

row_conv
-------------------------------

.. py:function:: paddle.fluid.layers.row_conv(input, future_context_size, param_attr=None, act=None)

行卷积(Row-convolution operator)称为超前卷积(lookahead convolution)。下面关于DeepSpeech2的paper中介绍了这个operator

    `<http://www.cs.cmu.edu/~dyogatam/papers/wang+etal.iclrworkshop2016.pdf>`_

双向的RNN在深度语音模型中很有用,它通过对整个序列执行正向和反向传递来学习序列的表示。然而,与单向RNNs不同的是,在线部署和低延迟设置中,双向RNNs具有难度。超前卷积将来自未来子序列的信息以一种高效的方式进行计算,以改进单向递归神经网络。 row convolution operator 与一维序列卷积不同,计算方法如下:

给定输入序列长度为 :math:`t` 的输入序列 :math:`X` 和输入维度 :math:`D` ,以及一个大小为 :math:`context * D` 的滤波器 :math:`W` ,输出序列卷积为:

.. math::
    out_i = \sum_{j=i}^{i+context-1} X_{j} · W_{j-i}

公式中:
    - :math:`out_i` : 第i行输出变量形为[1, D].
    - :math:`context` : 下文(future context)大小
    - :math:`X_j` : 第j行输出变量,形为[1,D]
    - :math:`W_{j-i}` : 第(j-i)行参数,其形状为[1,D]。

详细请参考 `设计文档  <https://github.com/PaddlePaddle/Paddle/issues/2228#issuecomment-303903645>`_  。

参数:
    - **input** (Variable) -- 输入是一个LodTensor,它支持可变时间长度的输入序列。这个LodTensor的内部张量是一个具有形状(T x N)的矩阵,其中T是这个mini batch中的总的timestep,N是输入数据维数。
    - **future_context_size** (int) -- 下文大小。请注意,卷积核的shape是[future_context_size + 1, D]。
    - **param_attr** (ParamAttr) --  参数的属性,包括名称、初始化器等。
    - **act** (str) -- 非线性激活函数。

返回: 输出(Out)是一个LodTensor,它支持可变时间长度的输入序列。这个LodTensor的内部量是一个形状为 T x N 的矩阵,和X的 shape 一样。


**代码示例**

..  code-block:: python

  import paddle.fluid as fluid

  x = fluid.layers.data(name='x', shape=[16],
                        dtype='float32', lod_level=1)
  out = fluid.layers.row_conv(input=x, future_context_size=2)


.. _cn_api_fluid_layers_sampled_softmax_with_cross_entropy:

sampled_softmax_with_cross_entropy
----------------------------------------------

.. py:function:: paddle.fluid.layers.sampled_softmax_with_cross_entropy(logits, label, num_samples, num_true=1, remove_accidental_hits=True, use_customized_samples=False, customized_samples=None, customized_probabilities=None, seed=0)

**Sampled Softmax With Cross Entropy Operator**

对于较大的输出类,采样的交叉熵损失Softmax被广泛地用作输出层。该运算符为所有示例采样若干个样本,并计算每行采样张量的SoftMax标准化值,然后计算交叉熵损失。

由于此运算符在内部对逻辑执行SoftMax,因此它需要未分级的逻辑。此运算符不应与SoftMax运算符的输出一起使用,因为这样会产生不正确的结果。

对于T真标签(T>=1)的示例,我们假设每个真标签的概率为1/T。对于每个样本,使用对数均匀分布生成S个样本。真正的标签与这些样本连接起来,形成每个示例的T+S样本。因此,假设逻辑的形状是[N x K],样本的形状是[N x(T+S)]。对于每个取样标签,计算出一个概率,对应于[Jean et al., 2014]( `http://arxiv.org/abs/1412.2007 <https://arxiv.org/abs/1412.2007>`_ )中的Q(y|x)。

根据采样标签对逻辑进行采样。如果remove_accidental_hits为“真”,如果sample[i, j] 意外匹配“真”标签,则相应的sampled_logits[i, j]减去1e20,使其SoftMax结果接近零。然后用logQ(y|x)减去采样的逻辑,这些采样的逻辑和重新索引的标签被用来计算具有交叉熵的SoftMax。

参数:
        - **logits** (Variable)- 非比例对数概率,是一个二维张量,形状为[N x K]。N是批大小,K是类别号。
        - **label** (Variable)- 基本事实,是一个二维张量。label是一个张量<int64>,其形状为[N x T],其中T是每个示例的真实标签数。
        - **num_samples** (int)- 每个示例的数目num_samples应该小于类的数目。
        - **num_true** (int)- 每个训练实例的目标类别总数。
        - **remove_accidental_hits** (bool)- 指示采样时是否删除意外命中的标签。如果为真,如果一个sample[i,j]意外地碰到了真标签,那么相应的sampled_logits[i,j]将被减去1e20,使其SoftMax结果接近零。默认值为True。
        - **use_customized_samples** (bool)- 是否使用自定义样本和可能性对logits进行抽样。
        - **customized_samples** (Variable)- 用户定义的示例,它是一个具有形状[N, T + S]的二维张量。S是num_samples,T是每个示例的真标签数。
        - **customized_probabilities** (Variable)- 用户定义的样本概率,与customized_samples形状相同的二维张量。
        - **seed** (int)- 用于生成随机数的随机种子,在采样过程中使用。默认值为0。

返回:交叉熵损失,是一个二维张量,形状为[N x 1]。

返回类型:Variable

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid

    input = fluid.layers.data(name='data', shape=[256], dtype='float32')
    label = fluid.layers.data(name='label', shape=[5], dtype='int64')
    fc = fluid.layers.fc(input=input, size=100)
    out = fluid.layers.sampled_softmax_with_cross_entropy(
              logits=fc, label=label, num_samples=25)







.. _cn_api_fluid_layers_sampling_id:

sampling_id
-------------------------------

.. py:function:: paddle.fluid.layers.sampling_id(x, min=0.0, max=1.0, seed=0, dtype='float32')

sampling_id算子。用于从输入的多项分布中对id进行采样的图层。为一个样本采样一个id。

参数:
        - **x** (Variable)- softmax的输入张量(Tensor)。2-D形状[batch_size,input_feature_dimensions]
        - **min** (Float)- 随机的最小值。(浮点数,默认为0.0)
        - **max** (Float)- 随机的最大值。(float,默认1.0)
        - **seed** (Float)- 用于随机数引擎的随机种子。0表示使用系统生成的种子。请注意,如果seed不为0,则此算子将始终每次生成相同的随机数。(int,默认为0)
        - **dtype** (np.dtype | core.VarDesc.VarType | str)- 输出数据的类型为float32,float_16,int等。

返回:       Id采样的数据张量。

返回类型:        输出(Variable)。


**代码示例:**

.. code-block:: python

X
xsrobin 已提交
7307
    import paddle.fluid as fluid
C
Cheerego 已提交
7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578
    x = fluid.layers.data(
    name="X",
    shape=[13, 11],
    dtype='float32',
    append_batch_size=False)

    out = fluid.layers.sampling_id(x)








.. _cn_api_fluid_layers_scale:

scale
-------------------------------

.. py:function:: paddle.fluid.layers.scale(x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None)

缩放算子

对输入张量应用缩放和偏移加法。

if ``bias_after_scale`` = True:

.. math::
                                Out=scale*X+bias

else:

.. math::
                                Out=scale*(X+bias)

参数:
        - **x** (Variable) - (Tensor) 要比例运算的输入张量(Tensor)。
        - **scale** (FLOAT) - 比例运算的比例因子。
        - **bias** (FLOAT) - 比例算子的偏差。
        - **bias_after_scale** (BOOLEAN) - 在缩放之后或之前添加bias。在某些情况下,对数值稳定性很有用。
        - **act** (basestring|None) - 应用于输出的激活函数。
        - **name** (basestring|None)- 输出的名称。

返回:        比例算子的输出张量(Tensor)

返回类型:        变量(Variable)

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
     
    x = fluid.layers.data(name="X", shape=[1, 2, 5, 5], dtype='float32')
    y = fluid.layers.scale(x, scale = 2.0, bias = 1.0)









.. _cn_api_fluid_layers_scatter:

scatter
-------------------------------

.. py:function:: paddle.fluid.layers.scatter(input, index, updates, name=None, overwrite=True)


通过更新输入在第一维度上指定索引位置处的元素来获得输出。

.. math::
          \\Out=XOut[Ids]=Updates\\


参数:
  - **input** (Variable) - 秩> = 1的源输入。
  - **index** (Variable) - 秩= 1的索引输入。 它的dtype应该是int32或int64,因为它用作索引。
  - **updates** (Variable) - scatter 要进行更新的变量。
  - **name** (str | None) - 输出变量名称。 默认None。
  - **overwrite** (bool) - 具有相同索引时更新输出的模式。如果为True,则使用覆盖模式更新相同索引的输出,如果为False,则使用accumulate模式更新相同索引的grad。默认值为True。您可以设置overwrite=False以使用scatter_add。

返回:张量变量, 与输入张量的shape相同

返回类型:output(Variable)

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
     
    input = fluid.layers.data(name='data', shape=[3, 5, 9], dtype='float32', append_batch_size=False)
    index = fluid.layers.data(name='index', shape=[3], dtype='int64', append_batch_size=False)
    updates = fluid.layers.data(name='update', shape=[3, 5, 9], dtype='float32', append_batch_size=False)
    
    output = fluid.layers.scatter(input, index, updates)











.. _cn_api_fluid_layers_selu:

selu
-------------------------------

.. py:function:: paddle.fluid.layers.selu(x, scale=None, alpha=None, name=None)

**实现Selu运算**

有如下等式:

.. math::
    selu= \lambda*
    \begin{cases}
         x                      &\quad \text{ if } x>0 \\
         \alpha * e^x - \alpha  &\quad \text{ if } x<=0
    \end{cases}

输入 ``x`` 可以选择性携带LoD信息。输出和它共享此LoD信息(如果有)。

参数:
  - **x** (Variable) – 输入张量
  - **scale** (float, None) – 如果标度没有设置,其默认值为 1.0507009873554804934193349852946。 详情请见: `Self-Normalizing Neural Networks <https://arxiv.org/abs/1706.02515.pdf>`_
  - **alpha** (float, None) – 如果没有设置改参数, 其默认值为 1.6732632423543772848170429916717。 详情请见: `Self-Normalizing Neural Networks <https://arxiv.org/abs/1706.02515.pdf>`_
  - **name** (str|None, default None) – 该层命名,若为None则自动为其命名

返回:一个形和输入张量相同的输出张量

返回类型:Variable

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
     
    input = fluid.layers.data(
         name="input", shape=[3, 9, 5], dtype="float32")

    output = fluid.layers.selu(input)













.. _cn_api_fluid_layers_sequence_concat:

sequence_concat
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_concat(input, name=None)

sequence_concat操作通过序列信息连接LoD张量(Tensor)。例如:X1的LoD = [0,3,7],X2的LoD = [0,7,9],结果的LoD为[0,(3 + 7),(7 + 9)],即[0,10,16]。

参数:
        - **input** (list) – 要连接变量的列表
        - **name** (str|None) – 此层的名称(可选)。如果没有设置,该层将被自动命名。

返回:     连接好的输出变量。

返回类型:   变量(Variable)


**代码示例**

..  code-block:: python

        import paddle.fluid as fluid
        x = fluid.layers.data(name='x', shape=[10], dtype='float32')
        y = fluid.layers.data(name='y', shape=[10], dtype='float32')
        out = fluid.layers.sequence_concat(input=[x, y])










.. _cn_api_fluid_layers_sequence_conv:

sequence_conv
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_conv(input, num_filters, filter_size=3, filter_stride=1, padding=None, bias_attr=None, param_attr=None, act=None, name=None)

该函数的输入参数中给出了滤波器和步长,通过利用输入以及滤波器和步长的常规配置来为sequence_conv创建操作符。

参数:
    - **input** (Variable) - (LoD张量)输入X是LoD张量,支持可变的时间量的长度输入序列。该LoDTensor的标记张量是一个维度为(T,N)的矩阵,其中T是mini-batch的总时间步数,N是input_hidden_size
    - **num_filters** (int) - 滤波器的数量
    - **filter_size** (int) - 滤波器大小(H和W)
    - **filter_stride** (int) - 滤波器的步长
    - **padding** (bool) - 若为真,添加填充
    - **bias_attr** (ParamAttr|bool|None) - sequence_conv偏离率参数属性。若设为False,输出单元则不加入偏离率。若设为None或ParamAttr的一个属性,sequence_conv将创建一个ParamAttr作为bias_attr。如果未设置bias_attr的初始化函数,则将bias初始化为0.默认:None
    - **param_attr** (ParamAttr|None) - 可学习参数/sequence_conv的权重参数属性。若设置为None或ParamAttr的一个属性,sequence_conv将创建ParamAttr作为param_attr。
    若未设置param_attr的初始化函数,则用Xavier初始化参数。默认:None

返回:sequence_conv的输出

返回类型:变量(Variable)

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[10,10], append_batch_size=False, dtype='float32')
    x_conved = fluid.layers.sequence_conv(x,2)







.. _cn_api_fluid_layers_sequence_enumerate:

sequence_enumerate
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_enumerate(input, win_size, pad_value=0, name=None)

为输入索引序列生成一个新序列,该序列枚举输入长度为 ``win_size`` 的所有子序列。 输入序列和枚举序列第一维上维度相同,第二维是 ``win_size`` ,在生成中如果需要,通过设置 ``pad_value`` 填充。

**例子:**

::

        输入:
            X.lod = [[0, 3, 5]]  X.data = [[1], [2], [3], [4], [5]]  X.dims = [5, 1]
        属性:
            win_size = 2  pad_value = 0
        输出:
            Out.lod = [[0, 3, 5]]  Out.data = [[1, 2], [2, 3], [3, 0], [4, 5], [5, 0]]  Out.dims = [5, 2]

参数:
        - **input** (Variable)- 作为索引序列的输入变量。
        - **win_size** (int)- 枚举所有子序列的窗口大小。
        - **pad_value** (int)- 填充值,默认为0。

返回:      枚举序列变量是LoD张量(LoDTensor)。

返回类型:   Variable

**代码示例**

..  code-block:: python

X
xsrobin 已提交
7579
      import paddle.fluid as fluid
C
Cheerego 已提交
7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649
      x = fluid.layers.data(shape[-1, 1], dtype='int32', lod_level=1)
      out = fluid.layers.sequence_enumerate(input=x, win_size=3, pad_value=0)









.. _cn_api_fluid_layers_sequence_expand:

sequence_expand
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_expand(x, y, ref_level=-1, name=None)

序列扩张层(Sequence Expand Layer)

将根据指定 y 的 level lod 展开输入变量x,请注意 x 的 lod level 最多为1,而 x 的秩最少为2。当 x 的秩大于2时,它就被看作是一个二维张量。下面的例子将解释 sequence_expand 是如何工作的:

::


    * 例1
      x is a LoDTensor:
    x.lod  = [[2,        2]]
    x.data = [[a], [b], [c], [d]]
    x.dims = [4, 1]

      y is a LoDTensor:
    y.lod = [[2,    2],
             [3, 3, 1, 1]]

      ref_level: 0

      then output is a 1-level LoDTensor:
    out.lod =  [[2,        2,        2,        2]]
    out.data = [[a], [b], [a], [b], [c], [d], [c], [d]]
    out.dims = [8, 1]

    * 例2
      x is a Tensor:
    x.data = [[a], [b], [c]]
    x.dims = [3, 1]

      y is a LoDTensor:
    y.lod = [[2, 0, 3]]

      ref_level: -1

      then output is a Tensor:
    out.data = [[a], [a], [c], [c], [c]]
    out.dims = [5, 1]

参数:
    - **x** (Variable) - 输入变量,张量或LoDTensor
    - **y** (Variable) - 输入变量,为LoDTensor
    - **ref_level** (int) - x表示的y的Lod层。若设为-1,表示lod的最后一层
    - **name** (str|None) - 该层名称(可选)。如果设为空,则自动为该层命名

返回:扩展变量,LoDTensor

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
7650
    import paddle.fluid as fluid
C
Cheerego 已提交
7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722
    import paddle.fluid.layers as layers
    x = fluid.layers.data(name='x', shape=[10], dtype='float32')
    y = fluid.layers.data(name='y', shape=[10, 20],
                 dtype='float32', lod_level=1)
    out = fluid.layers.sequence_expand(x=x, y=y, ref_level=0)









.. _cn_api_fluid_layers_sequence_expand_as:

sequence_expand_as
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_expand_as(x, y, name=None)

Sequence Expand As Layer

这一层将根据y的第0级lod扩展输入变量x。当前实现要求输入(Y)的lod层数必须为1,输入(X)的第一维应当和输入(Y)的第0层lod的大小相同,不考虑输入(X)的lod。

以下示例解释sequence_expand如何工作:

::

    * 例1:
    给定一维LoDTensor input(X)
        X.data = [[a], [b], [c], [d]]
        X.dims = [4, 1]
    和 input(Y)
        Y.lod = [[0, 3, 6, 7, 8]]
    ref_level: 0
    得到1级 LoDTensor
        Out.lod =  [[0,            3,              6,  7,  8]]
        Out.data = [[a], [a], [a], [b], [b], [b], [c], [d]]
        Out.dims = [8, 1]

    *例2

    给定一个 input(X):
        X.data = [[a, b], [c, d], [e, f]]
        X.dims = [3, 2]

    和 input(Y):
        Y.lod = [[0, 2, 3, 6]]
    ref_level: 0

    得到输出张量:

        Out.lod =  [[0,             2,     3,                    6]]
        Out.data = [[a, b], [a, b] [c, d], [e, f], [e, f], [e, f]]
        Out.dims = [6, 2]


参数:
    - **x** (Variable) - 输入变量,类型为Tensor或LoDTensor
    - **y** (Variable) - 输入变量,为LoDTensor
    - **name** (str|None) - 该层名称(可选)。如果设为空,则自动为该层命名

返回:扩展变量,LoDTensor

返回类型:变量(Variable)


**代码示例**:

.. code-block:: python

X
xsrobin 已提交
7723 7724 7725 7726 7727
    import paddle.fluid as fluid
    import paddle.fluid.layers as layers

    x = fluid.layers.data(name='x', shape=[10], dtype='float32')
    y = fluid.layers.data(name='y', shape=[10, 20],
C
Cheerego 已提交
7728
                     dtype='float32', lod_level=1)
X
xsrobin 已提交
7729 7730
    out = layers.sequence_expand_as(x=x, y=y)

C
Cheerego 已提交
7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769






.. _cn_api_fluid_layers_sequence_first_step:

sequence_first_step
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_first_step(input)

该功能获取序列的第一步

::

    x是1-level LoDTensor:

      x.lod = [[2, 3, 2]]

      x.data = [1, 3, 2, 4, 6, 5, 1]

      x.dims = [7, 1]

    输出为张量:

      out.dim = [3, 1]
      with condition len(x.lod[-1]) == out.dims[0]
      out.data = [1, 2, 5], where 1=first(1,3), 2=first(2,4,6), 5=first(5,1)

参数:**input** (variable)-输入变量,为LoDTensor

返回:序列第一步,为张量

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
7770
    import paddle.fluid as fluid
C
Cheerego 已提交
7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817
    x = fluid.layers.data(name='x', shape=[7, 1],
                 dtype='float32', lod_level=1)
    x_first_step = fluid.layers.sequence_first_step(input=x)









.. _cn_api_fluid_layers_sequence_last_step:

sequence_last_step
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_last_step(input)

该API可以获取序列的最后一步

::

    x是level-1的LoDTensor:

        x.lod = [[2, 3, 2]]

        x.data = [1, 3, 2, 4, 6, 5, 1]

        x.dims = [7, 1]

    输出为Tensor:

        out.dim = [3, 1]

        且 len(x.lod[-1]) == out.dims[0]

        out.data = [3, 6, 1], where 3=last(1,3), 6=last(2,4,6), 1=last(5,1)

参数:**input** (variable)-输入变量,为LoDTensor

返回:序列的最后一步,为张量

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
7818
    import paddle.fluid as fluid
C
Cheerego 已提交
7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859
    x = fluid.layers.data(name='x', shape=[7, 1],
                 dtype='float32', lod_level=1)
    x_last_step = fluid.layers.sequence_last_step(input=x)









.. _cn_api_fluid_layers_sequence_mask:

sequence_mask
-------------------------------

.. py:function::  paddle.fluid.layers.sequence_mask(x, maxlen=None, dtype='int64', name=None)

该层根据输入 ``x`` 和 ``maxlen`` 输出一个掩码,数据类型为dtype。

假设x是一个形状为[d_1, d_2,…, d_n]的张量。, y是一个形为[d_1, d_2,… ,d_n, maxlen]的掩码,其中:

.. math::

  y(i_1, i_2,..., i_n, j) = (j < x(i_1, i_2,..., i_n))

参数:
  - **x** (Variable) - sequence_mask层的输入张量,其元素是小于maxlen的整数。
  - **maxlen** (int|None) - 序列的最大长度。如果maxlen为空,则用max(x)替换。
  - **dtype** (np.dtype|core.VarDesc.VarType|str) - 输出的数据类型
  - **name** (str|None) - 此层的名称(可选)。如果没有设置,该层将被自动命名。

返回: sequence mask 的输出

返回类型: Variable

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
7860
    import paddle.fluid as fluid
C
Cheerego 已提交
7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957
    import paddle.fluid.layers as layers
    
    x = fluid.layers.data(name='x', shape=[10], dtype='float32', lod_level=1)
    mask = layers.sequence_mask(x=x)










.. _cn_api_fluid_layers_sequence_pad:

sequence_pad
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_pad(x,pad_value,maxlen=None,name=None)

序列填充操作符(Sequence Pad Operator)

这个操作符将同一batch中的序列填充到一个一致的长度。长度由属性padded_length指定。填充的新元素的值具体由输入 ``PadValue`` 指定,并会添加到每一个序列的末尾,使得他们最终的长度保持一致。

以下的例子更清晰地解释此操作符的工作原理:

::

    例1:

    给定 1-level LoDTensor

    input(X):
        X.lod = [[0,2,5]]
        X.data = [a,b,c,d,e]
    input(PadValue):
        PadValue.data = [0]

    'padded_length'=4

    得到LoDTensor:
        Out.data = [[a,b,0,0],[c,d,e,0]]
        Length.data = [[2],[3]]

::

    例2:

    给定 1-level LoDTensor

    input(X):
        X.lod = [[0,2,5]]
        X.data = [[a1,a2],[b1,b2],[c1,c2],[d1,d2],[e1,e2]]
    input(PadValue):
        PadValue.data = [0]

    'padded_length' = -1,表示用最长输入序列的长度(此例中为3)

    得到LoDTensor:
        Out.data = [[[a1,a2],[b1,b2],[0,0]],[[c1,c2],[d1,d2],[e1,e2]]]
        Length.data = [[2],[3]]


::

    例3:

    给定 1-level LoDTensor

    input(X):
        X.lod = [[0,2,5]]
        X.data = [[a1,a2],[b1,b2],[c1,c2],[d1,d2],[e1,e2]]
    input(PadValue):
        PadValue.data = [p1,p2]

    'padded_length' = -1,表示用最长输入序列的长度(此例中为3)

    得到LoDTensor:
        Out.data = [[[a1,a2],[b1,b2],[p1,p2]],[[c1,c2],[d1,d2],[e1,e2]]]
        Length.data = [[2],[3]]


参数:
    - **x** (Vairable) - 输入变量,应包含lod信息
    - **pad_value** (Variable) - 变量,存有放入填充步的值。可以是标量或tensor,维度和序列的时间步长相等。如果是标量,则自动广播到时间步长的维度
    - **maxlen** (int,默认None) - 填充序列的长度。可以为空或者任意正整数。当为空时,以序列中最长序列的长度为准,其他所有序列填充至该长度。当是某个特定的正整数,最大长度必须大于最长初始序列的长度
    - **name** (str|None) – 该层的命名(可选项)。 如果为 None, 则自动命名

返回:填充序列批(batch)和填充前的初始长度。所有输出序列的长度相等

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
7958
    import paddle.fluid as fluid
C
Cheerego 已提交
7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199
    import numpy

    x = fluid.layers.data(name='y', shape=[10, 5],
                     dtype='float32', lod_level=1)
    pad_value = fluid.layers.assign(
        input=numpy.array([0.0], dtype=numpy.float32))
    out = fluid.layers.sequence_pad(x=x, pad_value=pad_value)









.. _cn_api_fluid_layers_sequence_pool:

sequence_pool
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_pool(input, pool_type, is_test=False, pad_value=0.0)

该函数为序列的池化添加操作符。将每个实例的所有时间步数特征池化,并用参数中提到的pool_type将特征运用到输入到首部。

支持四种pool_type:

- **average**: :math:`Out[i] = \frac{\sum_{i}X_{i}}{N}`
- **sum**: :math:`Out[i] = \sum _{j}X_{ij}`
- **sqrt**: :math:`Out[i] = \frac{ \sum _{j}X_{ij}}{\sqrt{len(\sqrt{X_{i}})}}`
- **max**: :math:`Out[i] = max(X_{i})`

::


    x是一级LoDTensor且**pad_value** = 0.0:
        x.lod = [[2, 3, 2, 0]]
        x.data = [1, 3, 2, 4, 6, 5, 1]
        x.dims = [7, 1]
    输出为张量(Tensor):
        out.dim = [4, 1]
        with condition len(x.lod[-1]) == out.dims[0]
    对于不同的pool_type:
        average: out.data = [2, 4, 3, 0.0], where 2=(1+3)/2, 4=(2+4+6)/3, 3=(5+1)/2
        sum    : out.data = [4, 12, 6, 0.0], where 4=1+3, 12=2+4+6, 6=5+1
        sqrt   : out.data = [2.82, 6.93, 4.24, 0.0], where 2.82=(1+3)/sqrt(2),
             6.93=(2+4+6)/sqrt(3), 4.24=(5+1)/sqrt(2)
        max    : out.data = [3, 6, 5, 0.0], where 3=max(1,3), 6=max(2,4,6), 5=max(5,1)
        last   : out.data = [3, 6, 1, 0.0], where 3=last(1,3), 6=last(2,4,6), 1=last(5,1)
        first  : out.data = [1, 2, 5, 0.0], where 1=first(1,3), 2=first(2,4,6), 5=first(5,1)
        
      且以上所有均满足0.0 = **pad_value**

参数:
    - **input** (variable) - 输入变量,为LoDTensor
    - **pool_type** (string) - 池化类型。支持average,sum,sqrt和max
    - **is_test** (bool, 默认为 False) - 用于区分训练模式和测试评分模式。默认为False。
    - **pad_value** (float) - 用于填充空输入序列的池化结果。

返回:sequence pooling 变量,类型为张量(Tensor)

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid

    x = fluid.layers.data(name='x', shape=[7, 1],
                 dtype='float32', lod_level=1)
    avg_x = fluid.layers.sequence_pool(input=x, pool_type='average')
    sum_x = fluid.layers.sequence_pool(input=x, pool_type='sum')
    sqrt_x = fluid.layers.sequence_pool(input=x, pool_type='sqrt')
    max_x = fluid.layers.sequence_pool(input=x, pool_type='max')
    last_x = fluid.layers.sequence_pool(input=x, pool_type='last')
    first_x = fluid.layers.sequence_pool(input=x, pool_type='first')









.. _cn_api_fluid_layers_sequence_reshape:

sequence_reshape
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_reshape(input, new_dim)

Sequence Reshape Layer
该层重排输入序列。用户设置新维度。每一个序列的的长度通过原始长度、原始维度和新的维度计算得出。以下实例帮助解释该层的功能

.. code-block:: python

    x是一个LoDTensor:
        x.lod  = [[0, 2, 6]]
        x.data = [[1,  2], [3,  4],
                [5,  6], [7,  8],
                [9, 10], [11, 12]]
        x.dims = [6, 2]
    设置 new_dim = 4
    输出为LoDTensor:
        out.lod  = [[0, 1, 3]]

        out.data = [[1,  2,  3,  4],
                    [5,  6,  7,  8],
                    [9, 10, 11, 12]]
        out.dims = [3, 4]

目前仅提供1-level LoDTensor,请确保(原长度*原维数)可以除以新的维数,每个序列没有余数。

参数:
    - **input** (Variable)-一个2-D LoDTensor,模型为[N,M],维度为M
    - **new_dim** (int)-新维度,输入LoDTensor重新塑造后的新维度

返回:根据新维度重新塑造的LoDTensor

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[2, 6], append_batch_size=False, dtype='float32', lod_level=1)
    x_reshaped = fluid.layers.sequence_reshape(input=x, new_dim=4)









.. _cn_api_fluid_layers_sequence_reverse:

sequence_reverse
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_reverse(x, name=None)


在第0维上将输入 ``x`` 的各序列倒序。

::

    假设 ``x`` 是一个形为 (5,4) 的LoDTensor, lod信息为 [[0, 2, 5]],其中,


    X.data() = [ [1, 2, 3, 4], [5, 6, 7, 8], # 索引为0,长度为2的序列

                 [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20] # 索引为1长度为3的序列

输出 ``Y`` 与 ``x`` 具有同样的维数和LoD信息。 于是有:

::

    Y.data() = [ [5, 6, 7, 8], [1, 2, 3, 4], # 索引为0,长度为2的逆序列
                 [17, 18, 19, 20], [13, 14, 15, 16], [9, 10, 11, 12] # 索引为1,长度为3的逆序列

该运算在建立反dynamic RNN 网络中十分有用。

目前仅支持LoD层次(LoD level)为1的张量倒序。

参数:
  - **x** (Variable) – 输入张量
  - **name** (basestring|None) – 输出变量的命名

返回:输出LoD张量

返回类型:Variable

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[2, 6], dtype='float32')
    x_reversed = fluid.layers.sequence_reverse(x)







.. _cn_api_fluid_layers_sequence_scatter:

sequence_scatter
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_scatter(input, index, updates, name=None)

序列散射层

这个operator将更新张量X,它使用Ids的LoD信息来选择要更新的行,并使用Ids中的值作为列来更新X的每一行。

**样例**:

::

    输入:

    input.data = [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
                  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
                  [1.0, 1.0, 1.0, 1.0, 1.0, 1.0]]
    input.dims = [3, 6]

    index.data = [[0], [1], [2], [5], [4], [3], [2], [1], [3], [2], [5], [4]]
    index.lod =  [[0,        3,                       8,                 12]]

    updates.data = [[0.3], [0.3], [0.4], [0.1], [0.2], [0.3], [0.4], [0.0], [0.2], [0.3], [0.1], [0.4]]
    updates.lod =  [[  0,            3,                                 8,                         12]]

    输出:

    out.data = [[1.3, 1.3, 1.4, 1.0, 1.0, 1.0],
                [1.0, 1.0, 1.4, 1.3, 1.2, 1.1],
                [1.0, 1.0, 1.3, 1.2, 1.4, 1.1]]
    out.dims = X.dims = [3, 6]



参数:
      - **input** (Variable) - input 秩(rank) >= 1。
      - **index** (Variable) - LoD Tensor, index 是 sequence scatter op 的输入索引,该函数的input将依据index进行更新。 秩(rank)=1。由于用于索引dtype应该是int32或int64。
      - **updates** (Variable) - 一个 LoD Tensor , update 的值将被 sactter 到输入x。update 的 LoD信息必须与index一致。
      - **name** (str|None) - 输出变量名。默认:None。

返回: 输出张量维度应该和输入张量相同

返回类型:Variable


**代码示例**:

..  code-block:: python

X
xsrobin 已提交
8200
    import paddle.fluid as fluid
C
Cheerego 已提交
8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261
    import paddle.fluid.layers as layers
     
    input = layers.data( name="x", shape=[3, 6], append_batch_size=False, dtype='float32' )
    index = layers.data( name='index', shape=[1], dtype='int32')
    updates = layers.data( name='updates', shape=[1], dtype='float32')
    output = fluid.layers.sequence_scatter(input, index, updates)










.. _cn_api_fluid_layers_sequence_slice:

sequence_slice
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_slice(input, offset, length, name=None)

**实现Sequence Slice(序列切片)运算**

该层从给定序列中截取子序列。截取依据为所给的开始 ``offset`` (偏移量) 和子序列长 ``length`` 。

仅支持序列数据,LoD level(LoD层次为1)
::
    输入变量:

        input.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]],
        input.lod = [[3, 2]],
        input.dims = (5, 2),

    以及 offset.data = [[0], [1]] and length.data = [[2], [1]],

    则输出变量为:

        out.data = [[a1, a2], [b1, b2], [e1, e2]],
        out.lod = [[2, 1]],
        out.dims = (3, 2).

.. note::
   ``input`` , ``offset`` , ``length`` 的第一维大小应相同。
   ``offset`` 从0开始。

参数:
  - **input** (Variable) – 输入变量 ,承载着完整的序列
  - **offset** (Variable) – 对每个序列切片的起始索引
  - **length** (Variable) – 每个子序列的长度
  - **name** (str|None) – 该层的命名,可选项。 如果None, 则自动命名该层

返回:输出目标子序列

返回类型:Variable

**代码示例**

..  code-block:: python

X
xsrobin 已提交
8262
  import paddle.fluid as fluid
C
Cheerego 已提交
8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311
  import numpy as np
  seqs = fluid.layers.data(name='x', shape=[10, 5],
       dtype='float32', lod_level=1)
  offset = fluid.layers.assign(input=np.array([[0, 1]]).astype("int32"))
  length = fluid.layers.assign(input=np.array([[2, 1]]).astype("int32"))
  subseqs = fluid.layers.sequence_slice(input=seqs, offset=offset,
                length=length)










.. _cn_api_fluid_layers_sequence_softmax:

sequence_softmax
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_softmax(input, use_cudnn=False, name=None)

该函数计算每一个序列所有时间步中的softmax激活函数。每个时间步的维度应为1。

输入张量的形状可为 :math:`[N,1]` 或者 :math:`[N]` , :math:`N` 是所有序列长度之和。

对mini-batch的第i序列:

.. math::

    Out\left ( X[lod[i]:lod[i+1]],: \right ) = \frac{exp(X[lod[i]:lod[i+1],:])}{\sum (exp(X[lod[i]:lod[i+1],:]))}

例如,对有3个序列(可变长度)的mini-batch,每个包含2,3,2时间步,其lod为[0,2,5,7],则在 :math:`X[0:2,:],X[2:5,:],X[5:7,:]` 中进行softmax运算,并且 :math:`N` 的结果为7.

参数:
    - **input** (Variable) - 输入变量,为LoDTensor
    - **use_cudnn** (bool) - 是否用cudnn核,仅当下载cudnn库才有效。默认:False
    - **name** (str|None) - 该层名称(可选)。若设为None,则自动为该层命名。默认:None

返回:sequence_softmax的输出

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
8312
    import paddle.fluid as fluid
C
Cheerego 已提交
8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369
    x = fluid.layers.data(name='x', shape=[7, 1],
                 dtype='float32', lod_level=1)
    x_sequence_softmax = fluid.layers.sequence_softmax(input=x)










.. _cn_api_fluid_layers_sequence_unpad:

sequence_unpad
-------------------------------

.. py:function:: paddle.fluid.layers.sequence_unpad(x, length, name=None)

**实现Sequence Unpad(去除序列填充值)运算**

该层从给定序列中删除padding(填充值),并且将该序列转变为未填充时的原序列作为该层的输出,并且实际长度可以在输出的LoD信息中取得。

::

    示例:

    给定输入变量 ``x`` :
        x.data = [[ 1.0,  2.0,  3.0,  4.0,  5.0],
                  [ 6.0,  7.0,  8.0,  9.0, 10.0],
                  [11.0, 12.0, 13.0, 14.0, 15.0]],

    其中包含 3 个被填充到长度为5的序列,实际长度由输入变量 ``length`` 指明:

        length.data = [[2], [3], [4]],

    则去填充(unpad)后的输出变量为:

        out.data = [[1.0, 2.0, 6.0, 7.0, 8.0, 11.0, 12.0, 13.0, 14.0]]
        out.lod = [[2, 3, 4]]



参数:
  - **x** (Variable) – 输入变量,承载着多个填充后等长的序列
  - **length** (Variable) – 变量,指明去填充后各个序列所具有的实际长度
  - **name** (str|None) – 可选项,该层名称。 若为 None, 将自动命名该层

返回:变量,承载着去填充处理后的序列

返回类型:Variable

**代码示例**

..  code-block:: python

X
xsrobin 已提交
8370
    import paddle.fluid as fluid
C
Cheerego 已提交
8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475
    x = fluid.layers.data(name='x', shape=[10, 5], dtype='float32')
    len = fluid.layers.data(name='length', shape=[1], dtype='int64')
    out = fluid.layers.sequence_unpad(x=x, length=len)












.. _cn_api_fluid_layers_shape:

shape
-------------------------------

.. py:function:: paddle.fluid.layers.shape(input)

shape层。

获得输入变量的形状。

参数:
        - **input** (Variable)-  输入的变量

返回: (Tensor),输入变量的形状

返回类型:    Variable

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid
    input = fluid.layers.data(
        name="input", shape=[3, 100, 100], dtype="float32")
    out = fluid.layers.shape(input)





.. _cn_api_fluid_layers_shuffle_channel:

shuffle_channel
-------------------------------

.. py:function:: paddle.fluid.layers.shuffle_channel(x, group, name=None)

**Shuffle Channel 运算(通道重排运算)**

该算子将输入 ``x`` 的通道混洗重排。 它将每个组中的输入通道分成 ``group`` 个子组,并通过逐个从每个子组中选择元素来获得新的顺序。

请参阅 https://arxiv.org/pdf/1707.01083.pdf

::

    输入一个形为 (N, C, H, W) 的4-D tensor:

    input.shape = (1, 4, 2, 2)
    input.data =[[[[0.1, 0.2],
                   [0.2, 0.3]],

                  [[0.3, 0.4],
                   [0.4, 0.5]],

                  [[0.5, 0.6],
                   [0.6, 0.7]],

                  [[0.7, 0.8],
                   [0.8, 0.9]]]]

    指定组数 group: 2
    可得到与输入同形的输出 4-D tensor:

    out.shape = (1, 4, 2, 2)
    out.data = [[[[0.1, 0.2],
                  [0.2, 0.3]],

                 [[0.5, 0.6],
                  [0.6, 0.7]],

                 [[0.3, 0.4],
                  [0.4, 0.5]],

                 [[0.7, 0.8],
                  [0.8, 0.9]]]]

参数:
  - **x** (Variable) – 输入张量变量。 应是形状为[N,C,H,W]的4-D张量
  - **group** (int) – 表示子组的数目,它应该整除通道数。

返回:通道混洗结果是一个张量变量,其形状和类型与输入相同。

返回类型:输出(Variable)


**代码示例:**

.. code-block:: python

X
xsrobin 已提交
8476
    import paddle.fluid as fluid
C
Cheerego 已提交
8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537
    input = fluid.layers.data(name='input', shape=[4,2,2], dtype='float32')
    out = fluid.layers.shuffle_channel(x=input, group=2)









.. _cn_api_fluid_layers_sigmoid_cross_entropy_with_logits:

sigmoid_cross_entropy_with_logits
-------------------------------

.. py:function:: paddle.fluid.layers.sigmoid_cross_entropy_with_logits(x, label, ignore_index=-100, name=None, normalize=False)

在类别不相互独立的分类任务中,该函数可以衡量按元素的可能性误差。可以这么认为,为单一数据点预测标签,它们之间不是完全互斥的关系。例如,一篇新闻文章可以同时关于政治,科技,体育或者同时不包含这些内容。

逻辑loss可通过下式计算:

.. math::
    loss = -Labels * log(sigma(X)) - (1 - Labels) * log(1 - sigma(X))

已知:

.. math::
    sigma(X) = \frac{1}{1 + exp(-X)}

代入最开始的式子,

.. math::
    loss = X - X * Labels + log(1 + exp(-X))

为了计算稳定性,防止 :math:`exp(-X)` 溢出,当 :math:`X<0` 时,loss将采用以下公式计算:

.. math::
    loss = max(X, 0) - X * Labels + log(1 + exp(-|X|))

输入 ``X`` 和 ``label`` 都可以携带LoD信息。然而输出仅采用输入 ``X`` 的LoD。



参数:
  - **x** (Variable) - (Tensor, 默认 Tensor<float>),形为 N x D 的二维张量,N为batch大小,D为类别数目。该输入是一个由先前运算得出的logit组成的张量。logit是未标准化(unscaled)的log概率, 公式为 :math:`log(\frac{p}{1-p})`
  - **label** (Variable) -  (Tensor, 默认 Tensor<float>) 具有和X相同类型,相同形状的二维张量。该输入张量代表了每个logit的可能标签
  - **ignore_index** (int) - (int,默认kIgnoreIndex)指定被忽略的目标值,它不会影响输入梯度
  - **name** (basestring|None) - 输出的名称
  - **normalize** (bool) - 如果为true,则将输出除以除去ignore_index对应目标外的目标数

返回: (Tensor, 默认Tensor<float>), 形为 N x D 的二维张量,其值代表了按元素的逻辑loss

返回类型:Variable



**代码示例**

..  code-block:: python

X
xsrobin 已提交
8538
    import paddle.fluid as fluid
C
Cheerego 已提交
8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576
    input = fluid.layers.data(
        name='data', shape=[10], dtype='float32')
    label = fluid.layers.data(
        name='data', shape=[10], dtype='float32')
    loss = fluid.layers.sigmoid_cross_entropy_with_logits(
        x=input,
        label=label,
        ignore_index=-1,
        normalize=True) # or False
    # loss = fluid.layers.reduce_sum(loss) # loss之和







.. _cn_api_fluid_layers_sign:

sign
-------------------------------

.. py:function:: paddle.fluid.layers.sign(x)

此函数返回x中每个元素的正负号:1代表正,-1代表负,0代表零。

参数:
    - **x** (Variable|numpy.ndarray) – 输入张量。

返回:输出正负号张量,和x有着相同的形状和数据类型。

返回类型:Variable

**代码示例**

..  code-block:: python

    # [1, 0, -1]
X
xsrobin 已提交
8577
    import paddle.fluid as fluid
C
Cheerego 已提交
8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667
    data = fluid.layers.sign(np.array([3, 0, -2]))





.. _cn_api_fluid_layers_similarity_focus:

similarity_focus
-------------------------------

.. py:function:: paddle.fluid.layers.similarity_focus(input, axis, indexes, name=None)

**实现SimilarityFocus(相似度聚焦)运算**

通过以下三种方式,该层生成一个和输入 ``input`` 同形的mask(掩码):

1. 根据 ``axis`` 和 ``indexes`` 提取一个三维张量,第一维为batch大小。
   例如,如果 ``axis=1, indexes=[a]`` , 将得到矩阵 T=X[:, a, :, :] 。
   该例中,如果输入X的形为 (BatchSize, A, B, C) ,则输出张量T的形为 (BatchSize, B, C) 。
2. 对于每一个索引,在输出T中找到最大值。所以同一行、同一列最多只有一个数字,这意味着如果在第i行,第j列中找到最大值,那么在相应行、列中的其他数值都将被忽略。然后再在剩余的数值中找到下一个最大值。显然,将会产生 min(B,C)个数字,并把三维相似聚焦掩码张量相应位置的元素置为1,其余则置为0。对每个索引按元素进行or运算。
3. 将这个三维相似度聚焦mask调整、适配于输入 ``input`` 的形状

请参照 `Similarity Focus Layer <http://www.aclweb.org/anthology/N16-1108>`_ 。

::

    例如 :

    给定四维张量 x 形为 (BatchSize, C, A, B), 其中C 为通道Channel数目,
    特征图(feature map)的形为(A,B):

        x.shape = (2, 3, 2, 2)
        x.data = [[[[0.8, 0.1],
                    [0.4, 0.5]],

                   [[0.9, 0.7],
                    [0.9, 0.9]],

                   [[0.8, 0.9],
                    [0.1, 0.2]]],


                  [[[0.2, 0.5],
                    [0.3, 0.4]],

                   [[0.9, 0.7],
                    [0.8, 0.4]],

                   [[0.0, 0.2],
                    [0.4, 0.7]]]]

    给定轴: 1 (即channel轴)
    给定索引: [0]

    于是我们得到一个与输入同形的四维输出张量:
        out.shape = (2, 3, 2, 2)
        out.data = [[[[1.0, 0.0],
                      [0.0, 1.0]],

                     [[1.0, 0.0],
                      [0.0, 1.0]],

                     [[1.0, 0.0],
                      [0.0, 1.0]]],

                    [[[0.0, 1.0],
                      [1.0, 0.0]],

                     [[0.0, 1.0],
                      [1.0, 0.0]],

                     [[0.0, 1.0],
                      [1.0, 0.0]]]]



参数:
  - **input** (Variable) – 输入张量(默认类型为float)。应为一个四维张量,形为[BatchSize, A, B, C]
  - **axis** (int) – 指明要选择的轴。 可能取值为 1, 2 或 3.
  - **indexes** (list) – 指明选择维度的索引列表

返回:一个和输入张量同形、同类型的张量变量

返回类型:Variable

**代码示例**

..  code-block:: python

X
xsrobin 已提交
8668
            import paddle.fluid as fluid
C
Cheerego 已提交
8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770
            data = fluid.layers.data(
              name='data', shape=[-1, 3, 2, 2], dtype='float32')
            fluid.layers.similarity_focus(input=data, axis=1, indexes=[0])












.. _cn_api_fluid_layers_slice:

slice
-------------------------------

.. py:function:: paddle.fluid.layers.slice(input, axes, starts, ends)

slice算子。

沿多个轴生成输入张量的切片。与numpy类似: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html Slice使用 ``axes`` 、 ``starts`` 和 ``ends`` 属性来指定轴列表中每个轴的起点和终点维度,它使用此信息来对输入数据张量切片。如果向 ``starts`` 或 ``ends`` 传递负值,则表示该维度结束之前的元素数目。如果传递给 ``starts`` 或 ``end`` 的值大于n(此维度中的元素数目),则表示n。当切片一个未知数量的唯独时,建议传入INT_MAX. ``axes`` 的大小必须和 ``starts`` 和 ``ends`` 的相等。以下示例将解释切片如何工作:

::

        案例1:
                给定:
                     data=[[1,2,3,4],[5,6,7,8],]
                     axes=[0,1]
                     starts=[1,0]
                     ends=[2,3]
                则:
                     result=[[5,6,7],]

        案例2:
                给定:
                     data=[[1,2,3,4],[5,6,7,8],]
                     starts=[0,1]
                     ends=[-1,1000]
                则:
                     result=[[2,3,4],]

参数:
        - **input** (Variable)- 提取切片的数据张量(Tensor)。
        - **axes** (List)- (list <int>)开始和结束的轴适用于。它是可选的。如果不存在,将被视为[0,1,...,len(starts)- 1]。
        - **starts** (List)- (list <int>)在轴上开始相应轴的索引。
        - **ends** (List)- (list <int>)在轴上结束相应轴的索引。

返回:        切片数据张量(Tensor).

返回类型:        输出(Variable)。


**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid

    starts = [1, 0, 2]
    ends = [3, 3, 4]
    axes = [0, 1, 2]

    input = fluid.layers.data(
        name="input", shape=[3, 4, 5, 6], dtype='float32')

    out = fluid.layers.slice(input, axes=axes, starts=starts, ends=ends)







.. _cn_api_fluid_layers_smooth_l1:

smooth_l1
-------------------------------

.. py:function:: paddle.fluid.layers.smooth_l1(x, y, inside_weight=None, outside_weight=None, sigma=None)

该layer计算变量 ``x`` 和 ``y`` 的smooth L1 loss,它以 ``x`` 和 ``y`` 的第一维大小作为批处理大小。对于每个实例,按元素计算smooth L1 loss,然后计算所有loss。输出变量的形状是[batch_size, 1]


参数:
        - **x** (Variable) - rank至少为2的张量。输入x的smmoth L1 loss 的op,shape为[batch_size, dim1,…],dimN]。
        - **y** (Variable) - rank至少为2的张量。与 ``x`` 形状一致的的smooth L1 loss  op目标值。
        - **inside_weight** (Variable|None) - rank至少为2的张量。这个输入是可选的,与x的形状应该相同。如果给定, ``(x - y)`` 的结果将乘以这个张量元素。
        - **outside_weight** (变量|None) - 一个rank至少为2的张量。这个输入是可选的,它的形状应该与 ``x`` 相同。如果给定,那么 smooth L1 loss 就会乘以这个张量元素。
        - **sigma** (float|None) - smooth L1 loss layer的超参数。标量,默认值为1.0。

返回: smooth L1 loss, shape为 [batch_size, 1]

返回类型:  Variable

**代码示例**

..  code-block:: python

X
xsrobin 已提交
8771
    import paddle.fluid as fluid
C
Cheerego 已提交
8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922
    data = fluid.layers.data(name='data', shape=[128], dtype='float32')
    label = fluid.layers.data(
        name='label', shape=[100], dtype='float32')
    fc = fluid.layers.fc(input=data, size=100)
    out = fluid.layers.smooth_l1(x=fc, y=label)










.. _cn_api_fluid_layers_soft_relu:

soft_relu
-------------------------------

.. py:function:: paddle.fluid.layers.soft_relu(x, threshold=40.0, name=None)

SoftRelu 激活函数

.. math::   out=ln(1+exp(max(min(x,threshold),threshold))

参数:
    - **x** (variable) - SoftRelu operator的输入
    - **threshold** (FLOAT|40.0) - SoftRelu的阈值
    - **name** (str|None) - 该层的名称(可选)。如果设置为None,该层将被自动命名

**代码示例:**

.. code-block:: python

    import paddle.fluid as fluid

    x = fluid.layers.data(name=”x”, shape=[2,3,16,16], dtype=”float32”)
    y = fluid.layers.soft_relu(x, threshold=20.0)








.. _cn_api_fluid_layers_softmax:

softmax
-------------------------------

.. py:function:: paddle.fluid.layers.softmax(input, use_cudnn=False, name=None, axis=-1)

softmax操作符的输入是任意阶的张量,输出张量和输入张量的维度相同。

输入变量的 ``axis`` 维会被排列到最后一维。然后逻辑上将输入张量压平至二维矩阵。矩阵的第二维(行数)和输入张量的 ``axis`` 维相同。第一维(列数)
是输入张量除最后一维之外的所有维长度乘积。对矩阵的每一行来说,softmax操作将含有任意实数值的K维向量(K是矩阵的宽度,也就是输入张量 ``axis`` 维度的大小)压缩成K维含有取值为[0,1]中实数的向量,并且这些值和为1。


softmax操作符计算k维向量输入中所有其他维的指数和指数值的累加和。维的指数比例和所有其他维的指数值之和作为softmax操作符的输出。

对矩阵中的每行i和每列j有:

.. math::

    Out[i,j] = \frac{exp(X[i,j])}{\sum_j exp(X[i,j])}

参数:
    - **input** (Variable) - 输入变量
    - **use_cudnn** (bool) - 是否用cudnn核,只有在cudnn库安装时有效。为了数学稳定性,默认该项为False。
    - **name** (str|None) - 该层名称(可选)。若为空,则自动为该层命名。默认:None
    - **axis** (Variable) - 执行softmax计算的维度索引,应该在 :math:`[-1,rank-1]` 范围内,其中rank是输入变量的秩。 默认值:-1。

返回: softmax输出

返回类型:变量(Variable)

**代码示例**

.. code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[2], dtype='float32')
    fc = fluid.layers.fc(input=x, size=10)
    # 在第二维执行softmax
    softmax = fluid.layers.softmax(input=fc, axis=1)
    # 在最后一维执行softmax
    softmax = fluid.layers.softmax(input=fc, axis=-1)









.. _cn_api_fluid_layers_softmax_with_cross_entropy:

softmax_with_cross_entropy
-------------------------------

.. py:function:: paddle.fluid.layers.softmax_with_cross_entropy(logits, label, soft_label=False, ignore_index=-100, numeric_stable_mode=True, return_softmax=False, axis=-1)

使用softmax的交叉熵在输出层已被广泛使用。该函数计算输入张量在axis轴上的softmax标准化值,而后计算交叉熵。通过此种方式,可以得到更具数字稳定性的梯度值。

因为该运算是在内部进行logit上的softmax运算,所以它需要未标准化(unscaled)的logit。该运算不应该对softmax运算的输出进行操作,否则会得出错误结果。

当 ``soft_label`` 为 ``False`` 时,该运算接受互斥的硬标签,batch中的每一个样本都以为1的概率分类到一个类别中,并且仅有一个标签。

涉及到的等式如下:

1.硬标签,即 one-hot label, 每个样本仅可分到一个类别

.. math::
   loss_j =  -\text{logit}_{label_j} +\log\left(\sum_{i=0}^{K}\exp(\text{logit}_i)\right), j = 1,..., K

2.软标签,每个样本可能被分配至多个类别中

.. math::
   loss_j =  -\sum_{i=0}^{K}\text{label}_i\left(\text{logit}_i - \log\left(\sum_{i=0}^{K}\exp(\text{logit}_i)\right)\right), j = 1,...,K

3.如果 ``numeric_stable_mode`` 为真,在通过softmax和标签计算交叉熵损失前, softmax 首先经由下式计算得出:

.. math::
    max_j           &= \max_{i=0}^{K}{\text{logit}_i} \\
    log\_max\_sum_j &= \log\sum_{i=0}^{K}\exp(logit_i - max_j)\\
    softmax_j &= \exp(logit_j - max_j - {log\_max\_sum}_j)


参数:
  - **logits** (Variable) - 未标准化(unscaled)对数概率的输入张量。
  - **label** (Variable) - 真实值张量。如果 ``soft_label`` 为True,则该参数是一个和logits形状相同的的Tensor<float/double> 。如果 ``soft_label`` 为False,label是一个在axis维上形为1,其它维上与logits形对应相同的Tensor<int64>。
  - **soft_label** (bool) - 是否将输入标签当作软标签。默认为False。
  - **ignore_index** (int) - 指明要无视的目标值,使之不对输入梯度有贡献。仅在 ``soft_label`` 为False时有效,默认为kIgnoreIndex。 
  - **numeric_stable_mode** (bool) – 标志位,指明是否使用一个具有更佳数学稳定性的算法。仅在 ``soft_label`` 为 False的GPU模式下生效。若 ``soft_label`` 为 True 或者执行场所为CPU, 算法一直具有数学稳定性。 注意使用稳定算法时速度可能会变慢。默认为 True。
  - **return_softmax** (bool) – 标志位,指明是否额外返回一个softmax值, 同时返回交叉熵计算结果。默认为False。
  - **axis** (int) – 执行softmax计算的维度索引。 它应该在范围 :math:`[ -  1,rank  -  1]` 中,而 :math:`rank` 是输入logits的秩。 默认值:-1。

返回:
  - 如果 ``return_softmax`` 为 False,则返回交叉熵损失
  - 如果 ``return_softmax`` 为 True,则返回元组 (loss, softmax) ,其中softmax和输入logits形状相同;除了axis维上的形为1,其余维上交叉熵损失和输入logits形状相同

返回类型:变量或者两个变量组成的元组


**代码示例**

..  code-block:: python

X
xsrobin 已提交
8923
  import paddle.fluid as fluid
C
Cheerego 已提交
8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127
  data = fluid.layers.data(name='data', shape=[128], dtype='float32')
        label = fluid.layers.data(name='label', shape=[1], dtype='int64')
        fc = fluid.layers.fc(input=data, size=100)
        out = fluid.layers.softmax_with_cross_entropy(
        logits=fc, label=label)










.. _cn_api_fluid_layers_space_to_depth:

space_to_depth
-------------------------------

.. py:function:: paddle.fluid.layers.space_to_depth(x, blocksize, name=None)

给该函数一个 ``blocksize`` 值,可以对形为[batch, channel, height, width]的输入LoD张量进行space_to_depth(广度至深度)运算。

该运算对成块的空间数据进行重组,形成深度。确切地说,该运算输出一个输入LoD张量的拷贝,其高度,宽度维度上的值移动至通道维度上。

``blocksize`` 参数指明了数据块大小。

重组时,依据 ``blocksize`` , 生成形为 :math:`[batch, channel * blocksize * blocksize, height/blocksize, width/blocksize]` 的输出:

该运算适用于在卷积间重放缩激励函数,并保持所有的数据。

 - 在各位置上,不重叠的,大小为 :math:`block\_size * block\_size` 的块重组入深度depth
 - 输出张量的深度为 :math:`block\_size * block\_size * input\_channel`
 - 输入各个块中的Y,X坐标变为输出张量通道索引的高序部位
 - channel可以被blocksize的平方整除
 - 高度,宽度可以被blocksize整除

参数:
  - **x** (variable) – 输入LoD张量
  - **blocksize** (variable) – 在每个特征图上选择元素时采用的块大小,应该 > 2

返回:输出LoD tensor

返回类型:Variable

抛出异常:
  - ``TypeError`` - ``blocksize`` 必须是long类型

**代码示例**

..  code-block:: python

    import paddle.fluid as fluid
    import numpy as np

    data = fluid.layers.data(
        name='data', shape=[1, 4, 2, 2], dtype='float32', append_batch_size=False)
    space_to_depthed = fluid.layers.space_to_depth(
        x=data, blocksize=2)

    exe = fluid.Executor(fluid.CUDAPlace(0))
    data_np = np.arange(0,16).reshape((1,4,2,2)).astype('float32')
    out_main = exe.run(fluid.default_main_program(),
                  feed={'data': data_np},
                  fetch_list=[space_to_depthed])





.. _cn_api_fluid_layers_spectral_norm:

spectral_norm
-------------------------------

.. py:function:: paddle.fluid.layers.spectral_norm(weight, dim=0, power_iters=1, eps=1e-12, name=None)

**Spectral Normalization Layer**

该层计算了fc、conv1d、conv2d、conv3d层的权重参数的谱正则值,其参数应分别为2-D, 3-D, 4-D, 5-D。计算结果如下。

步骤1:生成形状为[H]的向量U,以及形状为[W]的向量V,其中H是输入权重的第 ``dim`` 个维度,W是剩余维度的乘积。

步骤2: ``power_iters`` 应该是一个正整数,用U和V迭代计算 ``power_iters`` 轮。

.. math::

    \mathbf{v} &:= \frac{\mathbf{W}^{T} \mathbf{u}}{\|\mathbf{W}^{T} \mathbf{u}\|_2}\\
    \mathbf{u} &:= \frac{\mathbf{W}^{T} \mathbf{v}}{\|\mathbf{W}^{T} \mathbf{v}\|_2}

步骤3:计算 \sigma(\mathbf{W}) 并权重值归一化。

.. math::
    \sigma(\mathbf{W}) &= \mathbf{u}^{T} \mathbf{W} \mathbf{v}\\
    \mathbf{W} &= \frac{\mathbf{W}}{\sigma(\mathbf{W})}

可参考: `Spectral Normalization <https://arxiv.org/abs/1802.05957>`_

参数:
    - **weight** (Variable)-spectral_norm算子的输入权重张量,可以是2-D, 3-D, 4-D, 5-D张量,它是fc、conv1d、conv2d、conv3d层的权重。
    - **dim** (int)-将输入(weight)重塑为矩阵之前应排列到第一个的维度索引,如果input(weight)是fc层的权重,则应设置为0;如果input(weight)是conv层的权重,则应设置为1,默认为0。
    - **power_iters** (int)-将用于计算spectral norm的功率迭代次数,默认值1
    - **eps** (float)-epsilon用于计算规范中的数值稳定性
    - **name** (str)-此层的名称,可选。

返回:谱正则化后权重参数的张量变量

返回类型:Variable

**代码示例**:

.. code-block:: python

   import paddle.fluid as fluid

   weight = fluid.layers.data(name='weight', shape=[2, 8, 32, 32], append_batch_size=False, dtype='float32')
   x = fluid.layers.spectral_norm(weight=weight, dim=1, power_iters=2)





.. _cn_api_fluid_layers_split:

split
-------------------------------

.. py:function:: paddle.fluid.layers.split(input,num_or_sections,dim=-1,name=None)

将输入张量分解成多个子张量

参数:
    - **input** (Variable)-输入变量,类型为Tensor或者LoDTensor
    - **num_or_sections** (int|list)-如果num_or_sections是整数,则表示张量平均划分为的相同大小子张量的数量。如果num_or_sections是一列整数,列表的长度代表子张量的数量,整数依次代表子张量的dim维度的大小
    - **dim** (int)-将要划分的维。如果dim<0,划分的维为rank(input)+dim
    - **name** (str|None)-该层名称(可选)。如果设置为空,则自动为该层命名

返回:一列分割张量

返回类型:列表(Variable)

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid

    # 输入是维为[-1, 3,9,5]的张量:
    input = fluid.layers.data(
         name="input", shape=[3, 9, 5], dtype="float32")

    x0, x1, x2 = fluid.layers.split(x, num_or_sections=3, dim=2)
    # x0.shape  [-1, 3, 3, 5]
    # x1.shape  [-1, 3, 3, 5]
    # x2.shape  [-1, 3, 3, 5]
    
    x0, x1, x2 = fluid.layers.split(input, num_or_sections=[2, 3, 4], dim=2)
    # x0.shape  [-1, 3, 2, 5]
    # x1.shape  [-1, 3, 3, 5]
    # x2.shape  [-1, 3, 4, 5]









.. _cn_api_fluid_layers_square_error_cost:

square_error_cost
-------------------------------

.. py:function:: paddle.fluid.layers.square_error_cost(input,label)

方差估计层(Square error cost layer)

该层接受输入预测值和目标值,并返回方差估计

对于预测值X和目标值Y,公式为:

.. math::

    Out = (X-Y)^{2}

在以上等式中:
    - **X** : 输入预测值,张量(Tensor)
    - **Y** : 输入目标值,张量(Tensor)
    - **Out** : 输出值,维度和X的相同

参数:
    - **input** (Variable) - 输入张量(Tensor),带有预测值
    - **label** (Variable) - 标签张量(Tensor),带有目标值

返回:张量变量,存储输入张量和标签张量的方差

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
9128
    import paddle.fluid as fluid
C
Cheerego 已提交
9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180
    y = fluid.layers.data(name='y', shape=[1], dtype='float32')
    y_predict = fluid.layers.data(name='y_predict', shape=[1], dtype='float32')
    cost = fluid.layers.square_error_cost(input=y_predict, label=y)









.. _cn_api_fluid_layers_squeeze:

squeeze
-------------------------------

.. py:function:: paddle.fluid.layers.squeeze(input, axes, name=None)

向张量维度中移除单维输入。传入用于压缩的轴。如果未提供轴,所有的单一维度将从维中移除。如果选择的轴的形状条目不等于1,则报错。

::


    例如:

    例1:
        给定
            X.shape = (1,3,1,5)
            axes = [0]
        得到
            Out.shape = (3,1,5)
    例2:
        给定
            X.shape = (1,3,1,5)
            axes = []
        得到
            Out.shape = (3,5)

参数:
        - **input** (Variable)-将要压缩的输入变量
        - **axes** (list)-一列整数,代表压缩的维
        - **name** (str|None)-该层名称

返回:输出压缩的变量

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
9181
    import paddle.fluid as fluid
C
Cheerego 已提交
9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260
    import paddle.fluid.layers as layers
    x = fluid.layers.data(name='x', shape=[5, 1, 10])
    y = fluid.layers.sequeeze(input=x, axes=[1])









.. _cn_api_fluid_layers_stack:

stack
-------------------------------

.. py:function:: paddle.fluid.layers.stack(x, axis=0)

实现了stack层。

沿 ``axis`` 轴,该层对输入 ``x`` 进行stack运算。

输入 x 可以是单个变量, 或是多个变量组成的列表或元组。如果 x 是一个列表或元组, 那么这些变量必须同形。 假设每个输入的形都为 :math:`[d_0,d_1,...,d_{n−1}]` , 则输出变量的形为 :math:`[d_0,d_1,...,d_{axis}=len(x),...,d_{n−1}]` 。 如果 ``axis`` < 0, 则将其取代为 :math:`axis+rank(x[0])+1` 。 如果 ``axis`` 为 None, 则认为它是 0。


例如:

.. code-block:: text

    例1:
      输入:
        x[0].data = [ [1.0 , 2.0 ] ]
        x[0].dims = [1, 2]
        x[1].data = [ [3.0 , 4.0 ] ]
        x[1].dims = [1, 2]
        x[2].data = [ [5.0 , 6.0 ] ]
        x[2].dims = [1, 2]

      参数:
        axis = 0

      输出:
        Out.data =[ [ [1.0, 2.0] ],
                    [ [3.0, 4.0] ],
                    [ [5.0, 6.0] ] ]
        Out.dims = [3, 1, 2]

    例2:
      如果
        x[0].data = [ [1.0 , 2.0 ] ]
        x[0].dims = [1, 2]
        x[1].data = [ [3.0 , 4.0 ] ]
        x[1].dims = [1, 2]
        x[2].data = [ [5.0 , 6.0 ] ]
        x[2].dims = [1, 2]

      参数:
        axis = 1 or axis = -2

      输出:
        Out.data =[ [ [1.0, 2.0]
                      [3.0, 4.0]
                      [5.0, 6.0] ] ]
        Out.dims = [1, 3, 2]

参数:

  - **x** (Variable|list(Variable)|tuple(Variable)) – 输入变量
  - **axis** (int|None) – 对输入进行stack运算所在的轴

返回: 经stack运算后的变量

返回类型: Variable

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
9261
    import paddle.fluid as fluid
C
Cheerego 已提交
9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298
    import paddle.fluid.layers as layers
    x1 = layers.data(name='x1', shape=[1, 2], dtype='int32')
    x2 = layers.data(name='x2', shape=[1, 2], dtype='int32')
    data = layers.stack([x1,x2])







.. _cn_api_fluid_layers_stanh:

stanh
-------------------------------

.. py:function:: paddle.fluid.layers.stanh(x, scale_a=0.6666666666666666, scale_b=1.7159, name=None)

STanh 激活算子(STanh Activation Operator.)

.. math::
          \\out=b*\frac{e^{a*x}-e^{-a*x}}{e^{a*x}+e^{-a*x}}\\

参数:
    - **x** (Variable) - STanh operator的输入
    - **scale_a** (FLOAT|2.0 / 3.0) - 输入的a的缩放参数
    - **scale_b** (FLOAT|1.7159) - b的缩放参数
    - **name** (str|None) - 这个层的名称(可选)。如果设置为None,该层将被自动命名。

返回: STanh操作符的输出

返回类型: 输出(Variable)

**代码示例:**

.. code-block:: python

X
xsrobin 已提交
9299
    import paddle.fluid as fluid
C
Cheerego 已提交
9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331
    x = fluid.layers.data(name="x", shape=[3,10,32,32], dtype="float32")
    y = fluid.layers.stanh(x, scale_a=0.67, scale_b=1.72)







.. _cn_api_fluid_layers_sum:

sum
-------------------------------

.. py:function:: paddle.fluid.layers.sum(x)

sum算子。

该算子对输入张量求和。所有输入都可以携带LoD(详细程度)信息,但是输出仅与第一个输入共享LoD信息。

参数:
        - **x** (Variable)- (vector <Tensor>)sum算子的输入张量(Tensor)。

返回:        (Tensor)求和算子的输出张量。

返回类型:        Variable


**代码示例:**

.. code-block:: python

X
xsrobin 已提交
9332
    import paddle.fluid as fluid
C
Cheerego 已提交
9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369
    import paddle.fluid.layers as layers
    input0 = fluid.layers.data(name="input0", shape=[13, 11], dtype='float32')
    input1 = layers.data(name="input1", shape=[13, 11], dtype='float32')
    out = fluid.layers.sum([input0,input1])







.. _cn_api_fluid_layers_swish:

swish
-------------------------------

.. py:function:: paddle.fluid.layers.swish(x, beta=1.0, name=None)

Swish 激活函数

.. math::
         out = \frac{x}{1 + e^{- beta x}}

参数:
    - **x** (Variable) -  Swish operator 的输入
    - **beta** (浮点|1.0) - Swish operator 的常量beta
    - **name** (str|None) - 这个层的名称(可选)。如果设置为None,该层将被自动命名。

返回: Swish operator 的输出

返回类型: output(Variable)


**代码示例:**

.. code-block:: python

X
xsrobin 已提交
9370
  import paddle.fluid as fluid
C
Cheerego 已提交
9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463
  x = fluid.layers.data(name="x", shape=[3,10,32,32], dtype="float32")
  y = fluid.layers.swish(x, beta=2.0)


.. _cn_api_fluid_layers_teacher_student_sigmoid_loss:

teacher_student_sigmoid_loss
-----------------------------------

.. py:function:: paddle.fluid.layers.teacher_student_sigmoid_loss(input, label, soft_max_up_bound=15.0, soft_max_lower_bound=-15.0)

**Teacher Student Log Loss Layer(教师--学生对数损失层)**

此图层接受输入预测和目标标签,并返回teacher_student损失。

.. math::

    loss = max(x, 0) - x * z + log(1 + exp(-abs(x))) + max(x, 0) - x * z' + log(1 + exp(-abs(x)))


参数:
  - **input**  (Variable|list) – 形状为[N x 1]的二维张量,其中N是批大小batch size。 该输入是由前一个运算计算而得的概率。
  - **label**  (Variable|list) – 具有形状[N x 1]的二维张量的真实值,其中N是批大小batch_size。
  - **soft_max_up_bound**  (float) – 若input > soft_max_up_bound, 输入会被向下限制。默认为15.0
  - **soft_max_lower_bound**  (float) – 若input < soft_max_lower_bound, 输入将会被向上限制。默认为-15.0

返回:具有形状[N x 1]的2-D张量,teacher_student_sigmoid_loss。

返回类型:变量

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid
     
    batch_size = 64
    label = fluid.layers.data(
              name="label", shape=[batch_size, 1], dtype="int64", append_batch_size=False)
    similarity = fluid.layers.data(
              name="similarity", shape=[batch_size, 1], dtype="float32", append_batch_size=False)
    cost = fluid.layers.teacher_student_sigmoid_loss(input=similarity, label=label)


.. _cn_api_fluid_layers_temporal_shift:

temporal_shift
-------------------------------
.. py:function:: paddle.fluid.layers.temporal_shift(x, seg_num, shift_ratio=0.25, name=None)

**Temporal Shift Operator**

此运算符计算输入(x)的时间移位特征。

输入(x)的形状应为[N*T, C, H, W],N是批大小,T是 ``seg_num`` 指定的时间段号,C是通道号,H和W是特征的高度和宽度。

时间偏移计算如下:

步骤1:将输入(X)重塑为[N、T、C、H、W]。

步骤2:填充0到第二个(T)尺寸的变形结果,填充宽度每边为1,填充结果的形状为[N,T+2,C,H,W]。

步骤3:假设shift_ratio为1/4,切片填充结果如下:

.. math::

    slice1 &= x[:, :T, :C/4, :, :]

    slice2 &= x[:, 2:T+2, C/4:C/2, :, :]

    slice3 &= x[:, 1:T+1, C/2:, :, :]

步骤4:沿第3(C)维连接三个切片,并将结果重塑为[N*T, C, H, W]。

有关时间移动的详细信息,请参阅文件: `Temporal Shift Module <https://arxiv.org/abs/1811.08383>`_

参数:
  - **x**  (Variable) – 时移算符的输入张量。这是一个4维张量,形状为[N*T,C,H,W]。N为批量大小,T为时间段数,C为信道数,H为特征高度,W为特征宽度
  - **seg_num**  (int) – 时间段编号,这应该是一个正整数。
  - **shift_ratio**  (float) – 通道的移位比、通道的第一个 ``shift_ratio`` 部分沿时间维度移动-1,通道的第二个 ``shift_ratio`` 部分沿时间维度移动1。默认值0.25
  - **name**  (str, default None) – 该层名称

返回:时间移位结果是一个与输入形状和类型相同的张量变量

返回类型:out(Variable)

抛出异常: ``TypeError`` – seg_num 必须是int类型


**代码示例**:

.. code-block:: python

X
xsrobin 已提交
9464
    import paddle.fluid as fluid
C
Cheerego 已提交
9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517
    input = fluid.layers.data(name='input', shape=[4,2,2], dtype='float32')
    out = fluid.layers.temporal_shift(x=input, seg_num=2, shift_ratio=0.2)



.. _cn_api_fluid_layers_topk:

topk
-------------------------------
.. py:function:: paddle.fluid.layers.topk(input, k, name=None)

这个算子用于查找最后一维的前k个最大项,返回它们的值和索引。

如果输入是(1-D Tensor),则找到向量的前k最大项,并以向量的形式输出前k最大项的值和索引。values[j]是输入中第j最大项,其索引为indices[j]。
如果输入是更高阶的张量,则该operator会基于最后一维计算前k项

例如:

.. code-block:: text


    如果:
        input = [[5, 4, 2, 3],
                [9, 7, 10, 25],
                [6, 2, 10, 1]]
        k = 2

    则:
        第一个输出:
        values = [[5, 4],
                [10, 25],
                [6, 10]]

        第二个输出:
        indices = [[0, 1],
                [2, 3],
                [0, 2]]

参数:
    - **input** (Variable)-输入变量可以是一个向量或者更高阶的张量
    - **k** (int|Variable)-在输入最后一维中寻找的前项数目
    - **name** (str|None)-该层名称(可选)。如果设为空,则自动为该层命名。默认为空

返回:含有两个元素的元组。元素都是变量。第一个元素是最后维切片的前k项。第二个元素是输入最后维里值索引

返回类型:元组[变量]

抛出异常: ``ValueError`` - 如果k<1或者k不小于输入的最后维

**代码示例**:

.. code-block:: python

X
xsrobin 已提交
9518
    import paddle.fluid as fluid
C
Cheerego 已提交
9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595
    import paddle.fluid.layers as layers
    input = layers.data(name="input", shape=[13, 11], dtype='float32')
    top5_values, top5_indices = fluid.layers.topk(input, k=5)









.. _cn_api_fluid_layers_transpose:

transpose
-------------------------------

.. py:function:: paddle.fluid.layers.transpose(x,perm,name=None)

根据perm对输入矩阵维度进行重排。

返回张量(tensor)的第i维对应输入维度矩阵的perm[i]。

参数:
    - **x** (Variable) - 输入张量(Tensor)
    - **perm** (list) - 输入维度矩阵的转置
    - **name** (str) - 该层名称(可选)

返回: 转置后的张量(Tensor)

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

    # 请使用 append_batch_size=False 来避免
    # 在数据张量中添加多余的batch大小维度
    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[5, 10, 15],
                    dtype='float32', append_batch_size=False)
    x_transposed = fluid.layers.transpose(x, perm=[1, 0, 2])




.. _cn_api_fluid_layers_tree_conv:

tree_conv
-------------------------------

.. py:function:: paddle.fluid.layers.tree_conv(nodes_vector, edge_set, output_size, num_filters=1, max_depth=2, act='tanh', param_attr=None, bias_attr=None, name=None)

基于树结构的卷积Tree-Based Convolution运算。

基于树的卷积是基于树的卷积神经网络(TBCNN,Tree-Based Convolution Neural Network)的一部分,它用于对树结构进行分类,例如抽象语法树。 Tree-Based Convolution提出了一种称为连续二叉树的数据结构,它将多路(multiway)树视为二叉树。 提出基于树的卷积论文: https://arxiv.org/abs/1409.5718v1

参数:
    - **nodes_vector**  (Variable) – (Tensor) 树上每个节点的特征向量(vector)。特征向量的形状必须为[max_tree_node_size,feature_size]
    - **edge_set**  (Variable) – (Tensor) 树的边。边必须带方向。边集的形状必须是[max_tree_node_size,2]
    - **output_size**  (int) – 输出特征宽度
    - **num_filters**  (int) – filter数量,默认值1
    - **max_depth**  (int) – filter的最大深度,默认值2
    - **act**  (str) – 激活函数,默认 tanh
    - **param_attr**  (ParamAttr) – filter的参数属性,默认None
    - **bias_attr**  (ParamAttr) – 此层bias的参数属性,默认None
    - **name**  (str) – 此层的名称(可选)。如果设置为None,则将自动命名层,默认为None


返回: (Tensor)子树的特征向量。输出张量的形状是[max_tree_node_size,output_size,num_filters]。输出张量可以是下一个树卷积层的新特征向量

返回类型:out(Variable)

**代码示例**:

.. code-block:: python
    
X
xsrobin 已提交
9596
    import paddle.fluid as fluid
C
Cheerego 已提交
9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650
    # 10 代表数据集的最大节点大小max_node_size,5 代表向量宽度
    nodes_vector = fluid.layers.data(name='vectors', shape=[10, 5], dtype='float32')
    # 10 代表数据集的最大节点大小max_node_size, 2 代表每条边连接两个节点
    # 边必须为有向边
    edge_set = fluid.layers.data(name='edge_set', shape=[10, 2], dtype='float32')

    # 输出的形状会是[None, 10, 6, 1],
    # 10 代表数据集的最大节点大小max_node_size, 6 代表输出大小output size, 1 代表 1 个filter
    
    out_vector = fluid.layers.tree_conv(nodes_vector, edge_set, 6, 1, 2)
    # reshape之后, 输出张量output tensor为下一个树卷积的nodes_vector
    out_vector = fluid.layers.reshape(out_vector, shape=[-1, 10, 6])
    
    
    out_vector_2 = fluid.layers.tree_conv(out_vector, edge_set, 3, 4, 2)
    
    # 输出tensor也可以用来池化(论文中称为global pooling)
    pooled = fluid.layers.reduce_max(out_vector, dims=2) # 全局池化






.. _cn_api_fluid_layers_uniform_random_batch_size_like:

uniform_random_batch_size_like
-------------------------------

.. py:function:: paddle.fluid.layers.uniform_random_batch_size_like(input, shape, dtype='float32', input_dim_idx=0, output_dim_idx=0, min=-1.0, max=1.0, seed=0)

uniform_random_batch_size_like算子。

此算子使用与输入张量(Tensor)相同的batch_size初始化张量(Tensor),并使用从均匀分布中采样的随机值。

参数:
        - **input** (Variable)- 其input_dim_idx'th维度指定batch_size的张量(Tensor)。
        - **shape** (元组|列表)- 输出的形状。
        - **input_dim_idx** (Int)- 默认值0.输入批量大小维度的索引。
        - **output_dim_idx** (Int)- 默认值0.输出批量大小维度的索引。
        - **min** (Float)- (默认 1.0)均匀随机的最小值。
        - **max** (Float)- (默认 1.0)均匀随机的最大值。
        - **seed** (Int)- (int,default 0)用于生成样本的随机种子。0表示使用系统生成的种子。注意如果seed不为0,则此算子将始终每次生成相同的随机数。
        - **dtype** (np.dtype | core.VarDesc.VarType | str) - 数据类型:float32,float_16,int等。

返回:        指定形状的张量(Tensor)将使用指定值填充。

返回类型:        Variable


**代码示例:**

.. code-block:: python

X
xsrobin 已提交
9651
    import paddle.fluid as fluid
C
Cheerego 已提交
9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794
    import paddle.fluid.layers as layers

    input = fluid.layers.data(name="input", shape=[13, 11], dtype='float32')
    out = fluid.layers.uniform_random_batch_size_like(input, [-1, 11])





.. _cn_api_fluid_layers_unsqueeze:

unsqueeze
-------------------------------

.. py:function:: paddle.fluid.layers.unsqueeze(input, axes, name=None)

向张量shape中插入一个维度。该接口接受axes列表,来指定要插入的维度位置。相应维度变化可以在输出变量中axes指定的索引位置上体现。

比如:
    给定一个张量,例如维度为[3,4,5]的张量,使用 axes列表为[0,4]来unsqueeze它,则输出维度为[1,3,4,5,1]

参数:
    - **input** (Variable)- 未压缩的输入变量
    - **axes** (list)- 一列整数,代表要插入的维数
    - **name** (str|None) - 该层名称

返回:输出未压缩变量

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[5, 10])
    y = fluid.layers.unsequeeze(input=x, axes=[1])










.. _cn_api_fluid_layers_unstack:

unstack
-------------------------------

.. py:function:: paddle.fluid.layers.unstack(x, axis=0, num=None)

实现了unstack层。

沿 ``axis`` 轴,该层对输入 ``x`` 进行unstack运算。

如果 ``axis`` <0,则将其以 :math:`axis+rank(x)` 代之。

如果 ``num`` 为 None,则它可以从 ``x.shape[axis]`` 中推断而来。

如果 ``x.shape[axis]`` <= 0或者Unknown, 则抛出异常 ``ValueError`` 。

参数:
  - **x** (Variable|list(Variable)|tuple(Variable)) – 输入变量
  - **axis** (int|None) – 对输入进行unstack运算所在的轴
  - **num** (int|None) - 输出变量的数目

返回: 经unstack运算后的变量

返回类型: list(Variable)

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid
    x = fluid.layers.data(name='x', shape=[5, 10], dtype='float32')
    y = fluid.layers.unstack(x, axis=1)







.. _cn_api_fluid_layers_warpctc:

warpctc
-------------------------------

.. py:function:: paddle.fluid.layers.warpctc(input, label, blank=0, norm_by_times=False, use_cudnn=False)

该操作符集成了 `开源Warp-CTC库 <https://github.com/baidu-research/warp-ctc>`_ ,计算基于神经网络的时序类分类(CTC)损失。原生softmax激活函数集成到Wrap-CTC库中,操作符也可称作含CTC的softmax,将输入张量每一行的值正则化。

参数:
    - **input** (Variable) - 变长序列的非尺度化概率,是一个含LoD信息的二维张量。shape为[Lp,num_classes+1],Lp是所有输出序列长度之和,num_classes是实际类别数。(不包括空白标签)
    - **label** (Variable) - 变长序列中正确标记的数据,是一个含LoD信息的二维张量。shape为[Lg,1],Lg是所有标签长度之和
    - **blank** (int,默认0) - 基于神经网络的时序类分类(CTC)损失的空白标签索引,在半开区间间隔内[0,num_classes+1]
    - **norm_by_times** (bool,默认false) - 是否利用时间步长(即序列长度)的数量对梯度进行正则化。如果warpctc层后面跟着mean_op则无需对梯度正则化。
    - **use_cudnn** (bool, 默认false) - 是否使用cudnn

返回:基于神经网络的时序类分类(CTC)损失,是一个shape为[batch_size,1]的二维张量

返回类型:变量(Variable)

**代码示例**:

.. code-block:: python

    import paddle.fluid as fluid
    label = fluid.layers.data(name='label', shape=[11, 8],
                              dtype='float32', lod_level=1)
    predict = fluid.layers.data(name='predict', shape=[11, 1],
                                dtype='float32')
    cost = fluid.layers.warpctc(input=predict, label=label)




.. _cn_api_fluid_layers_where:

where
-------------------------------

.. py:function:: paddle.fluid.layers.where(condition)
     
返回一个秩为2的int64型张量,指定condition中真实元素的坐标。
     
输出的第一维是真实元素的数量,第二维是condition的秩(维数)。如果没有真实元素,则将生成空张量。
        
参数:
    - **condition** (Variable) - 秩至少为1的布尔型张量。

返回:存储一个二维张量的张量变量

返回类型:变量(Variable)
     
**代码示例**:

.. code-block:: python

X
xsrobin 已提交
9795 9796 9797
        import paddle.fluid as fluid
        import paddle.fluid.layers as layers
        import numpy as np
C
Cheerego 已提交
9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810
        # condition为张量[True, False, True]
        out = fluid.layers.where(condition) # [[0], [2]]
     
        # condition为张量[[True, False], [False, True]]
        out = fluid.layers.where(condition) # [[0, 0], [1, 1]]
     
        # condition为张量[False, False, False]
        out = fluid.layers.where(condition) # [[]]