crop_tensor

  • paddle.fluid.layers.crop_tensor(x, shape=None, offsets=None, name=None)[源代码]

根据偏移量(offsets)和形状(shape),裁剪输入(x)Tensor。

示例

  1. * 示例1(输入为2-D Tensor):
  2.  
  3. 输入:
  4. X.shape = [3, 5]
  5. X.data = [[0, 1, 2, 0, 0],
  6. [0, 3, 4, 0, 0],
  7. [0, 0, 0, 0, 0]]
  8.  
  9. 参数:
  10. shape = [2, 2]
  11. offsets = [0, 1]
  12.  
  13. 输出:
  14. Out.shape = [2, 2]
  15. Out.data = [[1, 2],
  16. [3, 4]]
  17.  
  18. * 示例2(输入为3-D Tensor):
  19.  
  20. 输入:
  21.  
  22. X.shape = [2, 3, 4]
  23. X.data = [[[0, 1, 2, 3],
  24. [0, 5, 6, 7],
  25. [0, 0, 0, 0]],
  26. [[0, 3, 4, 5],
  27. [0, 6, 7, 8],
  28. [0, 0, 0, 0]]]
  29.  
  30. 参数:
  31. shape = [2, 2, -1]
  32. offsets = [0, 0, 1]
  33.  
  34. 输出:
  35. Out.shape = [2, 2, 3]
  36. Out.data = [[[1, 2, 3],
  37. [5, 6, 7]],
  38. [[3, 4, 5],
  39. [6, 7, 8]]]
  • 参数:
    • x (Variable): 1-D到6-D Tensor,数据类型为float32、float64、int32或者int64。
    • shape (list|tuple|Variable) - 输出Tensor的形状,数据类型为int32。如果是列表或元组,则其长度必须与x的维度大小相同,如果是Variable,则其应该是1-D Tensor。当它是列表时,每一个元素可以是整数或者形状为[1]的Tensor。含有Variable的方式适用于每次迭代时需要改变输出形状的情况。
    • offsets (list|tuple|Variable,可选) - 每个维度上裁剪的偏移量,数据类型为int32。如果是列表或元组,则其长度必须与x的维度大小相同,如果是Variable,则其应是1-D Tensor。当它是列表时,每一个元素可以是整数或者形状为[1]的Variable。含有Variable的方式适用于每次迭代的偏移量(offset)都可能改变的情况。默认值:None,每个维度的偏移量为0。
    • name (str,可选) - 具体用法请参见 Name ,一般无需设置,默认值为None。

返回: 裁剪后的Tensor,数据类型与输入(x)相同。

返回类型: Variable

  • 抛出异常:
    • TypeError - x 的数据类型应该是float32、float64、int32或者int64。
    • TypeError - shape 应该是列表、元组或Variable。
    • TypeError - shape 的数据类型应该是int32。
    • TypeError - offsets 应该是列表、元组、Variable或None。
    • TypeError - offsets 的数据类型应该是int32。
    • TypeError - offsets 的元素应该大于等于0。

代码示例:

  1. import paddle.fluid as fluid
  2. x = fluid.data(name="x", shape=[None, 3, 5], dtype="float32")
  3. # x.shape = [-1, 3, 5], where -1 indicates batch size, and it will get the exact value in runtime.
  4.  
  5. # shape is a 1-D Tensor
  6. crop_shape = fluid.data(name="crop_shape", shape=[3], dtype="int32")
  7. crop0 = fluid.layers.crop_tensor(x, shape=crop_shape)
  8. # crop0.shape = [-1, -1, -1], it means crop0.shape[0] = x.shape[0] in runtime.
  9.  
  10. # or shape is a list in which each element is a constant
  11. crop1 = fluid.layers.crop_tensor(x, shape=[-1, -1, 3], offsets=[0, 1, 0])
  12. # crop1.shape = [-1, 2, 3]
  13.  
  14. # or shape is a list in which each element is a constant or Tensor
  15. y = fluid.data(name="y", shape=[3, 8, 8], dtype="float32")
  16. dim1 = fluid.layers.data(name="dim1", shape=[1], dtype="int32")
  17. crop2 = fluid.layers.crop_tensor(y, shape=[3, dim1, 4])
  18. # crop2.shape = [3, -1, 4]
  19.  
  20. # offsets is a 1-D Tensor
  21. crop_offsets = fluid.data(name="crop_offsets", shape=[3], dtype="int32")
  22. crop3 = fluid.layers.crop_tensor(x, shape=[-1, 2, 3], offsets=crop_offsets)
  23. # crop3.shape = [-1, 2, 3]
  24.  
  25. # offsets is a list in which each element is a constant or Tensor
  26. offsets_var = fluid.data(name="dim1", shape=[1], dtype="int32")
  27. crop4 = fluid.layers.crop_tensor(x, shape=[-1, 2, 3], offsets=[0, 1, offsets_var])
  28. # crop4.shape = [-1, 2, 3]