From 987052fd0722268fab7dec8f4e8960d44345b39b Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Tue, 19 Sep 2023 13:13:03 +0800 Subject: [PATCH 1/6] [xdoctest] reformat example code with google style in No.356 --- python/paddle/tensor/manipulation.py | 1636 ++++++++++++++------------ python/paddle/tensor/search.py | 583 ++++----- 2 files changed, 1188 insertions(+), 1031 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 62de7b884275b7..4f78c1df20a59e 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -111,15 +111,15 @@ def tensor_array_to_tensor(input, axis=1, use_stack=False, name=None): Examples: .. code-block:: python - import numpy - import paddle - x0 = paddle.assign(numpy.random.rand(2, 2).astype("float32")) - x1 = paddle.assign(numpy.random.rand(2, 2).astype("float32")) - i = paddle.full(shape=[1], dtype="int64", fill_value=0) - array = paddle.tensor.array.create_array(dtype='float32') - paddle.tensor.array.array_write(x0, i, array) - paddle.tensor.array.array_write(x1, i + 1, array) - output, output_index = paddle.tensor.manipulation.tensor_array_to_tensor(input=array) + >>> import numpy + >>> import paddle + >>> x0 = paddle.assign(numpy.random.rand(2, 2).astype("float32")) + >>> x1 = paddle.assign(numpy.random.rand(2, 2).astype("float32")) + >>> i = paddle.full(shape=[1], dtype="int64", fill_value=0) + >>> array = paddle.tensor.array.create_array(dtype='float32') + >>> paddle.tensor.array.array_write(x0, i, array) + >>> paddle.tensor.array.array_write(x1, i + 1, array) + >>> output, output_index = paddle.tensor.manipulation.tensor_array_to_tensor(input=array) """ if in_dynamic_mode(): assert isinstance( @@ -174,10 +174,10 @@ def cast(x, dtype): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([2, 3, 4], 'float64') - y = paddle.cast(x, 'uint8') + >>> x = paddle.to_tensor([2, 3, 4], 'float64') + >>> y = paddle.cast(x, 'uint8') """ if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)): dtype = convert_np_dtype_to_dtype_(dtype) @@ -294,22 +294,34 @@ def slice(input, axes, starts, ends): Examples: .. code-block:: python - import paddle - - input = paddle.rand(shape=[4, 5, 6], dtype='float32') - # example 1: - # attr starts is a list which doesn't contain tensor. - axes = [0, 1, 2] - starts = [-3, 0, 2] - ends = [3, 2, 4] - sliced_1 = paddle.slice(input, axes=axes, starts=starts, ends=ends) - # sliced_1 is input[1:3, 0:2, 2:4]. - - # example 2: - # attr starts is a list which contain tensor. - minus_3 = paddle.full([1], -3, "int32") - sliced_2 = paddle.slice(input, axes=axes, starts=[minus_3, 0, 2], ends=ends) - # sliced_2 is input[1:3, 0:2, 2:4]. + >>> import paddle + >>> paddle.seed(1) + >>> input = paddle.rand(shape=[4, 5, 6], dtype='float32') + >>> # example 1: + >>> # attr starts is a list which doesn't contain tensor. + >>> axes = [0, 1, 2] + >>> starts = [-3, 0, 2] + >>> ends = [3, 2, 4] + >>> sliced_1 = paddle.slice(input, axes=axes, starts=starts, ends=ends) + >>> # sliced_1 is input[1:3, 0:2, 2:4]. + >>> print(sliced_1) + Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[0.19634065, 0.73838711], + [0.72018963, 0.61661047]], + [[0.58115059, 0.07706697], + [0.14850789, 0.80731255]]]) + + >>> # example 2: + >>> # attr starts is a list which contain tensor. + >>> minus_3 = paddle.full([1], -3, "int32") + >>> sliced_2 = paddle.slice(input, axes=axes, starts=[minus_3, 0, 2], ends=ends) + >>> # sliced_2 is input[1:3, 0:2, 2:4]. + >>> print(sliced_2) + Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[0.19634065, 0.73838711], + [0.72018963, 0.61661047]], + [[0.58115059, 0.07706697], + [0.14850789, 0.80731255]]]) """ if in_dynamic_or_pir_mode(): attrs = () @@ -468,12 +480,12 @@ def transpose(x, perm, name=None): .. code-block:: python - import paddle + >>> import paddle - x = paddle.randn([2, 3, 4]) - x_transposed = paddle.transpose(x, perm=[1, 0, 2]) - print(x_transposed.shape) - # [3L, 2L, 4L] + >>> x = paddle.randn([2, 3, 4]) + >>> x_transposed = paddle.transpose(x, perm=[1, 0, 2]) + >>> print(x_transposed.shape) + [3, 2, 4] """ if in_dynamic_mode(): @@ -547,9 +559,9 @@ def unstack(x, axis=0, num=None): Examples: .. code-block:: python - import paddle - x = paddle.ones(name='x', shape=[2, 3, 5], dtype='float32') # create a tensor with shape=[2, 3, 5] - y = paddle.unstack(x, axis=1) # unstack with second axis, which results 3 tensors with shape=[2, 5] + >>> import paddle + >>> x = paddle.ones(name='x', shape=[2, 3, 5], dtype='float32') # create a tensor with shape=[2, 3, 5] + >>> y = paddle.unstack(x, axis=1) # unstack with second axis, which results 3 tensors with shape=[2, 5] """ if not (-x.ndim <= axis < x.ndim): @@ -622,14 +634,15 @@ def shard_index(input, index_num, nshards, shard_id, ignore_value=-1): Examples: .. code-block:: python - import paddle - label = paddle.to_tensor([[16], [1]], "int64") - shard_label = paddle.shard_index(input=label, - index_num=20, - nshards=2, - shard_id=0) - print(shard_label) - # [[-1], [1]] + >>> import paddle + >>> label = paddle.to_tensor([[16], [1]], "int64") + >>> shard_label = paddle.shard_index(input=label, + ... index_num=20, + ... nshards=2, + ... shard_id=0) + >>> print(shard_label) + [[-1], + [ 1]] """ if in_dynamic_mode(): return _C_ops.shard_index( @@ -721,29 +734,29 @@ def crop(x, shape=None, offsets=None, name=None): .. code-block:: python - 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]] + >>> 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]] """ @@ -878,12 +891,13 @@ def fill_(x, value): Examples: .. code-block:: python - import paddle + >>> import paddle - tensor = paddle.to_tensor([0, 1, 2, 3, 4]) + >>> tensor = paddle.to_tensor([0, 1, 2, 3, 4]) - tensor.fill_(0) - print(tensor.tolist()) #[0, 0, 0, 0, 0] + >>> tensor.fill_(0) + >>> print(tensor.tolist()) + [0, 0, 0, 0, 0] """ if not isinstance(value, (float, int)): @@ -911,12 +925,13 @@ def zero_(x): Examples: .. code-block:: python - import paddle + >>> import paddle - tensor = paddle.to_tensor([0, 1, 2, 3, 4]) + >>> tensor = paddle.to_tensor([0, 1, 2, 3, 4]) - tensor.zero_() - print(tensor.tolist()) #[0, 0, 0, 0, 0] + >>> tensor.zero_() + >>> print(tensor.tolist()) + [0, 0, 0, 0, 0] """ return _C_ops.fill_(x, 0.0) @@ -942,10 +957,11 @@ def fill_diagonal_(x, value, offset=0, wrap=False, name=None): Examples: .. code-block:: python - import paddle - x = paddle.ones((4, 3)) * 2 - x.fill_diagonal_(1.0) - print(x.tolist()) #[[1.0, 2.0, 2.0], [2.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 2.0, 2.0]] + >>> import paddle + >>> x = paddle.ones((4, 3)) * 2 + >>> x.fill_diagonal_(1.0) + >>> print(x.tolist()) + [[1.0, 2.0, 2.0], [2.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 2.0, 2.0]] """ if in_dynamic_mode(): if len(x.shape) == 2: @@ -1008,12 +1024,13 @@ def fill_diagonal_tensor_(x, y, offset=0, dim1=0, dim2=1, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.ones((4, 3)) * 2 - y = paddle.ones((3,)) - x.fill_diagonal_tensor_(y) - print(x.tolist()) #[[1.0, 2.0, 2.0], [2.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 2.0, 2.0]] + >>> x = paddle.ones((4, 3)) * 2 + >>> y = paddle.ones((3,)) + >>> x.fill_diagonal_tensor_(y) + >>> print(x.tolist()) + [[1.0, 2.0, 2.0], [2.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 2.0, 2.0]] """ return _fill_diagonal_tensor_impl( @@ -1039,12 +1056,13 @@ def fill_diagonal_tensor(x, y, offset=0, dim1=0, dim2=1, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.ones((4, 3)) * 2 - y = paddle.ones((3,)) - nx = x.fill_diagonal_tensor(y) - print(nx.tolist()) #[[1.0, 2.0, 2.0], [2.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 2.0, 2.0]] + >>> x = paddle.ones((4, 3)) * 2 + >>> y = paddle.ones((3,)) + >>> nx = x.fill_diagonal_tensor(y) + >>> print(nx.tolist()) + [[1.0, 2.0, 2.0], [2.0, 1.0, 2.0], [2.0, 2.0, 1.0], [2.0, 2.0, 2.0]] """ return _fill_diagonal_tensor_impl( @@ -1070,14 +1088,16 @@ def tolist(x): Examples: .. code-block:: python - import paddle + >>> import paddle - t = paddle.to_tensor([0,1,2,3,4]) - expectlist = t.tolist() - print(expectlist) #[0, 1, 2, 3, 4] + >>> t = paddle.to_tensor([0,1,2,3,4]) + >>> expectlist = t.tolist() + >>> print(expectlist) + [0, 1, 2, 3, 4] - expectlist = paddle.tolist(t) - print(expectlist) #[0, 1, 2, 3, 4] + >>> expectlist = paddle.tolist(t) + >>> print(expectlist) + [0, 1, 2, 3, 4] """ # TODO(zhouwei): will remove 0-D Tensor.numpy() hack @@ -1104,28 +1124,28 @@ def concat(x, axis=0, name=None): Examples: .. code-block:: python - import paddle - - x1 = paddle.to_tensor([[1, 2, 3], - [4, 5, 6]]) - x2 = paddle.to_tensor([[11, 12, 13], - [14, 15, 16]]) - x3 = paddle.to_tensor([[21, 22], - [23, 24]]) - zero = paddle.full(shape=[1], dtype='int32', fill_value=0) - # When the axis is negative, the real axis is (axis + Rank(x)) - # As follow, axis is -1, Rank(x) is 2, the real axis is 1 - out1 = paddle.concat(x=[x1, x2, x3], axis=-1) - out2 = paddle.concat(x=[x1, x2], axis=0) - out3 = paddle.concat(x=[x1, x2], axis=zero) - # out1 - # [[ 1 2 3 11 12 13 21 22] - # [ 4 5 6 14 15 16 23 24]] - # out2 out3 - # [[ 1 2 3] - # [ 4 5 6] - # [11 12 13] - # [14 15 16]] + >>> import paddle + + >>> x1 = paddle.to_tensor([[1, 2, 3], + ... [4, 5, 6]]) + >>> x2 = paddle.to_tensor([[11, 12, 13], + ... [14, 15, 16]]) + >>> x3 = paddle.to_tensor([[21, 22], + ... [23, 24]]) + >>> zero = paddle.full(shape=[1], dtype='int32', fill_value=0) + >>> # When the axis is negative, the real axis is (axis + Rank(x)) + >>> # As follow, axis is -1, Rank(x) is 2, the real axis is 1 + >>> out1 = paddle.concat(x=[x1, x2, x3], axis=-1) + >>> out2 = paddle.concat(x=[x1, x2], axis=0) + >>> out3 = paddle.concat(x=[x1, x2], axis=zero) + >>> # out1 + >>> # [[ 1 2 3 11 12 13 21 22] + >>> # [ 4 5 6 14 15 16 23 24]] + >>> # out2 out3 + >>> # [[ 1 2 3] + >>> # [ 4 5 6] + >>> # [11 12 13] + >>> # [14 15 16]] """ input = x if in_dynamic_or_pir_mode(): @@ -1232,12 +1252,12 @@ def broadcast_tensors(input, name=None): Examples: .. code-block:: python - import paddle - x1 = paddle.rand([1, 2, 3, 4]).astype('float32') - x2 = paddle.rand([1, 2, 1, 4]).astype('float32') - x3 = paddle.rand([1, 1, 3, 1]).astype('float32') - out1, out2, out3 = paddle.broadcast_tensors(input=[x1, x2, x3]) - # out1, out2, out3: tensors broadcasted from x1, x2, x3 with shape [1,2,3,4] + >>> import paddle + >>> x1 = paddle.rand([1, 2, 3, 4]).astype('float32') + >>> x2 = paddle.rand([1, 2, 1, 4]).astype('float32') + >>> x3 = paddle.rand([1, 1, 3, 1]).astype('float32') + >>> out1, out2, out3 = paddle.broadcast_tensors(input=[x1, x2, x3]) + >>> # out1, out2, out3: tensors broadcasted from x1, x2, x3 with shape [1,2,3,4] """ num_inputs = len(input) @@ -1342,15 +1362,27 @@ def flip(x, axis, name=None): Examples: .. code-block:: python - import paddle - - image_shape=(3, 2, 2) - img = paddle.arange(image_shape[0] * image_shape[1] * image_shape[2]).reshape(image_shape) - tmp = paddle.flip(img, [0,1]) - print(tmp) # [[[10,11],[8, 9]], [[6, 7],[4, 5]], [[2, 3],[0, 1]]] - - out = paddle.flip(tmp,-1) - print(out) # [[[11,10],[9, 8]], [[7, 6],[5, 4]], [[3, 2],[1, 0]]] + >>> import paddle + + >>> image_shape=(3, 2, 2) + >>> img = paddle.arange(image_shape[0] * image_shape[1] * image_shape[2]).reshape(image_shape) + >>> tmp = paddle.flip(img, [0,1]) + >>> print(tmp) + [[[10,11], + [8, 9]], + [[6, 7], + [4, 5]], + [[2, 3], + [0, 1]]] + + >>> out = paddle.flip(tmp,-1) + >>> print(out) + [[[11,10], + [9, 8]], + [[7, 6], + [5, 4]], + [[3, 2], + [1, 0]]] """ if isinstance(axis, int): axis = [axis] @@ -1402,38 +1434,38 @@ def rot90(x, k=1, axes=[0, 1], name=None): Examples: .. code-block:: python - import paddle - - data = paddle.arange(4) - data = paddle.reshape(data, (2, 2)) - print(data) - #[[0, 1], - # [2, 3]] - - y = paddle.rot90(data, 1, [0, 1]) - print(y) - #[[1, 3], - # [0, 2]] - - y= paddle.rot90(data, -1, [0, 1]) - print(y) - #[[2, 0], - # [3, 1]] - - data2 = paddle.arange(8) - data2 = paddle.reshape(data2, (2,2,2)) - print(data2) - #[[[0, 1], - # [2, 3]], - # [[4, 5], - # [6, 7]]] - - y = paddle.rot90(data2, 1, [1, 2]) - print(y) - #[[[1, 3], - # [0, 2]], - # [[5, 7], - # [4, 6]]] + >>> import paddle + + >>> data = paddle.arange(4) + >>> data = paddle.reshape(data, (2, 2)) + >>> print(data) + [[0, 1], + [2, 3]] + + >>> y = paddle.rot90(data, 1, [0, 1]) + >>> print(y) + [[1, 3], + [0, 2]] + + >>> y= paddle.rot90(data, -1, [0, 1]) + >>> print(y) + [[2, 0], + [3, 1]] + + >>> data2 = paddle.arange(8) + >>> data2 = paddle.reshape(data2, (2,2,2)) + >>> print(data2) + [[[0, 1], + [2, 3]], + [[4, 5], + [6, 7]]] + + >>> y = paddle.rot90(data2, 1, [1, 2]) + >>> print(y) + [[[1, 3], + [0, 2]], + [[5, 7], + [4, 6]]] """ helper = LayerHelper("rot90", **locals()) @@ -1544,19 +1576,20 @@ def flatten(x, start_axis=0, stop_axis=-1, name=None): .. code-block:: python - import paddle + >>> import paddle - image_shape=(2, 3, 4, 4) + >>> image_shape=(2, 3, 4, 4) - x = paddle.arange(end=image_shape[0] * image_shape[1] * image_shape[2] * image_shape[3]) - img = paddle.reshape(x, image_shape) + >>> x = paddle.arange(end=image_shape[0] * image_shape[1] * image_shape[2] * image_shape[3]) + >>> img = paddle.reshape(x, image_shape) - out = paddle.flatten(img, start_axis=1, stop_axis=2) - # out shape is [2, 12, 4] + >>> out = paddle.flatten(img, start_axis=1, stop_axis=2) + >>> # out shape is [2, 12, 4] - # out shares data with img in dygraph mode - img[0, 0, 0, 0] = -1 - print(out[0, 0, 0]) # [-1] + >>> # out shares data with img in dygraph mode + >>> img[0, 0, 0, 0] = -1 + >>> print(out[0, 0, 0]) + [-1] """ if not (isinstance(x, Variable)): raise ValueError("The input x should be a Tensor") @@ -1685,26 +1718,26 @@ def roll(x, shifts, axis=None, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1.0, 2.0, 3.0], - [4.0, 5.0, 6.0], - [7.0, 8.0, 9.0]]) - out_z1 = paddle.roll(x, shifts=1) - print(out_z1) - #[[9. 1. 2.] - # [3. 4. 5.] - # [6. 7. 8.]] - out_z2 = paddle.roll(x, shifts=1, axis=0) - print(out_z2) - #[[7. 8. 9.] - # [1. 2. 3.] - # [4. 5. 6.]] - out_z3 = paddle.roll(x, shifts=1, axis=1) - print(out_z3) - #[[3. 1. 2.] - # [6. 4. 5.] - # [9. 7. 8.]] + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0, 3.0], + ... [4.0, 5.0, 6.0], + ... [7.0, 8.0, 9.0]]) + >>> out_z1 = paddle.roll(x, shifts=1) + >>> print(out_z1) + [[9. 1. 2.] + [3. 4. 5.] + [6. 7. 8.]] + >>> out_z2 = paddle.roll(x, shifts=1, axis=0) + >>> print(out_z2) + [[7. 8. 9.] + [1. 2. 3.] + [4. 5. 6.]] + >>> out_z3 = paddle.roll(x, shifts=1, axis=1) + >>> print(out_z3) + [[3. 1. 2.] + [6. 4. 5.] + [9. 7. 8.]] """ origin_shape = x.shape if type(shifts) == int: @@ -1831,25 +1864,25 @@ def stack(x, axis=0, name=None): Example: .. code-block:: python - import paddle + >>> import paddle - x1 = paddle.to_tensor([[1.0, 2.0]]) - x2 = paddle.to_tensor([[3.0, 4.0]]) - x3 = paddle.to_tensor([[5.0, 6.0]]) + >>> x1 = paddle.to_tensor([[1.0, 2.0]]) + >>> x2 = paddle.to_tensor([[3.0, 4.0]]) + >>> x3 = paddle.to_tensor([[5.0, 6.0]]) - out = paddle.stack([x1, x2, x3], axis=0) - print(out.shape) # [3, 1, 2] - print(out) - # [[[1., 2.]], - # [[3., 4.]], - # [[5., 6.]]] + >>> out = paddle.stack([x1, x2, x3], axis=0) + >>> print(out.shape) # [3, 1, 2] + >>> print(out) + [[[1., 2.]], + [[3., 4.]], + [[5., 6.]]] out = paddle.stack([x1, x2, x3], axis=-2) print(out.shape) # [1, 3, 2] print(out) - # [[[1., 2.], - # [3., 4.], - # [5., 6.]]] + [[[1., 2.], + [3., 4.], + [5., 6.]]] """ axis = 0 if axis is None else axis @@ -1938,31 +1971,43 @@ def split(x, num_or_sections, axis=0, name=None): Example: .. code-block:: python - import paddle - - # x is a Tensor of shape [3, 9, 5] - x = paddle.rand([3, 9, 5]) - - out0, out1, out2 = paddle.split(x, num_or_sections=3, axis=1) - print(out0.shape) # [3, 3, 5] - print(out1.shape) # [3, 3, 5] - print(out2.shape) # [3, 3, 5] - - out0, out1, out2 = paddle.split(x, num_or_sections=[2, 3, 4], axis=1) - print(out0.shape) # [3, 2, 5] - print(out1.shape) # [3, 3, 5] - print(out2.shape) # [3, 4, 5] - - out0, out1, out2 = paddle.split(x, num_or_sections=[2, 3, -1], axis=1) - print(out0.shape) # [3, 2, 5] - print(out1.shape) # [3, 3, 5] - print(out2.shape) # [3, 4, 5] - - # axis is negative, the real axis is (rank(x) + axis)=1 - out0, out1, out2 = paddle.split(x, num_or_sections=3, axis=-2) - print(out0.shape) # [3, 3, 5] - print(out1.shape) # [3, 3, 5] - print(out2.shape) # [3, 3, 5] + >>> import paddle + + >>> # x is a Tensor of shape [3, 9, 5] + >>> x = paddle.rand([3, 9, 5]) + + >>> out0, out1, out2 = paddle.split(x, num_or_sections=3, axis=1) + >>> print(out0.shape) + [3, 3, 5] + >>> print(out1.shape) + [3, 3, 5] + >>> print(out2.shape) + [3, 3, 5] + + >>> out0, out1, out2 = paddle.split(x, num_or_sections=[2, 3, 4], axis=1) + >>> print(out0.shape) + [3, 2, 5] + >>> print(out1.shape) + [3, 3, 5] + >>> print(out2.shape) + [3, 4, 5] + + >>> out0, out1, out2 = paddle.split(x, num_or_sections=[2, 3, -1], axis=1) + >>> print(out0.shape) + [3, 2, 5] + >>> print(out1.shape) + [3, 3, 5] + >>> print(out2.shape) + [3, 4, 5] + + >>> # axis is negative, the real axis is (rank(x) + axis)=1 + >>> out0, out1, out2 = paddle.split(x, num_or_sections=3, axis=-2) + >>> print(out0.shape) + [3, 3, 5] + >>> print(out1.shape) + [3, 3, 5] + >>> print(out2.shape) + [3, 3, 5] """ input = x dim = axis @@ -2112,21 +2157,29 @@ def vsplit(x, num_or_sections, name=None): Example: .. code-block:: python - import paddle - - # x is a Tensor of shape [8, 6, 7] - x = paddle.rand([8, 6, 7]) - out0, out1 = paddle.vsplit(x, num_or_sections=2) - print(out0.shape) # [4, 6, 7] - print(out1.shape) # [4, 6, 7] - out0, out1, out2 = paddle.vsplit(x, num_or_sections=[1, 3, 4]) - print(out0.shape) # [1, 6, 7] - print(out1.shape) # [3, 6, 7] - print(out2.shape) # [4, 6, 7] - out0, out1, out2 = paddle.vsplit(x, num_or_sections=[2, 3, -1]) - print(out0.shape) # [2, 6, 7] - print(out1.shape) # [3, 6, 7] - print(out2.shape) # [3, 6, 7] + >>> import paddle + + >>> # x is a Tensor of shape [8, 6, 7] + >>> x = paddle.rand([8, 6, 7]) + >>> out0, out1 = paddle.vsplit(x, num_or_sections=2) + >>> print(out0.shape) + [4, 6, 7] + >>> print(out1.shape) + [4, 6, 7] + >>> out0, out1, out2 = paddle.vsplit(x, num_or_sections=[1, 3, 4]) + >>> print(out0.shape) + [1, 6, 7] + >>> print(out1.shape) + [3, 6, 7] + >>> print(out2.shape) + [4, 6, 7] + >>> out0, out1, out2 = paddle.vsplit(x, num_or_sections=[2, 3, -1]) + >>> print(out0.shape) + [2, 6, 7] + >>> print(out1.shape) + [3, 6, 7] + >>> print(out2.shape) + [3, 6, 7] """ if x.ndim < 2: raise ValueError( @@ -2197,17 +2250,20 @@ def squeeze(x, axis=None, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.rand([5, 1, 10]) - output = paddle.squeeze(x, axis=1) + >>> x = paddle.rand([5, 1, 10]) + >>> output = paddle.squeeze(x, axis=1) - print(x.shape) # [5, 1, 10] - print(output.shape) # [5, 10] + >>> print(x.shape) + [5, 1, 10] + >>> print(output.shape) + [5, 10] - # output shares data with x in dygraph mode - x[0, 0, 0] = 10. - print(output[0, 0]) # [10.] + >>> # output shares data with x in dygraph mode + >>> x[0, 0, 0] = 10. + >>> print(output[0, 0]) + [10.] """ if axis is None: @@ -2322,37 +2378,37 @@ def unique_consecutive( Example: .. code-block:: python - import paddle - - x = paddle.to_tensor([1, 1, 2, 2, 3, 1, 1, 2]) - output = paddle.unique_consecutive(x) # - print(output) - # Tensor(shape=[5], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [1, 2, 3, 1, 2]) - - _, inverse, counts = paddle.unique_consecutive(x, return_inverse=True, return_counts=True) - print(inverse) - # Tensor(shape=[8], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [0, 0, 1, 1, 2, 3, 3, 4]) - print(counts) - # Tensor(shape=[5], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [2, 2, 1, 2, 1]) - - x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3], [2, 1, 3]]) - output = paddle.unique_consecutive(x, axis=0) # - print(output) - # Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [[2, 1, 3], - # [3, 0, 1], - # [2, 1, 3]]) - - x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3], [2, 1, 3]]) - output = paddle.unique_consecutive(x, axis=0) # - print(output) - # Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [[2, 1, 3], - # [3, 0, 1], - # [2, 1, 3]]) + >>> import paddle + + >>> x = paddle.to_tensor([1, 1, 2, 2, 3, 1, 1, 2]) + >>> output = paddle.unique_consecutive(x) # + >>> print(output) + Tensor(shape=[5], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [1, 2, 3, 1, 2]) + + >>> _, inverse, counts = paddle.unique_consecutive(x, return_inverse=True, return_counts=True) + >>> print(inverse) + Tensor(shape=[8], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [0, 0, 1, 1, 2, 3, 3, 4]) + >>> print(counts) + Tensor(shape=[5], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [2, 2, 1, 2, 1]) + + >>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3], [2, 1, 3]]) + >>> output = paddle.unique_consecutive(x, axis=0) # + >>> print(output) + Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[2, 1, 3], + [3, 0, 1], + [2, 1, 3]]) + + >>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3], [2, 1, 3]]) + >>> output = paddle.unique_consecutive(x, axis=0) # + >>> print(output) + Tensor(shape=[3, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[2, 1, 3], + [3, 0, 1], + [2, 1, 3]]) """ if axis is None: @@ -2451,36 +2507,36 @@ def unique( Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([2, 3, 3, 1, 5, 3]) - unique = paddle.unique(x) - print(unique) - # Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [1, 2, 3, 5]) - - _, indices, inverse, counts = paddle.unique(x, return_index=True, return_inverse=True, return_counts=True) - print(indices) - # Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [3, 0, 1, 4]) - print(inverse) - # Tensor(shape=[6], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [1, 2, 2, 0, 3, 2]) - print(counts) - # Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [1, 1, 3, 1]) - - x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3]]) - unique = paddle.unique(x) - print(unique) - # Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [0, 1, 2, 3]) - - unique = paddle.unique(x, axis=0) - print(unique) - # Tensor(shape=[2, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True, - # [[2, 1, 3], - # [3, 0, 1]]) + >>> import paddle + + >>> x = paddle.to_tensor([2, 3, 3, 1, 5, 3]) + >>> unique = paddle.unique(x) + >>> print(unique) + Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [1, 2, 3, 5]) + + >>> _, indices, inverse, counts = paddle.unique(x, return_index=True, return_inverse=True, return_counts=True) + >>> print(indices) + Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [3, 0, 1, 4]) + >>> print(inverse) + Tensor(shape=[6], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [1, 2, 2, 0, 3, 2]) + >>> print(counts) + Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [1, 1, 3, 1]) + + >>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1], [2, 1, 3]]) + >>> unique = paddle.unique(x) + >>> print(unique) + Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [0, 1, 2, 3]) + + >>> unique = paddle.unique(x, axis=0) + >>> print(unique) + Tensor(shape=[2, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[2, 1, 3], + [3, 0, 1]]) """ if axis is None: axis = [] @@ -2586,26 +2642,33 @@ def unsqueeze(x, axis, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.rand([5, 10]) - print(x.shape) # [5, 10] + >>> x = paddle.rand([5, 10]) + >>> print(x.shape) + [5, 10] - out1 = paddle.unsqueeze(x, axis=0) - print(out1.shape) # [1, 5, 10] + >>> out1 = paddle.unsqueeze(x, axis=0) + >>> print(out1.shape) + [1, 5, 10] - out2 = paddle.unsqueeze(x, axis=[0, 2]) - print(out2.shape) # [1, 5, 1, 10] + >>> out2 = paddle.unsqueeze(x, axis=[0, 2]) + >>> print(out2.shape) + [1, 5, 1, 10] - axis = paddle.to_tensor([0, 1, 2]) - out3 = paddle.unsqueeze(x, axis=axis) - print(out3.shape) # [1, 1, 1, 5, 10] + >>> axis = paddle.to_tensor([0, 1, 2]) + >>> out3 = paddle.unsqueeze(x, axis=axis) + >>> print(out3.shape) + [1, 1, 1, 5, 10] - # out1, out2, out3 share data with x in dygraph mode - x[0, 0] = 10. - print(out1[0, 0, 0]) # [10.] - print(out2[0, 0, 0, 0]) # [10.] - print(out3[0, 0, 0, 0, 0]) # [10.] + >>> # out1, out2, out3 share data with x in dygraph mode + >>> x[0, 0] = 10. + >>> print(out1[0, 0, 0]) + [10.] + >>> print(out2[0, 0, 0, 0]) + [10.] + >>> print(out3[0, 0, 0, 0, 0]) + [10.] """ input = x @@ -2729,12 +2792,12 @@ def gather(x, index, axis=None, name=None): .. code-block:: python - import paddle + >>> import paddle - input = paddle.to_tensor([[1,2],[3,4],[5,6]]) - index = paddle.to_tensor([0,1]) - output = paddle.gather(input, index, axis=0) - # expected output: [[1,2],[3,4]] + >>> input = paddle.to_tensor([[1,2],[3,4],[5,6]]) + >>> index = paddle.to_tensor([0,1]) + >>> output = paddle.gather(input, index, axis=0) + >>> # expected output: [[1,2],[3,4]] """ if axis is None: axis = 0 @@ -2798,21 +2861,21 @@ def unbind(input, axis=0): Example: .. code-block:: python - import paddle + >>> import paddle - # input is a Tensor which shape is [3, 4, 5] - input = paddle.rand([3, 4, 5]) + >>> # input is a Tensor which shape is [3, 4, 5] + >>> input = paddle.rand([3, 4, 5]) - [x0, x1, x2] = paddle.unbind(input, axis=0) - # x0.shape [4, 5] - # x1.shape [4, 5] - # x2.shape [4, 5] + >>> [x0, x1, x2] = paddle.unbind(input, axis=0) + >>> # x0.shape [4, 5] + >>> # x1.shape [4, 5] + >>> # x2.shape [4, 5] - [x0, x1, x2, x3] = paddle.unbind(input, axis=1) - # x0.shape [3, 5] - # x1.shape [3, 5] - # x2.shape [3, 5] - # x3.shape [3, 5] + >>> [x0, x1, x2, x3] = paddle.unbind(input, axis=1) + >>> # x0.shape [3, 5] + >>> # x1.shape [3, 5] + >>> # x2.shape [3, 5] + >>> # x3.shape [3, 5] """ if not isinstance(axis, (int)): raise TypeError( @@ -2872,26 +2935,26 @@ def scatter(x, index, updates, overwrite=True, name=None): .. code-block:: python :name: code-example1 - import paddle - #input: - x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32') - index = paddle.to_tensor([2, 1, 0, 1], dtype='int64') - # shape of updates should be the same as x - # shape of updates with dim > 1 should be the same as input - updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32') - overwrite = False - # calculation: - if not overwrite: - for i in range(len(index)): - x[index[i]] = paddle.zeros([2]) - for i in range(len(index)): - if (overwrite): - x[index[i]] = updates[i] - else: - x[index[i]] += updates[i] - # output: - out = paddle.to_tensor([[3, 3], [6, 6], [1, 1]]) - out.shape # [3, 2] + >>> import paddle + >>> #input: + >>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32') + >>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64') + >>> # shape of updates should be the same as x + >>> # shape of updates with dim > 1 should be the same as input + >>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32') + >>> overwrite = False + >>> # calculation: + >>> if not overwrite: + ... for i in range(len(index)): + ... x[index[i]] = paddle.zeros([2]) + >>> for i in range(len(index)): + ... if (overwrite): + ... x[index[i]] = updates[i] + ... else: + ... x[index[i]] += updates[i] + >>> # output: + >>> out = paddle.to_tensor([[3, 3], [6, 6], [1, 1]]) + >>> out.shape # [3, 2] **NOTICE**: The order in which updates are applied is nondeterministic, so the output will be nondeterministic if index contains duplicates. @@ -2913,31 +2976,33 @@ def scatter(x, index, updates, overwrite=True, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32') - index = paddle.to_tensor([2, 1, 0, 1], dtype='int64') - updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32') - - output1 = paddle.scatter(x, index, updates, overwrite=False) - # [[3., 3.], - # [6., 6.], - # [1., 1.]] - - output2 = paddle.scatter(x, index, updates, overwrite=True) - # CPU device: - # [[3., 3.], - # [4., 4.], - # [1., 1.]] - # GPU device maybe have two results because of the repeated numbers in index - # result 1: - # [[3., 3.], - # [4., 4.], - # [1., 1.]] - # result 2: - # [[3., 3.], - # [2., 2.], - # [1., 1.]] + >>> import paddle + + >>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32') + >>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64') + >>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32') + + >>> output1 = paddle.scatter(x, index, updates, overwrite=False) + >>> print(output1) + [[3., 3.], + [6., 6.], + [1., 1.]] + + >>> output2 = paddle.scatter(x, index, updates, overwrite=True) + >>> print(output2) + >>> # CPU device: + [[3., 3.], + [4., 4.], + [1., 1.]] + >>> # GPU device maybe have two results because of the repeated numbers in index + >>> # result 1: + [[3., 3.], + [4., 4.], + [1., 1.]] + >>> # result 2: + [[3., 3.], + [2., 2.], + [1., 1.]] """ if in_dynamic_mode(): return _C_ops.scatter(x, index, updates, overwrite) @@ -3026,17 +3091,17 @@ def scatter_nd_add(x, index, updates, name=None): .. code-block:: python - import paddle + >>> import paddle - x = paddle.rand(shape=[3, 5, 9, 10], dtype='float32') - updates = paddle.rand(shape=[3, 9, 10], dtype='float32') - index = paddle.to_tensor([[1, 1], - [0, 1], - [1, 3]], dtype='int64') + >>> x = paddle.rand(shape=[3, 5, 9, 10], dtype='float32') + >>> updates = paddle.rand(shape=[3, 9, 10], dtype='float32') + >>> index = paddle.to_tensor([[1, 1], + ... [0, 1], + ... [1, 3]], dtype='int64') - output = paddle.scatter_nd_add(x, index, updates) - print(output.shape) - # [3, 5, 9, 10] + >>> output = paddle.scatter_nd_add(x, index, updates) + >>> print(output.shape) + [3, 5, 9, 10] """ if in_dynamic_mode(): return _C_ops.scatter_nd_add(x, index, updates) @@ -3083,15 +3148,15 @@ def scatter_nd(index, updates, shape, name=None): .. code-block:: python - import paddle + >>> import paddle - index = paddle.to_tensor([[1, 1], - [0, 1], - [1, 3]], dtype="int64") - updates = paddle.rand(shape=[3, 9, 10], dtype='float32') - shape = [3, 5, 9, 10] + >>> index = paddle.to_tensor([[1, 1], + ... [0, 1], + ... [1, 3]], dtype="int64") + >>> updates = paddle.rand(shape=[3, 9, 10], dtype='float32') + >>> shape = [3, 5, 9, 10] - output = paddle.scatter_nd(index, updates, shape) + >>> output = paddle.scatter_nd(index, updates, shape) """ return scatter_nd_add(zeros(shape, updates.dtype), index, updates, name) @@ -3114,22 +3179,22 @@ def chunk(x, chunks, axis=0, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.rand([3, 9, 5]) + >>> x = paddle.rand([3, 9, 5]) - out0, out1, out2 = paddle.chunk(x, chunks=3, axis=1) - # out0.shape [3, 3, 5] - # out1.shape [3, 3, 5] - # out2.shape [3, 3, 5] + >>> out0, out1, out2 = paddle.chunk(x, chunks=3, axis=1) + >>> # out0.shape [3, 3, 5] + >>> # out1.shape [3, 3, 5] + >>> # out2.shape [3, 3, 5] - # axis is negative, the real axis is (rank(x) + axis) which real - # value is 1. - out0, out1, out2 = paddle.chunk(x, chunks=3, axis=-2) - # out0.shape [3, 3, 5] - # out1.shape [3, 3, 5] - # out2.shape [3, 3, 5] + >>> # axis is negative, the real axis is (rank(x) + axis) which real + >>> # value is 1. + >>> out0, out1, out2 = paddle.chunk(x, chunks=3, axis=-2) + >>> # out0.shape [3, 3, 5] + >>> # out1.shape [3, 3, 5] + >>> # out2.shape [3, 3, 5] """ check_type(chunks, 'chunks', (int), 'chunk') return split(x, num_or_sections=chunks, axis=axis, name=name) @@ -3155,26 +3220,26 @@ def tile(x, repeat_times, name=None): Examples: .. code-block:: python - import paddle - - data = paddle.to_tensor([1, 2, 3], dtype='int32') - out = paddle.tile(data, repeat_times=[2, 1]) - print(out) - # Tensor(shape=[2, 3], dtype=int32, place=Place(gpu:0), stop_gradient=True, - # [[1, 2, 3], - # [1, 2, 3]]) - - out = paddle.tile(data, repeat_times=(2, 2)) - print(out) - # Tensor(shape=[2, 6], dtype=int32, place=Place(gpu:0), stop_gradient=True, - # [[1, 2, 3, 1, 2, 3], - # [1, 2, 3, 1, 2, 3]]) - - repeat_times = paddle.to_tensor([1, 2], dtype='int32') - out = paddle.tile(data, repeat_times=repeat_times) - print(out) - # Tensor(shape=[1, 6], dtype=int32, place=Place(gpu:0), stop_gradient=True, - # [[1, 2, 3, 1, 2, 3]]) + >>> import paddle + + >>> data = paddle.to_tensor([1, 2, 3], dtype='int32') + >>> out = paddle.tile(data, repeat_times=[2, 1]) + >>> print(out) + Tensor(shape=[2, 3], dtype=int32, place=Place(gpu:0), stop_gradient=True, + [[1, 2, 3], + [1, 2, 3]]) + + >>> out = paddle.tile(data, repeat_times=(2, 2)) + >>> print(out) + Tensor(shape=[2, 6], dtype=int32, place=Place(gpu:0), stop_gradient=True, + [[1, 2, 3, 1, 2, 3], + [1, 2, 3, 1, 2, 3]]) + + >>> repeat_times = paddle.to_tensor([1, 2], dtype='int32') + >>> out = paddle.tile(data, repeat_times=repeat_times) + >>> print(out) + Tensor(shape=[1, 6], dtype=int32, place=Place(gpu:0), stop_gradient=True, + [[1, 2, 3, 1, 2, 3]]) """ if in_dynamic_mode(): if isinstance(repeat_times, core.eager.Tensor): @@ -3279,15 +3344,15 @@ def expand_as(x, y, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - data_x = paddle.to_tensor([1, 2, 3], 'int32') - data_y = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], 'int32') - out = paddle.expand_as(data_x, data_y) - print(out) - # Tensor(shape=[2, 3], dtype=int32, place=Place(gpu:0), stop_gradient=True, - # [[1, 2, 3], - # [1, 2, 3]]) + >>> data_x = paddle.to_tensor([1, 2, 3], 'int32') + >>> data_y = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], 'int32') + >>> out = paddle.expand_as(data_x, data_y) + >>> print(out) + Tensor(shape=[2, 3], dtype=int32, place=Place(gpu:0), stop_gradient=True, + [[1, 2, 3], + [1, 2, 3]]) """ if in_dynamic_mode(): return _C_ops.expand_as(x, None, y.shape) @@ -3349,12 +3414,12 @@ def broadcast_to(x, shape, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - data = paddle.to_tensor([1, 2, 3], dtype='int32') - out = paddle.broadcast_to(data, shape=[2, 3]) - print(out) - # [[1, 2, 3], [1, 2, 3]] + >>> data = paddle.to_tensor([1, 2, 3], dtype='int32') + >>> out = paddle.broadcast_to(data, shape=[2, 3]) + >>> print(out) + [[1, 2, 3], [1, 2, 3]] """ if in_dynamic_mode(): return _C_ops.expand(x, shape) @@ -3451,12 +3516,12 @@ def expand(x, shape, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - data = paddle.to_tensor([1, 2, 3], dtype='int32') - out = paddle.expand(data, shape=[2, 3]) - print(out) - # [[1, 2, 3], [1, 2, 3]] + >>> data = paddle.to_tensor([1, 2, 3], dtype='int32') + >>> out = paddle.expand(data, shape=[2, 3]) + >>> print(out) + [[1, 2, 3], [1, 2, 3]] """ if in_dynamic_or_pir_mode(): return _C_ops.expand(x, shape) @@ -3569,27 +3634,59 @@ def reshape(x, shape, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.rand([2, 4, 6], dtype="float32") - positive_four = paddle.full([1], 4, "int32") - - out = paddle.reshape(x, [-1, 0, 3, 2]) - print(out) - # the shape is [2,4,3,2]. - - out = paddle.reshape(x, shape=[positive_four, 12]) - print(out) - # the shape of out_2 is [4, 12]. - - shape_tensor = paddle.to_tensor([8, 6], dtype=paddle.int32) - out = paddle.reshape(x, shape=shape_tensor) - print(out.shape) - # the shape is [8, 6]. - # out shares data with x in dygraph mode - x[0, 0, 0] = 10. - print(out[0, 0]) - # the value is [10.] + >>> import paddle + >>> paddle.seed(1) + >>> x = paddle.rand([2, 4, 6], dtype="float32") + >>> positive_four = paddle.full([1], 4, "int32") + + >>> out = paddle.reshape(x, [-1, 0, 3, 2]) + >>> print(out) + >>> # the shape is [2,4,3,2]. + Tensor(shape=[2, 4, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[[0.01787627, 0.76492310], + [0.67605734, 0.04620579], + [0.38763246, 0.96462214]], + [[0.62356627, 0.64948404], + [0.53736508, 0.09874519], + [0.47123933, 0.85700107]], + [[0.34647217, 0.62869102], + [0.54760450, 0.18061899], + [0.55075216, 0.71997911]], + [[0.78576684, 0.76743823], + [0.35644373, 0.63325852], + [0.26549375, 0.68052763]]], + [[[0.40971112, 0.95848298], + [0.41229674, 0.05506011], + [0.18543524, 0.63480365]], + [[0.81180859, 0.94548297], + [0.19634065, 0.73838711], + [0.42057949, 0.96017945]], + [[0.72278100, 0.93858665], + [0.72018963, 0.61661047], + [0.33307818, 0.86660689]], + [[0.55808324, 0.12933673], + [0.42916751, 0.22745337], + [0.60508877, 0.82193440]]]]) + >>> out = paddle.reshape(x, shape=[positive_four, 12]) + >>> print(out) + >>> # the shape of out_2 is [4, 12]. + Tensor(shape=[4, 12], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.16556728, 0.98233348, 0.64653695, 0.68488085, 0.26045629, 0.32933319, + 0.29747701, 0.85176629, 0.84460896, 0.86866283, 0.46335083, 0.33930254], + [0.42339125, 0.14102051, 0.69832194, 0.63159829, 0.91087127, 0.31725556, + 0.09400324, 0.25861803, 0.26968575, 0.38659596, 0.25225133, 0.26315665], + [0.83726203, 0.33221707, 0.98031831, 0.38393897, 0.00512545, 0.04543629, + 0.90596122, 0.70148915, 0.26110184, 0.55263036, 0.68636090, 0.67877120], + [0.60491085, 0.96849394, 0.08488113, 0.48084566, 0.61894107, 0.92885363, + 0.37273413, 0.08096626, 0.74429852, 0.85212839, 0.60244918, 0.76718718]]) + >>> shape_tensor = paddle.to_tensor([8, 6], dtype=paddle.int32) + >>> out = paddle.reshape(x, shape=shape_tensor) + >>> print(out.shape) + [8, 6]. + >>> # out shares data with x in dygraph mode + >>> x[0, 0, 0] = 10. + >>> print(out[0, 0]) + [10.] """ if in_dynamic_or_pir_mode(): @@ -3789,13 +3886,15 @@ def gather_nd(x, index, name=None): .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([[[1, 2], [3, 4], [5, 6]], - [[7, 8], [9, 10], [11, 12]]]) - index = paddle.to_tensor([[0, 1]]) + >>> x = paddle.to_tensor([[[1, 2], [3, 4], [5, 6]], + ... [[7, 8], [9, 10], [11, 12]]]) + >>> index = paddle.to_tensor([[0, 1]]) - output = paddle.gather_nd(x, index) #[[3, 4]] + >>> output = paddle.gather_nd(x, index) + >>> print(output) + [[3, 4]] """ if in_dynamic_mode(): @@ -3899,22 +3998,22 @@ def strided_slice(x, axes, starts, ends, strides, name=None): Examples: .. code-block:: python - import paddle - x = paddle.zeros(shape=[3,4,5,6], dtype="float32") - # example 1: - # attr starts is a list which doesn't contain Tensor. - axes = [1, 2, 3] - starts = [-3, 0, 2] - ends = [3, 2, 4] - strides_1 = [1, 1, 1] - strides_2 = [1, 1, 2] - sliced_1 = paddle.strided_slice(x, axes=axes, starts=starts, ends=ends, strides=strides_1) - # sliced_1 is x[:, 1:3:1, 0:2:1, 2:4:1]. - # example 2: - # attr starts is a list which contain tensor Tensor. - minus_3 = paddle.full(shape=[1], fill_value=-3, dtype='int32') - sliced_2 = paddle.strided_slice(x, axes=axes, starts=[minus_3, 0, 2], ends=ends, strides=strides_2) - # sliced_2 is x[:, 1:3:1, 0:2:1, 2:4:2]. + >>> import paddle + >>> x = paddle.zeros(shape=[3,4,5,6], dtype="float32") + >>> # example 1: + >>> # attr starts is a list which doesn't contain Tensor. + >>> axes = [1, 2, 3] + >>> starts = [-3, 0, 2] + >>> ends = [3, 2, 4] + >>> strides_1 = [1, 1, 1] + >>> strides_2 = [1, 1, 2] + >>> sliced_1 = paddle.strided_slice(x, axes=axes, starts=starts, ends=ends, strides=strides_1) + >>> # sliced_1 is x[:, 1:3:1, 0:2:1, 2:4:1]. + >>> # example 2: + >>> # attr starts is a list which contain tensor Tensor. + >>> minus_3 = paddle.full(shape=[1], fill_value=-3, dtype='int32') + >>> sliced_2 = paddle.strided_slice(x, axes=axes, starts=[minus_3, 0, 2], ends=ends, strides=strides_2) + >>> # sliced_2 is x[:, 1:3:1, 0:2:1, 2:4:2]. """ if in_dynamic_mode(): return _C_ops.strided_slice(x, axes, starts, ends, strides) @@ -4088,74 +4187,80 @@ def tensordot(x, y, axes=2, name=None): Examples: .. code-block:: python - import paddle - - data_type = 'float64' - - # For two 2-d tensor x and y, the case axes=0 is equivalent to outer product. - # Note that tensordot supports empty axis sequence, so all the axes=0, axes=[], axes=[[]], and axes=[[],[]] are equivalent cases. - x = paddle.arange(4, dtype=data_type).reshape([2, 2]) - y = paddle.arange(4, dtype=data_type).reshape([2, 2]) - z = paddle.tensordot(x, y, axes=0) - # z = [[[[0., 0.], - # [0., 0.]], - # - # [[0., 1.], - # [2., 3.]]], - # - # - # [[[0., 2.], - # [4., 6.]], - # - # [[0., 3.], - # [6., 9.]]]] - - - # For two 1-d tensor x and y, the case axes=1 is equivalent to inner product. - x = paddle.arange(10, dtype=data_type) - y = paddle.arange(10, dtype=data_type) - z1 = paddle.tensordot(x, y, axes=1) - z2 = paddle.dot(x, y) - # z1 = z2 = 285. - - - # For two 2-d tensor x and y, the case axes=1 is equivalent to matrix multiplication. - x = paddle.arange(6, dtype=data_type).reshape([2, 3]) - y = paddle.arange(12, dtype=data_type).reshape([3, 4]) - z1 = paddle.tensordot(x, y, axes=1) - z2 = paddle.matmul(x, y) - # z1 = z2 = [[20., 23., 26., 29.], - # [56., 68., 80., 92.]] - - - # When axes is a 1-d int list, x and y will be contracted along the same given axes. - # Note that axes=[1, 2] is equivalent to axes=[[1, 2]], axes=[[1, 2], []], axes=[[1, 2], [1]], and axes=[[1, 2], [1, 2]]. - x = paddle.arange(24, dtype=data_type).reshape([2, 3, 4]) - y = paddle.arange(36, dtype=data_type).reshape([3, 3, 4]) - z = paddle.tensordot(x, y, axes=[1, 2]) - # z = [[506. , 1298., 2090.], - # [1298., 3818., 6338.]] - - - # When axes is a list containing two 1-d int list, the first will be applied to x and the second to y. - x = paddle.arange(60, dtype=data_type).reshape([3, 4, 5]) - y = paddle.arange(24, dtype=data_type).reshape([4, 3, 2]) - z = paddle.tensordot(x, y, axes=([1, 0], [0, 1])) - # z = [[4400., 4730.], - # [4532., 4874.], - # [4664., 5018.], - # [4796., 5162.], - # [4928., 5306.]] - - - # Thanks to the support of axes expansion, axes=[[0, 1, 3, 4], [1, 0, 3, 4]] can be abbreviated as axes= [[0, 1, 3, 4], [1, 0]]. - x = paddle.arange(720, dtype=data_type).reshape([2, 3, 4, 5, 6]) - y = paddle.arange(720, dtype=data_type).reshape([3, 2, 4, 5, 6]) - z = paddle.tensordot(x, y, axes=[[0, 1, 3, 4], [1, 0]]) - # z = [[23217330., 24915630., 26613930., 28312230.], - # [24915630., 26775930., 28636230., 30496530.], - # [26613930., 28636230., 30658530., 32680830.], - # [28312230., 30496530., 32680830., 34865130.]] + >>> import paddle + + >>> data_type = 'float64' + + >>> # For two 2-d tensor x and y, the case axes=0 is equivalent to outer product. + >>> # Note that tensordot supports empty axis sequence, so all the axes=0, axes=[], axes=[[]], and axes=[[],[]] are equivalent cases. + >>> x = paddle.arange(4, dtype=data_type).reshape([2, 2]) + >>> y = paddle.arange(4, dtype=data_type).reshape([2, 2]) + >>> z = paddle.tensordot(x, y, axes=0) + >>> print(z) + [[[[0., 0.], + [0., 0.]], + [[0., 1.], + [2., 3.]]], + [[[0., 2.], + [4., 6.]], + [[0., 3.], + [6., 9.]]]] + + + >>> # For two 1-d tensor x and y, the case axes=1 is equivalent to inner product. + >>> x = paddle.arange(10, dtype=data_type) + >>> y = paddle.arange(10, dtype=data_type) + >>> z1 = paddle.tensordot(x, y, axes=1) + >>> z2 = paddle.dot(x, y) + >>> print(z1) + [285.] + >>> print(z2) + [285.] + + >>> # For two 2-d tensor x and y, the case axes=1 is equivalent to matrix multiplication. + >>> x = paddle.arange(6, dtype=data_type).reshape([2, 3]) + >>> y = paddle.arange(12, dtype=data_type).reshape([3, 4]) + >>> z1 = paddle.tensordot(x, y, axes=1) + >>> print(z1) + [[20., 23., 26., 29.], + [56., 68., 80., 92.]] + >>> z2 = paddle.matmul(x, y) + >>> print(z2) + [[20., 23., 26., 29.], + [56., 68., 80., 92.]] + + + >>> # When axes is a 1-d int list, x and y will be contracted along the same given axes. + >>> # Note that axes=[1, 2] is equivalent to axes=[[1, 2]], axes=[[1, 2], []], axes=[[1, 2], [1]], and axes=[[1, 2], [1, 2]]. + >>> x = paddle.arange(24, dtype=data_type).reshape([2, 3, 4]) + >>> y = paddle.arange(36, dtype=data_type).reshape([3, 3, 4]) + >>> z = paddle.tensordot(x, y, axes=[1, 2]) + >>> print(z) + [[506. , 1298., 2090.], + [1298., 3818., 6338.]] + + + >>> # When axes is a list containing two 1-d int list, the first will be applied to x and the second to y. + >>> x = paddle.arange(60, dtype=data_type).reshape([3, 4, 5]) + >>> y = paddle.arange(24, dtype=data_type).reshape([4, 3, 2]) + >>> z = paddle.tensordot(x, y, axes=([1, 0], [0, 1])) + >>> print(z) + [[4400., 4730.], + [4532., 4874.], + [4664., 5018.], + [4796., 5162.], + [4928., 5306.]] + + + >>> # Thanks to the support of axes expansion, axes=[[0, 1, 3, 4], [1, 0, 3, 4]] can be abbreviated as axes= [[0, 1, 3, 4], [1, 0]]. + >>> x = paddle.arange(720, dtype=data_type).reshape([2, 3, 4, 5, 6]) + >>> y = paddle.arange(720, dtype=data_type).reshape([3, 2, 4, 5, 6]) + >>> z = paddle.tensordot(x, y, axes=[[0, 1, 3, 4], [1, 0]]) + >>> print(z) + [[23217330., 24915630., 26613930., 28312230.], + [24915630., 26775930., 28636230., 30496530.], + [26613930., 28636230., 30658530., 32680830.], + [28312230., 30496530., 32680830., 34865130.]] """ op_type = 'tensordot' input_dtype = ['float16', 'float32', 'float64'] @@ -4279,14 +4384,13 @@ def as_complex(x, name=None): Examples: .. code-block:: python - import paddle - x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2]) - y = paddle.as_complex(x) - print(y) - - # Tensor(shape=[2, 3], dtype=complex64, place=Place(gpu:0), stop_gradient=True, - # [[1j , (2+3j) , (4+5j) ], - # [(6+7j) , (8+9j) , (10+11j)]]) + >>> import paddle + >>> x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2]) + >>> y = paddle.as_complex(x) + >>> print(y) + Tensor(shape=[2, 3], dtype=complex64, place=Place(gpu:0), stop_gradient=True, + [[1j , (2+3j) , (4+5j) ], + [(6+7j) , (8+9j) , (10+11j)]]) """ if in_dynamic_mode(): return _C_ops.as_complex(x) @@ -4326,20 +4430,18 @@ def as_real(x, name=None): Examples: .. code-block:: python - import paddle - x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2]) - y = paddle.as_complex(x) - z = paddle.as_real(y) - print(z) - - # Tensor(shape=[2, 3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[[0. , 1. ], - # [2. , 3. ], - # [4. , 5. ]], - - # [[6. , 7. ], - # [8. , 9. ], - # [10., 11.]]]) + >>> import paddle + >>> x = paddle.arange(12, dtype=paddle.float32).reshape([2, 3, 2]) + >>> y = paddle.as_complex(x) + >>> z = paddle.as_real(y) + >>> print(z) + Tensor(shape=[2, 3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[[0. , 1. ], + [2. , 3. ], + [4. , 5. ]], + [[6. , 7. ], + [8. , 9. ], + [10., 11.]]]) """ if in_dynamic_mode(): return _C_ops.as_real(x) @@ -4376,20 +4478,20 @@ def repeat_interleave(x, repeats, axis=None, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]]) - repeats = paddle.to_tensor([3, 2, 1], dtype='int32') + >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]]) + >>> repeats = paddle.to_tensor([3, 2, 1], dtype='int32') - paddle.repeat_interleave(x, repeats, 1) - # [[1, 1, 1, 2, 2, 3], - # [4, 4, 4, 5, 5, 6]] + >>> paddle.repeat_interleave(x, repeats, 1) + >>> # [[1, 1, 1, 2, 2, 3], + >>> # [4, 4, 4, 5, 5, 6]] - paddle.repeat_interleave(x, 2, 0) - # [[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]] + >>> paddle.repeat_interleave(x, 2, 0) + >>> # [[1, 2, 3], [1, 2, 3], [4, 5, 6], [4, 5, 6]] - paddle.repeat_interleave(x, 2, None) - # [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6] + >>> paddle.repeat_interleave(x, 2, None) + >>> # [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6] """ if axis is None: @@ -4444,15 +4546,15 @@ def moveaxis(x, source, destination, name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.ones([3, 2, 4]) - paddle.moveaxis(x, [0, 1], [1, 2]).shape - # [4, 3, 2] + >>> x = paddle.ones([3, 2, 4]) + >>> paddle.moveaxis(x, [0, 1], [1, 2]).shape + >>> # [4, 3, 2] - x = paddle.ones([2, 3]) - paddle.moveaxis(x, 0, 1).shape # equivalent to paddle.t(x) - # [3, 2] + >>> x = paddle.ones([2, 3]) + >>> paddle.moveaxis(x, 0, 1).shape # equivalent to paddle.t(x) + >>> # [3, 2] """ src = [source] if isinstance(source, int) else source dst = [destination] if isinstance(destination, int) else destination @@ -4581,14 +4683,14 @@ def take_along_axis(arr, indices, axis): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7,8,9]]) - index = paddle.to_tensor([[0]]) - axis = 0 - result = paddle.take_along_axis(x, index, axis) - print(result) - # [[1, 2, 3]] + >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7,8,9]]) + >>> index = paddle.to_tensor([[0]]) + >>> axis = 0 + >>> result = paddle.take_along_axis(x, index, axis) + >>> print(result) + [[1, 2, 3]] """ if len(arr.shape) != len(indices.shape): raise ValueError( @@ -4658,16 +4760,16 @@ def put_along_axis(arr, indices, values, axis, reduce='assign'): Examples: .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([[10, 30, 20], [60, 40, 50]]) - index = paddle.to_tensor([[0]]) - value = 99 - axis = 0 - result = paddle.put_along_axis(x, index, value, axis) - print(result) - # [[99, 99, 99], - # [60, 40, 50]] + >>> x = paddle.to_tensor([[10, 30, 20], [60, 40, 50]]) + >>> index = paddle.to_tensor([[0]]) + >>> value = 99 + >>> axis = 0 + >>> result = paddle.put_along_axis(x, index, value, axis) + >>> print(result) + [[99, 99, 99], + [60, 40, 50]] """ if len(arr.shape) != len(indices.shape): @@ -4760,18 +4862,18 @@ def index_add(x, index, axis, value, name=None): Examples: .. code-block:: python - # required: gpu - import paddle - - input_tensor = paddle.to_tensor(paddle.ones((3, 3)), dtype="float32") - index = paddle.to_tensor([0, 2], dtype="int32") - value = paddle.to_tensor([[1, 1, 1], [1, 1, 1]], dtype="float32") - outplace_res = paddle.index_add(input_tensor, index, 0, value) - print(outplace_res) - # Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[2., 2., 2.], - # [1., 1., 1.], - # [2., 2., 2.]]) + >>> # required: gpu + >>> import paddle + + >>> input_tensor = paddle.to_tensor(paddle.ones((3, 3)), dtype="float32") + >>> index = paddle.to_tensor([0, 2], dtype="int32") + >>> value = paddle.to_tensor([[1, 1, 1], [1, 1, 1]], dtype="float32") + >>> outplace_res = paddle.index_add(input_tensor, index, 0, value) + >>> print(outplace_res) + Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[2., 2., 2.], + [1., 1., 1.], + [2., 2., 2.]]) """ if in_dynamic_mode(): return _C_ops.index_add(x, index, value, axis) @@ -4820,18 +4922,18 @@ def index_add_(x, index, axis, value, name=None): Examples: .. code-block:: python - # required: gpu - import paddle - - input_tensor = paddle.to_tensor(paddle.ones((3, 3)), dtype="float32") - index = paddle.to_tensor([0, 2], dtype="int32") - value = paddle.to_tensor([[1, 1], [1, 1], [1, 1]], dtype="float32") - inplace_res = paddle.index_add_(input_tensor, index, 1, value) - print(inplace_res) - # Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[2., 1., 2.], - # [2., 1., 2.], - # [2., 1., 2.]]) + >>> # required: gpu + >>> import paddle + + >>> input_tensor = paddle.to_tensor(paddle.ones((3, 3)), dtype="float32") + >>> index = paddle.to_tensor([0, 2], dtype="int32") + >>> value = paddle.to_tensor([[1, 1], [1, 1], [1, 1]], dtype="float32") + >>> inplace_res = paddle.index_add_(input_tensor, index, 1, value) + >>> print(inplace_res) + Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[2., 1., 2.], + [2., 1., 2.], + [2., 1., 2.]]) """ return _C_ops.index_add_(x, index, value, axis) @@ -4857,25 +4959,25 @@ def index_put_(x, indices, value, accumulate=False, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.zeros([3, 3]) - value = paddle.ones([3]) - ix1 = paddle.to_tensor([0,1,2]) - ix2 = paddle.to_tensor([1,2,1]) - indices=(ix1,ix2) - - out = paddle.index_put_(x,indices,value) - print(x) - # Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[0., 1., 0.], - # [0., 0., 1.], - # [0., 1., 0.]]) - print(out) - # Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[0., 1., 0.], - # [0., 0., 1.], - # [0., 1., 0.]]) + >>> import paddle + + >>> x = paddle.zeros([3, 3]) + >>> value = paddle.ones([3]) + >>> ix1 = paddle.to_tensor([0,1,2]) + >>> ix2 = paddle.to_tensor([1,2,1]) + >>> indices=(ix1,ix2) + + >>> out = paddle.index_put_(x,indices,value) + >>> print(x) + Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[0., 1., 0.], + [0., 0., 1.], + [0., 1., 0.]]) + >>> print(out) + Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[0., 1., 0.], + [0., 0., 1.], + [0., 1., 0.]]) """ return _C_ops.index_put_(x, indices, value, accumulate) @@ -4888,25 +4990,25 @@ def index_put(x, indices, value, accumulate=False, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.zeros([3, 3]) - value = paddle.ones([3]) - ix1 = paddle.to_tensor([0,1,2]) - ix2 = paddle.to_tensor([1,2,1]) - indices=(ix1,ix2) - - out = paddle.index_put(x,indices,value) - print(x) - # Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[0., 0., 0.], - # [0., 0., 0.], - # [0., 0., 0.]]) - print(out) - # Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, - # [[0., 1., 0.], - # [0., 0., 1.], - # [0., 1., 0.]]) + >>> import paddle + + >>> x = paddle.zeros([3, 3]) + >>> value = paddle.ones([3]) + >>> ix1 = paddle.to_tensor([0,1,2]) + >>> ix2 = paddle.to_tensor([1,2,1]) + >>> indices=(ix1,ix2) + + >>> out = paddle.index_put(x,indices,value) + >>> print(x) + Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[0., 0., 0.], + [0., 0., 0.], + [0., 0., 0.]]) + >>> print(out) + Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[0., 1., 0.], + [0., 0., 1.], + [0., 1., 0.]]) """ if in_dynamic_mode(): return _C_ops.index_put(x, indices, value, accumulate) @@ -4959,28 +5061,28 @@ def unflatten(x, axis, shape, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.randn(shape=[4, 6, 8]) - shape = [2, 3] - axis = 1 - res = paddle.unflatten(x, axis, shape) - print(res.shape) - # [4, 2, 3, 8] - - x = paddle.randn(shape=[4, 6, 8]) - shape = (-1, 2) - axis = -1 - res = paddle.unflatten(x, axis, shape) - print(res.shape) - # [4, 6, 4, 2] - - x = paddle.randn(shape=[4, 6, 8]) - shape = paddle.to_tensor([2, 2]) - axis = 0 - res = paddle.unflatten(x, axis, shape) - print(res.shape) - # [2, 2, 6, 8] + >>> import paddle + + >>> x = paddle.randn(shape=[4, 6, 8]) + >>> shape = [2, 3] + >>> axis = 1 + >>> res = paddle.unflatten(x, axis, shape) + >>> print(res.shape) + [4, 2, 3, 8] + + >>> x = paddle.randn(shape=[4, 6, 8]) + >>> shape = (-1, 2) + >>> axis = -1 + >>> res = paddle.unflatten(x, axis, shape) + >>> print(res.shape) + [4, 6, 4, 2] + + >>> x = paddle.randn(shape=[4, 6, 8]) + >>> shape = paddle.to_tensor([2, 2]) + >>> axis = 0 + >>> res = paddle.unflatten(x, axis, shape) + >>> print(res.shape) + [2, 2, 6, 8] """ # determine whether the input axis is valid. @@ -5029,15 +5131,22 @@ def as_strided(x, shape, stride, offset=0, name=None): Examples: .. code-block:: python - import paddle - paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) - - x = paddle.rand([2, 4, 6], dtype="float32") - - out = paddle.as_strided(x, [8, 6], [6, 1]) - print(out) - # the shape is [8, 6]. - # the stride is [6, 1]. + >>> import paddle + >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) + >>> paddle.seed(1) + >>> x = paddle.rand([2, 4, 6], dtype="float32") + + >>> out = paddle.as_strided(x, [8, 6], [6, 1]) + >>> print(out) + Tensor(shape=[8, 6], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.01787627, 0.76492310, 0.67605734, 0.04620579, 0.38763246, 0.96462214], + [0.62356627, 0.64948404, 0.53736508, 0.09874519, 0.47123933, 0.85700107], + [0.34647217, 0.62869102, 0.54760450, 0.18061899, 0.55075216, 0.71997911], + [0.78576684, 0.76743823, 0.35644373, 0.63325852, 0.26549375, 0.68052763], + [0.40971112, 0.95848298, 0.41229674, 0.05506011, 0.18543524, 0.63480365], + [0.81180859, 0.94548297, 0.19634065, 0.73838711, 0.42057949, 0.96017945], + [0.72278100, 0.93858665, 0.72018963, 0.61661047, 0.33307818, 0.86660689], + [0.55808324, 0.12933673, 0.42916751, 0.22745337, 0.60508877, 0.82193440]]) """ return _C_ops.as_strided(x, shape, stride, offset) @@ -5061,22 +5170,48 @@ def view(x, shape_or_dtype, name=None): Examples: .. code-block:: python - import paddle - paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) - - x = paddle.rand([2, 4, 6], dtype="float32") - - out = paddle.view(x, [8, 6]) - print(out) - - - import paddle - paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) - - x = paddle.rand([2, 4, 6], dtype="float32") - - out = paddle.view(x, "uint8") - print(out) + >>> import paddle + >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) + >>> paddle.seed(1) + >>> x = paddle.rand([2, 4, 6], dtype="float32") + + >>> out = paddle.view(x, [8, 6]) + >>> print(out) + Tensor(shape=[8, 6], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.01787627, 0.76492310, 0.67605734, 0.04620579, 0.38763246, 0.96462214], + [0.62356627, 0.64948404, 0.53736508, 0.09874519, 0.47123933, 0.85700107], + [0.34647217, 0.62869102, 0.54760450, 0.18061899, 0.55075216, 0.71997911], + [0.78576684, 0.76743823, 0.35644373, 0.63325852, 0.26549375, 0.68052763], + [0.40971112, 0.95848298, 0.41229674, 0.05506011, 0.18543524, 0.63480365], + [0.81180859, 0.94548297, 0.19634065, 0.73838711, 0.42057949, 0.96017945], + [0.72278100, 0.93858665, 0.72018963, 0.61661047, 0.33307818, 0.86660689], + [0.55808324, 0.12933673, 0.42916751, 0.22745337, 0.60508877, 0.82193440]]) + + + >>> import paddle + >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) + + >>> x = paddle.rand([2, 4, 6], dtype="float32") + + >>> out = paddle.view(x, "uint8") + >>> print(out) + Tensor(shape=[2, 4, 24], dtype=uint8, place=Place(cpu), stop_gradient=True, + [[[71 , 109, 163, 60 , 60 , 56 , 7 , 63 , 11 , 192, 144, 61 , 203, 148, + 77 , 63 , 132, 130, 47 , 61 , 250, 35 , 91 , 63 ], + [105, 99 , 158, 62 , 111, 71 , 114, 62 , 6 , 18 , 191, 62 , 236, 133, + 12 , 63 , 77 , 90 , 39 , 62 , 59 , 179, 115, 62 ], + [16 , 25 , 166, 62 , 71 , 248, 177, 61 , 107, 253, 25 , 63 , 24 , 89 , + 226, 62 , 118, 134, 198, 61 , 86 , 130, 32 , 61 ], + [144, 79 , 55 , 63 , 53 , 46 , 139, 62 , 173, 73 , 91 , 63 , 107, 97 , + 57 , 63 , 247, 236, 72 , 63 , 44 , 100, 65 , 62 ]], + [[45 , 58 , 104, 63 , 201, 126, 97 , 63 , 11 , 2 , 162, 62 , 145, 2 , + 44 , 63 , 7 , 6 , 4 , 63 , 89 , 241, 89 , 61 ], + [25 , 65 , 164, 62 , 47 , 75 , 89 , 63 , 100, 232, 16 , 60 , 139, 91 , + 54 , 63 , 165, 227, 11 , 63 , 255, 208, 190, 62 ], + [36 , 167, 233, 62 , 233, 125, 5 , 63 , 100, 238, 112, 63 , 102, 204, + 145, 62 , 217, 161, 65 , 63 , 175, 206, 226, 62 ], + [255, 40 , 62 , 63 , 49 , 216, 148, 62 , 50 , 206, 175, 62 , 159, 194, + 41 , 61 , 189, 22 , 144, 62 , 57 , 199, 253, 62 ]]]) """ if isinstance(shape_or_dtype, (list, tuple)): return _C_ops.view_shape(x, shape_or_dtype) @@ -5105,14 +5240,23 @@ def view_as(x, other, name=None): Examples: .. code-block:: python - import paddle - paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) - - x = paddle.rand([2, 4, 6], dtype="float32") - y = paddle.rand([8, 6], dtype="float32") - - out = paddle.view_as(x, y) - print(out) + >>> import paddle + >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) + + >>> x = paddle.rand([2, 4, 6], dtype="float32") + >>> y = paddle.rand([8, 6], dtype="float32") + + >>> out = paddle.view_as(x, y) + >>> print(out) + Tensor(shape=[8, 6], dtype=float32, place=Place(cpu), stop_gradient=True, + [[0.23340422, 0.62036550, 0.12185233, 0.88982582, 0.61383390, 0.22415221], + [0.85698199, 0.87602723, 0.00502827, 0.92127788, 0.34579527, 0.85451263], + [0.68906647, 0.05126054, 0.25322226, 0.43883288, 0.49656981, 0.68857300], + [0.21424839, 0.99576813, 0.62306029, 0.48010525, 0.31222206, 0.48758999], + [0.35929242, 0.12802263, 0.80540675, 0.76783085, 0.84970695, 0.00389719], + [0.69256896, 0.73718327, 0.97155327, 0.50271672, 0.60357946, 0.59580350], + [0.68182445, 0.28539398, 0.13948134, 0.66333318, 0.72601736, 0.31108484], + [0.58283192, 0.89317679, 0.11751470, 0.04111906, 0.04056534, 0.75454420]]) """ return _C_ops.view_shape(x, other.shape) @@ -5138,13 +5282,15 @@ def unfold(x, axis, size, step, name=None): Examples: .. code-block:: python - import paddle - paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) + >>> import paddle + >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) - x = paddle.arange(9, dtype="float64") + >>> x = paddle.arange(9, dtype="float64") - out = paddle.unfold(x, 0, 2, 4) - print(out) # [[0, 1], [4, 5]] + >>> out = paddle.unfold(x, 0, 2, 4) + >>> print(out) + [[0., 1.], + [4., 5.]] """ return _C_ops.tensor_unfold(x, axis, size, step) diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index 5a90657016067e..945a67859ce9ac 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -58,42 +58,42 @@ def argsort(x, axis=-1, descending=False, name=None): .. code-block:: python - import paddle - - x = paddle.to_tensor([[[5,8,9,5], - [0,0,1,7], - [6,9,2,4]], - [[5,2,4,2], - [4,7,7,9], - [1,7,0,6]]], - dtype='float32') - out1 = paddle.argsort(x, axis=-1) - out2 = paddle.argsort(x, axis=0) - out3 = paddle.argsort(x, axis=1) - - print(out1) - #[[[0 3 1 2] - # [0 1 2 3] - # [2 3 0 1]] - # [[1 3 2 0] - # [0 1 2 3] - # [2 0 3 1]]] - - print(out2) - #[[[0 1 1 1] - # [0 0 0 0] - # [1 1 1 0]] - # [[1 0 0 0] - # [1 1 1 1] - # [0 0 0 1]]] - - print(out3) - #[[[1 1 1 2] - # [0 0 2 0] - # [2 2 0 1]] - # [[2 0 2 0] - # [1 1 0 2] - # [0 2 1 1]]] + >>> import paddle + + >>> x = paddle.to_tensor([[[5,8,9,5], + ... [0,0,1,7], + ... [6,9,2,4]], + ... [[5,2,4,2], + ... [4,7,7,9], + ... [1,7,0,6]]], + ... dtype='float32') + >>> out1 = paddle.argsort(x, axis=-1) + >>> out2 = paddle.argsort(x, axis=0) + >>> out3 = paddle.argsort(x, axis=1) + + >>> print(out1) + [[[0 3 1 2] + [0 1 2 3] + [2 3 0 1]] + [[1 3 2 0] + [0 1 2 3] + [2 0 3 1]]] + + >>> print(out2) + [[[0 1 1 1] + [0 0 0 0] + [1 1 1 0]] + [[1 0 0 0] + [1 1 1 1] + [0 0 0 1]]] + + >>> print(out3) + [[[1 1 1 2] + [0 0 2 0] + [2 2 0 1]] + [[2 0 2 0] + [1 1 0 2] + [0 2 1 1]]] """ if in_dynamic_mode(): _, ids = _C_ops.argsort(x, axis, descending) @@ -153,22 +153,22 @@ def argmax(x, axis=None, keepdim=False, dtype="int64", name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[5,8,9,5], - [0,0,1,7], - [6,9,2,4]]) - out1 = paddle.argmax(x) - print(out1) # 2 - out2 = paddle.argmax(x, axis=0) - print(out2) - # [2, 2, 0, 1] - out3 = paddle.argmax(x, axis=-1) - print(out3) - # [2, 3, 1] - out4 = paddle.argmax(x, axis=0, keepdim=True) - print(out4) - # [[2, 2, 0, 1]] + >>> import paddle + + >>> x = paddle.to_tensor([[5,8,9,5], + ... [0,0,1,7], + ... [6,9,2,4]]) + >>> out1 = paddle.argmax(x) + >>> print(out1) # 2 + >>> out2 = paddle.argmax(x, axis=0) + >>> print(out2) + [2, 2, 0, 1] + >>> out3 = paddle.argmax(x, axis=-1) + >>> print(out3) + [2, 3, 1] + >>> out4 = paddle.argmax(x, axis=0, keepdim=True) + >>> print(out4) + [[2, 2, 0, 1]] """ if axis is not None and not isinstance(axis, (int, Variable)): raise TypeError( @@ -243,22 +243,22 @@ def argmin(x, axis=None, keepdim=False, dtype="int64", name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[5,8,9,5], - [0,0,1,7], - [6,9,2,4]]) - out1 = paddle.argmin(x) - print(out1) # 4 - out2 = paddle.argmin(x, axis=0) - print(out2) - # [1, 1, 1, 2] - out3 = paddle.argmin(x, axis=-1) - print(out3) - # [0, 0, 2] - out4 = paddle.argmin(x, axis=0, keepdim=True) - print(out4) - # [[1, 1, 1, 2]] + >>> import paddle + + >>> x = paddle.to_tensor([[5,8,9,5], + ... [0,0,1,7], + ... [6,9,2,4]]) + >>> out1 = paddle.argmin(x) + >>> print(out1) # 4 + >>> out2 = paddle.argmin(x, axis=0) + >>> print(out2) + [1, 1, 1, 2] + >>> out3 = paddle.argmin(x, axis=-1) + >>> print(out3) + [0, 0, 2] + >>> out4 = paddle.argmin(x, axis=0, keepdim=True) + >>> print(out4) + [[1, 1, 1, 2]] """ if axis is not None and not isinstance(axis, (int, Variable)): raise TypeError( @@ -330,20 +330,21 @@ def index_select(x, index, axis=0, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], - [5.0, 6.0, 7.0, 8.0], - [9.0, 10.0, 11.0, 12.0]]) - index = paddle.to_tensor([0, 1, 1], dtype='int32') - out_z1 = paddle.index_select(x=x, index=index) - #[[1. 2. 3. 4.] - # [5. 6. 7. 8.] - # [5. 6. 7. 8.]] - out_z2 = paddle.index_select(x=x, index=index, axis=1) - #[[ 1. 2. 2.] - # [ 5. 6. 6.] - # [ 9. 10. 10.]] + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], + ... [5.0, 6.0, 7.0, 8.0], + ... [9.0, 10.0, 11.0, 12.0]]) + >>> index = paddle.to_tensor([0, 1, 1], dtype='int32') + >>> out_z1 = paddle.index_select(x=x, index=index) + print(out_z1) + [[1. 2. 3. 4.] + [5. 6. 7. 8.] + [5. 6. 7. 8.]] + >>> out_z2 = paddle.index_select(x=x, index=index, axis=1) + [[ 1. 2. 2.] + [ 5. 6. 6.] + [ 9. 10. 10.]] """ if in_dynamic_mode(): @@ -404,35 +405,35 @@ def nonzero(x, as_tuple=False): .. code-block:: python - import paddle - - x1 = paddle.to_tensor([[1.0, 0.0, 0.0], - [0.0, 2.0, 0.0], - [0.0, 0.0, 3.0]]) - x2 = paddle.to_tensor([0.0, 1.0, 0.0, 3.0]) - out_z1 = paddle.nonzero(x1) - print(out_z1) - #[[0 0] - # [1 1] - # [2 2]] - out_z1_tuple = paddle.nonzero(x1, as_tuple=True) - for out in out_z1_tuple: - print(out) - #[[0] - # [1] - # [2]] - #[[0] - # [1] - # [2]] - out_z2 = paddle.nonzero(x2) - print(out_z2) - #[[1] - # [3]] - out_z2_tuple = paddle.nonzero(x2, as_tuple=True) - for out in out_z2_tuple: - print(out) - #[[1] - # [3]] + >>> import paddle + + >>> x1 = paddle.to_tensor([[1.0, 0.0, 0.0], + ... [0.0, 2.0, 0.0], + ... [0.0, 0.0, 3.0]]) + >>> x2 = paddle.to_tensor([0.0, 1.0, 0.0, 3.0]) + >>> out_z1 = paddle.nonzero(x1) + >>> print(out_z1) + [[0 0] + [1 1] + [2 2]] + >>> out_z1_tuple = paddle.nonzero(x1, as_tuple=True) + >>> for out in out_z1_tuple: + ... print(out) + [[0] + [1] + [2]] + [[0] + [1] + [2]] + >>> out_z2 = paddle.nonzero(x2) + >>> print(out_z2) + [[1] + [3]] + >>> out_z2_tuple = paddle.nonzero(x2, as_tuple=True) + >>> for out in out_z2_tuple: + ... print(out) + [[1] + [3]] """ list_out = [] @@ -502,39 +503,39 @@ def sort(x, axis=-1, descending=False, name=None): .. code-block:: python - import paddle - - x = paddle.to_tensor([[[5,8,9,5], - [0,0,1,7], - [6,9,2,4]], - [[5,2,4,2], - [4,7,7,9], - [1,7,0,6]]], - dtype='float32') - out1 = paddle.sort(x=x, axis=-1) - out2 = paddle.sort(x=x, axis=0) - out3 = paddle.sort(x=x, axis=1) - print(out1) - #[[[5. 5. 8. 9.] - # [0. 0. 1. 7.] - # [2. 4. 6. 9.]] - # [[2. 2. 4. 5.] - # [4. 7. 7. 9.] - # [0. 1. 6. 7.]]] - print(out2) - #[[[5. 2. 4. 2.] - # [0. 0. 1. 7.] - # [1. 7. 0. 4.]] - # [[5. 8. 9. 5.] - # [4. 7. 7. 9.] - # [6. 9. 2. 6.]]] - print(out3) - #[[[0. 0. 1. 4.] - # [5. 8. 2. 5.] - # [6. 9. 9. 7.]] - # [[1. 2. 0. 2.] - # [4. 7. 4. 6.] - # [5. 7. 7. 9.]]] + >>> import paddle + + >>> x = paddle.to_tensor([[[5,8,9,5], + ... [0,0,1,7], + ... [6,9,2,4]], + ... [[5,2,4,2], + ... [4,7,7,9], + ... [1,7,0,6]]], + ... dtype='float32') + >>> out1 = paddle.sort(x=x, axis=-1) + >>> out2 = paddle.sort(x=x, axis=0) + >>> out3 = paddle.sort(x=x, axis=1) + >>> print(out1) + [[[5. 5. 8. 9.] + [0. 0. 1. 7.] + [2. 4. 6. 9.]] + [[2. 2. 4. 5.] + [4. 7. 7. 9.] + [0. 1. 6. 7.]]] + >>> print(out2) + [[[5. 2. 4. 2.] + [0. 0. 1. 7.] + [1. 7. 0. 4.]] + [[5. 8. 9. 5.] + [4. 7. 7. 9.] + [6. 9. 2. 6.]]] + >>> print(out3) + [[[0. 0. 1. 4.] + [5. 8. 2. 5.] + [6. 9. 9. 7.]] + [[1. 2. 0. 2.] + [4. 7. 4. 6.] + [5. 7. 7. 9.]]] """ if in_dynamic_mode(): outs, _ = _C_ops.argsort(x, axis, descending) @@ -575,16 +576,16 @@ def mode(x, axis=-1, keepdim=False, name=None): .. code-block:: python - import paddle + >>> import paddle - tensor = paddle.to_tensor([[[1,2,2],[2,3,3]],[[0,5,5],[9,9,0]]], dtype=paddle.float32) - res = paddle.mode(tensor, 2) - print(res) - # (Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # [[2., 3.], - # [5., 9.]]), Tensor(shape=[2, 2], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [[2, 2], - # [2, 1]])) + >>> tensor = paddle.to_tensor([[[1,2,2],[2,3,3]],[[0,5,5],[9,9,0]]], dtype=paddle.float32) + >>> res = paddle.mode(tensor, 2) + >>> print(res) + Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, + [[2., 3.], + [5., 9.]]), Tensor(shape=[2, 2], dtype=int64, place=CUDAPlace(0), stop_gradient=True, + [[2, 2], + [2, 1]]) """ if in_dynamic_mode(): @@ -637,20 +638,20 @@ def where(condition, x=None, y=None, name=None): .. code-block:: python - import paddle + >>> import paddle - x = paddle.to_tensor([0.9383, 0.1983, 3.2, 1.2]) - y = paddle.to_tensor([1.0, 1.0, 1.0, 1.0]) + >>> x = paddle.to_tensor([0.9383, 0.1983, 3.2, 1.2]) + >>> y = paddle.to_tensor([1.0, 1.0, 1.0, 1.0]) - out = paddle.where(x>1, x, y) - print(out) - #out: [1.0, 1.0, 3.2, 1.2] + >>> out = paddle.where(x>1, x, y) + >>> print(out) + [1.0, 1.0, 3.2, 1.2] - out = paddle.where(x>1) - print(out) - #out: (Tensor(shape=[2, 1], dtype=int64, place=CPUPlace, stop_gradient=True, - # [[2], - # [3]]),) + >>> out = paddle.where(x>1) + >>> print(out) + Tensor(shape=[2, 1], dtype=int64, place=CPUPlace, stop_gradient=True, + [[2], + [3]]) """ if np.isscalar(x): x = paddle.full([1], x, np.array([x]).dtype.name) @@ -791,41 +792,41 @@ def index_sample(x, index): .. code-block:: python - import paddle - - x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], - [5.0, 6.0, 7.0, 8.0], - [9.0, 10.0, 11.0, 12.0]], dtype='float32') - index = paddle.to_tensor([[0, 1, 2], - [1, 2, 3], - [0, 0, 0]], dtype='int32') - target = paddle.to_tensor([[100, 200, 300, 400], - [500, 600, 700, 800], - [900, 1000, 1100, 1200]], dtype='int32') - out_z1 = paddle.index_sample(x, index) - print(out_z1) - #[[1. 2. 3.] - # [6. 7. 8.] - # [9. 9. 9.]] - - # Use the index of the maximum value by topk op - # get the value of the element of the corresponding index in other tensors - top_value, top_index = paddle.topk(x, k=2) - out_z2 = paddle.index_sample(target, top_index) - print(top_value) - #[[ 4. 3.] - # [ 8. 7.] - # [12. 11.]] - - print(top_index) - #[[3 2] - # [3 2] - # [3 2]] - - print(out_z2) - #[[ 400 300] - # [ 800 700] - # [1200 1100]] + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], + ... [5.0, 6.0, 7.0, 8.0], + ... [9.0, 10.0, 11.0, 12.0]], dtype='float32') + >>> index = paddle.to_tensor([[0, 1, 2], + ... [1, 2, 3], + ... [0, 0, 0]], dtype='int32') + >>> target = paddle.to_tensor([[100, 200, 300, 400], + ... [500, 600, 700, 800], + ... [900, 1000, 1100, 1200]], dtype='int32') + >>> out_z1 = paddle.index_sample(x, index) + >>> print(out_z1) + [[1. 2. 3.] + [6. 7. 8.] + [9. 9. 9.]] + + >>> # Use the index of the maximum value by topk op + >>> # get the value of the element of the corresponding index in other tensors + >>> top_value, top_index = paddle.topk(x, k=2) + >>> out_z2 = paddle.index_sample(target, top_index) + >>> print(top_value) + [[ 4. 3.] + [ 8. 7.] + [12. 11.]] + + >>> print(top_index) + [[3 2] + [3 2] + [3 2]] + + >>> print(out_z2) + [[ 400 300] + [ 800 700] + [1200 1100]] """ if in_dynamic_mode(): @@ -880,16 +881,17 @@ def masked_select(x, mask, name=None): .. code-block:: python - import paddle - - x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], - [5.0, 6.0, 7.0, 8.0], - [9.0, 10.0, 11.0, 12.0]]) - mask = paddle.to_tensor([[True, False, False, False], - [True, True, False, False], - [True, False, False, False]]) - out = paddle.masked_select(x, mask) - #[1.0 5.0 6.0 9.0] + >>> import paddle + + >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], + ... [5.0, 6.0, 7.0, 8.0], + ... [9.0, 10.0, 11.0, 12.0]]) + >>> mask = paddle.to_tensor([[True, False, False, False], + ... [True, True, False, False], + ... [True, False, False, False]]) + >>> out = paddle.masked_select(x, mask) + >>> print(out) + [1.0 5.0 6.0 9.0] """ if in_dynamic_mode(): @@ -940,25 +942,33 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None): .. code-block:: python - import paddle + >>> import paddle - data_1 = paddle.to_tensor([1, 4, 5, 7]) - value_1, indices_1 = paddle.topk(data_1, k=1) - print(value_1) # [7] - print(indices_1) # [3] + >>> data_1 = paddle.to_tensor([1, 4, 5, 7]) + >>> value_1, indices_1 = paddle.topk(data_1, k=1) + >>> print(value_1) + [7] + >>> print(indices_1) + [3] - data_2 = paddle.to_tensor([[1, 4, 5, 7], [2, 6, 2, 5]]) - value_2, indices_2 = paddle.topk(data_2, k=1) - print(value_2) # [[7], [6]] - print(indices_2) # [[3], [1]] + >>> data_2 = paddle.to_tensor([[1, 4, 5, 7], [2, 6, 2, 5]]) + >>> value_2, indices_2 = paddle.topk(data_2, k=1) + >>> print(value_2) + [[7], [6]] + >>> print(indices_2) + [[3], [1]] - value_3, indices_3 = paddle.topk(data_2, k=1, axis=-1) - print(value_3) # [[7], [6]] - print(indices_3) # [[3], [1]] + >>> value_3, indices_3 = paddle.topk(data_2, k=1, axis=-1) + >>> print(value_3) + [[7], [6]] + >>> print(indices_3) + [[3], [1]] - value_4, indices_4 = paddle.topk(data_2, k=1, axis=0) - print(value_4) # [[2, 6, 5, 7]] - print(indices_4) # [[1, 1, 0, 0]] + >>> value_4, indices_4 = paddle.topk(data_2, k=1, axis=0) + >>> print(value_4) + [[2, 6, 5, 7]] + >>> print(indices_4) + [[1, 1, 0, 0]] """ @@ -1013,30 +1023,30 @@ def bucketize(x, sorted_sequence, out_int32=False, right=False, name=None): .. code-block:: python - import paddle - - sorted_sequence = paddle.to_tensor([2, 4, 8, 16], dtype='int32') - x = paddle.to_tensor([[0, 8, 4, 16], [-1, 2, 8, 4]], dtype='int32') - out1 = paddle.bucketize(x, sorted_sequence) - print(out1) - # Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, - # [[0, 2, 1, 3], - # [0, 0, 2, 1]]) - out2 = paddle.bucketize(x, sorted_sequence, right=True) - print(out2) - # Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, - # [[0, 3, 2, 4], - # [0, 1, 3, 2]]) - out3 = x.bucketize(sorted_sequence) - print(out3) - # Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, - # [[0, 2, 1, 3], - # [0, 0, 2, 1]]) - out4 = x.bucketize(sorted_sequence, right=True) - print(out4) - # Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, - # [[0, 3, 2, 4], - # [0, 1, 3, 2]]) + >>> import paddle + + >>> sorted_sequence = paddle.to_tensor([2, 4, 8, 16], dtype='int32') + >>> x = paddle.to_tensor([[0, 8, 4, 16], [-1, 2, 8, 4]], dtype='int32') + >>> out1 = paddle.bucketize(x, sorted_sequence) + >>> print(out1) + Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, + [[0, 2, 1, 3], + [0, 0, 2, 1]]) + >>> out2 = paddle.bucketize(x, sorted_sequence, right=True) + >>> print(out2) + Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, + [[0, 3, 2, 4], + [0, 1, 3, 2]]) + >>> out3 = x.bucketize(sorted_sequence) + >>> print(out3) + Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, + [[0, 2, 1, 3], + [0, 0, 2, 1]]) + >>> out4 = x.bucketize(sorted_sequence, right=True) + >>> print(out4) + Tensor(shape=[2, 4], dtype=int64, place=CPUPlace, stop_gradient=True, + [[0, 3, 2, 4], + [0, 1, 3, 2]]) """ check_variable_and_dtype( @@ -1073,27 +1083,27 @@ def searchsorted( .. code-block:: python - import paddle - - sorted_sequence = paddle.to_tensor([[1, 3, 5, 7, 9, 11], - [2, 4, 6, 8, 10, 12]], dtype='int32') - values = paddle.to_tensor([[3, 6, 9, 10], [3, 6, 9, 10]], dtype='int32') - out1 = paddle.searchsorted(sorted_sequence, values) - print(out1) - # Tensor(shape=[2, 4], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [[1, 3, 4, 5], - # [1, 2, 4, 4]]) - out2 = paddle.searchsorted(sorted_sequence, values, right=True) - print(out2) - # Tensor(shape=[2, 4], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [[2, 3, 5, 5], - # [1, 3, 4, 5]]) - sorted_sequence_1d = paddle.to_tensor([1, 3, 5, 7, 9, 11, 13]) - out3 = paddle.searchsorted(sorted_sequence_1d, values) - print(out3) - # Tensor(shape=[2, 4], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [[1, 3, 4, 5], - # [1, 3, 4, 5]]) + >>> import paddle + + >>> sorted_sequence = paddle.to_tensor([[1, 3, 5, 7, 9, 11], + ... [2, 4, 6, 8, 10, 12]], dtype='int32') + >>> values = paddle.to_tensor([[3, 6, 9, 10], [3, 6, 9, 10]], dtype='int32') + >>> out1 = paddle.searchsorted(sorted_sequence, values) + >>> print(out1) + Tensor(shape=[2, 4], dtype=int64, place=CUDAPlace(0), stop_gradient=True, + [[1, 3, 4, 5], + [1, 2, 4, 4]]) + >>> out2 = paddle.searchsorted(sorted_sequence, values, right=True) + >>> print(out2) + Tensor(shape=[2, 4], dtype=int64, place=CUDAPlace(0), stop_gradient=True, + [[2, 3, 5, 5], + [1, 3, 4, 5]]) + >>> sorted_sequence_1d = paddle.to_tensor([1, 3, 5, 7, 9, 11, 13]) + >>> out3 = paddle.searchsorted(sorted_sequence_1d, values) + >>> print(out3) + Tensor(shape=[2, 4], dtype=int64, place=CUDAPlace(0), stop_gradient=True, + [[1, 3, 4, 5], + [1, 3, 4, 5]]) """ if in_dynamic_mode(): @@ -1145,23 +1155,24 @@ def kthvalue(x, k, axis=None, keepdim=False, name=None): .. code-block:: python - import paddle - - x = paddle.randn((2,3,2)) - # Tensor(shape=[2, 3, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # [[[ 0.22954939, -0.01296274], - # [ 1.17135799, -0.34493217], - # [-0.19550551, -0.17573971]], - # - # [[ 0.15104349, -0.93965352], - # [ 0.14745511, 0.98209465], - # [ 0.10732264, -0.55859774]]]) - y = paddle.kthvalue(x, 2, 1) - # (Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - # [[ 0.22954939, -0.17573971], - # [ 0.14745511, -0.55859774]]), Tensor(shape=[2, 2], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - # [[0, 2], - # [1, 2]])) + >>> import paddle + + >>> x = paddle.randn((2,3,2)) + >>> print(x) + Tensor(shape=[2, 3, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, + [[[ 0.22954939, -0.01296274], + [ 1.17135799, -0.34493217], + [-0.19550551, -0.17573971]], + [[ 0.15104349, -0.93965352], + [ 0.14745511, 0.98209465], + [ 0.10732264, -0.55859774]]]) + >>> y = paddle.kthvalue(x, 2, 1) + >>> print(y) + Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, + [[ 0.22954939, -0.17573971], + [ 0.14745511, -0.55859774]]), Tensor(shape=[2, 2], dtype=int64, place=CUDAPlace(0), stop_gradient=True, + [[0, 2], + [1, 2]]) """ if in_dynamic_mode(): if axis is not None: From 436f46a89e03a4501fd5033c95b7a37e236f163b Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Tue, 19 Sep 2023 13:48:06 +0800 Subject: [PATCH 2/6] Update search.py --- python/paddle/tensor/search.py | 255 +++++++++++++++++++-------------- 1 file changed, 147 insertions(+), 108 deletions(-) diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index 945a67859ce9ac..324972555cb9f3 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -72,28 +72,31 @@ def argsort(x, axis=-1, descending=False, name=None): >>> out3 = paddle.argsort(x, axis=1) >>> print(out1) - [[[0 3 1 2] - [0 1 2 3] - [2 3 0 1]] - [[1 3 2 0] - [0 1 2 3] - [2 0 3 1]]] + Tensor(shape=[2, 3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[[0, 3, 1, 2], + [0, 1, 2, 3], + [2, 3, 0, 1]], + [[1, 3, 2, 0], + [0, 1, 2, 3], + [2, 0, 3, 1]]]) >>> print(out2) - [[[0 1 1 1] - [0 0 0 0] - [1 1 1 0]] - [[1 0 0 0] - [1 1 1 1] - [0 0 0 1]]] + Tensor(shape=[2, 3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[[0, 1, 1, 1], + [0, 0, 0, 0], + [1, 1, 1, 0]], + [[1, 0, 0, 0], + [1, 1, 1, 1], + [0, 0, 0, 1]]]) >>> print(out3) - [[[1 1 1 2] - [0 0 2 0] - [2 2 0 1]] - [[2 0 2 0] - [1 1 0 2] - [0 2 1 1]]] + Tensor(shape=[2, 3, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[[1, 1, 1, 2], + [0, 0, 2, 0], + [2, 2, 0, 1]], + [[2, 0, 2, 0], + [1, 1, 0, 2], + [0, 2, 1, 1]]]) """ if in_dynamic_mode(): _, ids = _C_ops.argsort(x, axis, descending) @@ -159,16 +162,21 @@ def argmax(x, axis=None, keepdim=False, dtype="int64", name=None): ... [0,0,1,7], ... [6,9,2,4]]) >>> out1 = paddle.argmax(x) - >>> print(out1) # 2 + >>> print(out1) + Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, + 2) >>> out2 = paddle.argmax(x, axis=0) >>> print(out2) - [2, 2, 0, 1] + Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, + [2, 2, 0, 1]) >>> out3 = paddle.argmax(x, axis=-1) >>> print(out3) - [2, 3, 1] + Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, + [2, 3, 1]) >>> out4 = paddle.argmax(x, axis=0, keepdim=True) >>> print(out4) - [[2, 2, 0, 1]] + Tensor(shape=[1, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[2, 2, 0, 1]]) """ if axis is not None and not isinstance(axis, (int, Variable)): raise TypeError( @@ -249,16 +257,21 @@ def argmin(x, axis=None, keepdim=False, dtype="int64", name=None): ... [0,0,1,7], ... [6,9,2,4]]) >>> out1 = paddle.argmin(x) - >>> print(out1) # 4 + >>> print(out1) + Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, + 4) >>> out2 = paddle.argmin(x, axis=0) >>> print(out2) - [1, 1, 1, 2] + Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, + [1, 1, 1, 2]) >>> out3 = paddle.argmin(x, axis=-1) >>> print(out3) - [0, 0, 2] + Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, + [0, 0, 2]) >>> out4 = paddle.argmin(x, axis=0, keepdim=True) >>> print(out4) - [[1, 1, 1, 2]] + Tensor(shape=[1, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1, 1, 1, 2]]) """ if axis is not None and not isinstance(axis, (int, Variable)): raise TypeError( @@ -337,14 +350,17 @@ def index_select(x, index, axis=0, name=None): ... [9.0, 10.0, 11.0, 12.0]]) >>> index = paddle.to_tensor([0, 1, 1], dtype='int32') >>> out_z1 = paddle.index_select(x=x, index=index) - print(out_z1) - [[1. 2. 3. 4.] - [5. 6. 7. 8.] - [5. 6. 7. 8.]] + >>> print(out_z1) + Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1., 2., 3., 4.], + [5., 6., 7., 8.], + [5., 6., 7., 8.]]) >>> out_z2 = paddle.index_select(x=x, index=index, axis=1) - [[ 1. 2. 2.] - [ 5. 6. 6.] - [ 9. 10. 10.]] + >>> print(out_z2) + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1. , 2. , 2. ], + [5. , 6. , 6. ], + [9. , 10., 10.]]) """ if in_dynamic_mode(): @@ -413,28 +429,32 @@ def nonzero(x, as_tuple=False): >>> x2 = paddle.to_tensor([0.0, 1.0, 0.0, 3.0]) >>> out_z1 = paddle.nonzero(x1) >>> print(out_z1) - [[0 0] - [1 1] - [2 2]] + Tensor(shape=[3, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0, 0], + [1, 1], + [2, 2]]) >>> out_z1_tuple = paddle.nonzero(x1, as_tuple=True) >>> for out in out_z1_tuple: ... print(out) - [[0] - [1] - [2]] - [[0] - [1] - [2]] + Tensor(shape=[3, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0], + [1], + [2]]) + Tensor(shape=[3, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0], + [1], + [2]]) >>> out_z2 = paddle.nonzero(x2) >>> print(out_z2) - [[1] - [3]] + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1], + [3]]) >>> out_z2_tuple = paddle.nonzero(x2, as_tuple=True) >>> for out in out_z2_tuple: ... print(out) - [[1] - [3]] - + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1], + [3]]) """ list_out = [] shape = x.shape @@ -516,26 +536,29 @@ def sort(x, axis=-1, descending=False, name=None): >>> out2 = paddle.sort(x=x, axis=0) >>> out3 = paddle.sort(x=x, axis=1) >>> print(out1) - [[[5. 5. 8. 9.] - [0. 0. 1. 7.] - [2. 4. 6. 9.]] - [[2. 2. 4. 5.] - [4. 7. 7. 9.] - [0. 1. 6. 7.]]] + Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[5., 5., 8., 9.], + [0., 0., 1., 7.], + [2., 4., 6., 9.]], + [[2., 2., 4., 5.], + [4., 7., 7., 9.], + [0., 1., 6., 7.]]]) >>> print(out2) - [[[5. 2. 4. 2.] - [0. 0. 1. 7.] - [1. 7. 0. 4.]] - [[5. 8. 9. 5.] - [4. 7. 7. 9.] - [6. 9. 2. 6.]]] + Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[5., 2., 4., 2.], + [0., 0., 1., 7.], + [1., 7., 0., 4.]], + [[5., 8., 9., 5.], + [4., 7., 7., 9.], + [6., 9., 2., 6.]]]) >>> print(out3) - [[[0. 0. 1. 4.] - [5. 8. 2. 5.] - [6. 9. 9. 7.]] - [[1. 2. 0. 2.] - [4. 7. 4. 6.] - [5. 7. 7. 9.]]] + Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[0., 0., 1., 4.], + [5., 8., 2., 5.], + [6., 9., 9., 7.]], + [[1., 2., 0., 2.], + [4., 7., 4., 6.], + [5., 7., 7., 9.]]]) """ if in_dynamic_mode(): outs, _ = _C_ops.argsort(x, axis, descending) @@ -581,11 +604,11 @@ def mode(x, axis=-1, keepdim=False, name=None): >>> tensor = paddle.to_tensor([[[1,2,2],[2,3,3]],[[0,5,5],[9,9,0]]], dtype=paddle.float32) >>> res = paddle.mode(tensor, 2) >>> print(res) - Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, + (Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, [[2., 3.], - [5., 9.]]), Tensor(shape=[2, 2], dtype=int64, place=CUDAPlace(0), stop_gradient=True, + [5., 9.]]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, [[2, 2], - [2, 1]]) + [2, 1]])) """ if in_dynamic_mode(): @@ -645,13 +668,14 @@ def where(condition, x=None, y=None, name=None): >>> out = paddle.where(x>1, x, y) >>> print(out) - [1.0, 1.0, 3.2, 1.2] + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [1. , 1. , 3.20000005, 1.20000005]) >>> out = paddle.where(x>1) >>> print(out) - Tensor(shape=[2, 1], dtype=int64, place=CPUPlace, stop_gradient=True, - [[2], - [3]]) + (Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[2], + [3]]),) """ if np.isscalar(x): x = paddle.full([1], x, np.array([x]).dtype.name) @@ -805,28 +829,32 @@ def index_sample(x, index): ... [900, 1000, 1100, 1200]], dtype='int32') >>> out_z1 = paddle.index_sample(x, index) >>> print(out_z1) - [[1. 2. 3.] - [6. 7. 8.] - [9. 9. 9.]] + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[1., 2., 3.], + [6., 7., 8.], + [9., 9., 9.]]) >>> # Use the index of the maximum value by topk op >>> # get the value of the element of the corresponding index in other tensors >>> top_value, top_index = paddle.topk(x, k=2) >>> out_z2 = paddle.index_sample(target, top_index) >>> print(top_value) - [[ 4. 3.] - [ 8. 7.] - [12. 11.]] + Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[4. , 3. ], + [8. , 7. ], + [12., 11.]]) >>> print(top_index) - [[3 2] - [3 2] - [3 2]] + Tensor(shape=[3, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[3, 2], + [3, 2], + [3, 2]]) >>> print(out_z2) - [[ 400 300] - [ 800 700] - [1200 1100]] + Tensor(shape=[3, 2], dtype=int32, place=Place(cpu), stop_gradient=True, + [[400 , 300 ], + [800 , 700 ], + [1200, 1100]]) """ if in_dynamic_mode(): @@ -891,7 +919,8 @@ def masked_select(x, mask, name=None): ... [True, False, False, False]]) >>> out = paddle.masked_select(x, mask) >>> print(out) - [1.0 5.0 6.0 9.0] + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [1., 5., 6., 9.]) """ if in_dynamic_mode(): @@ -947,30 +976,40 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None): >>> data_1 = paddle.to_tensor([1, 4, 5, 7]) >>> value_1, indices_1 = paddle.topk(data_1, k=1) >>> print(value_1) - [7] + Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True, + [7]) >>> print(indices_1) - [3] + Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True, + [3]) >>> data_2 = paddle.to_tensor([[1, 4, 5, 7], [2, 6, 2, 5]]) >>> value_2, indices_2 = paddle.topk(data_2, k=1) >>> print(value_2) - [[7], [6]] + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[7], + [6]]) >>> print(indices_2) - [[3], [1]] + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[3], + [1]]) >>> value_3, indices_3 = paddle.topk(data_2, k=1, axis=-1) >>> print(value_3) - [[7], [6]] + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[7], + [6]]) >>> print(indices_3) - [[3], [1]] + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[3], + [1]]) >>> value_4, indices_4 = paddle.topk(data_2, k=1, axis=0) >>> print(value_4) - [[2, 6, 5, 7]] + Tensor(shape=[1, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[2, 6, 5, 7]]) >>> print(indices_4) - [[1, 1, 0, 0]] - - + Tensor(shape=[1, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1, 1, 0, 0]]) """ if in_dynamic_mode(): @@ -1156,23 +1195,23 @@ def kthvalue(x, k, axis=None, keepdim=False, name=None): .. code-block:: python >>> import paddle - + >>> paddle.seed(1) >>> x = paddle.randn((2,3,2)) >>> print(x) - Tensor(shape=[2, 3, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - [[[ 0.22954939, -0.01296274], - [ 1.17135799, -0.34493217], - [-0.19550551, -0.17573971]], - [[ 0.15104349, -0.93965352], - [ 0.14745511, 0.98209465], - [ 0.10732264, -0.55859774]]]) + Tensor(shape=[2, 3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[-0.30557564, 0.11855337], + [ 0.41220093, -0.09968963], + [ 1.50014710, 1.24004936]], + [[-0.92485696, 0.08612321], + [ 1.15149164, -0.09276631], + [ 1.22873247, -1.46587241]]]) >>> y = paddle.kthvalue(x, 2, 1) >>> print(y) - Tensor(shape=[2, 2], dtype=float32, place=CUDAPlace(0), stop_gradient=True, - [[ 0.22954939, -0.17573971], - [ 0.14745511, -0.55859774]]), Tensor(shape=[2, 2], dtype=int64, place=CUDAPlace(0), stop_gradient=True, - [[0, 2], - [1, 2]]) + (Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[ 0.41220093, 0.11855337], + [ 1.15149164, -0.09276631]]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1, 0], + [1, 1]])) """ if in_dynamic_mode(): if axis is not None: From abfa027e22312e79f1954febe7cd88d85a43c123 Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Thu, 21 Sep 2023 23:04:23 +0800 Subject: [PATCH 3/6] Update manipulation.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 先浅改了一下下,请大佬帮我康康 --- python/paddle/tensor/manipulation.py | 141 ++++++++++++++++----------- 1 file changed, 86 insertions(+), 55 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 4f78c1df20a59e..520d517afaf3be 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -641,8 +641,9 @@ def shard_index(input, index_num, nshards, shard_id, ignore_value=-1): ... nshards=2, ... shard_id=0) >>> print(shard_label) + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, [[-1], - [ 1]] + [ 1]]) """ if in_dynamic_mode(): return _C_ops.shard_index( @@ -1368,21 +1369,23 @@ def flip(x, axis, name=None): >>> img = paddle.arange(image_shape[0] * image_shape[1] * image_shape[2]).reshape(image_shape) >>> tmp = paddle.flip(img, [0,1]) >>> print(tmp) - [[[10,11], - [8, 9]], - [[6, 7], - [4, 5]], - [[2, 3], - [0, 1]]] + Tensor(shape=[3, 2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[[10, 11], + [8 , 9 ]], + [[6 , 7 ], + [4 , 5 ]], + [[2 , 3 ], + [0 , 1 ]]]) >>> out = paddle.flip(tmp,-1) >>> print(out) - [[[11,10], - [9, 8]], - [[7, 6], - [5, 4]], - [[3, 2], - [1, 0]]] + Tensor(shape=[3, 2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[[11, 10], + [9 , 8 ]], + [[7 , 6 ], + [5 , 4 ]], + [[3 , 2 ], + [1 , 0 ]]]) """ if isinstance(axis, int): axis = [axis] @@ -1439,33 +1442,38 @@ def rot90(x, k=1, axes=[0, 1], name=None): >>> data = paddle.arange(4) >>> data = paddle.reshape(data, (2, 2)) >>> print(data) + Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, [[0, 1], - [2, 3]] + [2, 3]]) >>> y = paddle.rot90(data, 1, [0, 1]) >>> print(y) + Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, [[1, 3], - [0, 2]] + [0, 2]]) >>> y= paddle.rot90(data, -1, [0, 1]) >>> print(y) + Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, [[2, 0], - [3, 1]] + [3, 1]]) >>> data2 = paddle.arange(8) >>> data2 = paddle.reshape(data2, (2,2,2)) >>> print(data2) + Tensor(shape=[2, 2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, [[[0, 1], [2, 3]], [[4, 5], - [6, 7]]] + [6, 7]]]) >>> y = paddle.rot90(data2, 1, [1, 2]) >>> print(y) + Tensor(shape=[2, 2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, [[[1, 3], [0, 2]], [[5, 7], - [4, 6]]] + [4, 6]]]) """ helper = LayerHelper("rot90", **locals()) @@ -1589,7 +1597,8 @@ def flatten(x, start_axis=0, stop_axis=-1, name=None): >>> # out shares data with img in dygraph mode >>> img[0, 0, 0, 0] = -1 >>> print(out[0, 0, 0]) - [-1] + Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True, + [-1]) """ if not (isinstance(x, Variable)): raise ValueError("The input x should be a Tensor") @@ -1725,19 +1734,22 @@ def roll(x, shifts, axis=None, name=None): ... [7.0, 8.0, 9.0]]) >>> out_z1 = paddle.roll(x, shifts=1) >>> print(out_z1) - [[9. 1. 2.] - [3. 4. 5.] - [6. 7. 8.]] + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[9., 1., 2.], + [3., 4., 5.], + [6., 7., 8.]]) >>> out_z2 = paddle.roll(x, shifts=1, axis=0) >>> print(out_z2) - [[7. 8. 9.] - [1. 2. 3.] - [4. 5. 6.]] + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[7., 8., 9.], + [1., 2., 3.], + [4., 5., 6.]]) >>> out_z3 = paddle.roll(x, shifts=1, axis=1) >>> print(out_z3) - [[3. 1. 2.] - [6. 4. 5.] - [9. 7. 8.]] + Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, + [[3., 1., 2.], + [6., 4., 5.], + [9., 7., 8.]]) """ origin_shape = x.shape if type(shifts) == int: @@ -1871,11 +1883,13 @@ def stack(x, axis=0, name=None): >>> x3 = paddle.to_tensor([[5.0, 6.0]]) >>> out = paddle.stack([x1, x2, x3], axis=0) - >>> print(out.shape) # [3, 1, 2] + >>> print(out.shape) + [3, 1, 2] >>> print(out) + Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=True, [[[1., 2.]], - [[3., 4.]], - [[5., 6.]]] + [[3., 4.]], + [[5., 6.]]]) out = paddle.stack([x1, x2, x3], axis=-2) print(out.shape) # [1, 3, 2] @@ -2935,11 +2949,6 @@ def scatter(x, index, updates, overwrite=True, name=None): .. code-block:: python :name: code-example1 - >>> import paddle - >>> #input: - >>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32') - >>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64') - >>> # shape of updates should be the same as x >>> # shape of updates with dim > 1 should be the same as input >>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32') >>> overwrite = False @@ -2984,25 +2993,29 @@ def scatter(x, index, updates, overwrite=True, name=None): >>> output1 = paddle.scatter(x, index, updates, overwrite=False) >>> print(output1) + Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, [[3., 3.], [6., 6.], - [1., 1.]] + [1., 1.]]) >>> output2 = paddle.scatter(x, index, updates, overwrite=True) >>> print(output2) >>> # CPU device: + Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, [[3., 3.], [4., 4.], - [1., 1.]] + [1., 1.]]) >>> # GPU device maybe have two results because of the repeated numbers in index >>> # result 1: + Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[3., 3.], [4., 4.], - [1., 1.]] + [1., 1.]]) >>> # result 2: + Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[3., 3.], [2., 2.], - [1., 1.]] + [1., 1.]]) """ if in_dynamic_mode(): return _C_ops.scatter(x, index, updates, overwrite) @@ -3419,7 +3432,9 @@ def broadcast_to(x, shape, name=None): >>> data = paddle.to_tensor([1, 2, 3], dtype='int32') >>> out = paddle.broadcast_to(data, shape=[2, 3]) >>> print(out) - [[1, 2, 3], [1, 2, 3]] + Tensor(shape=[2, 3], dtype=int32, place=Place(gpu:0), stop_gradient=True, + [[1, 2, 3], + [1, 2, 3]]) """ if in_dynamic_mode(): return _C_ops.expand(x, shape) @@ -3521,7 +3536,9 @@ def expand(x, shape, name=None): >>> data = paddle.to_tensor([1, 2, 3], dtype='int32') >>> out = paddle.expand(data, shape=[2, 3]) >>> print(out) - [[1, 2, 3], [1, 2, 3]] + Tensor(shape=[2, 3], dtype=int32, place=Place(gpu:0), stop_gradient=True, + [[1, 2, 3], + [1, 2, 3]]) """ if in_dynamic_or_pir_mode(): return _C_ops.expand(x, shape) @@ -3682,11 +3699,12 @@ def reshape(x, shape, name=None): >>> shape_tensor = paddle.to_tensor([8, 6], dtype=paddle.int32) >>> out = paddle.reshape(x, shape=shape_tensor) >>> print(out.shape) - [8, 6]. + [8, 6] >>> # out shares data with x in dygraph mode >>> x[0, 0, 0] = 10. >>> print(out[0, 0]) - [10.] + Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [10.]) """ if in_dynamic_or_pir_mode(): @@ -3894,7 +3912,8 @@ def gather_nd(x, index, name=None): >>> output = paddle.gather_nd(x, index) >>> print(output) - [[3, 4]] + Tensor(shape=[1, 2], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[3, 4]]) """ if in_dynamic_mode(): @@ -4197,6 +4216,7 @@ def tensordot(x, y, axes=2, name=None): >>> y = paddle.arange(4, dtype=data_type).reshape([2, 2]) >>> z = paddle.tensordot(x, y, axes=0) >>> print(z) + Tensor(shape=[2, 2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=True, [[[[0., 0.], [0., 0.]], [[0., 1.], @@ -4204,7 +4224,7 @@ def tensordot(x, y, axes=2, name=None): [[[0., 2.], [4., 6.]], [[0., 3.], - [6., 9.]]]] + [6., 9.]]]]) >>> # For two 1-d tensor x and y, the case axes=1 is equivalent to inner product. @@ -4213,21 +4233,25 @@ def tensordot(x, y, axes=2, name=None): >>> z1 = paddle.tensordot(x, y, axes=1) >>> z2 = paddle.dot(x, y) >>> print(z1) - [285.] + Tensor(shape=[1], dtype=float64, place=Place(cpu), stop_gradient=True, + [285.]) >>> print(z2) - [285.] + Tensor(shape=[1], dtype=float64, place=Place(cpu), stop_gradient=True, + [285.]) >>> # For two 2-d tensor x and y, the case axes=1 is equivalent to matrix multiplication. >>> x = paddle.arange(6, dtype=data_type).reshape([2, 3]) >>> y = paddle.arange(12, dtype=data_type).reshape([3, 4]) >>> z1 = paddle.tensordot(x, y, axes=1) >>> print(z1) + Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=True, [[20., 23., 26., 29.], - [56., 68., 80., 92.]] + [56., 68., 80., 92.]]) >>> z2 = paddle.matmul(x, y) >>> print(z2) + Tensor(shape=[2, 4], dtype=float64, place=Place(cpu), stop_gradient=True, [[20., 23., 26., 29.], - [56., 68., 80., 92.]] + [56., 68., 80., 92.]]) >>> # When axes is a 1-d int list, x and y will be contracted along the same given axes. @@ -4236,8 +4260,9 @@ def tensordot(x, y, axes=2, name=None): >>> y = paddle.arange(36, dtype=data_type).reshape([3, 3, 4]) >>> z = paddle.tensordot(x, y, axes=[1, 2]) >>> print(z) + Tensor(shape=[2, 3], dtype=float64, place=Place(cpu), stop_gradient=True, [[506. , 1298., 2090.], - [1298., 3818., 6338.]] + [1298., 3818., 6338.]]) >>> # When axes is a list containing two 1-d int list, the first will be applied to x and the second to y. @@ -4245,11 +4270,12 @@ def tensordot(x, y, axes=2, name=None): >>> y = paddle.arange(24, dtype=data_type).reshape([4, 3, 2]) >>> z = paddle.tensordot(x, y, axes=([1, 0], [0, 1])) >>> print(z) + Tensor(shape=[5, 2], dtype=float64, place=Place(cpu), stop_gradient=True, [[4400., 4730.], [4532., 4874.], [4664., 5018.], [4796., 5162.], - [4928., 5306.]] + [4928., 5306.]]) >>> # Thanks to the support of axes expansion, axes=[[0, 1, 3, 4], [1, 0, 3, 4]] can be abbreviated as axes= [[0, 1, 3, 4], [1, 0]]. @@ -4257,10 +4283,11 @@ def tensordot(x, y, axes=2, name=None): >>> y = paddle.arange(720, dtype=data_type).reshape([3, 2, 4, 5, 6]) >>> z = paddle.tensordot(x, y, axes=[[0, 1, 3, 4], [1, 0]]) >>> print(z) + Tensor(shape=[4, 4], dtype=float64, place=Place(cpu), stop_gradient=True, [[23217330., 24915630., 26613930., 28312230.], [24915630., 26775930., 28636230., 30496530.], [26613930., 28636230., 30658530., 32680830.], - [28312230., 30496530., 32680830., 34865130.]] + [28312230., 30496530., 32680830., 34865130.]]) """ op_type = 'tensordot' input_dtype = ['float16', 'float32', 'float64'] @@ -4690,7 +4717,8 @@ def take_along_axis(arr, indices, axis): >>> axis = 0 >>> result = paddle.take_along_axis(x, index, axis) >>> print(result) - [[1, 2, 3]] + Tensor(shape=[1, 3], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1, 2, 3]]) """ if len(arr.shape) != len(indices.shape): raise ValueError( @@ -4768,8 +4796,9 @@ def put_along_axis(arr, indices, values, axis, reduce='assign'): >>> axis = 0 >>> result = paddle.put_along_axis(x, index, value, axis) >>> print(result) + Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, [[99, 99, 99], - [60, 40, 50]] + [60, 40, 50]]) """ if len(arr.shape) != len(indices.shape): @@ -5243,6 +5272,8 @@ def view_as(x, other, name=None): >>> import paddle >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) + + >>> paddle.seed(1) >>> x = paddle.rand([2, 4, 6], dtype="float32") >>> y = paddle.rand([8, 6], dtype="float32") From fdd9d0a652c4da14fe07ed6be0e725be0c8af438 Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Tue, 26 Sep 2023 21:53:14 +0800 Subject: [PATCH 4/6] Update manipulation.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 应该还有一个文件要review@megemini --- python/paddle/tensor/manipulation.py | 115 +++++++++++++++------------ 1 file changed, 63 insertions(+), 52 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 520d517afaf3be..b4be6ea0154219 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -1592,13 +1592,14 @@ def flatten(x, start_axis=0, stop_axis=-1, name=None): >>> img = paddle.reshape(x, image_shape) >>> out = paddle.flatten(img, start_axis=1, stop_axis=2) - >>> # out shape is [2, 12, 4] + >>> print(out.shape) + [2, 12, 4] >>> # out shares data with img in dygraph mode >>> img[0, 0, 0, 0] = -1 >>> print(out[0, 0, 0]) - Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True, - [-1]) + Tensor(shape=[], dtype=int64, place=Place(gpu:0), stop_gradient=True, + -1) """ if not (isinstance(x, Variable)): raise ValueError("The input x should be a Tensor") @@ -2277,7 +2278,8 @@ def squeeze(x, axis=None, name=None): >>> # output shares data with x in dygraph mode >>> x[0, 0, 0] = 10. >>> print(output[0, 0]) - [10.] + Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, + [10.]) """ if axis is None: @@ -2678,11 +2680,14 @@ def unsqueeze(x, axis, name=None): >>> # out1, out2, out3 share data with x in dygraph mode >>> x[0, 0] = 10. >>> print(out1[0, 0, 0]) - [10.] + Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, + [10.]) >>> print(out2[0, 0, 0, 0]) - [10.] + Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, + [10.]) >>> print(out3[0, 0, 0, 0, 0]) - [10.] + Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, + [10.]) """ input = x @@ -2946,9 +2951,13 @@ def scatter(x, index, updates, overwrite=True, name=None): **Scatter Layer** Output is obtained by updating the input on selected indices based on updates. - .. code-block:: python + .. code-block:: text :name: code-example1 - + >>> import paddle + >>> #input: + >>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32') + >>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64') + >>> # shape of updates should be the same as x >>> # shape of updates with dim > 1 should be the same as input >>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32') >>> overwrite = False @@ -3686,16 +3695,17 @@ def reshape(x, shape, name=None): [0.60508877, 0.82193440]]]]) >>> out = paddle.reshape(x, shape=[positive_four, 12]) >>> print(out) - >>> # the shape of out_2 is [4, 12]. Tensor(shape=[4, 12], dtype=float32, place=Place(cpu), stop_gradient=True, - [[0.16556728, 0.98233348, 0.64653695, 0.68488085, 0.26045629, 0.32933319, - 0.29747701, 0.85176629, 0.84460896, 0.86866283, 0.46335083, 0.33930254], - [0.42339125, 0.14102051, 0.69832194, 0.63159829, 0.91087127, 0.31725556, - 0.09400324, 0.25861803, 0.26968575, 0.38659596, 0.25225133, 0.26315665], - [0.83726203, 0.33221707, 0.98031831, 0.38393897, 0.00512545, 0.04543629, - 0.90596122, 0.70148915, 0.26110184, 0.55263036, 0.68636090, 0.67877120], - [0.60491085, 0.96849394, 0.08488113, 0.48084566, 0.61894107, 0.92885363, - 0.37273413, 0.08096626, 0.74429852, 0.85212839, 0.60244918, 0.76718718]]) + [[0.01787627, 0.76492310, 0.67605734, 0.04620579, 0.38763246, 0.96462214, + 0.62356627, 0.64948404, 0.53736508, 0.09874519, 0.47123933, 0.85700107], + [0.34647217, 0.62869102, 0.54760450, 0.18061899, 0.55075216, 0.71997911, + 0.78576684, 0.76743823, 0.35644373, 0.63325852, 0.26549375, 0.68052763], + [0.40971112, 0.95848298, 0.41229674, 0.05506011, 0.18543524, 0.63480365, + 0.81180859, 0.94548297, 0.19634065, 0.73838711, 0.42057949, 0.96017945], + [0.72278100, 0.93858665, 0.72018963, 0.61661047, 0.33307818, 0.86660689, + 0.55808324, 0.12933673, 0.42916751, 0.22745337, 0.60508877, 0.82193440]]) + >>> print(out.shape) + [4, 12] >>> shape_tensor = paddle.to_tensor([8, 6], dtype=paddle.int32) >>> out = paddle.reshape(x, shape=shape_tensor) >>> print(out.shape) @@ -3703,8 +3713,8 @@ def reshape(x, shape, name=None): >>> # out shares data with x in dygraph mode >>> x[0, 0, 0] = 10. >>> print(out[0, 0]) - Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, - [10.]) + Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, + 10.) """ if in_dynamic_or_pir_mode(): @@ -4233,11 +4243,11 @@ def tensordot(x, y, axes=2, name=None): >>> z1 = paddle.tensordot(x, y, axes=1) >>> z2 = paddle.dot(x, y) >>> print(z1) - Tensor(shape=[1], dtype=float64, place=Place(cpu), stop_gradient=True, - [285.]) + Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True, + 285.) >>> print(z2) - Tensor(shape=[1], dtype=float64, place=Place(cpu), stop_gradient=True, - [285.]) + Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True, + 285.) >>> # For two 2-d tensor x and y, the case axes=1 is equivalent to matrix multiplication. >>> x = paddle.arange(6, dtype=data_type).reshape([2, 3]) @@ -4891,7 +4901,7 @@ def index_add(x, index, axis, value, name=None): Examples: .. code-block:: python - >>> # required: gpu + >>> # doctest: +REQUIRES(env:GPU) >>> import paddle >>> input_tensor = paddle.to_tensor(paddle.ones((3, 3)), dtype="float32") @@ -4951,7 +4961,7 @@ def index_add_(x, index, axis, value, name=None): Examples: .. code-block:: python - >>> # required: gpu + >>> # doctest: +REQUIRES(env:GPU) >>> import paddle >>> input_tensor = paddle.to_tensor(paddle.ones((3, 3)), dtype="float32") @@ -5219,28 +5229,28 @@ def view(x, shape_or_dtype, name=None): >>> import paddle >>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True}) - + >>> paddle.seed(1) >>> x = paddle.rand([2, 4, 6], dtype="float32") >>> out = paddle.view(x, "uint8") >>> print(out) Tensor(shape=[2, 4, 24], dtype=uint8, place=Place(cpu), stop_gradient=True, - [[[71 , 109, 163, 60 , 60 , 56 , 7 , 63 , 11 , 192, 144, 61 , 203, 148, - 77 , 63 , 132, 130, 47 , 61 , 250, 35 , 91 , 63 ], - [105, 99 , 158, 62 , 111, 71 , 114, 62 , 6 , 18 , 191, 62 , 236, 133, - 12 , 63 , 77 , 90 , 39 , 62 , 59 , 179, 115, 62 ], - [16 , 25 , 166, 62 , 71 , 248, 177, 61 , 107, 253, 25 , 63 , 24 , 89 , - 226, 62 , 118, 134, 198, 61 , 86 , 130, 32 , 61 ], - [144, 79 , 55 , 63 , 53 , 46 , 139, 62 , 173, 73 , 91 , 63 , 107, 97 , - 57 , 63 , 247, 236, 72 , 63 , 44 , 100, 65 , 62 ]], - [[45 , 58 , 104, 63 , 201, 126, 97 , 63 , 11 , 2 , 162, 62 , 145, 2 , - 44 , 63 , 7 , 6 , 4 , 63 , 89 , 241, 89 , 61 ], - [25 , 65 , 164, 62 , 47 , 75 , 89 , 63 , 100, 232, 16 , 60 , 139, 91 , - 54 , 63 , 165, 227, 11 , 63 , 255, 208, 190, 62 ], - [36 , 167, 233, 62 , 233, 125, 5 , 63 , 100, 238, 112, 63 , 102, 204, - 145, 62 , 217, 161, 65 , 63 , 175, 206, 226, 62 ], - [255, 40 , 62 , 63 , 49 , 216, 148, 62 , 50 , 206, 175, 62 , 159, 194, - 41 , 61 , 189, 22 , 144, 62 , 57 , 199, 253, 62 ]]]) + [[[64 , 113, 146, 60 , 0 , 210, 67 , 63 , 24 , 18 , 45 , 63 , 71 , 66 , + 61 , 61 , 195, 119, 198, 62 , 122, 241, 118, 63 ], + [10 , 162, 31 , 63 , 150, 68 , 38 , 63 , 194, 144, 9 , 63 , 235, 58 , + 202, 61 , 72 , 70 , 241, 62 , 108, 100, 91 , 63 ], + [205, 100, 177, 62 , 229, 241, 32 , 63 , 207, 47 , 12 , 63 , 47 , 244, + 56 , 62 , 24 , 254, 12 , 63 , 141, 80 , 56 , 63 ], + [4 , 40 , 73 , 63 , 213, 118, 68 , 63 , 203, 127, 182, 62 , 59 , 29 , + 34 , 63 , 204, 238, 135, 62 , 15 , 55 , 46 , 63 ]], + [[168, 197, 209, 62 , 36 , 95 , 117, 63 , 143, 24 , 211, 62 , 183, 134, + 97 , 61 , 188, 226, 61 , 62 , 126, 130, 34 , 63 ], + [176, 210, 79 , 63 , 44 , 11 , 114, 63 , 134, 13 , 73 , 62 , 240, 6 , + 61 , 63 , 50 , 86 , 215, 62 , 82 , 206, 117, 63 ], + [45 , 8 , 57 , 63 , 55 , 71 , 112, 63 , 89 , 94 , 56 , 63 , 47 , 218, + 29 , 63 , 57 , 137, 170, 62 , 243, 217, 93 , 63 ], + [139, 222, 14 , 63 , 217, 112, 4 , 62 , 216, 187, 219, 62 , 137, 233, + 104, 62 , 25 , 231, 26 , 63 , 75 , 106, 82 , 63 ]]]) """ if isinstance(shape_or_dtype, (list, tuple)): return _C_ops.view_shape(x, shape_or_dtype) @@ -5280,14 +5290,14 @@ def view_as(x, other, name=None): >>> out = paddle.view_as(x, y) >>> print(out) Tensor(shape=[8, 6], dtype=float32, place=Place(cpu), stop_gradient=True, - [[0.23340422, 0.62036550, 0.12185233, 0.88982582, 0.61383390, 0.22415221], - [0.85698199, 0.87602723, 0.00502827, 0.92127788, 0.34579527, 0.85451263], - [0.68906647, 0.05126054, 0.25322226, 0.43883288, 0.49656981, 0.68857300], - [0.21424839, 0.99576813, 0.62306029, 0.48010525, 0.31222206, 0.48758999], - [0.35929242, 0.12802263, 0.80540675, 0.76783085, 0.84970695, 0.00389719], - [0.69256896, 0.73718327, 0.97155327, 0.50271672, 0.60357946, 0.59580350], - [0.68182445, 0.28539398, 0.13948134, 0.66333318, 0.72601736, 0.31108484], - [0.58283192, 0.89317679, 0.11751470, 0.04111906, 0.04056534, 0.75454420]]) + [[0.01787627, 0.76492310, 0.67605734, 0.04620579, 0.38763246, 0.96462214], + [0.62356627, 0.64948404, 0.53736508, 0.09874519, 0.47123933, 0.85700107], + [0.34647217, 0.62869102, 0.54760450, 0.18061899, 0.55075216, 0.71997911], + [0.78576684, 0.76743823, 0.35644373, 0.63325852, 0.26549375, 0.68052763], + [0.40971112, 0.95848298, 0.41229674, 0.05506011, 0.18543524, 0.63480365], + [0.81180859, 0.94548297, 0.19634065, 0.73838711, 0.42057949, 0.96017945], + [0.72278100, 0.93858665, 0.72018963, 0.61661047, 0.33307818, 0.86660689], + [0.55808324, 0.12933673, 0.42916751, 0.22745337, 0.60508877, 0.82193440]]) """ return _C_ops.view_shape(x, other.shape) @@ -5320,8 +5330,9 @@ def unfold(x, axis, size, step, name=None): >>> out = paddle.unfold(x, 0, 2, 4) >>> print(out) + Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True, [[0., 1.], - [4., 5.]] + [4., 5.]]) """ return _C_ops.tensor_unfold(x, axis, size, step) From 512559abeae40f82264ca6648426dd882ca2e3d3 Mon Sep 17 00:00:00 2001 From: yuchen202 <103028470+yuchen202@users.noreply.github.com> Date: Wed, 27 Sep 2023 23:22:46 +0800 Subject: [PATCH 5/6] Update manipulation.py --- python/paddle/tensor/manipulation.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index b4be6ea0154219..8e60f6a64a1570 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -2680,14 +2680,14 @@ def unsqueeze(x, axis, name=None): >>> # out1, out2, out3 share data with x in dygraph mode >>> x[0, 0] = 10. >>> print(out1[0, 0, 0]) - Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, - [10.]) + Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, + 10.) >>> print(out2[0, 0, 0, 0]) - Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, - [10.]) + Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, + 10.) >>> print(out3[0, 0, 0, 0, 0]) - Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, - [10.]) + Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, + 10.) """ input = x From 6820f9732163e8545385f2a212c87523f4ea8381 Mon Sep 17 00:00:00 2001 From: Nyakku Shigure Date: Tue, 10 Oct 2023 19:40:54 +0800 Subject: [PATCH 6/6] Apply suggestions from code review --- python/paddle/tensor/search.py | 60 +++++++++++++++++----------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/python/paddle/tensor/search.py b/python/paddle/tensor/search.py index 324972555cb9f3..ac215ef6fdf725 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -61,11 +61,11 @@ def argsort(x, axis=-1, descending=False, name=None): >>> import paddle >>> x = paddle.to_tensor([[[5,8,9,5], - ... [0,0,1,7], - ... [6,9,2,4]], - ... [[5,2,4,2], - ... [4,7,7,9], - ... [1,7,0,6]]], + ... [0,0,1,7], + ... [6,9,2,4]], + ... [[5,2,4,2], + ... [4,7,7,9], + ... [1,7,0,6]]], ... dtype='float32') >>> out1 = paddle.argsort(x, axis=-1) >>> out2 = paddle.argsort(x, axis=0) @@ -159,8 +159,8 @@ def argmax(x, axis=None, keepdim=False, dtype="int64", name=None): >>> import paddle >>> x = paddle.to_tensor([[5,8,9,5], - ... [0,0,1,7], - ... [6,9,2,4]]) + ... [0,0,1,7], + ... [6,9,2,4]]) >>> out1 = paddle.argmax(x) >>> print(out1) Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, @@ -253,9 +253,9 @@ def argmin(x, axis=None, keepdim=False, dtype="int64", name=None): >>> import paddle - >>> x = paddle.to_tensor([[5,8,9,5], - ... [0,0,1,7], - ... [6,9,2,4]]) + >>> x = paddle.to_tensor([[5, 8, 9, 5], + ... [0, 0, 1, 7], + ... [6, 9, 2, 4]]) >>> out1 = paddle.argmin(x) >>> print(out1) Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, @@ -346,8 +346,8 @@ def index_select(x, index, axis=0, name=None): >>> import paddle >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], - ... [5.0, 6.0, 7.0, 8.0], - ... [9.0, 10.0, 11.0, 12.0]]) + ... [5.0, 6.0, 7.0, 8.0], + ... [9.0, 10.0, 11.0, 12.0]]) >>> index = paddle.to_tensor([0, 1, 1], dtype='int32') >>> out_z1 = paddle.index_select(x=x, index=index) >>> print(out_z1) @@ -424,8 +424,8 @@ def nonzero(x, as_tuple=False): >>> import paddle >>> x1 = paddle.to_tensor([[1.0, 0.0, 0.0], - ... [0.0, 2.0, 0.0], - ... [0.0, 0.0, 3.0]]) + ... [0.0, 2.0, 0.0], + ... [0.0, 0.0, 3.0]]) >>> x2 = paddle.to_tensor([0.0, 1.0, 0.0, 3.0]) >>> out_z1 = paddle.nonzero(x1) >>> print(out_z1) @@ -526,12 +526,12 @@ def sort(x, axis=-1, descending=False, name=None): >>> import paddle >>> x = paddle.to_tensor([[[5,8,9,5], - ... [0,0,1,7], - ... [6,9,2,4]], - ... [[5,2,4,2], - ... [4,7,7,9], - ... [1,7,0,6]]], - ... dtype='float32') + ... [0,0,1,7], + ... [6,9,2,4]], + ... [[5,2,4,2], + ... [4,7,7,9], + ... [1,7,0,6]]], + ... dtype='float32') >>> out1 = paddle.sort(x=x, axis=-1) >>> out2 = paddle.sort(x=x, axis=0) >>> out3 = paddle.sort(x=x, axis=1) @@ -819,14 +819,14 @@ def index_sample(x, index): >>> import paddle >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], - ... [5.0, 6.0, 7.0, 8.0], - ... [9.0, 10.0, 11.0, 12.0]], dtype='float32') + ... [5.0, 6.0, 7.0, 8.0], + ... [9.0, 10.0, 11.0, 12.0]], dtype='float32') >>> index = paddle.to_tensor([[0, 1, 2], - ... [1, 2, 3], - ... [0, 0, 0]], dtype='int32') + ... [1, 2, 3], + ... [0, 0, 0]], dtype='int32') >>> target = paddle.to_tensor([[100, 200, 300, 400], - ... [500, 600, 700, 800], - ... [900, 1000, 1100, 1200]], dtype='int32') + ... [500, 600, 700, 800], + ... [900, 1000, 1100, 1200]], dtype='int32') >>> out_z1 = paddle.index_sample(x, index) >>> print(out_z1) Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True, @@ -912,11 +912,11 @@ def masked_select(x, mask, name=None): >>> import paddle >>> x = paddle.to_tensor([[1.0, 2.0, 3.0, 4.0], - ... [5.0, 6.0, 7.0, 8.0], - ... [9.0, 10.0, 11.0, 12.0]]) + ... [5.0, 6.0, 7.0, 8.0], + ... [9.0, 10.0, 11.0, 12.0]]) >>> mask = paddle.to_tensor([[True, False, False, False], - ... [True, True, False, False], - ... [True, False, False, False]]) + ... [True, True, False, False], + ... [True, False, False, False]]) >>> out = paddle.masked_select(x, mask) >>> print(out) Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,