Tensorflow全局設(shè)置可見(jiàn)GPU編號(hào)操作
筆者需要tensorflow僅運(yùn)行在一個(gè)GPU上(機(jī)器本身有多GPU),而且需要依據(jù)系統(tǒng)參數(shù)動(dòng)態(tài)調(diào)節(jié),故無(wú)法簡(jiǎn)單使用CUDA_VISIBLE_DEVICES。
一種方式是全局使用tf.device函數(shù)生成的域,但設(shè)備號(hào)需要在繪制Graph前指定,仍然不夠靈活。
查閱文檔發(fā)現(xiàn)config的GPUOptions中的visible_device_list可以定義GPU編號(hào)從visible到virtual的映射,即可以設(shè)置tensorflow可見(jiàn)的GPU device,從而全局設(shè)置了tensorflow可見(jiàn)的GPU編號(hào)。代碼如下:
config = tf.ConfigProto() config.gpu_options.visible_device_list = str(device_num) sess = tf.Session(config=config)
參考 多卡服務(wù)器下隱藏部分 GPU 和 TensorFlow 的顯存使用設(shè)置,還可以通過(guò)os包設(shè)置全局變量CUDA_VISIBLE_DEVICES,代碼如下:
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
補(bǔ)充知識(shí):TensorFlow 設(shè)置程序可見(jiàn)GPU與邏輯分區(qū)
TensorFlow 設(shè)置程序可見(jiàn)GPU(多GPU情況)
import matplotlib as mpl import matplotlib.pyplot as plt %matplotlib inline import numpy as np import sklearn import pandas as pd import os import sys import time import tensorflow as tf from tensorflow_core.python.keras.api._v2 import keras print(tf.__version__) print(sys.version_info) for module in mpl, np, pd, sklearn, tf, keras: print(module.__name__, module.__version__) # 打印變量所在位置 tf.debugging.set_log_device_placement(True) # 獲取物理GPU的個(gè)數(shù) gpus = tf.config.experimental.list_physical_devices("GPU") if len(gpus) >= 1: # 設(shè)置第幾個(gè)GPU 當(dāng)前程序可見(jiàn) tf.config.experimental.set_visible_devices(gpus[0], "GPU") print("物理GPU個(gè)數(shù):", len(gpus)) # 獲取邏輯GPU的個(gè)數(shù) logical_gpus = tf.config.experimental.list_logical_devices("GPU") print("邏輯GPU個(gè)數(shù):", len(logical_gpus))
TensorFlow 設(shè)置GPU的 邏輯分區(qū)
import matplotlib as mpl import matplotlib.pyplot as plt %matplotlib inline import numpy as np import sklearn import pandas as pd import os import sys import time import tensorflow as tf from tensorflow_core.python.keras.api._v2 import keras print(tf.__version__) print(sys.version_info) for module in mpl, np, pd, sklearn, tf, keras: print(module.__name__, module.__version__) # 打印變量所在位置 tf.debugging.set_log_device_placement(True) # 獲取物理GPU的個(gè)數(shù) gpus = tf.config.experimental.list_physical_devices("GPU") if len(gpus) >= 1: # 設(shè)置第幾個(gè)GPU 當(dāng)前程序可見(jiàn) tf.config.experimental.set_visible_devices(gpus[0], "GPU") # 設(shè)置GPU的 邏輯分區(qū) tf.config.experimental.set_virtual_device_configuration( gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=3072), tf.config.experimental.VirtualDeviceConfiguration(memory_limit=3072)]) print("物理GPU個(gè)數(shù):", len(gpus)) # 獲取邏輯GPU的個(gè)數(shù) logical_gpus = tf.config.experimental.list_logical_devices("GPU") print("邏輯GPU個(gè)數(shù):", len(logical_gpus))
TensorFlow 手動(dòng)設(shè)置處理GPU
import matplotlib as mpl import matplotlib.pyplot as plt %matplotlib inline import numpy as np import sklearn import pandas as pd import os import sys import time import tensorflow as tf from tensorflow_core.python.keras.api._v2 import keras print(tf.__version__) print(sys.version_info) for module in mpl, np, pd, sklearn, tf, keras: print(module.__name__, module.__version__) # 打印變量所在位置 tf.debugging.set_log_device_placement(True) # 自動(dòng)指定處理設(shè)備 tf.config.set_soft_device_placement(True) # 獲取物理GPU的個(gè)數(shù) gpus = tf.config.experimental.list_physical_devices("GPU") for gpu in gpus: # 設(shè)置內(nèi)存自增長(zhǎng)方式 tf.config.experimental.set_memory_growth(gpu, True) print("物理GPU個(gè)數(shù):", len(gpus)) # 獲取邏輯GPU的個(gè)數(shù) logical_gpus = tf.config.experimental.list_logical_devices("GPU") print("邏輯GPU個(gè)數(shù):", len(logical_gpus)) c = [] # 循環(huán)遍歷當(dāng)前邏輯GPU for gpu in logical_gpus: print(gpu.name) # 手動(dòng)設(shè)置處理GPU with tf.device(gpu.name): a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]) # 矩陣相乘 并且添加至列表 c.append(tf.matmul(a, b)) # 手動(dòng)設(shè)置處理GPU with tf.device("/GPU:0"): matmul_sum = tf.add_n(c) print(matmul_sum)
以上這篇Tensorflow全局設(shè)置可見(jiàn)GPU編號(hào)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python Pandas Dataframe.describe()使用及代碼實(shí)例
這篇文章主要介紹了Python Pandas Dataframe.describe()使用及代碼實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09淺析Django 接收所有文件,前端展示文件(包括視頻,文件,圖片)ajax請(qǐng)求
這篇文章主要介紹了Django 接收所有文件,前端展示文件(包括視頻,文件,圖片)ajax請(qǐng)求,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2020-03-03python統(tǒng)計(jì)中文字符數(shù)量的兩種方法
今天小編就為大家分享一篇python統(tǒng)計(jì)中文字符數(shù)量的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01通過(guò)OpenCV實(shí)現(xiàn)對(duì)指定顏色的物體追蹤
這篇文章主要介紹的是通過(guò)OpenCV實(shí)現(xiàn)對(duì)特定顏色的物體追蹤,文中實(shí)驗(yàn)用的是綠蘿的樹(shù)葉。本文的示例代碼講解詳細(xì),對(duì)學(xué)習(xí)OPenCV有一定的幫助,感興趣的小伙伴可以了解一下2021-12-12TensorBoard 計(jì)算圖的可視化實(shí)現(xiàn)
今天小編就為大家分享一篇TensorBoard 計(jì)算圖的可視化實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-022020年10款優(yōu)秀的Python第三方庫(kù),看看有你中意的嗎?
2020已經(jīng)過(guò)去,在過(guò)去的一年里,又有非常多優(yōu)秀的Python庫(kù)涌現(xiàn)出來(lái)。相對(duì)于numpy、TensorFlow、pandas這些已經(jīng)經(jīng)過(guò)多年維護(hù)、迭代,對(duì)于大多數(shù)Python開(kāi)發(fā)者耳熟能詳?shù)膸?kù)不同。2021-01-01Python K-means實(shí)現(xiàn)簡(jiǎn)單圖像聚類(lèi)的示例代碼
本文主要介紹了Python K-means實(shí)現(xiàn)簡(jiǎn)單圖像聚類(lèi)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Matplotlib實(shí)戰(zhàn)之柱狀圖繪制詳解
柱狀圖,是一種使用矩形條,對(duì)不同類(lèi)別進(jìn)行數(shù)值比較的統(tǒng)計(jì)圖表,這篇文章主要為大家詳細(xì)介紹了如何使用Matplotlib繪制柱狀圖,需要的可以參考下2023-08-08