提交 cc4a4639 编写于 作者: H HexToString 提交者: jzhang533

update code example test=document_fix

上级 56a65abd
...@@ -9048,7 +9048,7 @@ def crop_tensor(x, shape=None, offsets=None, name=None): ...@@ -9048,7 +9048,7 @@ def crop_tensor(x, shape=None, offsets=None, name=None):
be changed each iteration. be changed each iteration.
offsets (list|tuple|Variable, optional): Specifies the cropping offsets (list|tuple|Variable, optional): Specifies the cropping
offsets at each dimension. Its data type is int32. If a list/tuple, it's length offsets at each dimension. Its data type is int32. If a list/tuple, it's length
must be the same as the dimension size of `x`. If a Variable, it should be a 1-D must be the same as the dimension size of `x`. If a Tensor, it should be a 1-D
Tensor. When it is a list, each element can be an integer or a Tensor of shape: [1]. Tensor. When it is a list, each element can be an integer or a Tensor of shape: [1].
If Variable contained, it is suitable for the case that the offsets may be changed If Variable contained, it is suitable for the case that the offsets may be changed
each iteration. Default: None, the offsets are 0 at each dimension. 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,51 +9058,33 @@ def crop_tensor(x, shape=None, offsets=None, name=None):
Returns: Returns:
Tensor: The cropped Tensor has same data type with `x`. Tensor: The cropped Tensor has same data type with `x`.
Raises:
TypeError: If the data type of `x` is not in: float32, float64, int32, int64.
TypeError: If `shape` is not a list, tuple or Tensor.
TypeError: If the data type of `shape` is not int32.
TypeError: If `offsets` is not None and not a list, tuple or Tensor.
TypeError: If the data type of `offsets` is not int32.
ValueError: If the element in `offsets` is less than zero.
Examples: Examples:
.. code-block:: python .. code-block:: python
:name: code-example1 :name: code-example1
import paddle import paddle
import numpy as np x = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np_data_x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype('int32')
x = paddle.to_tensor(np_data_x)
# x.shape = [3, 3] # x.shape = [3, 3]
# x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# shape can be a 1-D Tensor or list or tuple. # shape can be a 1-D Tensor or list or tuple.
np_data_shape = np.array([2, 2]).astype('int32') shape = paddle.to_tensor([2, 2], dtype='int32')
shape_tensor = paddle.to_tensor(np_data_shape) # shape = [2, 2]
# shape_list = [2, 2] # shape = (2, 2)
# shape_tuple = (2, 2) out = paddle.crop(x, shape)
out = paddle.crop(x, shape_tensor)
# out = paddle.crop(x, shape_list)
# out = paddle.crop(x, shape_tuple)
np_out = out.numpy()
print('out = ', np_out)
# out.shape = [2, 2] # out.shape = [2, 2]
# out = [[1,2], [4,5]] # out = [[1,2], [4,5]]
# offsets can be a 1-D Tensor or list or tuple. # offsets can be a 1-D Tensor or list or tuple.
np_data_offsets = np.array([0, 1]).astype('int32') offsets = paddle.to_tensor([0, 1], dtype='int32')
offsets_tensor = paddle.to_tensor(np_data_offsets) # offsets = [1, 0]
# offsets_list = [1, 1] # offsets = (1, 1)
# offsets_tuple = (0, 1) out = paddle.crop(x, shape, offsets)
out = paddle.crop(x, shape_tensor, offsets_tensor)
# out = paddle.crop(x, shape_tensor, offsets_list)
# out = paddle.crop(x, shape_tensor, offsets_tuple)
np_out = out.numpy()
print('out = ', np_out)
# out.shape = [2, 2] # out.shape = [2, 2]
# if offsets = [0, 0], out = [[1,2], [4,5]]
# if offsets = [0, 1], out = [[2,3], [5,6]] # 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]] # if offsets = [1, 1], out = [[5,6], [8,9]]
""" """
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册