crop_tensor

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

    示例

    参数:

    • 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的方式适用于每次迭代时需要改变输出形状的情况。

    • name (str,可选) - 具体用法请参见 ,一般无需设置,默认值为None。

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

    返回类型: Variable

    抛出异常:

    • TypeError - x 的数据类型应该是float32、float64、int32或者int64。

    • 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. crop_shape = fluid.data(name="crop_shape", shape=[3], dtype="int32")
    4. crop0 = fluid.layers.crop_tensor(x, shape=crop_shape)
    5. # crop0.shape = [-1, -1, -1], it means crop0.shape[0] = x.shape[0] in runtime.
    6. # or shape is a list in which each element is a constant
    7. crop1 = fluid.layers.crop_tensor(x, shape=[-1, -1, 3], offsets=[0, 1, 0])
    8. # crop1.shape = [-1, 2, 3]
    9. # or shape is a list in which each element is a constant or Tensor
    10. y = fluid.data(name="y", shape=[3, 8, 8], dtype="float32")
    11. crop2 = fluid.layers.crop_tensor(y, shape=[3, dim1, 4])
    12. # crop2.shape = [3, -1, 4]
    13. # offsets is a 1-D Tensor
    14. crop_offsets = fluid.data(name="crop_offsets", shape=[3], dtype="int32")
    15. crop3 = fluid.layers.crop_tensor(x, shape=[-1, 2, 3], offsets=crop_offsets)
    16. # crop3.shape = [-1, 2, 3]
    17. # offsets is a list in which each element is a constant or Tensor
    18. offsets_var = fluid.data(name="offset", shape=[1], dtype="int32")
    19. # crop4.shape = [-1, 2, 3]