array_write

paddle.fluid.layers.array_write(x, i, array=None)[source]

This OP writes the input x into the i-th position of the array 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 array_read OP.

Parameters
  • 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

The input array after x is written into.

Return type

Variable

Examples

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.