在使用tensorflow的过程中,尤其是在使用tf2.0的过程中(tf2.0真的不好用,而且ubuntu上的GPU版还很难装。。),与keras混用,经常会出现各种奇奇怪怪的错,大多是版本不对应的问题,还好在网上一般能找到答案,其中就有一个错叫作:‘tensorflow’
has no attribute ‘get_default_graph’。

问题描述

在这里插入图片描述
在使用tensorflow2.0时,遇到了这个问题:
AttributeError: module ‘tensorflow’ has no attribute ‘get_default_graph’
这个报错的意思是:tensorflow模块没有get_default_graph属性
错误原因
这是由于Keras API(https://keras.io/)有多个实现,包括原始和参考实现(https://github.com/keras-team/keras),还有各种其他实现,包括tf.keras,它是TensorFlow的一部分。
由于TensorFlow 2默认为急切执行,因此Keras需要进行一些更改才能与之兼容
解决方法

  1. 方法一:
    将参考实现与TensorFlow后端一起使用。但是,此实现尚未更新以支持TensorFlow 2(截至2019年6月)。
  2. 方法二:
    使用TensorFlow的实现,tf.keras。这个适用于TF 2。
    例如你需要使用tf.keras,必须确保使用正确的导入:
from tensorflow import keras
而不是直接使用:import keras

 
 
 
 

    同样,在要使用keras下的其他模块时:

    from tensorflow.keras import layers
    而不是使用 from keras import layers
    
     
     
     
     

      希望这些可以帮助到你


      这个人写的很好了,这确实是一个解决办法,不过还有一种解决办法就是重新去安装对应版本keras与tensorflow。或者干脆直接不要混用吧。

      这两个网站不错:
      https://pypi.org/
      https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml
      装东西要形成自己的直觉,判断哪里会有问题,应该怎么可能解决这个问题,多试多查吧。。

      在安装过程中,有多种安装方式可以选择,这里介绍如何在python中 pip 安装 删除缓存(cache),不使用缓存。

      https://www.csdn.net/jiaocheng/1/1/1

      pip删除缓存 安装操作:

       pip --no-cache-dir install  包名
      If using pip 6.0 or newer, try adding the --no-cache-dir option.
      
      If using pip older than pip 6.0, upgrade it with pip install -U pip
      

      参考:
      https://blog.csdn.net/public669/article/details/99686151
      https://blog.csdn.net/u014466109/article/details/88877321
      https://www.cnblogs.com/devilyouwei/p/10617367.html
      https://blog.csdn.net/w2486696/article/details/101165558
      https://www.paddlepaddle.org.cn/install/quick
      https://blog.csdn.net/ljl86400/article/details/80051507
      GitHub 漫游指南

      Logo

      鸿蒙生态一站式服务平台。

      更多推荐