I-PING Ou
I-PING Ou

Reputation: 507

How to create a list of tensors and use tf.stack in tensorflow2 in a for loop

I am trying to create a list of tensors and stack them together using for loop in tensorflow2. I created a test example and tried it like the following.

import tensorflow as tf

@tf.function
def test(x):
    tensor_list = []
    for i in tf.range(x):
        tensor_list.append(tf.ones(4)*tf.cast(i, tf.float32))
    return  tf.stack(tensor_list)

result = test(5)
print(result)

but I get the following error like this:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    result = test(5)
  File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 457, in __call__
    result = self._call(*args, **kwds)
  File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 503, in _call
    self._initialize(args, kwds, add_initializers_to=initializer_map)
  File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 408, in _initialize
    *args, **kwds))
  File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1848, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2150, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2041, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 915, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 358, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "/root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 905, in wrapper
    raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.InaccessibleTensorError: in converted code:

    test.py:8 test  *
        return  tf.stack(tensor_list)
    /root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/util/dispatch.py:180 wrapper
        return target(*args, **kwargs)
    /root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py:1165 stack
        return gen_array_ops.pack(values, axis=axis, name=name)
    /root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_array_ops.py:6304 pack
        "Pack", values=values, axis=axis, name=name)
    /root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/op_def_library.py:793 _apply_op_helper
        op_def=op_def)
    /root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py:544 create_op
        inp = self.capture(inp)
    /root/.pyenv/versions/summarization-abstractive/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py:603 capture
        % (tensor, tensor.graph, self))

    InaccessibleTensorError: The tensor 'Tensor("mul:0", shape=(4,), dtype=float32)' cannot be accessed here: it is defined in another function or code block. Use return values, explicit Python locals or TensorFlow collections to access it. Defined in: FuncGraph(name=while_body_8, id=139870442952744); accessed from: FuncGraph(name=test, id=139870626510608).

Does anyone know what am I doing wrong? How do I create a list of tensor and stack them together with for loop in tensorflow 2?

Upvotes: 6

Views: 6123

Answers (2)

hello_world
hello_world

Reputation: 31

use tf.TensorArray instead of list

Upvotes: 3

Lau
Lau

Reputation: 1465

Looping over a tensor should be generally done by using ´tf.map_fn´. Here is a solution that works:

import tensorflow as tf
import numpy as np

@tf.function
def test(x):

    tensor_list = tf.map_fn(lambda inp: tf.ones(4)*tf.cast(inp, tf.float32), x, dtype=tf.dtypes.float32)

    return  tf.stack(tensor_list)

result = test(np.arange(5))
print(result)

However, you have to feed a real array in your test() function, but alternatively, you can call tf.range() inside the tf.function to convert the scalar into a tensor.

Upvotes: 4

Related Questions