@@ -9039,10 +9039,10 @@ def crop_tensor(x, shape=None, offsets=None, name=None):
9039
9039
[6, 7, 8]]]
9040
9040
9041
9041
Parameters:
9042
- x (Variable ): 1-D to 6-D Tensor, the data type is float32, float64, int32 or int64.
9043
- shape (list|tuple|Variable ): The output shape is specified
9042
+ x (Tensor ): 1-D to 6-D Tensor, the data type is float32, float64, int32 or int64.
9043
+ shape (list|tuple|Tensor ): The output shape is specified
9044
9044
by `shape`. Its data type is int32. If a list/tuple, it's length must be
9045
- the same as the dimension size of `x`. If a Variable , it should be a 1-D Tensor.
9045
+ the same as the dimension size of `x`. If a Tensor , it should be a 1-D Tensor.
9046
9046
When it is a list, each element can be an integer or a Tensor of shape: [1].
9047
9047
If Variable contained, it is suitable for the case that the shape may
9048
9048
be changed each iteration.
@@ -9056,51 +9056,54 @@ def crop_tensor(x, shape=None, offsets=None, name=None):
9056
9056
this property. For more information, please refer to :ref:`api_guide_Name` .
9057
9057
9058
9058
Returns:
9059
- Variable : The cropped Tensor has same data type with `x`.
9059
+ Tensor : The cropped Tensor has same data type with `x`.
9060
9060
9061
9061
Raises:
9062
9062
TypeError: If the data type of `x` is not in: float32, float64, int32, int64.
9063
- TypeError: If `shape` is not a list, tuple or Variable .
9063
+ TypeError: If `shape` is not a list, tuple or Tensor .
9064
9064
TypeError: If the data type of `shape` is not int32.
9065
- TypeError: If `offsets` is not None and not a list, tuple or Variable .
9065
+ TypeError: If `offsets` is not None and not a list, tuple or Tensor .
9066
9066
TypeError: If the data type of `offsets` is not int32.
9067
9067
ValueError: If the element in `offsets` is less than zero.
9068
9068
9069
9069
Examples:
9070
9070
9071
9071
.. code-block:: python
9072
+ :name: code-example1
9072
9073
9073
- import paddle.fluid as fluid
9074
- import paddle.fluid as fluid
9075
9074
import paddle
9076
- paddle.enable_static()
9077
- x = fluid.data(name="x", shape=[None, 3, 5], dtype="float32")
9078
- # x.shape = [-1, 3, 5], where -1 indicates batch size, and it will get the exact value in runtime.
9079
-
9080
- # shape is a 1-D Tensor
9081
- crop_shape = fluid.data(name="crop_shape", shape=[3], dtype="int32")
9082
- crop0 = fluid.layers.crop_tensor(x, shape=crop_shape)
9083
- # crop0.shape = [-1, -1, -1], it means crop0.shape[0] = x.shape[0] in runtime.
9084
-
9085
- # or shape is a list in which each element is a constant
9086
- crop1 = fluid.layers.crop_tensor(x, shape=[-1, -1, 3], offsets=[0, 1, 0])
9087
- # crop1.shape = [-1, 2, 3]
9088
-
9089
- # or shape is a list in which each element is a constant or Variable
9090
- y = fluid.data(name="y", shape=[3, 8, 8], dtype="float32")
9091
- dim1 = fluid.data(name="dim1", shape=[1], dtype="int32")
9092
- crop2 = fluid.layers.crop_tensor(y, shape=[3, dim1, 4])
9093
- # crop2.shape = [3, -1, 4]
9094
-
9095
- # offsets is a 1-D Tensor
9096
- crop_offsets = fluid.data(name="crop_offsets", shape=[3], dtype="int32")
9097
- crop3 = fluid.layers.crop_tensor(x, shape=[-1, 2, 3], offsets=crop_offsets)
9098
- # crop3.shape = [-1, 2, 3]
9099
-
9100
- # offsets is a list in which each element is a constant or Variable
9101
- offsets_var = fluid.data(name="dim1", shape=[1], dtype="int32")
9102
- crop4 = fluid.layers.crop_tensor(x, shape=[-1, 2, 3], offsets=[0, 1, offsets_var])
9103
- # crop4.shape = [-1, 2, 3]
9075
+ import numpy as np
9076
+ np_data_x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype('int32')
9077
+ x = paddle.to_tensor(np_data_x)
9078
+ # x.shape = [3, 3]
9079
+ # x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
9080
+
9081
+ # shape can be a 1-D Tensor or list or tuple.
9082
+ np_data_shape = np.array([2, 2]).astype('int32')
9083
+ shape_tensor = paddle.to_tensor(np_data_shape)
9084
+ # shape_list = [2, 2]
9085
+ # shape_tuple = (2, 2)
9086
+ out = paddle.crop(x, shape_tensor)
9087
+ # out = paddle.crop(x, shape_list)
9088
+ # out = paddle.crop(x, shape_tuple)
9089
+ np_out = out.numpy()
9090
+ print('out = ', np_out)
9091
+ # out.shape = [2, 2]
9092
+ # out = [[1,2], [4,5]]
9093
+
9094
+ # offsets can be a 1-D Tensor or list or tuple.
9095
+ np_data_offsets = np.array([0, 1]).astype('int32')
9096
+ offsets_tensor = paddle.to_tensor(np_data_offsets)
9097
+ # offsets_list = [1, 1]
9098
+ # offsets_tuple = (0, 1)
9099
+ out = paddle.crop(x, shape_tensor, offsets_tensor)
9100
+ # out = paddle.crop(x, shape_tensor, offsets_list)
9101
+ # out = paddle.crop(x, shape_tensor, offsets_tuple)
9102
+ np_out = out.numpy()
9103
+ print('out = ', np_out)
9104
+ # out.shape = [2, 2]
9105
+ # if offsets = [0, 1], out = [[2,3], [5,6]]
9106
+ # if offsets = [1, 1], out = [[5,6], [8,9]]
9104
9107
9105
9108
"""
9106
9109
helper = LayerHelper('crop_tensor', **locals())
0 commit comments