欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

tensorflow使用指定gpu的方法

 更新時間:2020年02月04日 11:20:42   作者:有石為玉  
TensorFlow是一個基于數(shù)據(jù)流編程(dataflow programming)的符號數(shù)學(xué)系統(tǒng),被廣泛應(yīng)用于各類機器學(xué)習(xí),這篇文章主要介紹了tensorflow使用指定gpu的方法,需要的朋友可以參考下

TensorFlow是一個基于數(shù)據(jù)流編程(dataflow programming)的符號數(shù)學(xué)系統(tǒng),被廣泛應(yīng)用于各類機器學(xué)習(xí)(machine learning)算法的編程實現(xiàn),其前身是谷歌的神經(jīng)網(wǎng)絡(luò)算法庫DistBelief [1]  。
Tensorflow擁有多層級結(jié)構(gòu),可部署于各類服務(wù)器、PC終端和網(wǎng)頁并支持GPU和TPU高性能數(shù)值計算,被廣泛應(yīng)用于谷歌內(nèi)部的產(chǎn)品開發(fā)和各領(lǐng)域的科學(xué)研究 。

TensorFlow由谷歌人工智能團隊谷歌大腦(Google Brain)開發(fā)和維護,擁有包括TensorFlow Hub、TensorFlow Lite、TensorFlow Research Cloud在內(nèi)的多個項目以及各類應(yīng)用程序接口(Application Programming Interface, API) 。自2015年11月9日起,TensorFlow依據(jù)阿帕奇授權(quán)協(xié)議(Apache 2.0 open source license)開放源代碼 。

持續(xù)監(jiān)控GPU使用情況命令:

$ watch -n 10 nvidia-smi

一、指定使用某個顯卡

如果機器中有多塊GPU,tensorflow會默認吃掉所有能用的顯存, 如果實驗室多人公用一臺服務(wù)器,希望指定使用特定某塊GPU。
可以在文件開頭加入如下代碼:

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "1"  # 使用第二塊GPU(從0開始)

也可以制定使用某幾塊GPU

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0, 2" # 使用第一, 三塊GPU

禁用GPU

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

支持的設(shè)備

在一套標(biāo)準(zhǔn)系統(tǒng)中通常有多臺計算設(shè)備。TensorFlow 支持 CPU 和 GPU 這兩種設(shè)備。它們均用 strings 表示。例如:

"/cpu:0":機器的 CPU。
"/device:GPU:0":機器的 GPU(如果有一個)。
"/device:GPU:1":機器的第二個 GPU(以此類推)。

如果 TensorFlow 指令中兼有 CPU 和 GPU 實現(xiàn),當(dāng)該指令分配到設(shè)備時,GPU 設(shè)備有優(yōu)先權(quán)。例如,如果 matmul 同時存在 CPU 和 GPU 核函數(shù),在同時有 cpu:0 和 gpu:0 設(shè)備的系統(tǒng)中,gpu:0 會被選來運行 matmul。

記錄設(shè)備分配方式

要找出您的指令和張量被分配到哪個設(shè)備,請創(chuàng)建會話并將 log_device_placement 配置選項設(shè)為 True。

#Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
#Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
#Runs the op.
print(sess.run(c))

應(yīng)該會看到以下輸出內(nèi)容:

Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/device:GPU:0
a: /job:localhost/replica:0/task:0/device:GPU:0
MatMul: /job:localhost/replica:0/task:0/device:GPU:0
[[ 22. 28.]
 [ 49. 64.]]

手動分配設(shè)備

如果您希望特定指令在您選擇的設(shè)備(而非系統(tǒng)自動為您選擇的設(shè)備)上運行,您可以使用 with tf.device 創(chuàng)建設(shè)備上下文,這個上下文中的所有指令都將被分配在同一個設(shè)備上運行。

# Creates a graph.
with tf.device('/cpu:0'):
 a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
 b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))

