日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tf.device()指定tensorflow运行的GPU或CPU设备

發布時間:2025/5/22 编程问答 62 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tf.device()指定tensorflow运行的GPU或CPU设备 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在tensorflow中,我們可以使用 tf.device() 指定模型運行的具體設備,可以指定運行在GPU還是CUP上,以及哪塊GPU上。


設置使用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() 中參數 log_device_placement=True? 會打印出執行操作所用的設備,以上輸出:



如果安裝的是GPU版本的tensorflow,機器上有支持的GPU,也正確安裝了顯卡驅動、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上執行:




設置使用cpu

tensorflow中不同的GPU使用/gpu:0和/gpu:1區分,而CPU不區分設備號,統一使用 /cpu:0

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)


轉載于:https://www.cnblogs.com/mtcnn/p/9411709.html

總結

以上是生活随笔為你收集整理的tf.device()指定tensorflow运行的GPU或CPU设备的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。