pad_constant_like

  • paddle.fluid.layers.pad_constant_like(x, y, pad_value=0.0, name=None)[源代码]

该OP使用 pad_value 填充 y ,填充到每个维度值的数量由x和y的形状而指定,((0,x.shape[0] - y.shape[0]), …, (0, x.shape[i] - y.shape[i]), …, (0, x.shape[n] - y.shape[n]))是每个维度填充的宽度,对于维度i,填充宽度 (0, x.shape[i] - y.shape[i]) ,表示在y的第i维开头不填充,而在末尾填充 x.shape[i] - y.shape[i] 个位置。该OP要求y与x具有相同的秩,并且对每个维度i, y.shape[i] <= x.shape[i]

样例

  1. Given:
  2. X = [[[[ 0, 1, 2],
  3. [ 3, 4, 5]],
  4. [[ 6, 7, 8],
  5. [ 9, 10, 11]],
  6. [[12, 13, 14],
  7. [15, 16, 17]]],
  8. [[[18, 19, 20],
  9. [21, 22, 23]],
  10. [[24, 25, 26],
  11. [27, 28, 29]],
  12. [[30, 31, 32],
  13. [33, 34, 35]]]]
  14. X.shape = (2, 3, 2, 3)
  15.  
  16. Y = [[[[35, 36, 37]],
  17. [[38, 39, 40]],
  18. [[41, 42, 43]]]]
  19. Y.shape = (1, 3, 1, 3)
  20.  
  21. and
  22. pad_value = 0.
  23.  
  24. Output is:
  25. out = [[[[35, 36, 37],
  26. [0, 0, 0]],
  27. [[38, 39, 40],
  28. [0, 0, 0]],
  29. [[41, 42, 43],
  30. [0, 0, 0]]],
  31. [[[0, 0, 0],
  32. [0, 0, 0]],
  33. [[0, 0, 0],
  34. [0, 0, 0]],
  35. [[0, 0, 0],
  36. [0, 0, 0]]]]
  37. out.shape = [2, 3, 2, 3]
  • 参数:
    • x (Variable)- 多维Tensor
    • y (Variable)- 多维Tensor,与x具有相同的秩,而且对任意维度 i ,要求满足 y.shape[i] <= x.shape[i] 。数据类型为float32或float64
    • pad_value (float,可选) - 用于填充的常量值。默认值为0.
    • name (str | None) - (str|None) - 该参数供开发人员打印调试信息时使用,具体用法请参见 Name ,默认值为None。

返回:经过维度填充后的Tensor,与x具有相同的shape,与y具有相同的数据类型

返回类型: Variable

示例代码

  1. # x是秩为4的tensor, x.shape = (2, 3, 2, 3)
  2. # y是秩为4的tensor, y.shape = (1, 3, 1, 3)
  3. import paddle.fluid as fluid
  4. x = fluid.layers.data(name='x', shape=[2,3,2,3], dtype='float32')
  5. y = fluid.layers.data(name='y', shape=[1,3,1,3], dtype='float32')
  6. out = fluid.layers.pad_constant_like(x=x, y=y, pad_value=0.)
  7. # out是秩为4的tensor, out.shape = [2, 3 ,2 , 3]