From 4534a036cb41ad83813dab974e8356d470dc901f Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 9 Sep 2023 19:25:21 +0800 Subject: [PATCH 1/2] [Doctest]fix No.274, test=docs_preview --- python/paddle/static/nn/sequence_lod.py | 334 ++++++++++++------------ 1 file changed, 170 insertions(+), 164 deletions(-) diff --git a/python/paddle/static/nn/sequence_lod.py b/python/paddle/static/nn/sequence_lod.py index 653541d92551c..ee96de8bdbaea 100644 --- a/python/paddle/static/nn/sequence_lod.py +++ b/python/paddle/static/nn/sequence_lod.py @@ -123,11 +123,11 @@ def sequence_conv( .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name='x', shape=[-1, 10], dtype='float32', lod_level=1) - x_conved = paddle.static.nn.sequence_conv(input=x, num_filters=2, filter_size=3, padding_start=-1) + >>> x = paddle.static.data(name='x', shape=[-1, 10], dtype='float32', lod_level=1) + >>> x_conved = paddle.static.nn.sequence_conv(input=x, num_filters=2, filter_size=3, padding_start=-1) """ assert ( @@ -221,16 +221,16 @@ def sequence_softmax(input, use_cudnn=False, name=None): .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name='x', shape=[7, 1], + >>> x = paddle.static.data(name='x', shape=[7, 1], dtype='float32', lod_level=1) - x_sequence_softmax_1 = paddle.static.nn.sequence_softmax(input=x) + >>> x_sequence_softmax_1 = paddle.static.nn.sequence_softmax(input=x) - y = paddle.static.data(name='y', shape=[7], - dtype='float32', lod_level=1) - x_sequence_softmax_2 = paddle.static.nn.sequence_softmax(input=y) + >>> y = paddle.static.data(name='y', shape=[7], + ... dtype='float32', lod_level=1) + >>> x_sequence_softmax_2 = paddle.static.nn.sequence_softmax(input=y) """ assert ( not in_dygraph_mode() @@ -326,16 +326,16 @@ def sequence_pool(input, pool_type, is_test=False, pad_value=0.0): .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) - avg_x = paddle.static.nn.sequence_pool(input=x, pool_type='average') - sum_x = paddle.static.nn.sequence_pool(input=x, pool_type='sum') - sqrt_x = paddle.static.nn.sequence_pool(input=x, pool_type='sqrt') - max_x = paddle.static.nn.sequence_pool(input=x, pool_type='max') - last_x = paddle.static.nn.sequence_pool(input=x, pool_type='last') - first_x = paddle.static.nn.sequence_pool(input=x, pool_type='first') + >>> x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) + >>> avg_x = paddle.static.nn.sequence_pool(input=x, pool_type='average') + >>> sum_x = paddle.static.nn.sequence_pool(input=x, pool_type='sum') + >>> sqrt_x = paddle.static.nn.sequence_pool(input=x, pool_type='sqrt') + >>> max_x = paddle.static.nn.sequence_pool(input=x, pool_type='max') + >>> last_x = paddle.static.nn.sequence_pool(input=x, pool_type='last') + >>> first_x = paddle.static.nn.sequence_pool(input=x, pool_type='first') """ assert ( not in_dygraph_mode() @@ -408,12 +408,12 @@ def sequence_concat(input, name=None): Examples: .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name='x', shape=[-1, 10], dtype='float32', lod_level=1) - y = paddle.static.data(name='y', shape=[-1, 10], dtype='float32', lod_level=1) - out = paddle.static.nn.sequence_concat(input=[x, y]) + >>> x = paddle.static.data(name='x', shape=[-1, 10], dtype='float32', lod_level=1) + >>> y = paddle.static.data(name='y', shape=[-1, 10], dtype='float32', lod_level=1) + >>> out = paddle.static.nn.sequence_concat(input=[x, y]) """ assert ( not in_dygraph_mode() @@ -484,11 +484,11 @@ def sequence_first_step(input): .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) - x_first_step = paddle.static.nn.sequence_first_step(input=x) + >>> x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) + >>> x_first_step = paddle.static.nn.sequence_first_step(input=x) """ check_variable_and_dtype( input, 'input', ['float32', 'float64'], 'sequence_first_step' @@ -543,11 +543,11 @@ def sequence_last_step(input): .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) - x_last_step = paddle.static.nn.sequence_last_step(input=x) + >>> x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) + >>> x_last_step = paddle.static.nn.sequence_last_step(input=x) """ check_variable_and_dtype( input, 'input', ['float32', 'float64'], 'sequence_last_step' @@ -605,16 +605,16 @@ def sequence_slice(input, offset, length, name=None): .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - import numpy as np - seqs = paddle.static.data(name='x', shape=[10, 5], - dtype='float32', lod_level=1) - offset = paddle.assign(np.array([[0, 1]]).astype("int32")) - length = paddle.assign(np.array([[2, 1]]).astype("int32")) - subseqs = paddle.static.nn.sequence_slice(input=seqs, offset=offset, - length=length) + >>> import numpy as np + >>> seqs = paddle.static.data(name='x', shape=[10, 5], + ... dtype='float32', lod_level=1) + >>> offset = paddle.assign(np.array([[0, 1]]).astype("int32")) + >>> length = paddle.assign(np.array([[2, 1]]).astype("int32")) + ... subseqs = paddle.static.nn.sequence_slice(input=seqs, offset=offset, + ... length=length) """ assert ( not in_dygraph_mode() @@ -729,46 +729,49 @@ def sequence_expand(x, y, ref_level=-1, name=None): Examples: .. code-block:: python - import paddle - from paddle import base - paddle.enable_static() - import numpy as np - - x = paddle.static.data(name='x', shape=[4, 1], dtype='float32') - y = paddle.static.data(name='y', shape=[8, 1], - dtype='float32', lod_level=1) - out = paddle.static.nn.sequence_expand(x=x, y=y, ref_level=0) - - exe = paddle.static.Executor(base.CPUPlace()) - place = paddle.CPUPlace() - - np_data = np.array([[1], [2], [3], [4]]).astype('float32') - x_lod_tensor = base.create_lod_tensor(np_data, [[2, 2]], place) - print(x_lod_tensor) - #lod: [[0, 2, 4]] - # dim: 4, 1 - # layout: NCHW - # dtype: float - # data: [1 2 3 4] - - np_data = np.array([[1], [2], [3], [4], [5], [6], [7], [8]]).astype('float32') - y_lod_tensor = base.create_lod_tensor(np_data, [[2, 2], [3,3,1,1]], place) - print(y_lod_tensor) - #lod: [[0, 2, 4][0, 3, 6, 7, 8]] - # dim: 8, 1 - # layout: NCHW - # dtype: int64_t - # data: [0 0 1 1 1 1 1 0] - - out_main = exe.run(base.default_main_program(), - feed={'x': x_lod_tensor, 'y': y_lod_tensor}, - fetch_list=[out], return_numpy=False) - print(out_main[0]) - #lod: [[0, 2, 4, 6, 8]] - # dim: 8, 1 - # layout: NCHW - # dtype: float - # data: [1 2 1 2 3 4 3 4] + >>> import paddle + >>> from paddle import base + >>> paddle.enable_static() + >>> import numpy as np + + >>> x = paddle.static.data(name='x', shape=[4, 1], dtype='float32') + >>> y = paddle.static.data(name='y', shape=[8, 1], + ... dtype='float32', lod_level=1) + >>> out = paddle.static.nn.sequence_expand(x=x, y=y, ref_level=0) + + >>> exe = paddle.static.Executor(base.CPUPlace()) + >>> place = paddle.CPUPlace() + + >>> np_data = np.array([[1], [2], [3], [4]]).astype('float32') + >>> x_lod_tensor = base.create_lod_tensor(np_data, [[2, 2]], place) + >>> print(x_lod_tensor) + - lod: {{0, 2, 4}} + - place: Place(cpu) + - shape: [4, 1] + - layout: NCHW + - dtype: float32 + - data: [1 2 3 4] + + >>> np_data = np.array([[1], [2], [3], [4], [5], [6], [7], [8]]).astype('float32') + >>> y_lod_tensor = base.create_lod_tensor(np_data, [[2, 2], [3,3,1,1]], place) + >>> print(y_lod_tensor) + - lod: {{0, 2, 4}{0, 3, 6, 7, 8}} + - place: Place(cpu) + - shape: [8, 1] + - layout: NCHW + - dtype: float32 + - data: [1 2 3 4 5 6 7 8] 0] + + >>> out_main = exe.run(base.default_main_program(), + ... feed={'x': x_lod_tensor, 'y': y_lod_tensor}, + ... fetch_list=[out], return_numpy=False) + >>> print(out_main[0]) + - lod: {{0, 2, 4, 6, 8}} + - place: Place(cpu) + - shape: [8, 1] + - layout: NCHW + - dtype: float32 + - data: [1 2 1 2 3 4 3 4] """ assert ( not in_dygraph_mode() @@ -852,45 +855,48 @@ def sequence_expand_as(x, y, name=None): Examples: .. code-block:: python - import paddle - import paddle.base as base - paddle.enable_static() - import numpy as np - - x = paddle.static.data(name='x', shape=[4, 1], dtype='float32') - y = paddle.static.data(name='y', shape=[8, 1], dtype='float32', lod_level=1) - out = paddle.static.nn.sequence_expand_as(x=x, y=y) - - exe = base.Executor(base.CPUPlace()) - place = base.CPUPlace() - - np_data = np.array([[1], [2], [3], [4]]).astype('float32') - x_lod_tensor = base.create_lod_tensor(np_data, [[2, 2]], place) - print(x_lod_tensor) - #lod: [[0, 2, 4]] - # dim: 4, 1 - # layout: NCHW - # dtype: float - # data: [1 2 3 4] - - np_data = np.array([[1], [2], [3], [4], [5], [6], [7], [8]]).astype('float32') - y_lod_tensor = base.create_lod_tensor(np_data, [[3,3,1,1]], place) - print(y_lod_tensor) - #lod: [[0, 3, 6, 7, 8]] - # dim: 8, 1 - # layout: NCHW - # dtype: int64_t - # data: [0 0 1 0 1 1 1 0] - - out_main = exe.run(base.default_main_program(), - feed={'x': x_lod_tensor, 'y': y_lod_tensor}, - fetch_list=[out], return_numpy=False) - print(out_main[0]) - #lod: [[0, 3, 6, 7, 8]] - # dim: 8, 1 - # layout: NCHW - # dtype: float - # data: [1 1 1 2 2 2 3 4] + >>> import paddle + >>> import paddle.base as base + >>> paddle.enable_static() + >>> import numpy as np + + >>> x = paddle.static.data(name='x', shape=[4, 1], dtype='float32') + >>> y = paddle.static.data(name='y', shape=[8, 1], dtype='float32', lod_level=1) + >>> out = paddle.static.nn.sequence_expand_as(x=x, y=y) + + >>> exe = base.Executor(base.CPUPlace()) + >>> place = base.CPUPlace() + + >>> np_data = np.array([[1], [2], [3], [4]]).astype('float32') + >>> x_lod_tensor = base.create_lod_tensor(np_data, [[2, 2]], place) + >>> print(x_lod_tensor) + - lod: {{0, 2, 4}} + - place: Place(cpu) + - shape: [4, 1] + - layout: NCHW + - dtype: float32 + - data: [1 2 3 4] + + >>> np_data = np.array([[1], [2], [3], [4], [5], [6], [7], [8]]).astype('float32') + >>> y_lod_tensor = base.create_lod_tensor(np_data, [[3,3,1,1]], place) + >>> print(y_lod_tensor) + - lod: {{0, 3, 6, 7, 8}} + - place: Place(cpu) + - shape: [8, 1] + - layout: NCHW + - dtype: float32 + - data: [1 2 3 4 5 6 7 8] + + >>> out_main = exe.run(base.default_main_program(), + ... feed={'x': x_lod_tensor, 'y': y_lod_tensor}, + ... fetch_list=[out], return_numpy=False) + >>> print(out_main[0]) + - lod: {{0, 3, 6, 7, 8}} + - place: Place(cpu) + - shape: [8, 1] + - layout: NCHW + - dtype: float32 + - data: [1 1 1 2 2 2 3 4] """ assert ( not in_dygraph_mode() @@ -984,15 +990,15 @@ def sequence_pad(x, pad_value, maxlen=None, name=None): Examples: .. code-block:: python - import paddle - paddle.enable_static() - import paddle.base as base - import numpy + >>> import paddle + >>> paddle.enable_static() + >>> import paddle.base as base + >>> import numpy - x = paddle.static.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) - pad_value = paddle.assign( - numpy.array([0.0], dtype=numpy.float32)) - out = paddle.static.nn.sequence_pad(x=x, pad_value=pad_value) + >>> x = paddle.static.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) + >>> pad_value = paddle.assign( + ... numpy.array([0.0], dtype=numpy.float32)) + >>> out = paddle.static.nn.sequence_pad(x=x, pad_value=pad_value) """ assert ( @@ -1070,18 +1076,18 @@ def sequence_unpad(x, length, name=None): Examples: .. code-block:: python - import paddle - paddle.enable_static() - import paddle.base as base - import numpy + >>> import paddle + >>> paddle.enable_static() + >>> import paddle.base as base + >>> import numpy - # pad data - x = paddle.static.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) - pad_value = paddle.assign(numpy.array([0.0], dtype=numpy.float32)) - pad_data, len = paddle.static.nn.sequence_pad(x=x, pad_value=pad_value) + >>> # pad data + >>> x = paddle.static.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) + >>> pad_value = paddle.assign(numpy.array([0.0], dtype=numpy.float32)) + >>> pad_data, len = paddle.static.nn.sequence_pad(x=x, pad_value=pad_value) - # unpad data - unpad_data = paddle.static.nn.sequence_unpad(x=pad_data, length=len) + >>> # unpad data + >>> unpad_data = paddle.static.nn.sequence_unpad(x=pad_data, length=len) """ assert ( @@ -1156,11 +1162,11 @@ def sequence_reshape(input, new_dim): Examples: .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name='x', shape=[None, 16], dtype='float32', lod_level=1) - x_reshaped = paddle.static.nn.sequence_reshape(input=x, new_dim=4) + >>> x = paddle.static.data(name='x', shape=[None, 16], dtype='float32', lod_level=1) + >>> x_reshaped = paddle.static.nn.sequence_reshape(input=x, new_dim=4) """ assert ( not in_dygraph_mode() @@ -1238,13 +1244,13 @@ def sequence_scatter(input, index, updates, name=None): .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - input = paddle.static.data(name="x", shape=[None, 3, 6], dtype='float32' ) - index = paddle.static.data(name='index', shape=[12, 1], dtype='int64', lod_level=1) - updates = paddle.static.data(name='updates', shape=[12, 1], dtype='float32', lod_level=1) - output = paddle.static.nn.sequence_scatter(input, index, updates) + >>> input = paddle.static.data(name="x", shape=[None, 3, 6], dtype='float32' ) + >>> index = paddle.static.data(name='index', shape=[12, 1], dtype='int64', lod_level=1) + >>> updates = paddle.static.data(name='updates', shape=[12, 1], dtype='float32', lod_level=1) + >>> output = paddle.static.nn.sequence_scatter(input, index, updates) """ assert ( @@ -1323,11 +1329,11 @@ def sequence_enumerate(input, win_size, pad_value=0, name=None): Examples: .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name='x', shape=[-1, 1], dtype='int32', lod_level=1) - out = paddle.static.nn.sequence_enumerate(input=x, win_size=3, pad_value=0) + >>> x = paddle.static.data(name='x', shape=[-1, 1], dtype='int32', lod_level=1) + >>> out = paddle.static.nn.sequence_enumerate(input=x, win_size=3, pad_value=0) """ assert ( not in_dygraph_mode() @@ -1395,15 +1401,15 @@ def sequence_mask(x, maxlen=None, dtype='int64', name=None): Examples: .. code-block:: python - import paddle + >>> import paddle - lengths = paddle.to_tensor([10, 9, 8]) - mask = paddle.nn.functional.sequence_mask(lengths) + >>> lengths = paddle.to_tensor([10, 9, 8]) + >>> mask = paddle.nn.functional.sequence_mask(lengths) - print(mask.numpy()) - # [[1 1 1 1 1 1 1 1 1 1] - # [1 1 1 1 1 1 1 1 1 0] - # [1 1 1 1 1 1 1 1 0 0]] + >>> print(mask.numpy()) + [[1 1 1 1 1 1 1 1 1 1] + [1 1 1 1 1 1 1 1 1 0] + [1 1 1 1 1 1 1 1 0 0]] """ @@ -1452,11 +1458,11 @@ def sequence_reverse(x, name=None): Examples: .. code-block:: python - import paddle - paddle.enable_static() + >>> import paddle + >>> paddle.enable_static() - x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) - x_reversed = paddle.static.nn.sequence_reverse(x) + >>> x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) + >>> x_reversed = paddle.static.nn.sequence_reverse(x) """ assert ( not in_dygraph_mode() From bbbf861a6331f35754353c53940899881a98484e Mon Sep 17 00:00:00 2001 From: ooooo <3164076421@qq.com> Date: Sat, 9 Sep 2023 21:52:50 +0800 Subject: [PATCH 2/2] fix --- python/paddle/static/nn/sequence_lod.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/static/nn/sequence_lod.py b/python/paddle/static/nn/sequence_lod.py index ee96de8bdbaea..eb4329d22086a 100644 --- a/python/paddle/static/nn/sequence_lod.py +++ b/python/paddle/static/nn/sequence_lod.py @@ -760,7 +760,7 @@ def sequence_expand(x, y, ref_level=-1, name=None): - shape: [8, 1] - layout: NCHW - dtype: float32 - - data: [1 2 3 4 5 6 7 8] 0] + - data: [1 2 3 4 5 6 7 8] >>> out_main = exe.run(base.default_main_program(), ... feed={'x': x_lod_tensor, 'y': y_lod_tensor},