tensorflow學習1.3-創建會話,啟動會話
- 會話的由來與作用
- 由來
- 作用
- 會話的定義與結構
- 定義
- 用法
- 基本用法
- 上下文管理器
- 執行部分計算圖
- 獲取多個結果
- 總結
- 練習代碼
- 報錯
- 原因:
- TensorFlow 2.x中的Eager Execution
- 使用兼容模式來啟用Session
- Eager Execution和計算圖的混合使用
- 總結
- 修改
在TensorFlow 1.x版本中,
Session
會話是一個非常重要的概念。它提供了一個執行計算圖(computation graph)的環境。TensorFlow 2.x 版本引入了Eager Execution模式,使得大多數操作立即執行,而不再需要顯式的會話管理。但是,為了理解 TensorFlow 的基礎,以及在某些情況下可能仍然需要使用的低級操作,我們還是有必要了解一下 TensorFlow 1.x 中的會話機制。
會話的由來與作用
由來
TensorFlow最初是由谷歌大腦團隊開發的,用于大規模機器學習任務。最初的設計目標之一是能夠高效地在分布式環境中執行計算圖。為了實現這一點,TensorFlow引入了 Session
概念來管理和執行計算圖。
作用
Session
的主要作用包括:
- 管理資源:分配和管理計算所需的資源,如GPU和內存。
- 執行計算圖:具體執行計算圖中的操作(ops),并返回結果。
- 控制生命周期:在會話的生命周期內,可以反復執行計算圖的一部分或全部。
會話的定義與結構
在 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()
用法
基本用法
- 創建會話:可以通過
tf.Session()
創建一個會話對象。 - 執行計算:使用
sess.run()
方法執行計算圖中的節點。 - 關閉會話:使用
sess.close()
關閉會話,釋放資源。
上下文管理器
為了確保會話在使用后正確關閉,可以使用 Python 的上下文管理器(with
語句):
import tensorflow as tfa = tf.constant(5.0)
b = tf.constant(6.0)
c = a + bwith tf.Session() as sess:result = sess.run(c)print(result) # 輸出:11.0
使用上下文管理器的好處是會在代碼塊執行完畢后自動關閉會話。
執行部分計算圖
會話允許你執行計算圖的一部分,這對于大型復雜的計算圖尤其有用:
import tensorflow as tfa = tf.constant(5.0)
b = tf.constant(6.0)
c = a + b
d = c * 2with 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 tfa = tf.constant(5.0)
b = tf.constant(6.0)
c = a + b
d = c * 2with tf.Session() as sess:result_c, result_d = sess.run([c, d])print(result_c) # 輸出:11.0print(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方法來執行矩陣懲罰opresult = 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 * breturn dresult = 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方法來執行矩陣乘法opresult = 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方法來執行矩陣乘法opresult = 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
確保了會話在使用完畢后正確關閉,避免了資源泄露和錯誤的釋放。