tensorflow學習之 Eager execution


  首先tensorflow本身就是一個聲明式的編程。而不是命令式的編程。

 

 

 

    1、聲明式的編程可以簡單理解為先統一列出計算形式或者是表達式,然后最終在會話中進行計算。

    2、而命令式就像是python本身就是。有初始值,再寫出計算式的時候,運行到這一步其實就相當於已經的除了結果。

    下面我們可以用斐波那契數列舉例:

 

 

 

  「Eager Execution」,它是一個命令式、由運行定義的接口,一旦從 Python 被調用,其操作立即被執行。

 

  在 TensorFlow 1.X 版本中, 必須 在導入 TensorFlow 庫后調用tf.enable_eager_execution()函數以啟用 Eager Execution 模式。

import tensorflow as tf
#因為我這里是2.0的版本,默認是命令式編程,所以我們需要手動進行取消
tf.compat.v1.disable_eager_execution()
state = tf.Variable(tf.zeros([4,5]))
with tf.compat.v1.Session() as sess:
    sess.run(tf.compat.v1.global_variables_initializer())
    print(sess.run(state))

 

 

 

 


  在 TensorFlow 2.0 版本中,Eager Execution 模式將成為默認模式,無需額外調用 tf.enable_eager_execution() 函數(不過若要關閉 Eager Execution,則需調用 tf.compat.v1.disable_eager_execution() 函數)。  

import tensorflow as tf
state = tf.Variable(tf.zeros([4,5]))
print(state)

 

 

 

 

 

 

 

  Eager Execution 有啥優點?

  1、快速調試即刻的運行錯誤並通過 Python 工具進行整合

 

  2、借助易於使用的 Python 控制流支持動態模型

 

  3、為自定義和高階梯度提供強大支持

 

  4、適用於幾乎所有可用的 TensorFlow 運算

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM