未验证 提交 77570ace 编写于 作者: L liym27 提交者: GitHub

[API 2.0] Add Chinese doc of api `paddle.reshape` and fix cn_doc of `layers.reshape` (#2435)

上级 8161fbeb
......@@ -5,11 +5,6 @@ reshape
.. py:function:: paddle.fluid.layers.reshape(x, shape, actual_shape=None, act=None, inplace=False, name=None)
:alias_main: paddle.reshape
:alias: paddle.reshape,paddle.tensor.reshape,paddle.tensor.manipulation.reshape
:old_api: paddle.fluid.layers.reshape
该OP在保持输入 ``x`` 数据不变的情况下,改变 ``x`` 的形状。
......@@ -31,23 +26,24 @@ reshape
2. 给定一个形状为[2,4,6]的三维张量x,目标形状为[2,3,-1,2],则将x变换为形状为[2,3,4,2]的4-D张量,且x的数据保持不变。在这种情况下,目标形状的一个维度被设置为-1,这个维度的值是从x的元素总数和剩余维度推断出来的。
3. 给定一个形状为[2,4,6]的三维张量x,目标形状为[-1,0,3,2],则将x变换为形状为[2,4,3,2]的4-D张量,且x的数据保持不变。在这种情况下,0对应位置的维度值将从x的对应维数中复制,-1对应位置的维度值由x的元素总数和剩余维度推断出来。
**注意:参数** ``actual_shape`` **之后将被舍弃,只用参数** ``shape`` **来表示目标形状。**
.. warning::
参数 ``actual_shape`` 之后将被舍弃,只用参数 ``shape`` 来表示目标形状。
参数:
- **x** (Variable)- 多维 ``Tensor`` 或 ``LoDTensor``,数据类型为 ``float32``,``float64``,``int32``,或 ``int64``。
- **shape** (list|tuple|Variable)- 数据类型是 ``int32`` 。定义目标形状。目标形状最多只能有一个维度为-1。如果 ``shape`` 的类型是 list 或 tuple, 它的元素可以是整数或者形状为[1]的 ``Tensor`` 或 ``LoDTensor``。如果 ``shape`` 的类型是 ``Variable``,则是1-D的 ``Tensor`` 或 ``LoDTensor``。
- **actual_shape** (Variable,可选)- 1-D ``Tensor`` 或 ``LoDTensor``,默认值:`None`。如果 ``actual_shape`` 被提供,``actual_shape`` 具有比 ``shape`` 更高的优先级,此时 ``shape`` 只能是整数列表或元组。更新提示:``actual_shape`` 在未来的版本中将被舍弃,并用 ``shape`` 代替。
- **x** (Tensor)- N-D ``Tensor``,数据类型为 ``float32``,``float64``,``int32``,或 ``int64``。
- **shape** (list|tuple|Tensor)- 数据类型是 ``int32`` 。定义目标形状。目标形状最多只能有一个维度为-1。如果 ``shape`` 的类型是 list 或 tuple, 它的元素可以是整数或者形状为[1]的 ``Tensor``。如果 ``shape`` 的类型是 ``Tensor``,则是1-D的 ``Tensor``。
- **actual_shape** (Tensor,可选)- 1-D ``Tensor``,默认值:`None`。如果 ``actual_shape`` 被提供,``actual_shape`` 具有比 ``shape`` 更高的优先级,此时 ``shape`` 只能是整数列表或元组。更新提示:``actual_shape`` 在未来的版本中将被舍弃,并用 ``shape`` 代替。
- **act** (str,可选)- 对形状改变后的输入变量做非线性激活操作,激活函数类型可以参考 :ref:`api_guide_activations` 。默认值: ``None``。
- **inplace** (bool,可选)- 如果 ``inplace`` 为 ``True``,则 ``layers.reshape`` 的输入和输出是同一个变量,否则 ``layers.reshape`` 的输入和输出是不同的变量。默认值:``False``。请注意,如果 ``x`` 是多个OP的输入,则 ``inplace`` 必须为False。
- **name** (str,可选)- 具体用法请参见 :ref:`api_guide_Name` ,一般无需设置。默认值: ``None``。
返回:多维 ``Tensor`` 或 ``LoDTensor``,数据类型与 ``input`` 相同。如果 ``inplace`` 为 ``False``,则返回一个新的变量,否则将改变输入变量 ``x`` 自身。如果 ``act`` 为 ``None``,则直接返回形状改变后的变量,否则返回经过激活函数后的变量。
返回类型:Variable
返回:
:::::::::
``Tensor``,改变形状后的 ``Tensor``,数据类型与 ``x`` 相同。如果 ``inplace`` 为 ``False``,则返回一个新的变量,否则将改变输入变量 ``x`` 自身。如果 ``act`` 为 ``None``,则直接返回形状改变后的变量,否则返回经过激活函数后的变量
抛出异常:
- :code:`TypeError`:``actual_shape`` 的类型应该是 Variable 或 None。
- :code:`TypeError`:``starts`` 的类型应该是list、tuple 或 Variable
- :code:`TypeError`:``actual_shape`` 的类型应该是 Tensor 或 None。
- :code:`TypeError`:``starts`` 的类型应该是list、tuple 或 Tensor
- :code:`ValueError`:``shape`` 中至多有一个元素可以是-1。
- :code:`ValueError`:``shape`` 中的元素为0时,对应的维度应该小于等于``x``的维度。
- :code:`ValueError`:``shape`` 中的元素除了-1之外,都应该是非负值。
......@@ -59,7 +55,7 @@ reshape
import paddle.fluid as fluid
# example 1:
# attr shape is a list which doesn't contain tensor Variable.
# attr shape is a list which doesn't contain Tensors.
data_1 = fluid.data(
name='data_1', shape=[2, 4, 6], dtype='float32')
reshaped_1 = fluid.layers.reshape(
......@@ -67,7 +63,7 @@ reshape
# the shape of reshaped_1 is [2,4,3,2].
# example 2:
# attr shape is a list which contains tensor Variable.
# attr shape is a list which contains Tensors.
data_2 = fluid.layers.fill_constant([2,25], "int32", 3)
dim = fluid.layers.fill_constant([1], "int32", 5)
reshaped_2 = fluid.layers.reshape(data_2, shape=[dim, 10])
......
......@@ -2,6 +2,68 @@
reshape
-------------------------------
:doc_source: paddle.fluid.layers.reshape
.. py:function:: paddle.reshape(x, shape, name=None)
:alias_main: paddle.reshape
:alias: paddle.reshape,paddle.tensor.reshape,paddle.tensor.manipulation.reshape
该OP在保持输入 ``x`` 数据不变的情况下,改变 ``x`` 的形状。
在指定目标shape时存在一些技巧:
.. code-block:: text
1. -1 表示这个维度的值是从x的元素总数和剩余维度推断出来的。因此,有且只有一个维度可以被设置为-1。
2. 0 表示实际的维数是从x的对应维数中复制出来的,因此shape中0的索引值不能超过x的维度。
这里有一些例子来解释它们:
.. code-block:: text
1. 给定一个形状为[2,4,6]的三维张量x,目标形状为[6,8],则将x变换为形状为[6,8]的2-D张量,且x的数据保持不变。
2. 给定一个形状为[2,4,6]的三维张量x,目标形状为[2,3,-1,2],则将x变换为形状为[2,3,4,2]的4-D张量,且x的数据保持不变。在这种情况下,目标形状的一个维度被设置为-1,这个维度的值是从x的元素总数和剩余维度推断出来的。
3. 给定一个形状为[2,4,6]的三维张量x,目标形状为[-1,0,3,2],则将x变换为形状为[2,4,3,2]的4-D张量,且x的数据保持不变。在这种情况下,0对应位置的维度值将从x的对应维数中复制,-1对应位置的维度值由x的元素总数和剩余维度推断出来。
参数:
- **x** (Tensor)- 多维 ``Tensor``,数据类型为 ``float32``,``float64``,``int32``,或 ``int64``。
- **shape** (list|tuple|Tensor)- 数据类型是 ``int32`` 。定义目标形状。目标形状最多只能有一个维度为-1。如果 ``shape`` 的类型是 list 或 tuple, 它的元素可以是整数或者形状为[1]的 ``Tensor`` 或 ``LoDTensor``。如果 ``shape`` 的类型是 ``Tensor``,则是1-D的 ``Tensor`` 或 ``LoDTensor``。
- **name** (str,可选)- 具体用法请参见 :ref:`api_guide_Name` ,一般无需设置。默认值: ``None``。
返回:
:::::::::
``Tensor``, 改变形状后的 ``Tensor``,数据类型与 ``x`` 相同。
抛出异常:
- :code:`TypeError`:``starts`` 的类型应该是list、tuple 或 Tensor。
- :code:`ValueError`:``shape`` 中至多有一个元素可以是-1。
- :code:`ValueError`:``shape`` 中的元素为0时,对应的维度应该小于等于``x``的维度。
- :code:`ValueError`:``shape`` 中的元素除了-1之外,都应该是非负值。
**代码示例**
.. code-block:: python
import numpy as np
import paddle
paddle.disable_static()
data = np.random.random([2, 4, 6]).astype("float32")
x = paddle.to_tensor(data)
positive_four = paddle.fill_constant([1], "int32", 4)
out_1 = paddle.reshape(x, [-1, 0, 3, 2])
# the shape of out_1 is [2,4,3,2].
out_2 = paddle.reshape(x, shape=[positive_four, 12])
# the shape of out_2 is [4, 12].
shape_tensor = paddle.to_tensor(np.array([8, 6]).astype("int32"))
out_3 = paddle.reshape(x, shape=shape_tensor)
# the shape of out_2 is [8, 6].
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册