Tensorflow全局設置可見GPU編號操作
筆者需要tensorflow僅運行在一個GPU上(機器本身有多GPU),而且需要依據系統參數動態(tài)調節(jié),故無法簡單使用CUDA_VISIBLE_DEVICES。
一種方式是全局使用tf.device函數生成的域,但設備號需要在繪制Graph前指定,仍然不夠靈活。
查閱文檔發(fā)現config的GPUOptions中的visible_device_list可以定義GPU編號從visible到virtual的映射,即可以設置tensorflow可見的GPU device,從而全局設置了tensorflow可見的GPU編號。代碼如下:
config = tf.ConfigProto() config.gpu_options.visible_device_list = str(device_num) sess = tf.Session(config=config)
參考 多卡服務器下隱藏部分 GPU 和 TensorFlow 的顯存使用設置,還可以通過os包設置全局變量CUDA_VISIBLE_DEVICES,代碼如下:
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
補充知識:TensorFlow 設置程序可見GPU與邏輯分區(qū)
TensorFlow 設置程序可見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的個數 gpus = tf.config.experimental.list_physical_devices("GPU") if len(gpus) >= 1: # 設置第幾個GPU 當前程序可見 tf.config.experimental.set_visible_devices(gpus[0], "GPU") print("物理GPU個數:", len(gpus)) # 獲取邏輯GPU的個數 logical_gpus = tf.config.experimental.list_logical_devices("GPU") print("邏輯GPU個數:", len(logical_gpus))
TensorFlow 設置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的個數 gpus = tf.config.experimental.list_physical_devices("GPU") if len(gpus) >= 1: # 設置第幾個GPU 當前程序可見 tf.config.experimental.set_visible_devices(gpus[0], "GPU") # 設置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個數:", len(gpus)) # 獲取邏輯GPU的個數 logical_gpus = tf.config.experimental.list_logical_devices("GPU") print("邏輯GPU個數:", len(logical_gpus))
TensorFlow 手動設置處理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) # 自動指定處理設備 tf.config.set_soft_device_placement(True) # 獲取物理GPU的個數 gpus = tf.config.experimental.list_physical_devices("GPU") for gpu in gpus: # 設置內存自增長方式 tf.config.experimental.set_memory_growth(gpu, True) print("物理GPU個數:", len(gpus)) # 獲取邏輯GPU的個數 logical_gpus = tf.config.experimental.list_logical_devices("GPU") print("邏輯GPU個數:", len(logical_gpus)) c = [] # 循環(huán)遍歷當前邏輯GPU for gpu in logical_gpus: print(gpu.name) # 手動設置處理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)) # 手動設置處理GPU with tf.device("/GPU:0"): matmul_sum = tf.add_n(c) print(matmul_sum)
以上這篇Tensorflow全局設置可見GPU編號操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python Pandas Dataframe.describe()使用及代碼實例
這篇文章主要介紹了Python Pandas Dataframe.describe()使用及代碼實例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09淺析Django 接收所有文件,前端展示文件(包括視頻,文件,圖片)ajax請求
這篇文章主要介紹了Django 接收所有文件,前端展示文件(包括視頻,文件,圖片)ajax請求,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值 ,需要的朋友可以參考下2020-03-032020年10款優(yōu)秀的Python第三方庫,看看有你中意的嗎?
2020已經過去,在過去的一年里,又有非常多優(yōu)秀的Python庫涌現出來。相對于numpy、TensorFlow、pandas這些已經經過多年維護、迭代,對于大多數Python開發(fā)者耳熟能詳的庫不同。2021-01-01