@@ -9048,7 +9048,7 @@ def crop_tensor(x, shape=None, offsets=None, name=None):
9048
9048
be changed each iteration.
9049
9049
offsets (list|tuple|Variable, optional): Specifies the cropping
9050
9050
offsets at each dimension. Its data type is int32. If a list/tuple, it's length
9051
- must be the same as the dimension size of `x`. If a Variable , it should be a 1-D
9051
+ must be the same as the dimension size of `x`. If a Tensor , it should be a 1-D
9052
9052
Tensor. When it is a list, each element can be an integer or a Tensor of shape: [1].
9053
9053
If Variable contained, it is suitable for the case that the offsets may be changed
9054
9054
each iteration. Default: None, the offsets are 0 at each dimension.
@@ -9058,51 +9058,33 @@ def crop_tensor(x, shape=None, offsets=None, name=None):
9058
9058
Returns:
9059
9059
Tensor: The cropped Tensor has same data type with `x`.
9060
9060
9061
- Raises:
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 Tensor.
9064
- TypeError: If the data type of `shape` is not int32.
9065
- TypeError: If `offsets` is not None and not a list, tuple or Tensor.
9066
- TypeError: If the data type of `offsets` is not int32.
9067
- ValueError: If the element in `offsets` is less than zero.
9068
-
9069
9061
Examples:
9070
9062
9071
9063
.. code-block:: python
9072
9064
:name: code-example1
9073
9065
9074
9066
import paddle
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)
9067
+ x = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
9078
9068
# x.shape = [3, 3]
9079
9069
# x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
9080
9070
9081
9071
# 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)
9072
+ shape = paddle.to_tensor([2, 2], dtype='int32')
9073
+ # shape = [2, 2]
9074
+ # shape = (2, 2)
9075
+ out = paddle.crop(x, shape)
9091
9076
# out.shape = [2, 2]
9092
9077
# out = [[1,2], [4,5]]
9093
9078
9094
9079
# 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)
9080
+ offsets = paddle.to_tensor([0, 1], dtype='int32')
9081
+ # offsets = [1, 0]
9082
+ # offsets = (1, 1)
9083
+ out = paddle.crop(x, shape, offsets)
9104
9084
# out.shape = [2, 2]
9085
+ # if offsets = [0, 0], out = [[1,2], [4,5]]
9105
9086
# if offsets = [0, 1], out = [[2,3], [5,6]]
9087
+ # if offsets = [1, 0], out = [[4,5], [7,8]]
9106
9088
# if offsets = [1, 1], out = [[5,6], [8,9]]
9107
9089
9108
9090
"""
0 commit comments