|
| 1 | +.. _cn_api_fluid_layers_crop_tensor: |
| 2 | + |
| 3 | +crop_tensor |
| 4 | +------------------------------- |
| 5 | + |
| 6 | +.. py:function:: paddle.fluid.layers.crop_tensor(x, shape=None, offsets=None, name=None) |
| 7 | +
|
| 8 | +根据偏移量(offsets)和形状(shape),裁剪输入张量。 |
| 9 | + |
| 10 | +**样例**: |
| 11 | + |
| 12 | +:: |
| 13 | + |
| 14 | + * Case 1: |
| 15 | + Given |
| 16 | + X = [[0, 1, 2, 0, 0] |
| 17 | + [0, 3, 4, 0, 0] |
| 18 | + [0, 0, 0, 0, 0]], |
| 19 | + and |
| 20 | + shape = [2, 2], |
| 21 | + offsets = [0, 1], |
| 22 | + output is: |
| 23 | + Out = [[1, 2], |
| 24 | + [3, 4]]. |
| 25 | + * Case 2: |
| 26 | + Given |
| 27 | + X = [[[0, 1, 2, 3] |
| 28 | + [0, 5, 6, 7] |
| 29 | + [0, 0, 0, 0]], |
| 30 | + |
| 31 | + [[0, 3, 4, 5] |
| 32 | + [0, 6, 7, 8] |
| 33 | + [0, 0, 0, 0]]]. |
| 34 | + and |
| 35 | + shape = [2, 2, 3], |
| 36 | + offsets = [0, 0, 1], |
| 37 | + output is: |
| 38 | + Out = [[[1, 2, 3] |
| 39 | + [5, 6, 7]], |
| 40 | + |
| 41 | + [[3, 4, 5] |
| 42 | + [6, 7, 8]]]. |
| 43 | + |
| 44 | +参数: |
| 45 | + - **x** (Variable): 输入张量。 |
| 46 | + - **shape** (Variable|list|tuple of integer) - 输出张量的形状由参数shape指定,它可以是一个1-D的变量/列表/整数元组。如果是1-D的变量,它的秩必须与x相同。如果是列表或整数元组,则其长度必须与x的秩相同。当它是列表时,每一个元素可以是整数或者shape为[1]的变量。含有变量的方式适用于每次迭代时需要改变输出形状的情况。列表和元组中只有第一个元素可以被设置为-1,这意味着输出的第一维大小与输入相同。 |
| 47 | + - **offsets** (Variable|list|tuple of integer|None) - 指定每个维度上的裁剪的偏移量。它可以是一个1-D的变量/列表/整数元组。如果是1-D的变量,它的秩必须与x相同。如果是列表或整数元组,则其长度必须与x的秩相同。当它是列表时,每一个元素可以是整数或者shape为[1]的变量。含有变量的方式适用于每次迭代的偏移量(offset)都可能改变的情况。如果offsets=None,则每个维度的偏移量为0。 |
| 48 | + - **name** (str|None) - 该层的名称(可选)。如果设置为None,该层将被自动命名。 |
| 49 | + |
| 50 | +返回: 裁剪张量。 |
| 51 | + |
| 52 | +返回类型: 变量(Variable) |
| 53 | + |
| 54 | +抛出异常: 如果形状不是列表、元组或变量,抛出ValueError |
| 55 | + |
| 56 | +抛出异常: 如果偏移量不是None、列表、元组或变量,抛出ValueError |
| 57 | + |
| 58 | +**代码示例**: |
| 59 | + |
| 60 | +.. code-block:: python |
| 61 | + |
| 62 | + import paddle.fluid as fluid |
| 63 | + x = fluid.layers.data(name="x", shape=[3, 5], dtype="float32") |
| 64 | + # x.shape = [-1, 3, 5], where -1 indicates batch size, and it will get the exact value in runtime. |
| 65 | +
|
| 66 | + # shape is a 1-D tensor variable |
| 67 | + crop_shape = fluid.layers.data(name="crop_shape", shape=[3], dtype="int32", append_batch_size=False) |
| 68 | + crop0 = fluid.layers.crop_tensor(x, shape=crop_shape) |
| 69 | + # crop0.shape = [-1, -1, -1], it means crop0.shape[0] = x.shape[0] in runtime. |
| 70 | +
|
| 71 | + # or shape is a list in which each element is a constant |
| 72 | + crop1 = fluid.layers.crop_tensor(x, shape=[-1, 2, 3]) |
| 73 | + # crop1.shape = [-1, 2, 3] |
| 74 | +
|
| 75 | + # or shape is a list in which each element is a constant or variable |
| 76 | + y = fluid.layers.data(name="y", shape=[3, 8, 8], dtype="float32") |
| 77 | + dim1 = fluid.layers.data(name="dim1", shape=[1], dtype="int32", append_batch_size=False) |
| 78 | + crop2 = fluid.layers.crop_tensor(y, shape=[-1, 3, dim1, 4]) |
| 79 | + # crop2.shape = [-1, 3, -1, 4] |
| 80 | +
|
| 81 | + # offsets is a 1-D tensor variable |
| 82 | + crop_offsets = fluid.layers.data(name="crop_offsets", shape=[3], dtype="int32", append_batch_size=False) |
| 83 | + crop3 = fluid.layers.crop_tensor(x, shape=[-1, 2, 3], offsets=crop_offsets) |
| 84 | + # crop3.shape = [-1, 2, 3] |
| 85 | +
|
| 86 | + # offsets is a list in which each element is a constant or variable |
| 87 | + offsets_var = fluid.layers.data(name="dim1", shape=[1], dtype="int32", append_batch_size=False) |
| 88 | + crop4 = fluid.layers.crop_tensor(x, shape=[-1, 2, 3], offsets=[0, 1, offsets_var]) |
| 89 | + # crop4.shape = [-1, 2, 3] |
| 90 | +
|
| 91 | +
|
| 92 | +
|
| 93 | +
|
| 94 | +
|
| 95 | +
|
| 96 | +
|
| 97 | +
|
| 98 | +
|
0 commit comments