tensorflow2.0 GPU 版本安装测试教程及新特性初探
安裝與測試
TensorFlow2.0安裝:
pip install tensorflow-gpu==2.2.0 -i https://pypi.douban.com/simple/ conda install cudnn=7.6.0 # conda install cudatoolkit=10.0.130 # (可選)TensorFlow2.0的GPU版本測試代碼如下:
import tensorflow as tfprint('GPU', tf.test.is_gpu_available())a = tf.constant(2.0) b = tf.constant(4.0) print(a + b)注意:如果是第一次使用 tensorflow2.0,會在輸出 Adding visible gpu devices: 0 之后卡住一陣子,等幾分鐘就好了,下次運行就正常了。
結果如下就證明 tensorflow-gpu 安裝成功了
GPU True
tf.Tensor(6.0, shape=(), dtype=float32)
1.X 和 2.X 版本的差異
Eager Execution
細心的你會發現,在 tensorflow2.0 版本中,居然可以直接輸出張量的計算結果,如果是以前的1.x 版本,執行同樣的代碼,你得到的輸出是:
GPU True
Tensor(“add:0”, shape=(), dtype=float32)
在1.x 版本中,如果你想要得到真實的計算結果,需要初始化全局變量 → 建立會話 → 執行計算,最終才能打印出張量的運算結果:
import tensorflow as tf sess = tf.Session() a = tf.constant(2.0) b = tf.constant(4.0) print(sess.run(a + b))這是 TensorFlow 2.0 帶來的最大改變之一,他將 1.x 的 Graph Execution(圖與會話機制)更改為 Eager Execution(動態圖機制)。帶來的最直觀的好處是:不再需要手動管理圖和會話。例如,現在使用示例張量進行數學計算,可以像 Python 一樣直接相加。在 TensorFlow 2.0 中,Eager Execution 模式是默認開啟的,這意味著 TensorFlow 代碼被定義后會立即運行,而不是先將節點和邊緣添加一個圖上,稍后再在一個會話中運行。
對于定義的張量,還可以直接通過 .numpy() 方法輸出 numpy 數組:
c = tf.Variable([[1, 2], [3, 4]]) print('c:') print(c) print('c.numpy():') print(c.numpy())輸出:
c: <tf.Variable 'Variable:0' shape=(2, 2) dtype=int32, numpy= array([[1, 2],[3, 4]])> c.numpy(): [[1 2][3 4]]雖然程序是可以動態執行了,但是梯度計算怎么辦?在TensorFlow 1.x靜態圖時代,我們知道每個靜態圖都有兩部分,一部分是前向圖,另一部分是反向圖。反向圖就是用來計算梯度的,用在整個訓練過程中。而TensorFlow 2.0默認是eager模式,每行代碼順序執行,沒有了構建圖的過程,但是可以通過 tf.GradientTape api 來實現自動求導功能。具體內容不展開講,可以參考下面鏈接中的講解:
tf.GradientTape詳解:梯度求解利器
TensorFlow2.0教程-自動求導
TensorFlow 2.0引入的eager提高了代碼的簡潔性,而且更容易debug。但是對于性能來說,eager執行相比Graph模式會有一定的損失。但是好在,TensorFlow 2.0引入了tf.function和AutoGraph來縮小eager執行和Graph模式的性能差距,其核心是將一系列的Python語法轉化為高性能的graph操作。
AutoGraph
AutoGraph 主要是可以將一些常用的Python代碼轉化為TensorFlow支持的代碼,例如:
def square_if_positive(x):if x > 0:x = x * xelse:x = 0.0return x# eager 模式 print('results: %2.2f, %2.2f' % (square_if_positive(tf.constant(8.0)),square_if_positive(tf.constant(-9.0))))輸出:
results: 64.00, 0.00
上面我們定義了一個 square_if_positive 函數,它內部使用的 Python 的原生的if語法,對于TensorFlow 2.0的eager模式,這是沒有問題的。但是如果你需要在 1.x 版本執行,代碼就是這樣的:
# graph 模式 tf_square_if_positive = tf.autograph.to_graph(square_if_positive)with tf.Graph().as_default():# The result works like a regular op: takes tensors in, returns tensors.# You can inspect the graph using tf.get_default_graph().as_graph_def()g_out1 = tf_square_if_positive(tf.constant( 9.0))g_out2 = tf_square_if_positive(tf.constant(-9.0))with tf.compat.v1.Session() as sess:print('Graph results: %2.2f, %2.2f\n' % (sess.run(g_out1), sess.run(g_out2)))輸出:
results: 81.00, 0.00
大家要注意eager模式和Graph模式的差異,盡管結果是一樣的,但是Graph模式更高效。從本質上講,AutoGraph是將Python代碼轉為TensorFlow原生的代碼。
tf.keras
TensorFlow 2.0全面keras化:如果你想使用高級的layers,只能選擇keras。TensorFlow 1.x 存在 tf.layers以及tf.contrib.slim等高級API來創建模型,但是2.0僅僅支持tf.keras.layers。
參考文章:
Tensorflow 2.0到底好在哪里?
TensorFlow 2.0 新增變化特性
【060】tensorflow2.0 與tensorflow1.0 的性能區別和遷移
總結
以上是生活随笔為你收集整理的tensorflow2.0 GPU 版本安装测试教程及新特性初探的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Flash如何制作旋转的三棱锥
- 下一篇: TensorFlow 2.0 快速上手教