tf.TensorArray

View source on GitHub

Class wrapping dynamic-sized, per-time-step, write-once Tensor arrays.

tf.TensorArray(
    dtype, size=None, dynamic_size=None, clear_after_read=None,
    tensor_array_name=None, handle=None, flow=None, infer_shape=True,
    element_shape=None, colocate_with_first_write_call=True, name=None
)

This class is meant to be used with dynamic iteration primitives such as while_loop and map_fn. It supports gradient back-propagation via special "flow" control flow dependencies.

Example 1: plain reading and writing.

ta = tf.TensorArray(tf.float32, size=0, dynamic_size=True, clear_after_read=False) ta = ta.write(0, 10) ta = ta.write(1, 20) ta = ta.write(2, 30)

ta.read(0) ta.read(1) ta.read(2) ta.stack()

Example 2: Fibonacci sequence algorithm that writes in a loop then returns.

@tf.function ... def fibonacci(n): ... ta = tf.TensorArray(tf.float32, size=0, dynamic_size=True) ... ta = ta.unstack([0., 1.]) ... ... for i in range(2, n): ... ta = ta.write(i, ta.read(i - 1) + ta.read(i - 2)) ... ... return ta.stack()

fibonacci(7)

Example 3: A simple loop interacting with a tf.Variable.

v = tf.Variable(1)

@tf.function ... def f(x): ... ta = tf.TensorArray(tf.int32, size=0, dynamic_size=True) ... ... for i in tf.range(x): ... v.assign_add(i) ... ta = ta.write(i, v) ... ... return ta.stack()

f(5)

Args:

Attributes:

Raises:

Methods

close

View source

close(
    name=None
)

Close the current TensorArray.

NOTE The output of this function should be used. If it is not, a warning will be logged or an error may be raised. To mark the output as used, call its .mark_used() method.

concat

View source

concat(
    name=None
)

Return the values in the TensorArray as a concatenated Tensor.

All of the values must have been written, their ranks must match, and and their shapes must all match for all dimensions except the first.

Args:

Returns:

All the tensors in the TensorArray concatenated into one tensor.

gather

View source

gather(
    indices, name=None
)

Return selected values in the TensorArray as a packed Tensor.

All of selected values must have been written and their shapes must all match.

Args:

Returns:

The tensors in the TensorArray selected by indices, packed into one tensor.

grad

View source

grad(
    source, flow=None, name=None
)

identity

View source

identity()

Returns a TensorArray with the same content and properties.

Returns:

A new TensorArray object with flow that ensures the control dependencies from the contexts will become control dependencies for writes, reads, etc. Use this object all for subsequent operations.

read

View source

read(
    index, name=None
)

Read the value at location index in the TensorArray.

Args:

Returns:

The tensor at index index.

scatter

View source

scatter(
    indices, value, name=None
)

Scatter the values of a Tensor in specific indices of a TensorArray.

Args: indices: A 1-D Tensor taking values in [0, max_value). If the TensorArray is not dynamic, max_value=size(). value: (N+1)-D. Tensor of type dtype. The Tensor to unpack. name: A name for the operation (optional).

Returns: A new TensorArray object with flow that ensures the scatter occurs. Use this object all for subsequent operations.

Raises: ValueError: if the shape inference fails.

NOTE The output of this function should be used. If it is not, a warning will be logged or an error may be raised. To mark the output as used, call its .mark_used() method.

size

View source

size(
    name=None
)

Return the size of the TensorArray.

split

View source

split(
    value, lengths, name=None
)

Split the values of a Tensor into the TensorArray.

Args: value: (N+1)-D. Tensor of type dtype. The Tensor to split. lengths: 1-D. int32 vector with the lengths to use when splitting value along its first dimension. name: A name for the operation (optional).

Returns: A new TensorArray object with flow that ensures the split occurs. Use this object all for subsequent operations.

Raises: ValueError: if the shape inference fails.

NOTE The output of this function should be used. If it is not, a warning will be logged or an error may be raised. To mark the output as used, call its .mark_used() method.

stack

View source

stack(
    name=None
)

Return the values in the TensorArray as a stacked Tensor.

All of the values must have been written and their shapes must all match. If input shapes have rank-R, then output shape will have rank-(R+1).

Args:

Returns:

All the tensors in the TensorArray stacked into one tensor.

unstack

View source

unstack(
    value, name=None
)

Unstack the values of a Tensor in the TensorArray.

If input value shapes have rank-R, then the output TensorArray will contain elements whose shapes are rank-(R-1).

Args: value: (N+1)-D. Tensor of type dtype. The Tensor to unstack. name: A name for the operation (optional).

Returns: A new TensorArray object with flow that ensures the unstack occurs. Use this object all for subsequent operations.

Raises: ValueError: if the shape inference fails.

NOTE The output of this function should be used. If it is not, a warning will be logged or an error may be raised. To mark the output as used, call its .mark_used() method.

write

View source

write(
    index, value, name=None
)

Write value into index index of the TensorArray.

Args: index: 0-D. int32 scalar with the index to write to. value: N-D. Tensor of type dtype. The Tensor to write to this index. name: A name for the operation (optional).

Returns: A new TensorArray object with flow that ensures the write occurs. Use this object all for subsequent operations.

Raises: ValueError: if there are more writers than specified.

NOTE The output of this function should be used. If it is not, a warning will be logged or an error may be raised. To mark the output as used, call its .mark_used() method.