溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行Tensorflow中Session和InteractiveSession的比較

發(fā)布時(shí)間:2021-12-17 16:18:56 來源:億速云 閱讀:112 作者:柒染 欄目:大數(shù)據(jù)

如何進(jìn)行Tensorflow中Session和InteractiveSession的比較,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。


01

Session



每一個(gè)Session都維護(hù)各自變量的副本。

如下所示:


W = tf.Variable(10)

sess1 = tf.Session()

sess2 = tf.Session()

sess1.run(W.initializer)

sess2.run(W.initializer)

print sess1.run(W.assign_add(10)) # >> 20

print sess2.run(W.assign_sub(2)) # >> ?


?等號(hào)8,sess1和sess2各自維護(hù)W,所以sess1中W增加10,不會(huì)影響sess2的W,所以它等于10-2=8.




02

Session vs InteractiveSession


有時(shí)候我們會(huì)看到:InteractiveSession,而不是Session,它們區(qū)別是?


One major change is the use of an InteractiveSession, which allows us to run variables without needing to constantly refer to the session object (less typing!). 


InteractiveSession()


sess = tf.InteractiveSession()

a = tf.constant(5.0)

b = tf.constant(6.0)

c = a * b

# We can just use 'c.eval()' without specifying the context 'sess'

print(c.eval())

sess.close()


Session()


sess = tf.Session()

a = tf.constant(5.0)

b = tf.constant(6.0)

c = a * b

with tf.Session() as sess: 

    sess.run(print(c.eval()))

看完上述內(nèi)容,你們掌握如何進(jìn)行Tensorflow中Session和InteractiveSession的比較的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI