在TensorFlow2.x中執行TensorFlow1.x代碼的靜態圖執行模式
改為圖執行模式

TensorFlow2雖然和TensorFlow1.x有較大差異,不能直接兼容,但實際上還是提供了對TensorFlow1.x的API支持
TensorFlow 2中執行或開發TensorFlow1.x代碼,可以做如下處理:
- 匯入TensorFlow時使用
import tensorflow.compat.v1 as tf
- 禁用即時執行模式
tf.disable_eager_execution()
簡單兩步即可
舉例
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
node1 = tf.constant(3.0)
node2 = tf.constant(4.0)
node3 = tf.add(node1,node2)
print(node3)
由于是圖執行模式,這時僅僅是建立了計算圖,但沒有執行
定義好計算圖后,需要建立一個Session,使用會話物件來實作執行圖的執行
sess = tf.Session()
print("node1:",sess.run(node1))
print("node2:",sess.run(node2))
print("node3:",sess.run(node3))
Session.close()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/266301.html
標籤:其他
上一篇:狂神 springMVC筆記完整版(同步B站)含SSM整合小案例!
下一篇:音視頻小白入門
