tf.device()指定tensorflow运行的GPU或CPU设备
生活随笔
收集整理的這篇文章主要介紹了
tf.device()指定tensorflow运行的GPU或CPU设备
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在tensorflow中,我們可以使用 tf.device() 指定模型運行的具體設(shè)備,可以指定運行在GPU還是CUP上,以及哪塊GPU上。
import tensorflow as tfwith 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 + v2with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print sess.run(sumV12)
如果安裝的是GPU版本的tensorflow,機器上有支持的GPU,也正確安裝了顯卡驅(qū)動、CUDA和cuDNN,默認情況下,Session會在GPU上運行:
import tensorflow as tfv1 = 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 + v2with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print sess.run(sumV12)
import tensorflow as tfwith 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 + v2with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print sess.run(sumV12)
設(shè)置使用GPU
使用 tf.device('/gpu:1') 指定Session在第二塊GPU上運行:import tensorflow as tfwith 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 + v2with 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,機器上有支持的GPU,也正確安裝了顯卡驅(qū)動、CUDA和cuDNN,默認情況下,Session會在GPU上運行:
import tensorflow as tfv1 = 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 + v2with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print sess.run(sumV12)
默認在GPU:0上執(zhí)行:
設(shè)置使用cpu
tensorflow中不同的GPU使用/gpu:0和/gpu:1區(qū)分,而CPU不區(qū)分設(shè)備號,統(tǒng)一使用 /cpu:0import tensorflow as tfwith 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 + v2with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:print sess.run(sumV12)
轉(zhuǎn)載于:https://www.cnblogs.com/mtcnn/p/9411709.html
總結(jié)
以上是生活随笔為你收集整理的tf.device()指定tensorflow运行的GPU或CPU设备的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络爬虫基本练习
- 下一篇: 深度学习-超参数调整总结