tensorflow学习1.3-创建会话,启动会话

发布于:2024-07-04 ⋅ 阅读:(16) ⋅ 点赞:(0)


在TensorFlow 1.x版本中, Session 会话是一个非常重要的概念。它提供了一个执行计算图(computation graph)的环境。TensorFlow 2.x 版本引入了Eager Execution模式,使得大多数操作立即执行,而不再需要显式的会话管理。但是,为了理解 TensorFlow 的基础,以及在某些情况下可能仍然需要使用的低级操作,我们还是有必要了解一下 TensorFlow 1.x 中的会话机制。

会话的由来与作用

由来

TensorFlow最初是由谷歌大脑团队开发的,用于大规模机器学习任务。最初的设计目标之一是能够高效地在分布式环境中执行计算图。为了实现这一点,TensorFlow引入了 Session 概念来管理和执行计算图。

作用

Session 的主要作用包括:

  1. 管理资源:分配和管理计算所需的资源,如GPU和内存。
  2. 执行计算图:具体执行计算图中的操作(ops),并返回结果。
  3. 控制生命周期:在会话的生命周期内,可以反复执行计算图的一部分或全部。

会话的定义与结构

在 TensorFlow 1.x 中,会话是通过 tf.Session 类定义的。其主要结构和用法如下:

定义
# 创建一个计算图
import tensorflow as tf

# 定义一个计算图节点
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a + b

# 创建一个会话
sess = tf.Session()

# 在会话中运行计算图
result = sess.run(c)
print(result)  # 输出:11.0

# 关闭会话
sess.close()

用法

基本用法
  1. 创建会话:可以通过 tf.Session() 创建一个会话对象。
  2. 执行计算:使用 sess.run() 方法执行计算图中的节点。
  3. 关闭会话:使用 sess.close() 关闭会话,释放资源。
上下文管理器

为了确保会话在使用后正确关闭,可以使用 Python 的上下文管理器(with 语句):

import tensorflow as tf

a = tf.constant(5.0)
b = tf.constant(6.0)
c = a + b

with tf.Session() as sess:
    result = sess.run(c)
    print(result)  # 输出:11.0

使用上下文管理器的好处是会在代码块执行完毕后自动关闭会话。

执行部分计算图

会话允许你执行计算图的一部分,这对于大型复杂的计算图尤其有用:

import tensorflow as tf

a = tf.constant(5.0)
b = tf.constant(6.0)
c = a + b
d = c * 2

with tf.Session() as sess:
    # 只执行c节点
    result_c = sess.run(c)
    print(result_c)  # 输出:11.0

    # 执行d节点,TensorFlow会自动计算c节点的值
    result_d = sess.run(d)
    print(result_d)  # 输出:22.0
获取多个结果

可以在一次会话运行中获取多个节点的结果:

import tensorflow as tf

a = tf.constant(5.0)
b = tf.constant(6.0)
c = a + b
d = c * 2

with tf.Session() as sess:
    result_c, result_d = sess.run([c, d])
    print(result_c)  # 输出:11.0
    print(result_d)  # 输出:22.0

总结

Session 会话是 TensorFlow 1.x 中用于执行计算图的环境,通过会话可以管理资源、执行计算图并获取结果。在 TensorFlow 2.x 中,引入了更易用的 Eager Execution 模式,使得大部分操作可以立即执行,而不需要显式管理会话。然而,了解 Session 的概念对于理解 TensorFlow 的设计原理和使用低级 API 仍然是有帮助的。

练习代码

import tensorflow as tf



# 创建一个变量
m1 = tf.constant([[3,3]])

#创建一个常量
m2=tf.constant([[2],[3]])

#矩阵乘法 OP
product = tf.matmul(m1,m2)

print(product)

#定义会话
sess = tf.Session()

#调用sess中的run方法执行矩阵乘法op
result = sess.run(product)
print(result)
sess.close()

with tf.Session() as sess:
    # 调用sess中的run方法来执行矩阵惩罚op
    result = sess.run(product)
    print(result)

报错

在我的环境中运行会遇见以下报错:
sess = tf.Session() AttributeError: module 'tensorflow' has no attribute 'Session'. Did you mean: 'version'?

原因:

在TensorFlow 2.x中,Session已经被弃用了,取而代之的是更加直观和易用的Eager Execution模式。Eager Execution使得TensorFlow操作立即执行,并返回结果,而不是构建一个计算图,然后再通过会话来运行这些图。

尽管如此,如果你确实需要使用与TensorFlow 1.x兼容的功能,比如在某些情况下必须要用到计算图和会话,可以通过在TensorFlow 2.x中启用兼容模式来使用这些功能。

TensorFlow 2.x中的Eager Execution

默认情况下,TensorFlow 2.x启用了Eager Execution模式,这使得编写和调试代码更加直观。下面是一个简单的例子:

import tensorflow as tf

# Eager Execution模式下直接计算
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a + b
print(c)  # 输出:tf.Tensor(11.0, shape=(), dtype=float32)

