Excel2016打印預(yù)覽表格的教程是什么(excel2016怎么看打印預(yù)覽)">Excel2016打印預(yù)覽表格的教程是什么(excel2016怎么看打印預(yù)覽)
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
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)容。