詭異錯誤筆記TypeError: An op outside of the function building code is being passed a "Graph" tensor.

再進行SimpleRNNCell循環神經網絡學習過程中發現一個奇怪的bug,錯誤日誌如下:

 File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\execute.py", line 61, in quick_execute
    num_outputs)
TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: my_rnn/simple_rnn_cell/cond/Identity:0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "e:/python_project/tensor_test/tensor_advantage/sentiment_analysis_single_layer.py", line 111, 
in <module>
    main()
  File "e:/python_project/tensor_test/tensor_advantage/sentiment_analysis_single_layer.py", line 106, 
in main
    model.fit(db_train, epochs=epochs, validation_data=db_test)
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
    use_multiprocessing=use_multiprocessing)
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 342, in fit
    total_epochs=epochs)
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 128, in run_one_epoch
    batch_outs = execution_function(iterator)
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 98, in execution_function
    distributed_function(input_fn))
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 568, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\def_function.py", line 632, in _call
    return self._stateless_fn(*args, **kwds)
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py", line 2363, in __call__
    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py", line 1611, in _filtered_call
    self.captured_inputs)
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py", line 1692, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py", line 545, in call
    ctx=ctx)
thon\eager\execute.py", line 75, in quick_execute
    "tensors, but found {}".format(keras_symbolic_tensors))
tensorflow.python.eager.core._SymbolicException: Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'my_rnn/simple_rnn_cell/cond/Identity:0' shape=(None, 100) dtype=float32>, <tf.Tensor 'my_rnn/simple_rnn_cell_1/cond/Identity:0' shape=(100, 64) dtype=float32>]     

我在stackflow上也看到有人反映這個問題:

https://stackoverflow.com/questions/59403281/unable-to-use-mse-of-vgg-features-in-loss-function

 

In tensorflow 2.0, eager execution is enabled by default.

tensorflow2.0默認啓用Eager Execution

通過使用下列函數禁用eager_excution:

tf.compat.v1.disable_eager_execution()

我不太理解爲什麼禁用了就可以正常執行,也許是eager_excution有什麼Bug吧

 

另一種辦法是SimpleRNNCell初始化的時候放棄dropout參數,也可以解決這個問題

原來:

self.rnn_cell0 = layers.SimpleRNNCell(units, dropout=0.5)
self.rnn_cell1 = layers.SimpleRNNCell(units, dropout=0.5)

改爲:

self.rnn_cell0 = layers.SimpleRNNCell(units)
self.rnn_cell1 = layers.SimpleRNNCell(units)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章