诡异错误笔记TypeError: An op outside of the function building code is being passed a Graph tensor.

本文主要是介绍诡异错误笔记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.的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/521996

相关文章

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

SpringBoot3匹配Mybatis3的错误与解决方案

《SpringBoot3匹配Mybatis3的错误与解决方案》文章指出SpringBoot3与MyBatis3兼容性问题,因未更新MyBatis-Plus依赖至SpringBoot3专用坐标,导致类冲... 目录SpringBoot3匹配MyBATis3的错误与解决mybatis在SpringBoot3如果

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

nginx配置错误日志的实现步骤

《nginx配置错误日志的实现步骤》配置nginx代理过程中,如果出现错误,需要看日志,可以把nginx日志配置出来,以便快速定位日志问题,下面就来介绍一下nginx配置错误日志的实现步骤,感兴趣的可... 目录前言nginx配置错误日志总结前言在配置nginx代理过程中,如果出现错误,需要看日志,可以把

Python错误AttributeError: 'NoneType' object has no attribute问题的彻底解决方法

《Python错误AttributeError:NoneTypeobjecthasnoattribute问题的彻底解决方法》在Python项目开发和调试过程中,经常会碰到这样一个异常信息... 目录问题背景与概述错误解读:AttributeError: 'NoneType' object has no at

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socket read timed out的问题

《如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socketreadtimedout的问题》:本文主要介绍解决Druid线程... 目录异常信息触发场景找到版本发布更新的说明从版本更新信息可以看到该默认逻辑已经去除总结异常信息触发场景复