使用兼容模式来启用Session

如果你需要在TensorFlow 2.x中使用会话和计算图,可以启用兼容模式:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

# 创建一个计算图
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a + b

# 创建一个会话
sess = tf.Session()

# 在会话中运行计算图
result = sess.run(c)
print(result)  # 输出:11.0

# 关闭会话
sess.close()

Eager Execution和计算图的混合使用

在某些复杂场景中,你可能需要混合使用Eager Execution和计算图。这种情况下,你可以使用tf.function来定义需要构建为计算图的部分代码:

import tensorflow as tf

# Eager Execution模式下直接计算
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a + b
print(c)  # 输出:tf.Tensor(11.0, shape=(), dtype=float32)

# 使用tf.function将代码转换为计算图
@tf.function
def compute():
    d = a * b
    return d

result = compute()
print(result)  # 输出:tf.Tensor(30.0, shape=(), dtype=float32)

总结

在TensorFlow 2.x中,建议尽量使用Eager Execution模式,因为它更加直观和易于调试。如果你必须使用与TensorFlow 1.x兼容的功能,可以通过启用兼容模式来使用会话和计算图。在大多数情况下,Eager Execution模式已经足够强大,并且能够满足大多数深度学习任务的需求。

修改

在TensorFlow 2.x中,推荐使用Eager Execution模式,因为它更加直观和易于调试。以下是将代码转换为Eager Execution模式的版本:

import tensorflow as tf

# 确保Eager Execution模式已启用
tf.config.run_functions_eagerly(True)

# 创建一个变量
m1 = tf.constant([[3, 3]])

# 创建一个常量
m2 = tf.constant([[2], [3]])

# 矩阵乘法 OP
product = tf.matmul(m1, m2)

# 立即执行操作并返回结果
print(product.numpy())

# 在Eager Execution模式下,不需要显式定义会话
# 结果已经通过Eager Execution模式返回
result = product.numpy()
print(result)

在这个代码中,我们不需要显式定义会话。Eager Execution模式使得TensorFlow操作立即执行并返回结果,这样代码更加直观和易于调试。如果需要与TensorFlow 1.x兼容的功能,可以启用兼容模式,但在大多数情况下,Eager Execution模式已经足够强大,并且能够满足大多数深度学习任务的需求。

在TensorFlow 2.x中,直接使用Eager Execution模式会避免很多TensorFlow 1.x中的复杂性和问题。如果需要使用与TensorFlow 1.x兼容的功能,确保在兼容模式下正确地定义和使用计算图。

这里是修正后的代码,确保兼容模式下操作添加到计算图中:

import tensorflow as tf

# 使用兼容模式
tf.compat.v1.disable_eager_execution()

# 创建一个变量
m1 = tf.compat.v1.constant([[3, 3]])

# 创建一个常量
m2 = tf.compat.v1.constant([[2], [3]])

# 矩阵乘法 OP
product = tf.compat.v1.matmul(m1, m2)

# 定义会话
sess = tf.compat.v1.Session()

# 调用sess中的run方法执行矩阵乘法op
result = sess.run(product)
print(result)
sess.close()

# 使用上下文管理器定义会话
with tf.compat.v1.Session() as sess:
    # 调用sess中的run方法来执行矩阵乘法op
    result = sess.run(product)
    print(result)

在这个代码中,使用了 tf.compat.v1.disable_eager_execution() 来禁用Eager Execution,并确保所有操作都在兼容模式下添加到计算图中。然后,使用 tf.compat.v1.Session 来运行这些操作。这种方式能够确保在TensorFlow 2.x中使用与1.x兼容的会话模式。

使用上下文管理器定义会话

# 使用上下文管理器定义会话
with tf.compat.v1.Session() as sess:
    # 调用sess中的run方法来执行矩阵乘法op
    result = sess.run(product)
    print(result)
  • with tf.compat.v1.Session() as sess::使用 with 关键字创建一个 tf.compat.v1.Session() 对象,并将其赋值给 sess 变量。tf.compat.v1.Session() 是 TensorFlow 2.x 中兼容 TensorFlow 1.x 的会话对象。

  • sess.run(product):在会话中调用 run 方法来执行之前定义的矩阵乘法操作 product。这一步实际上会启动 TensorFlow 的计算图,并执行相应的计算。

  • print(result):打印执行结果 result,即矩阵乘法的结果。

上下文管理器的作用
使用 with 语句块可以确保在进入 with 代码块时会话 sess 被创建,并在代码块执行结束时自动关闭。这种方式避免了手动调用 sess.close() 来关闭会话,同时也确保了资源的正确释放,特别是在 TensorFlow 中,关闭会话能够释放计算资源和内存。

总结来说,这段代码的目的是使用 TensorFlow 2.x 的兼容模式创建一个会话,并在会话中执行矩阵乘法操作,最后打印执行结果。使用上下文管理器 with 确保了会话在使用完毕后正确关闭,避免了资源泄露和错误的释放。