From f18231412fc66e52f93659932dee70a78d115f17 Mon Sep 17 00:00:00 2001 From: zhouwei25 <52485244+zhouwei25@users.noreply.github.com> Date: Fri, 11 Oct 2019 12:06:47 +0800 Subject: [PATCH] fix English Doc of API:layers.array_read/array_write/array_length (#20345) * fix English Doc of API:layers.py_func/sum, test=document_fix * fix English Doc of API:layers.array_read/array_write/array_length,test=develop test=document_fix --- paddle/fluid/API.spec | 6 +- python/paddle/fluid/layers/control_flow.py | 176 ++++++++++++++------- 2 files changed, 125 insertions(+), 57 deletions(-) mode change 100644 => 100755 paddle/fluid/API.spec mode change 100644 => 100755 python/paddle/fluid/layers/control_flow.py diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec old mode 100644 new mode 100755 index 35a222067b..c19b740997 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -347,7 +347,7 @@ paddle.fluid.layers.Switch.__init__ (ArgSpec(args=['self', 'name'], varargs=None paddle.fluid.layers.Switch.case (ArgSpec(args=['self', 'condition'], varargs=None, keywords=None, defaults=None), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) paddle.fluid.layers.Switch.default (ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) paddle.fluid.layers.increment (ArgSpec(args=['x', 'value', 'in_place'], varargs=None, keywords=None, defaults=(1.0, True)), ('document', 'f88b5787bb80ae6b8bf513a70dabbdc1')) -paddle.fluid.layers.array_write (ArgSpec(args=['x', 'i', 'array'], varargs=None, keywords=None, defaults=(None,)), ('document', '3f913b5069ad40bd85d89b33e4aa5939')) +paddle.fluid.layers.array_write (ArgSpec(args=['x', 'i', 'array'], varargs=None, keywords=None, defaults=(None,)), ('document', 'd357f71a280bf06aab4c79de9bd4facf')) paddle.fluid.layers.create_array (ArgSpec(args=['dtype'], varargs=None, keywords=None, defaults=None), ('document', '556de793fdf24d515f3fc91260e2c048')) paddle.fluid.layers.less_than (ArgSpec(args=['x', 'y', 'force_cpu', 'cond'], varargs=None, keywords=None, defaults=(None, None)), ('document', '04af32422c3a3d8f6040aeb406c82768')) paddle.fluid.layers.less_equal (ArgSpec(args=['x', 'y', 'cond'], varargs=None, keywords=None, defaults=(None,)), ('document', '04e5623dd39b4437b9b08e0ce11071ca')) @@ -355,8 +355,8 @@ paddle.fluid.layers.greater_than (ArgSpec(args=['x', 'y', 'cond'], varargs=None, paddle.fluid.layers.greater_equal (ArgSpec(args=['x', 'y', 'cond'], varargs=None, keywords=None, defaults=(None,)), ('document', '44bdacd11299d72c0a52d2181e7ae6ca')) paddle.fluid.layers.equal (ArgSpec(args=['x', 'y', 'cond'], varargs=None, keywords=None, defaults=(None,)), ('document', '788aa651e8b9fec79d16931ef3a33e90')) paddle.fluid.layers.not_equal (ArgSpec(args=['x', 'y', 'cond'], varargs=None, keywords=None, defaults=(None,)), ('document', '8b76aaac4ba7cf9111750b9c2c9418cb')) -paddle.fluid.layers.array_read (ArgSpec(args=['array', 'i'], varargs=None, keywords=None, defaults=None), ('document', 'caf0d94349cdc28e1bda3b8a19411ac0')) -paddle.fluid.layers.array_length (ArgSpec(args=['array'], varargs=None, keywords=None, defaults=None), ('document', '6f24a9b872027634ad758ea2826c9727')) +paddle.fluid.layers.array_read (ArgSpec(args=['array', 'i'], varargs=None, keywords=None, defaults=None), ('document', 'b75c821cc1d22355c3c17e7bdf509510')) +paddle.fluid.layers.array_length (ArgSpec(args=['array'], varargs=None, keywords=None, defaults=None), ('document', 'c90d305395eb44e6dc772fab24ff2ef5')) paddle.fluid.layers.IfElse ('paddle.fluid.layers.control_flow.IfElse', ('document', '720054043e55273224682fdb6b9ad13b')) paddle.fluid.layers.IfElse.__init__ (ArgSpec(args=['self', 'cond', 'name'], varargs=None, keywords=None, defaults=(None,)), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) paddle.fluid.layers.IfElse.false_block (ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None), ('document', '6adf97f83acf6453d4a6a4b1070f3754')) diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py old mode 100644 new mode 100755 index 09c03f8452..a96eeee8bd --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -1057,31 +1057,53 @@ def increment(x, value=1.0, in_place=True): def array_write(x, i, array=None): """ - This function writes the given input variable to the specified position - indicating by the arrary index to an output LOD_TENSOR_ARRAY. If the - output LOD_TENSOR_ARRAY is not given(None), a new one will be created and - returned. + This OP writes the input ``x`` into the i-th position of the ``array`` + :ref:`api_fluid_LoDTensorArray` and returns the modified array. + If ``array`` is none, a new LoDTensorArray will be created and returned. + This OP is often used together with :ref:`api_fluid_layers_array_read` OP. Args: - x (Variable|list): The input tensor from which the data will be read. - i (Variable|list): The index of the output LOD_TENSOR_ARRAY, pointing to - the position to which the input tensor will be - written. - array (Variable|list): The output LOD_TENSOR_ARRAY to which the input - tensor will be written. If this parameter is - NONE, a new LOD_TENSOR_ARRAY will be created and - returned. + x (Variable): The input data to be written into array. It's multi-dimensional + Tensor or LoDTensor. Data type: float32, float64, int32, int64. + i (Variable): 1-D Tensor with shape [1], which represents the position into which + ``x`` is written. Data type: int64. + array (LoDTensorArray, optional): The LoDTensorArray into which ``x`` is written. + The default value is None, when a new LoDTensorArray will be created and returned + as a result. Returns: - Variable: The output LOD_TENSOR_ARRAY where the input tensor is written. + Variable: The input ``array`` after ``x`` is written into. Examples: .. code-block:: python - import paddle.fluid as fluid - tmp = fluid.layers.zeros(shape=[10], dtype='int32') - i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10) - arr = fluid.layers.array_write(tmp, i=i) + import paddle.fluid as fluid + tmp = fluid.layers.fill_constant(shape=[3, 2], dtype='int64', value=5) + i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10) + # Write tmp into the position of arr with subscript 10 and return arr. + arr = fluid.layers.array_write(tmp, i=i) + + # Now, arr is a LoDTensorArray with length 11. We can use array_read OP to read + # the data at subscript 10 and print it out. + item = fluid.layers.array_read(arr, i=i) + input = fluid.layers.Print(item, message="The content of i-th LoDTensor:") + main_program = fluid.default_main_program() + exe = fluid.Executor(fluid.CPUPlace()) + exe.run(main_program) + + # The printed result is: + # 1570533133 The content of i-th LoDTensor: The place is:CPUPlace + # Tensor[array_read_0.tmp_0] + # shape: [3,2,] + # dtype: l + # data: 5,5,5,5,5,5, + + # the output is 2-D Tensor with shape [3,2], which is tmp above. + # dtype is the corresponding C++ data type, which may vary in different environments. + # Eg: if the data type of tensor is int64, then the corresponding C++ data type is int64_t, + # so the dtype value is typeid(int64_t).Name(), which is 'x' on MacOS, 'l' on Linux, + # and '__int64' on Windows. They both represent 64-bit integer variables. + """ helper = LayerHelper('array_write', **locals()) if array is None: @@ -1365,37 +1387,64 @@ def not_equal(x, y, cond=None): def array_read(array, i): """ - This function performs the operation to read the data in as an - LOD_TENSOR_ARRAY. - - .. code-block:: text - - Given: - - array = [0.6, 0.1, 0.3, 0.1] - - And: - - i = 2 - - Then: - - output = 0.3 + This OP is used to read data at the specified position from the input array + :ref:`api_fluid_LoDTensorArray` . ``array`` is the input array and ``i`` + is the specified read position. This OP is often used together with + :ref:`api_fluid_layers_array_write` OP. + + Case 1: + :: + Input: + The shape of first three tensors are [1], and that of the last one is [1,2]: + array = ([0.6], [0.1], [0.3], [0.4, 0.2]) + And: + i = [3] + + Output: + output = [0.4, 0.2] Args: - array (Variable|list): The input tensor that store data to be read. - i (Variable|list): The index of the data to be read from input array. + array (LoDTensorArray): The input LoDTensorArray. + i (Variable): 1-D Tensor, whose shape is [1] and dtype is int64. It represents the + specified read position of ``array``. Returns: - Variable: The tensor type variable that has the data written to it. + Variable: The LoDTensor or Tensor that is read at the specified position of ``array``. Examples: .. code-block:: python - import paddle.fluid as fluid - array = fluid.layers.create_array(dtype='float32') - i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10) - item = fluid.layers.array_read(array, i) + # First we're going to create a LoDTensorArray, then we're going to write the Tensor into + # the specified position, and finally we're going to read the Tensor at that position. + import paddle.fluid as fluid + arr = fluid.layers.create_array(dtype='float32') + tmp = fluid.layers.fill_constant(shape=[3, 2], dtype='int64', value=5) + i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10) + # tmp is the Tensor with shape [3,2], and if we write it into the position with subscript 10 + # of the empty-array: arr, then the length of arr becomes 11. + arr = fluid.layers.array_write(tmp, i, array=arr) + # Read the data of the position with subscript 10. + item = fluid.layers.array_read(arr, i) + + # You can print out the data via executor. + input = fluid.layers.Print(item, message="The LoDTensor of the i-th position:") + main_program = fluid.default_main_program() + exe = fluid.Executor(fluid.CPUPlace()) + exe.run(main_program) + + # The printed result is: + + # 1569588169 The LoDTensor of the i-th position: The place is:CPUPlace + # Tensor[array_read_0.tmp_0] + # shape: [3,2,] + # dtype: l + # data: 5,5,5,5,5,5, + + # the output is 2-D Tensor with shape [3,2]. + # dtype is the corresponding C++ data type, which may vary in different environments. + # Eg: if the data type of tensor is int64, then the corresponding C++ data type is int64_t, + # so the dtype value is typeid(int64_t).Name(), which is 'x' on MacOS, 'l' on Linux, + # and '__int64' on Windows. They both represent 64-bit integer variables. """ helper = LayerHelper('array_read', **locals()) if not isinstance( @@ -1450,29 +1499,48 @@ def shrink_memory(x, i, table): def array_length(array): """ - **Get the Length of Input LoDTensorArray** - - This function performs the operation to find the length of the input - LOD_TENSOR_ARRAY. - - Related API: array_read, array_write, While. + This OP is used to get the length of the input array :ref:`api_fluid_LoDTensorArray` . + It can be used together with :ref:`api_fluid_layers_array_read` , :ref:`api_fluid_layers_array_write` , + :ref:`api_fluid_layers_While` OP to traverse, read and wirte LoDTensorArray. Args: - array (LOD_TENSOR_ARRAY): The input array that will be used - to compute the length. + array (LoDTensorArray): The input array that will be used to compute the length. Returns: - Variable: The length of the input LoDTensorArray. + Variable: 1-D Tensor with shape [1], which is the length of array. Datatype: int64. Examples: .. code-block:: python - import paddle.fluid as fluid - tmp = fluid.layers.zeros(shape=[10], dtype='int32') - i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10) - arr = fluid.layers.array_write(tmp, i=i) - arr_len = fluid.layers.array_length(arr) + import paddle.fluid as fluid + tmp = fluid.layers.zeros(shape=[10], dtype='int32') + i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10) + # tmp is 1-D Tensor with shape [10]. We write tmp into arr on subscript 10, + # then the length of arr becomes 11. + arr = fluid.layers.array_write(tmp, i=i) + # return the length of arr + arr_len = fluid.layers.array_length(arr) + + # You can use executor to print out the length of LoDTensorArray. + input = fluid.layers.Print(arr_len, message="The length of LoDTensorArray:") + main_program = fluid.default_main_program() + exe = fluid.Executor(fluid.CPUPlace()) + exe.run(main_program) + + # The printed result is: + # 1569576542 The length of LoDTensorArray: The place is:CPUPlace + # Tensor[array_length_0.tmp_0] + # shape: [1,] + # dtype: l + # data: 11, + + # 1-D Tensor with shape [1], whose value is 11. It means that the length of LoDTensorArray + # is 11. + # dtype is the corresponding C++ data type, which may vary in different environments. + # Eg: if the data type of tensor is int64, then the corresponding C++ data type is int64_t, + # so the dtype value is typeid(int64_t).Name(), which is 'x' on MacOS, 'l' on Linux, + # and '__int64' on Windows. They both represent 64-bit integer variables. """ helper = LayerHelper('array_length', **locals()) tmp = helper.create_variable_for_type_inference(dtype='int64') -- GitLab