概述
目录
1. module ‘tensorflow‘ has no attribute ‘Session‘
2. The Session graph is empty. Add operations to the graph before calling run().
3. AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
4. module 'tensorboard.summary._tf.summary' has no attribute 'FileWriter'
1. module ‘tensorflow‘ has no attribute ‘Session‘
指定一个会话来运行tensorflow程序,在使用tensorflow1.x版本中用tensorflow.Session即可
但当我的库版本升级到2.x之后,就会出现标题式报错,于是我去查看了tensorflow库的源码,原来是这个Session类被迁移到了另一个模块中,而后函数下面给出的教程中也出现了新的改变,可以看到tf.Session()被改成了tf.compat.v1.Session() /(ㄒoㄒ)/~~
所以把tf.Session改为tf.compat.v1.Session即可
2. The Session graph is empty. Add operations to the graph before calling run().
如果改完之后又出现了这个报错,其实还是版本的问题,需要再添加一行代码
tf.compat.v1.disable_eager_execution()
来看一下官方文档的解释
This function can only be called before any Graphs, Ops, or Tensors have been created. It can be used at the beginning of the program for complex migration projects from TensorFlow 1.x to 2.x.
可以看到官方的意思是,这个函数放到项目开头是为了把1.x版本迁移到2.x版本。而且这行代码按照文档解释,要添加在图的操作之前,放到后面的话还是会继续报错的,最好是直接放到最开头的地方
3. AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
将 tf.get_default_graph()改为
tf.compat.v1.get_default_graph()
4. module 'tensorboard.summary._tf.summary' has no attribute 'FileWriter'
将 tf.summary.()改为:
tf.compat.v1.summary.FileWriter
 ̄へ ̄,更新好麻烦,每次一更新就要有一些奇奇怪怪的改动,虽然可以理解是做了一个优化/(ㄒoㄒ)/~~
最后
以上就是冷酷刺猬为你收集整理的module ‘tensorflow‘ has no attribute xxx tensorflow版本问题类导致API函数的报错的全部内容,希望文章能够帮你解决module ‘tensorflow‘ has no attribute xxx tensorflow版本问题类导致API函数的报错所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复