本文主要是介绍诡异错误笔记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_executenum_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.functiondef 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:0During 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 mainmodel.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 fituse_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 fittotal_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_epochbatch_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_functiondistributed_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 _callreturn 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-accessFile "C:\Users\Jame_Peng\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\eager\function.py", line 1611, in _filtered_callself.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_flatctx, 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 callctx=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)
三、 不采用cell的方式创建网络,而是采用Sequential来创建网络,因为看paper发现dropout=0.2可以提升训练效果
self.rnn = Sequential([layers.SimpleRNN(units,dropout=0.2,return_sequences=True,unroll=True),layers.SimpleRNN(units, dropout=0.2, unroll=True)
])
这篇关于诡异错误笔记TypeError: An op outside of the function building code is being passed a Graph tensor.的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!