诡异错误笔记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 
    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 [, ]     

我在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)
])

 

你可能感兴趣的:(TensorFlow2.1,tensorflow,神经网络,机器学习,深度学习,python)