title: How to create a list of tensors and use tf.stack in tensorflow2 in a for loop
body: <p>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.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>but I get the following error like this:</p>
  
  <pre><code>Traceback (most recent call last):
    File "test.py", line 10, in &lt;module&gt;
      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).
  </code></pre>
  
  <p>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?</p>