Skip to content

Commit

Permalink
fix the document of API:fluid.Layers.array_read (PaddlePaddle#1391)
Browse files Browse the repository at this point in the history
fix the document of API:fluid.Layers.array_read

* fix the document of API:fluid.Layers.array_read

* fix the document of API:fluid.Layers.array_read
  • Loading branch information
zhwesky2010 authored Oct 8, 2019
1 parent 4789433 commit 607b83d
Showing 1 changed file with 41 additions and 22 deletions.
63 changes: 41 additions & 22 deletions doc/fluid/api_cn/layers_cn/array_read_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,59 @@ array_read

.. py:function:: paddle.fluid.layers.array_read(array,i)
此函数用于读取数据,数据以LOD_TENSOR_ARRAY数组的形式读入
该OP用于读取输入数组 :ref:`cn_api_fluid_LoDTensorArray` 中指定位置的数据, ``array`` 为输入的数组, ``i`` 为指定的读取位置。常与 :ref:`cn_api_fluid_layers_array_write` OP配合使用进行LoDTensorArray的读写。

例1:
::
输入:
包含4个Tensor的LoDTensorArray,前3个shape为[1],最后一个shape为[1,2]:
input = ([0.6], [0.1], [0.3], [0.4, 0.2])
并且:
i = [3]


Given:
array = [0.6,0.1,0.3,0.1]
And:
I=2
Then:
output = 0.3
输出:
output = [0.4, 0.2]

参数:
- **array** (Variable|list)-输入张量,存储要读的数据
- **i** (Variable|list)-输入数组中数据的索引
- **array** (Variable) - 输入的数组LoDTensorArray
- **i** (Variable) - shape为[1]的1-D Tensor,表示从 ``array`` 中读取数据的位置,数据类型为int64


返回:张量类型的变量,已有数据写入
返回:从 ``array`` 中指定位置读取的LoDTensor或Tensor

返回类型:变量(Variable
返回类型:Variable

**代码示例**
**代码示例**

.. code-block:: python
#先创建一个LoDTensorArray,再在指定位置写入Tensor,然后从该位置读取Tensor
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)
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是shape为[3,2]的Tensor,将其写入空数组arr的下标10的位置,则arr的长度变为11
arr = fluid.layers.array_write(tmp, i, array=arr)
#读取arr的下标10的位置的数据
item = fluid.layers.array_read(arr, i)
#可以通过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)
**输出结果**

.. code-block:: python
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,
#输出了shape为[3,2]的Tensor
#dtype为对应C++数据类型,在不同环境下可能显示值不同,但本质一致
#例如:如果Tensor中数据类型是int64,则对应的C++数据类型为int64_t,所以dtype值为typeid(int64_t).name(),
# 其在MacOS下为'x',linux下为'l',Windows下为'__int64',都表示64位整型变量

0 comments on commit 607b83d

Please sign in to comment.