crop¶
根据偏移量(offsets)和形状(shape),裁剪输入(x)Tensor。
示例:
* 示例 1(输入为 2-D Tensor):
输入:
X.shape = [3, 5]
X.data = [[0, 1, 2, 0, 0],
[0, 3, 4, 0, 0],
[0, 0, 0, 0, 0]]
参数:
shape = [2, 2]
offsets = [0, 1]
输出:
Out.shape = [2, 2]
Out.data = [[1, 2],
[3, 4]]
* 示例 2(输入为 3-D Tensor):
输入:
X.shape = [2, 3, 4]
X.data = [[[0, 1, 2, 3],
[0, 5, 6, 7],
[0, 0, 0, 0]],
[[0, 3, 4, 5],
[0, 6, 7, 8],
[0, 0, 0, 0]]]
参数:
shape = [2, 2, -1]
offsets = [0, 0, 1]
输出:
Out.shape = [2, 2, 3]
Out.data = [[[1, 2, 3],
[5, 6, 7]],
[[3, 4, 5],
[6, 7, 8]]]
参数¶
x (Tensor) - 1-D 到 6-D Tensor,数据类型为 float32、float64、int32 或者 int64。
shape (list|tuple|Tensor,可选) - 输出 Tensor 的形状,数据类型为 int32。如果是列表或元组,则其长度必须与 x 的维度大小相同,如果是 Tensor,则其应该是 1-D Tensor。当它是列表时,每一个元素可以是整数或者形状为[]的 0-D Tensor。含有 Tensor 的方式适用于每次迭代时需要改变输出形状的情况。
offsets (list|tuple|Tensor,可选) - 每个维度上裁剪的偏移量,数据类型为 int32。如果是列表或元组,则其长度必须与 x 的维度大小相同,如果是 Tensor,则其应是 1-D Tensor。当它是列表时,每一个元素可以是整数或者形状为[]的 0-D Tensor。含有 Tensor 的方式适用于每次迭代的偏移量(offset)都可能改变的情况。默认值:None,每个维度的偏移量为 0。
name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。
返回¶
裁剪后的 Tensor,数据类型与输入(x)相同。
代码示例¶
import paddle
x = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# x.shape = [3, 3]
# x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# shape can be a 1-D Tensor or list or tuple.
shape = paddle.to_tensor([2, 2], dtype='int32')
# shape = [2, 2]
# shape = (2, 2)
out = paddle.crop(x, shape)
# out.shape = [2, 2]
# out = [[1,2], [4,5]]
# offsets can be a 1-D Tensor or list or tuple.
offsets = paddle.to_tensor([0, 1], dtype='int32')
# offsets = [1, 0]
# offsets = (1, 1)
out = paddle.crop(x, shape, offsets)
# out.shape = [2, 2]
# if offsets = [0, 0], out = [[1,2], [4,5]]
# if offsets = [0, 1], out = [[2,3], [5,6]]
# if offsets = [1, 0], out = [[4,5], [7,8]]
# if offsets = [1, 1], out = [[5,6], [8,9]]