diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 62de7b884275b7..8e60f6a64a1570 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,16 @@ 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) + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[-1], + [ 1]]) """ if in_dynamic_mode(): return _C_ops.shard_index( @@ -721,29 +735,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 +892,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 +926,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 +958,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 +1025,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 +1057,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 +1089,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 +1125,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 +1253,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 +1363,29 @@ 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) + 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) + 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] @@ -1402,38 +1437,43 @@ 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) + Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[0, 1], + [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]]) + + >>> 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]]) + + >>> 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]]]) + + >>> 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]]]) """ helper = LayerHelper("rot90", **locals()) @@ -1544,19 +1584,22 @@ 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) + >>> 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]) # [-1] + >>> # out shares data with img in dygraph mode + >>> img[0, 0, 0, 0] = -1 + >>> print(out[0, 0, 0]) + 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") @@ -1685,26 +1728,29 @@ 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) + 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) + 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) + 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: @@ -1831,25 +1877,27 @@ 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) + Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[[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 +1986,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 +2172,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 +2265,21 @@ 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]) + Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, + [10.]) """ if axis is None: @@ -2322,37 +2394,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 +2523,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 +2658,36 @@ def unsqueeze(x, axis, name=None): Examples: .. code-block:: python - import paddle - - x = paddle.rand([5, 10]) - print(x.shape) # [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] - - 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.] + >>> import paddle + + >>> x = paddle.rand([5, 10]) + >>> print(x.shape) + [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] + + >>> 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]) + Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, + 10.) + >>> print(out2[0, 0, 0, 0]) + Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, + 10.) + >>> print(out3[0, 0, 0, 0, 0]) + Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, + 10.) """ input = x @@ -2729,12 +2811,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 +2880,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( @@ -2869,29 +2951,28 @@ 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 - # 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 +2994,37 @@ 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) + Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[3., 3.], + [6., 6.], + [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.]]) + >>> # 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.]]) + >>> # result 2: + Tensor(shape=[3, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, + [[3., 3.], + [2., 2.], + [1., 1.]]) """ if in_dynamic_mode(): return _C_ops.scatter(x, index, updates, overwrite) @@ -3026,17 +3113,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 +3170,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 +3201,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 +3242,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 +3366,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 +3436,14 @@ 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) + 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) @@ -3451,12 +3540,14 @@ 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) + 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) @@ -3569,27 +3660,61 @@ 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) + Tensor(shape=[4, 12], 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]]) + >>> 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) + [8, 6] + >>> # out shares data with x in dygraph mode + >>> x[0, 0, 0] = 10. + >>> print(out[0, 0]) + Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True, + 10.) """ if in_dynamic_or_pir_mode(): @@ -3789,13 +3914,16 @@ 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) + Tensor(shape=[1, 2], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[3, 4]]) """ if in_dynamic_mode(): @@ -3899,22 +4027,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 +4216,88 @@ 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) + Tensor(shape=[2, 2, 2, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[[[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) + Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True, + 285.) + >>> print(z2) + 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]) + >>> 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.]]) + >>> 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.]]) + + + >>> # 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) + Tensor(shape=[2, 3], dtype=float64, place=Place(cpu), stop_gradient=True, + [[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) + Tensor(shape=[5, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[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) + 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.]]) """ op_type = 'tensordot' input_dtype = ['float16', 'float32', 'float64'] @@ -4279,14 +4421,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 +4467,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 +4515,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 +4583,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 +4720,15 @@ 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) + Tensor(shape=[1, 3], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1, 2, 3]]) """ if len(arr.shape) != len(indices.shape): raise ValueError( @@ -4658,16 +4798,17 @@ 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) + Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True, + [[99, 99, 99], + [60, 40, 50]]) """ if len(arr.shape) != len(indices.shape): @@ -4760,18 +4901,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.]]) + >>> # doctest: +REQUIRES(env: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 +4961,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.]]) + >>> # doctest: +REQUIRES(env: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 +4998,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 +5029,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 +5100,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 +5170,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 +5209,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}) + >>> 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, + [[[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) @@ -5105,14 +5279,25 @@ 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}) + + + >>> paddle.seed(1) + >>> 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.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) @@ -5138,13 +5323,16 @@ 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) + Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True, + [[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..ac215ef6fdf725 100755 --- a/python/paddle/tensor/search.py +++ b/python/paddle/tensor/search.py @@ -58,42 +58,45 @@ 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) + 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) + 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) + 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) @@ -153,22 +156,27 @@ 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) + Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, + 2) + >>> out2 = paddle.argmax(x, axis=0) + >>> print(out2) + Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, + [2, 2, 0, 1]) + >>> out3 = paddle.argmax(x, axis=-1) + >>> print(out3) + Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, + [2, 3, 1]) + >>> out4 = paddle.argmax(x, axis=0, keepdim=True) + >>> print(out4) + 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( @@ -243,22 +251,27 @@ 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) + Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True, + 4) + >>> out2 = paddle.argmin(x, axis=0) + >>> print(out2) + Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True, + [1, 1, 1, 2]) + >>> out3 = paddle.argmin(x, axis=-1) + >>> print(out3) + Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, + [0, 0, 2]) + >>> out4 = paddle.argmin(x, axis=0, keepdim=True) + >>> print(out4) + 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( @@ -330,20 +343,24 @@ 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) + 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) + >>> 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(): @@ -404,36 +421,40 @@ 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) + 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) + 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) + 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) + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1], + [3]]) """ list_out = [] shape = x.shape @@ -502,39 +523,42 @@ 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) + 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) + 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) + 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) @@ -575,16 +599,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=Place(cpu), stop_gradient=True, + [[2., 3.], + [5., 9.]]), Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[2, 2], + [2, 1]])) """ if in_dynamic_mode(): @@ -637,20 +661,21 @@ 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) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [1. , 1. , 3.20000005, 1.20000005]) - 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=Place(cpu), stop_gradient=True, + [[2], + [3]]),) """ if np.isscalar(x): x = paddle.full([1], x, np.array([x]).dtype.name) @@ -791,41 +816,45 @@ 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) + 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) + Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, + [[4. , 3. ], + [8. , 7. ], + [12., 11.]]) + + >>> print(top_index) + Tensor(shape=[3, 2], dtype=int64, place=Place(cpu), stop_gradient=True, + [[3, 2], + [3, 2], + [3, 2]]) + + >>> print(out_z2) + Tensor(shape=[3, 2], dtype=int32, place=Place(cpu), stop_gradient=True, + [[400 , 300 ], + [800 , 700 ], + [1200, 1100]]) """ if in_dynamic_mode(): @@ -880,16 +909,18 @@ 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) + Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, + [1., 5., 6., 9.]) """ if in_dynamic_mode(): @@ -940,27 +971,45 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None): .. code-block:: python - 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_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_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]] - - + >>> import paddle + + >>> data_1 = paddle.to_tensor([1, 4, 5, 7]) + >>> value_1, indices_1 = paddle.topk(data_1, k=1) + >>> print(value_1) + Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True, + [7]) + >>> print(indices_1) + 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) + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[7], + [6]]) + >>> print(indices_2) + 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) + Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True, + [[7], + [6]]) + >>> print(indices_3) + 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) + Tensor(shape=[1, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[2, 6, 5, 7]]) + >>> print(indices_4) + Tensor(shape=[1, 4], dtype=int64, place=Place(cpu), stop_gradient=True, + [[1, 1, 0, 0]]) """ if in_dynamic_mode(): @@ -1013,30 +1062,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 +1122,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 +1194,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 + >>> paddle.seed(1) + >>> x = paddle.randn((2,3,2)) + >>> print(x) + 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=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: