stack

paddle.fluid.layers. stack ( x, axis=0 ) [源代码]

该OP沿 axis 轴对输入 x 进行堆叠操作。

  • 例1:
  1. 输入:
  2. x[0].shape = [1, 2]
  3. x[0].data = [ [1.0 , 2.0 ] ]
  4. x[1].shape = [1, 2]
  5. x[1].data = [ [3.0 , 4.0 ] ]
  6. x[2].shape = [1, 2]
  7. x[2].data = [ [5.0 , 6.0 ] ]
  8. 参数:
  9. axis = 0 #沿着第0维对输入x进行堆叠操作。
  10. 输出:
  11. Out.shape = [3, 1, 2]
  12. Out.data = [ [ [1.0, 2.0] ],
  13. [ [3.0, 4.0] ],
  14. [ [5.0, 6.0] ] ]
  • 例2:
  1. 输入:
  2. x[0].shape = [1, 2]
  3. x[0].data = [ [1.0 , 2.0 ] ]
  4. x[1].shape = [1, 2]
  5. x[1].data = [ [3.0 , 4.0 ] ]
  6. x[2].shape = [1, 2]
  7. x[2].data = [ [5.0 , 6.0 ] ]
  8. 参数:
  9. axis = 1 or axis = -2 #沿着第1维对输入进行堆叠操作。
  10. 输出:
  11. Out.shape = [1, 3, 2]
  12. Out.data = [ [ [1.0, 2.0]
  13. [3.0, 4.0]
  14. [5.0, 6.0] ] ]

参数:

  • x (list(Variable)|tuple(Variable)) – 输入 x 可以是单个Tensor,或是多个Tensor组成的列表。如果 x 是一个列表,那么这些Tensor的维度必须相同。 假设输入是N维Tensor

    stack - 图1

    ,则输出变量的维度为N+1维

    stack - 图2

    。支持的数据类型: float32,float64,int32,int64。

  • axis (int, 可选) – 指定对输入Tensor进行堆叠运算的轴,有效 axis 的范围是:

    stack - 图3

    ,R是输入中第一个Tensor的rank。如果 axis < 0,则 axis=axis+rank(x[0])+1axis=axis+rank(x[0])+1 。axis默认值为0。

返回: 堆叠运算后的Tensor,数据类型与输入Tensor相同。输出维度等于 rank(x[0])+1rank(x[0])+1 维。

返回类型: Variable

代码示例

  1. import paddle.fluid as fluid
  2. import paddle.fluid.layers as layers
  3. # set batch size=None
  4. x1 = fluid.data(name='x1', shape=[None, 1, 2], dtype='int32')
  5. x2 = fluid.data(name='x2', shape=[None, 1, 2], dtype='int32')
  6. # stack Tensor list
  7. data = layers.stack([x1,x2]) # stack according to axis 0, data.shape=[2, None, 1, 2]
  8. data = layers.stack([x1,x2], axis=1) # stack according to axis 1, data.shape=[None, 2, 1, 2]