詳解tf.device()指定tensorflow運(yùn)行的GPU或CPU設(shè)備實(shí)現(xiàn)
在tensorflow中,我們可以使用 tf.device() 指定模型運(yùn)行的具體設(shè)備,可以指定運(yùn)行在GPU還是CUP上,以及哪塊GPU上。
設(shè)置使用GPU
使用 tf.device('/gpu:1') 指定Session在第二塊GPU上運(yùn)行:
import tensorflow as tf with tf.device('/gpu:1'): v1 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v1') v2 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v2') sumV12 = v1 + v2 with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: print sess.run(sumV12)
ConfigProto() 中參數(shù) log_device_placement=True 會打印出執(zhí)行操作所用的設(shè)備,以上輸出:
如果安裝的是GPU版本的tensorflow,機(jī)器上有支持的GPU,也正確安裝了顯卡驅(qū)動、CUDA和cuDNN,默認(rèn)情況下,Session會在GPU上運(yùn)行:
import tensorflow as tf v1 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v1') v2 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v2') sumV12 = v1 + v2 with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: print sess.run(sumV12)
默認(rèn)在GPU:0上執(zhí)行:
設(shè)置使用cpu
tensorflow中不同的GPU使用/gpu:0和/gpu:1區(qū)分,而CPU不區(qū)分設(shè)備號,統(tǒng)一使用 /cpu:0
import tensorflow as tf with tf.device('/cpu:0'): v1 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v1') v2 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v2') sumV12 = v1 + v2 with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: print sess.run(sumV12)
到此這篇關(guān)于詳解tf.device()指定tensorflow運(yùn)行的GPU或CPU設(shè)備實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)tensorflow運(yùn)行GPU或CPU內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 運(yùn)行tensorflow python程序,限制對GPU和CPU的占用操作
- 基于Tensorflow使用CPU而不用GPU問題的解決
- 在tensorflow中設(shè)置使用某一塊GPU、多GPU、CPU的操作
- tensorflow指定CPU與GPU運(yùn)算的方法實(shí)現(xiàn)
- 卸載tensorflow-cpu重裝tensorflow-gpu操作
- 使用Tensorflow-GPU禁用GPU設(shè)置(CPU與GPU速度對比)
- 用gpu訓(xùn)練好的神經(jīng)網(wǎng)絡(luò),用tensorflow-cpu跑出錯的原因及解決方案
- Tensorflow中使用cpu和gpu有什么區(qū)別
- tensorflow之如何使用GPU而不是CPU問題
- TensorFlow安裝CPU版本和GPU版本的實(shí)現(xiàn)步驟
相關(guān)文章
Python內(nèi)建類型str源碼學(xué)習(xí)
這篇文章主要為大家介紹了Python內(nèi)建類型str的源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model詳解
這篇文章主要給大家介紹了關(guān)于django如何根據(jù)現(xiàn)有數(shù)據(jù)庫表生成model的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-08-08python如何把字符串類型list轉(zhuǎn)換成list
這篇文章主要介紹了python如何吧字符串類型list轉(zhuǎn)換成list,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02