未验证 提交 3ff941a2 编写于 作者: csdn5211's avatar csdn5211 提交者: GitHub

fix sequence_expand_as cn doc (#1350)

* fix sequence_expand_as cn doc

* only surpport LoDTensor

* add input output shape

* add code example

* add code example

* test=document_fix
上级 4689bd7a
......@@ -5,51 +5,55 @@ sequence_expand_as
.. py:function:: paddle.fluid.layers.sequence_expand_as(x, y, name=None)
Sequence Expand As Layer
Sequence Expand As Layer,该OP根据输入 ``y`` 的第0级lod对输入 ``x`` 进行扩展。当前实现要求 ``y`` 的lod层数(level)必须为1,且 ``x`` 的第一维必须和 ``y`` 的第0层lod大小相同,所以扩展后的LodTensor具有和 ``y`` 相同的lod。扩展结果与输入 ``x`` 的lod无关,所以无需考虑 ``x`` 的lod。
这一层将根据y的第0级lod扩展输入变量x。当前实现要求输入(Y)的lod层数必须为1,输入(X)的第一维应当和输入(Y)的第0层lod的大小相同,不考虑输入(X)的lod
注意,该OP的输入 ``x`` 可以是Tensor或LoDTensor, ``y`` 只能是LodTensor
以下示例解释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]
例1:
假设,有4个长度维1的序列[a]、[b]、[c]和[d],现在要将其扩展为长度是3、3、1、1的序列[a][a][a]、[b][b][b]、[c]和[d]。
显然,扩展后的序列lod为[0, 3, 6, 7, 8],则:
给定输入一维LoDTensor x
x.data = [[a], [b], [c], [d]]
x.dims = [4, 1]
和输入 y
y.lod = [[3, 3, 1, 1]] #为了便于理解这里用基于长度lod表示
经过sequence_expand_as运算,得到输出1级LoDTensor out
out.lod = [[0, 3, 6, 7, 8]] #基于偏移的lod,等价于基于长度的[[3, 3, 1, 1]]
out.data = [[a], [a], [a], [b], [b], [b], [c], [d]]
out.dims = [8, 1]
可见,输出out将x扩展至和y具有相同的lod。
*例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
例2:
设定与例1类似,给定输入一维LoDTensor x:
x.data = [[a, b], [c, d], [e, f]]
x.dims = [3, 2]
和输入 y:
y.lod = [[2, 1, 3]] #为了便于理解这里用基于长度lod表示
得到输出张量:
输出为1级LoDTensor:
out.lod = [[0, 2, 3, 6]] #基于偏移的lod,等价于基于长度的[[2, 1, 3]]
out.data = [[a, b], [a, b] [c, d], [e, f], [e, f], [e, f]]
out.dims = [6, 2]
Out.lod = [[0, 2, 3, 6]]
Out.data = [[a, b], [a, b] [c, d], [e, f], [e, f], [e, f]]
Out.dims = [6, 2]
可见,输出out将x扩展至和y具有相同的lod。
参数:
- **x** (Variable) - 输入变量,类型为Tensor或LoDTensor
- **y** (Variable) - 输入变量,为LoDTensor
- **name** (str|None) - 该层名称(可选)。如果设为空,则自动为该层命名
- **x** (Variable) - 输入变量,维度为 :math:`[M, K]` 的二维Tensor或LoDTensor,第一维必须与输入 ``y`` 的第0层lod大小相同,且仅支持lod_level为1。数据类型支持int32,int64,float32或float64。
- **y** (Variable) - 输入变量,LoDTensor,lod level必须为1。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name` ,一般无需设置,默认值为None。
返回:扩展变量,LoDTensor
返回:扩展变量,维度为 :math:`[N, K]` 的二维LoDTensor,N由输入 ``y`` 的lod决定,且仅支持lod_level为1。数据类型与输入 ``x`` 一致。
返回类型:变量(Variable)
返回类型:Variable
**代码示例**:
......@@ -58,12 +62,45 @@ Sequence Expand As Layer
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import numpy as np
x = fluid.layers.data(name='x', shape=[10], dtype='float32')
y = fluid.layers.data(name='y', shape=[10, 20],
dtype='float32', lod_level=1)
x = fluid.data(name='x', shape=[1], dtype='float32')
y = fluid.data(name='y', shape=[1], dtype='float32', lod_level=1)
out = layers.sequence_expand_as(x=x, y=y)
exe = fluid.Executor(fluid.CPUPlace())
place = fluid.CPUPlace()
np_data = np.array([[1], [2], [3], [4]]).astype('float32')
x_lod_tensor = fluid.create_lod_tensor(np_data, [[2, 2]], place)
print(x_lod_tensor)
#lod: [[0, 2, 4]]
# dim: 4, 1
# layout: NCHW
# dtype: float
# data: [1 2 3 4]
y_lod_tensor = fluid.create_random_int_lodtensor([[3,3,1,1]], [1],
place, low=0, high=1)
print(y_lod_tensor)
#lod: [[0, 3, 6, 7, 8]]
# dim: 8, 1
# layout: NCHW
# dtype: int64_t
# data: [0 0 1 0 1 1 1 0]
out_main = exe.run(fluid.default_main_program(),
feed={'x': x_lod_tensor, 'y': y_lod_tensor},
fetch_list=[out], return_numpy=False)
print(out_main[0])
#lod: [[0, 3, 6, 7, 8]]
# dim: 8, 1
# layout: NCHW
# dtype: float
# data: [1 1 1 2 2 2 3 4]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册