您會看到現(xiàn)在 a 和 b 被分配到 cpu:0。由于未明確指定運行 MatMul 指令的設(shè)備,因此 TensorFlow 運行時將根據(jù)指令和可用設(shè)備(此示例中的 gpu:0)選擇一個設(shè)備,并會根據(jù)要求自動復(fù)制設(shè)備間的張量。

Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/cpu:0
a: /job:localhost/replica:0/task:0/cpu:0
MatMul: /job:localhost/replica:0/task:0/device:GPU:0
[[ 22. 28.]
 [ 49. 64.]]

允許增加 GPU 內(nèi)存

默認情況下,TensorFlow 會映射進程可見的所有 GPU 的幾乎所有 GPU 內(nèi)存(取決于 CUDA_VISIBLE_DEVICES)。通過減少內(nèi)存碎片,可以更有效地使用設(shè)備上相對寶貴的 GPU 內(nèi)存資源。

在某些情況下,最理想的是進程只分配可用內(nèi)存的一個子集,或者僅根據(jù)進程需要增加內(nèi)存使用量。 TensorFlow 在 Session 上提供兩個 Config 選項來進行控制。

第一個是 allow_growth 選項,它試圖根據(jù)運行時的需要來分配 GPU 內(nèi)存:它剛開始分配很少的內(nèi)存,隨著 Session 開始運行并需要更多 GPU 內(nèi)存,我們會擴展 TensorFlow 進程所需的 GPU 內(nèi)存區(qū)域。請注意,我們不會釋放內(nèi)存,因為這可能導(dǎo)致出現(xiàn)更嚴(yán)重的內(nèi)存碎片情況。要開啟此選項,請通過以下方式在 ConfigProto 中設(shè)置選項:

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config, ...)

第二個是 per_process_gpu_memory_fraction 選項,它可以決定每個可見 GPU 應(yīng)分配到的內(nèi)存占總內(nèi)存量的比例。例如,您可以通過以下方式指定 TensorFlow 僅分配每個 GPU 總內(nèi)存的 40%:

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
session = tf.Session(config=config, ...)

如要真正限制 TensorFlow 進程可使用的 GPU 內(nèi)存量,這非常實用。

在多 GPU 系統(tǒng)中使用單一 GPU
如果您的系統(tǒng)中有多個 GPU,則默認情況下將選擇 ID 最小的 GPU。如果您希望在其他 GPU 上運行,則需要顯式指定偏好設(shè)置:

# Creates a graph.
with tf.device('/device:GPU:2'):
 a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
 b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
 c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))

如果您指定的設(shè)備不存在,您會看到 InvalidArgumentError:

InvalidArgumentError: Invalid argument: Cannot assign a device to node 'b':
Could not satisfy explicit device specification '/device:GPU:2'
 [[Node: b = Const[dtype=DT_FLOAT, value=Tensor<type: float shape: [3,2]
 values: 1 2 3...>, _device="/device:GPU:2"]()]]

當(dāng)指定設(shè)備不存在時,如果您希望 TensorFlow 自動選擇現(xiàn)有的受支持設(shè)備來運行指令,則可以在創(chuàng)建會話時將配置選項中的 allow_soft_placement 設(shè)為 True。

# Creates a graph.
with tf.device('/device:GPU:2'):
 a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
 b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
 c = tf.matmul(a, b)
# Creates a session with allow_soft_placement and log_device_placement set
# to True.
sess = tf.Session(config=tf.ConfigProto(
  allow_soft_placement=True, log_device_placement=True))
# Runs the op.
print(sess.run(c))

使用多個 GPU

如果您想要在多個 GPU 上運行 TensorFlow,則可以采用多塔式方式構(gòu)建模型,其中每個塔都會分配給不同 GPU。例如:

# Creates a graph.
c = []
for d in ['/device:GPU:2', '/device:GPU:3']:
 with tf.device(d):
 a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])
 b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
 c.append(tf.matmul(a, b))
