Этот код работает: < /p>
Код: Выделить всё
@tf.function
def test(inputs):
one_array = tf.TensorArray(tf.float32, size=tf.shape(inputs)[0])
for step in tf.range(tf.shape(inputs)[0]):
one_array = one_array.write(step, inputs[step])
return one_array.stack()
dummy_input = tf.random.normal([5])
test(dummy_input)
< /code>
Однако это не: < /p>
@tf.function
def test2(inputs):
n_layers = 2 # number doesn't matter
arrays = [tf.TensorArray(tf.float32, size=tf.shape(inputs)[0]) for _ in range(n_layers)]
for step in tf.range(tf.shape(inputs)[0]):
for ind in range(n_layers):
arrays[ind] = arrays[ind].write(step, inputs[step])
for ind in range(n_layers):
arrays[ind] = arrays[ind].stack()
return arrays # returning this directly without the stack() in the line above also crashes
test2(dummy_input)
< /code>
повышает: < /p>
---------------------------------------------------------------------------
InaccessibleTensorError Traceback (most recent call last)
Cell In[99], line 12
9 arrays[ind] = arrays[ind].stack()
10 return arrays
---> 12 test2(dummy_input)
File /project/jens/tf311/lib/python3.11/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback..error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
File /project/jens/tf311/lib/python3.11/site-packages/tensorflow/python/eager/polymorphic_function/autograph_util.py:52, in py_func_from_autograph..autograph_handler(*args, **kwargs)
50 except Exception as e: # pylint:disable=broad-except
51 if hasattr(e, "ag_error_metadata"):
---> 52 raise e.ag_error_metadata.to_exception(e)
53 else:
54 raise
InaccessibleTensorError: in user code:
File "/tmp/ipykernel_96618/173506222.py", line 9, in test2 *
arrays[ind] = arrays[ind].stack()
File "/project/jens/tf311/lib/python3.11/site-packages/tensorflow/core/function/capture/capture_container.py", line 144, in capture_by_value
graph._validate_in_scope(tensor) # pylint: disable=protected-access
InaccessibleTensorError: is out of scope and cannot be used here. Use return values, explicit Python locals or TensorFlow collections to access it.
Please see https://www.tensorflow.org/guide/function#all_outputs_of_a_tffunction_must_be_return_values for more information.
was defined here:
File "", line 198, in _run_module_as_main
File "", line 88, in _run_code
File "/project/jens/tf311/lib/python3.11/site-packages/ipykernel_launcher.py", line 17, in
File "/project/jens/tf311/lib/python3.11/site-packages/traitlets/config/application.py", line 1075, in launch_instance
File "/project/jens/tf311/lib/python3.11/site-packages/ipykernel/kernelapp.py", line 739, in start
File "/project/jens/tf311/lib/python3.11/site-packages/tornado/platform/asyncio.py", line 205, in start
File "/usr/lib/python3.11/asyncio/base_events.py", line 607, in run_forever
File "/usr/lib/python3.11/asyncio/base_events.py", line 1922, in _run_once
File "/usr/lib/python3.11/asyncio/events.py", line 80, in _run
File "/project/jens/tf311/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 542, in dispatch_queue
File "/project/jens/tf311/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 531, in process_one
File "/project/jens/tf311/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 437, in dispatch_shell
File "/project/jens/tf311/lib/python3.11/site-packages/ipykernel/ipkernel.py", line 359, in execute_request
File "/project/jens/tf311/lib/python3.11/site-packages/ipykernel/kernelbase.py", line 775, in execute_request
File "/project/jens/tf311/lib/python3.11/site-packages/ipykernel/ipkernel.py", line 446, in do_execute
File "/project/jens/tf311/lib/python3.11/site-packages/ipykernel/zmqshell.py", line 549, in run_cell
File "/project/jens/tf311/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3051, in run_cell
File "/project/jens/tf311/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3106, in _run_cell
File "/project/jens/tf311/lib/python3.11/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
File "/project/jens/tf311/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3311, in run_cell_async
File "/project/jens/tf311/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3493, in run_ast_nodes
File "/project/jens/tf311/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3553, in run_code
File "/tmp/ipykernel_96618/173506222.py", line 12, in
File "/tmp/ipykernel_96618/173506222.py", line 5, in test2
File "/tmp/ipykernel_96618/173506222.py", line 6, in test2
File "/tmp/ipykernel_96618/173506222.py", line 7, in test2
The tensor cannot be accessed from FuncGraph(name=test2, id=139903595515616), because it was defined in FuncGraph(name=while_body_262957, id=139903595521664), which is out of scope.
< /code>
Я не уверен, что здесь делать. Использование диапазона i Может иметь возможность сложить все в один большой TensorArray , а затем разобрать его в компонентах для каждого слоя, но я хотел посмотреть, есть ли что-то здесь, сначала мне не хватает.
Примечание: я прочитал ссылку, приведенную в трассировке стека; это было не очень полезно.
Подробнее здесь: https://stackoverflow.com/questions/793 ... -in-a-list