小白學(xué)習(xí)tensorflow教程】三、TF2新特性@tf.function和AutoGraph

      網(wǎng)友投稿 1628 2022-05-30

      @Author:Runsen

      19年接觸Tensorflow1.X,看過1.X的官方文檔、到19年暑假的時(shí)候,我記得tensorflow2的發(fā)布。tensorflow2拋棄了tf.seesion,tf.placeholder。

      AutoGraph 是 Tensorflow 2.0 新功能之一。

      function or seesion

      在tensorflow1.X,計(jì)算任何東西,必須在會話中啟動圖。Tensorflow 最大的思想是將所有的數(shù)值計(jì)算都表達(dá)為一個(gè)計(jì)算圖。

      下面的代碼在1.X更常用。唯一的區(qū)別是不需要在最后關(guān)閉會話,因?yàn)樗鼤詣雨P(guān)閉。

      with tf.Session() as sess: print(sess.run(c))

      1

      2

      但是到了tensorflow2,沒有了tf.seesion,

      Tensorflow 2.0 的主要變化之一是刪除了tf.Session對象。

      這種變化迫使用戶以更好的方式組織代碼:不再tf.Session需要傳遞對象,而只是可以通過簡單的修飾加速的 Python 函數(shù)。

      請參考:官方文檔 Functions,而不是 Sessions

      為了在 Tensorflow 2.0 中定義圖形,我們需要定義一個(gè) Python 函數(shù)并用@tf.function.

      import tensorflow as tf @tf.function def compute_z1(x, y): return tf.add(x, y) @tf.function def compute_z0(x): return compute_z1(x, tf.square(x)) z0 = compute_z0(2.) z1 = compute_z1(2., 2.) print(z0,z1) # tf.Tensor(6.0, shape=(), dtype=float32) tf.Tensor(4.0, shape=(), dtype=float32)

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      【小白學(xué)習(xí)tensorflow教程】三、TF2新特性@tf.function和AutoGraph

      15

      @tf.function是函數(shù)的裝飾器?!癟ensorFlow 函數(shù)”將計(jì)算定義為 TensorFlow 操作圖,具有命名參數(shù)和顯式返回值。

      AutoGraph

      在TensorFlow 2.0中,默認(rèn)情況下啟用了eager 模式。 對于用戶而言直觀且靈活(運(yùn)行一次性操作更容易,更快

      AutoGraph主要是可以將一些常用的Python代碼轉(zhuǎn)化為TensorFlow支持的Graph代碼

      下面是官方代碼

      def square_if_positive(x): if x > 0: x = x * x else: x = 0.0 return x # eager 模式 print('Eager results: %2.2f, %2.2f' % (square_if_positive(tf.constant(9.0)), square_if_positive(tf.constant(-9.0)))) # 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' % (sess.run(g_out1), sess.run(g_out2)))

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      上面定義了一個(gè)square_if_positive函數(shù),TensorFlow 2.0的eager先執(zhí)行,

      然而這是TensorFlow 1.x所不支持的eager模式,但是使用AutoGraph可以將這個(gè)函數(shù)轉(zhuǎn)為Graph函數(shù),其可以在Graph模式下運(yùn)行(tf2 沒有Session,Graph模式是tf1.x的特性,想使用tf1.x的話需要調(diào)用tf.compat.v1)。

      在tensorflow中,Graph execution or eager execution ?

      eager模式和Graph模式的結(jié)果是一樣的,但是官方說明Graph模式更高效,并給出具體的示例:

      def huber_loss(a): if tf.abs(a) <= delta: loss = a * a / 2 else: loss = delta * (tf.abs(a) - delta / 2) return loss

      1

      2

      3

      4

      5

      6

      上面代碼默認(rèn)使用 Eager Execution,但是操作會比較慢。

      為了給圖執(zhí)行做好準(zhǔn)備,你需要重寫代碼,使用 tf.cond() 等語句,但是這很繁瑣且難以實(shí)現(xiàn)。AutoGraph 可以自動完成該轉(zhuǎn)換,保持 Eager 編程的簡易性,同時(shí)還提升了計(jì)算圖執(zhí)行的性能。

      @autograph.convert() def huber_loss(a): if tf.abs(a) <= delta: loss = a * a / 2 else: loss = delta * (tf.abs(a) - delta / 2) return loss

      1

      2

      3

      4

      5

      6

      7

      對應(yīng)官方鏈接:https://blog.tensorflow.org/2018/07/autograph-converts-python-into-tensorflow-graphs.html

      Python TensorFlow

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:什么算是有價(jià)值的測試用例?
      下一篇:Git 分布式版本控制系統(tǒng)
      相關(guān)文章
      亚洲国产午夜福利在线播放 | 亚洲成a人片在线观看播放| 国产亚洲福利精品一区| 亚洲日韩VA无码中文字幕| 精品国产日韩亚洲一区91| 亚洲狠狠婷婷综合久久| 亚洲一久久久久久久久| 亚洲一区二区三区高清在线观看| 亚洲粉嫩美白在线| 亚洲日本乱码卡2卡3卡新区| 亚洲成a人片在线观看精品| 亚洲一区二区三区在线观看蜜桃 | 亚洲六月丁香六月婷婷色伊人| 久久精品国产亚洲AV无码偷窥| 久久久久久亚洲Av无码精品专口| 亚洲精品欧洲精品| 亚洲人成电影青青在线播放| 亚洲免费观看在线视频| 亚洲人成网站看在线播放| 中文字幕亚洲情99在线| 亚洲人成色77777在线观看| 亚洲av日韩专区在线观看| 亚洲AV无码一区二区三区国产| 亚洲av无码成人精品区| 亚洲精品无码久久不卡| 在线日韩日本国产亚洲| 亚洲AV无码成人网站久久精品大| 亚洲电影一区二区三区| 亚洲欧洲国产综合| 亚洲国产激情在线一区| 亚洲精品乱码久久久久蜜桃| www国产亚洲精品久久久日本| ZZIJZZIJ亚洲日本少妇JIZJIZ| 亚洲熟妇中文字幕五十中出| 亚洲高清在线观看| 亚洲av无码专区在线| 亚洲精品av无码喷奶水糖心| 亚洲精品综合久久| 亚洲国产另类久久久精品| 亚洲高清免费在线观看| 亚洲男人天堂2022|