Advertisement

tensorflow 2显示错误:模块'tensorflow'没有属性'Session'

阅读量:

问题识别与分析

AttributeError: module ‘tensorflow’ has no attribute ‘Session’

原因:

在TensorFlow 2版本中,tf.Session()这一接口已被移除。

解决方法概述

tf.Session() 需要调整为 tf.compat.v1.Session()
或选择将版本进行回退操作:pip install tensorflow==1.4

注意事项概述

在使用 tf.compat.v1.Session() 之前,应添加 tf.compat.v1.disable_eager_execution() 语句。

传统的 tensorflow 操作方式需要先构建计算图,随后启动会话,在该会话中张量按照图的结构进行流动和运算。而 tf 2.0 引入了 eager execution 这一特性,成为其最具代表性的改进之一,大幅简化了以往复杂的定义与计算流程。

eager execution 提供了两种模式:tf.compat.v1.enable_eager_execution() 和 tf.compat.v1.disable_eager_execution()。其中,默认启用的是第一种模式,因此若需调整,则需要进行相应更改。

eg.

复制代码
    import t

全部评论 (0)

还没有任何评论哟~