with tf.device('/cpu:0'):
 sum = tf.add_n(c)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(sum))

您會看到以下輸出內(nèi)容:

Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K20m, pci bus
id: 0000:02:00.0
/job:localhost/replica:0/task:0/device:GPU:1 -> device: 1, name: Tesla K20m, pci bus
id: 0000:03:00.0
/job:localhost/replica:0/task:0/device:GPU:2 -> device: 2, name: Tesla K20m, pci bus
id: 0000:83:00.0
/job:localhost/replica:0/task:0/device:GPU:3 -> device: 3, name: Tesla K20m, pci bus
id: 0000:84:00.0
Const_3: /job:localhost/replica:0/task:0/device:GPU:3
Const_2: /job:localhost/replica:0/task:0/device:GPU:3
MatMul_1: /job:localhost/replica:0/task:0/device:GPU:3
Const_1: /job:localhost/replica:0/task:0/device:GPU:2
Const: /job:localhost/replica:0/task:0/device:GPU:2
MatMul: /job:localhost/replica:0/task:0/device:GPU:2
AddN: /job:localhost/replica:0/task:0/cpu:0
[[ 44. 56.]
 [ 98. 128.]]

cifar10 教程就是個很好的例子,演示了如何使用多個 GPU 進行訓(xùn)練。

見官方教程:https://www.tensorflow.org/programmers_guide/using_gpu?hl=zh-cn

總結(jié)

以上所述是小編給大家介紹的tensorflow使用指定gpu的方法,希望對大家有所幫助!

相關(guān)文章

  • pywinauto自動化操作記事本

    pywinauto自動化操作記事本

    這篇文章主要為大家詳細介紹了pywinauto自動化操作記事本,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Python爬取騰訊視頻評論的思路詳解

    Python爬取騰訊視頻評論的思路詳解

    這篇文章主要介紹了Python爬取騰訊視頻評論功能,本文圖文詳解給大家提供實現(xiàn)思路,需要的朋友可以參考下
    2019-12-12
  • 使用python解析xml成對應(yīng)的html示例分享

    使用python解析xml成對應(yīng)的html示例分享

    這篇文章主要介紹了使用python解析xml成對應(yīng)的html示例,需要的朋友可以參考下
    2014-04-04
  • Python字典fromkeys()方法使用代碼實例

    Python字典fromkeys()方法使用代碼實例

    這篇文章主要介紹了Python字典fromkeys()方法使用代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • Python實現(xiàn)批量導(dǎo)入1000條xlsx數(shù)據(jù)

    Python實現(xiàn)批量導(dǎo)入1000條xlsx數(shù)據(jù)

    本文主要介紹了Python實現(xiàn)批量導(dǎo)入1000條xlsx數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • python如何實現(xiàn)TF-IDF算法

    python如何實現(xiàn)TF-IDF算法

    這篇文章主要介紹了python如何實現(xiàn)TF-IDF算法問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • django之用戶、用戶組及權(quán)限設(shè)置方式

    django之用戶、用戶組及權(quán)限設(shè)置方式

    這篇文章主要介紹了django之用戶、用戶組及權(quán)限設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Python二元算術(shù)運算常用方法解析

    Python二元算術(shù)運算常用方法解析

    這篇文章主要介紹了Python二元算術(shù)運算常用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • 詳解python使用金山詞霸的翻譯功能(調(diào)試工具斷點的使用)

    詳解python使用金山詞霸的翻譯功能(調(diào)試工具斷點的使用)

    這篇文章主要介紹了詳解python使用金山詞霸的翻譯功能(調(diào)試工具斷點的使用),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Python+Turtle制作獨特的表白圖

    Python+Turtle制作獨特的表白圖

    這篇文章主要利用Python和Turtle庫繪制獨特的表白圖,文中的示例代碼講解詳細,對我們學(xué)習(xí)Python有一定的幫助,感興趣的可以了解一下
    2022-04-04

最新評論