提交 37965806 编写于 作者: S ShenLiang 提交者: Yi Liu

fix gather_nd、scatter_nd_add、scatter_nd、center_loss (#1333)

* fix gather_nd scatter_nd_add scatter_nd, test=develop

* fix centerloss, test=develop

* fix the description, test=develop

* fix the doc, test=develop
上级 72fa6c92
......@@ -108,6 +108,7 @@ fluid.layers
layers_cn/floor_cn.rst
layers_cn/fsp_matrix_cn.rst
layers_cn/gather_cn.rst
layers_cn/gather_nd_cn.rst
layers_cn/gaussian_random_batch_size_like_cn.rst
layers_cn/gaussian_random_cn.rst
layers_cn/generate_mask_labels_cn.rst
......@@ -228,6 +229,8 @@ fluid.layers
layers_cn/sampling_id_cn.rst
layers_cn/scale_cn.rst
layers_cn/scatter_cn.rst
layers_cn/scatter_nd_add_cn.rst
layers_cn/scatter_nd_cn.rst
layers_cn/selu_cn.rst
layers_cn/sequence_concat_cn.rst
layers_cn/sequence_conv_cn.rst
......
......@@ -5,8 +5,8 @@ center_loss
.. py:function:: paddle.fluid.layers.center_loss(input, label, num_classes, alpha, param_attr, update_center=True)
center_loss层
该层接收一个来自于最后一个隐藏层的输出和目标标签作为输入,返回损失值。
该OP接收一个来自于最后一个隐藏层的输出和目标标签作为输入,返回损失值。为每一个类别提供一个类别中心,计算mini-batch中每个样本与对应类别中心的距离的平均值作为center loss。
对于输入,\(X\)和标签\(Y\),计算公式为:
.. math::
......@@ -17,14 +17,14 @@ center_loss层
参数:
- **input** (Variable) - 形为[N x M]的2维张量
- **label** (Variable) - groud truth,一个形为[N x 1]的2维张量,N表示batch size
- **num_class** (int) - 表示类别的数
- **alpha** (float|Variable) - 学习率
- **param_attr** (ParamAttr) - 参数初始化
- **update_center** (bool) - 是否更新损失值
- **input** (Variable) - 输入形状为[N x M]的2维张量,数据类型为float32,float64。
- **label** (Variable) - 输入的标签,一个形状为为[N x 1]的2维张量,N表示batch size,数据类型为int32。
- **num_class** (int32) - 输入类别的数量。
- **alpha** (float32|float64|Variable) - 学习率。数据类型为float32或者float64。
- **param_attr** (ParamAttr) - 指定权重参数属性的对象。具体用法请参见 :ref:`cn_api_fluid_ParamAttr` 。
- **update_center** (bool) - 是否更新类别中心的参数。
返回:形为[N x 1]的2维张量
返回:形状为[N x 1]的2维Tensor|LoDTensor
返回类型:Variable
......
.. _cn_api_fluid_layers_gather_nd:
gather_nd
-------------------------------
.. py:function:: paddle.fluid.layers.gather_nd(input, index, name=None)
该OP是 :code:`gather` 的高维推广,并且支持多轴同时索引。 :code:`index` 是一个K维度的张量,它可以认为是从 :code:`input` 中取K-1维张量,每一个元素是一个切片:
.. math::
output[(i_0, ..., i_{K-2})] = input[index[(i_0, ..., i_{K-2})]]
显然, :code:`index.shape[-1] <= input.rank` 并且输出张量的维度是 :code:`index.shape[:-1] + input.shape[index.shape[-1]:]` 。
示例:
::
给定:
input = [[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]]
input.shape = (2, 3, 4)
- 案例 1:
index = [[1]]
gather_nd(input, index)
= [input[1, :, :]]
= [[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]
- 案例 2:
index = [[0,2]]
gather_nd(input, index)
= [input[0, 2, :]]
= [8, 9, 10, 11]
- 案例 3:
index = [[1, 2, 3]]
gather_nd(input, index)
= [input[1, 2, 3]]
= [23]
参数:
- **input** (Variable) - 输入张量,数据类型可以是int32,int64,float32,float64。
- **index** (int) - 输入的索引张量,数据类型为非负int32或非负int64。它的维度 :code:`index.rank` 必须大于1,并且 :code:`index.shape[-1] <= input.rank` 。
- **name** (string) - 该层的名字,默认值为None,表示会自动命名。
返回:shape为index.shape[:-1] + input.shape[index.shape[-1]:]的Tensor|LoDTensor,数据类型与 :code:`input` 一致。
返回类型:Variable
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
x = fluid.layers.data(name='x', shape=[3, 4, 5], dtype='float32')
index = fluid.layers.data(name='index', shape=[2, 2], dtype='int32')
output = fluid.layers.gather_nd(x, index)
.. _cn_api_fluid_layers_scatter_nd_add:
scatter_nd_add
-------------------------------
.. py:function:: paddle.fluid.layers.scatter_nd_add(ref, index, updates, name=None)
该OP通过对Variable中的单个值或切片应用稀疏加法,从而得到输出的Variable。
:code:`ref` 是维度为 :code:`R` 的张量。 :code:`index` 是维度为 :code:`K` 的张量。因此, :code:`index` 的形状是 :math:`[i_0, i_1, ..., i_{K-2}, Q]` ,其中 :math:`Q \leq R` 。:code:`updates` 是一个维度为 :math:`K - 1 + R - Q` 的张量,它的形状是 :math:`index.shape[:-1] + ref.shape[index.shape[-1]:]` 。
根据 :code:`index` 的 :math:`[i_0, i_1, ..., i_{K-2}]` 得到相应的 :code:`updates` 切片,将其加到根据 :code:`index` 的最后一维得到 :code:`ref` 切片上,从而得到最终的输出张量。
示例:
::
- 案例 1:
ref = [0, 1, 2, 3, 4, 5]
index = [[1], [2], [3], [1]]
updates = [9, 10, 11, 12]
得到:
output = [0, 22, 12, 14, 4, 5]
- 案例 2:
ref = [[65, 17], [-14, -25]]
index = [[], []]
updates = [[[-1, -2], [1, 2]],
[[3, 4], [-3, -4]]]
ref.shape = (2, 2)
index.shape = (2, 0)
updates.shape = (2, 2, 2)
得到:
output = [[67, 19], [-16, -27]]
参数:
- **ref** (Variable) - 输入张量,数据类型可以是int32,int64,float32,float64。
- **index** (Variable) - 输入的索引张量,数据类型为非负int32或非负int64。它的维度 :code:`index.rank` 必须大于1,并且 :code:`index.shape[-1] <= ref.rank`
- **updates** (Variable) - 输入的更新张量,它必须和 :code:`ref` 有相同的数据类型。形状必须是 :code:`index.shape[:-1] + ref.shape[index.shape[-1]:]` 。
- **name** (string) - 该层的名字,默认值为None,表示会自动命名。
返回:数据类型和形状都与 :code:`ref` 相同的Tensor|LoDTensor。
返回类型:Variable
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
ref = fluid.layers.data(name='ref', shape=[3, 5, 9, 10], dtype='float32', append_batch_size=False)
index = fluid.layers.data(name='index', shape=[3, 2], dtype='int32', append_batch_size=False)
updates = fluid.layers.data(name='update', shape=[3, 9, 10], dtype='float32', append_batch_size=False)
output = fluid.layers.scatter_nd_add(ref, index, updates)
.. _cn_api_fluid_layers_scatter_nd:
scatter_nd
-------------------------------
.. py:function:: paddle.fluid.layers.scatter_nd(index, updates, shape, name=None)
该OP根据 :code:`index` ,将 :code:`updates` 添加到一个新的张量中,从而得到输出的Variable。这个操作与 :code:`scatter_nd_add` 类似,除了形状为 :code:`shape` 的张量是通过零初始化的。相应地, :code:`scatter_nd(index, updates, shape)` 等价于 :code:`scatter_nd_add(fluid.layers.zeros(shape, updates.dtype), index, updates)` 。如果 :code:`index` 有重复元素,则将累积相应的更新,因此,由于数值近似问题,索引中重复元素的顺序不同可能会导致不同的输出结果。具体的计算方法可以参见 :code:`scatter_nd_add` 。该OP是 :code:`gather_nd` 的反函数。
参数:
- **index** (Variable) - 输入的索引张量,数据类型为非负int32或非负int64。它的维度 :code:`index.rank` 必须大于1,并且 :code:`index.shape[-1] <= len(shape)`
- **updates** (Variable) - 输入的更新张量。形状必须是 :code:`index.shape[:-1] + shape[index.shape[-1]:]` 。数据类型可以是int32,int64,float32,float64。
- **shape** (tuple|list) - 要求输出张量的形状。类型是tuple或者list。
- **name** (string) - 该层的名字,默认值为None,表示会自动命名。
返回:数据类型与 :code:`updates` 相同,形状是 :code:`shape` 的Tensor|LoDTensor。
返回类型:Variable
**代码示例**:
.. code-block:: python
import paddle.fluid as fluid
index = fluid.layers.data(name='index', shape=[3, 2], dtype='int64', append_batch_size=False)
updates = fluid.layers.data(name='update', shape=[3, 9, 10], dtype='float32', append_batch_size=False)
shape = [3, 5, 9, 10]
output = fluid.layers.scatter_nd(index, updates, shape)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册