激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Tensorflow全局設置可見GPU編號操作

Tensorflow全局設置可見GPU編號操作

2020-06-30 10:23silent56_th Python

這篇文章主要介紹了Tensorflow全局設置可見GPU編號操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

筆者需要tensorflow僅運行在一個GPU上(機器本身有多GPU),而且需要依據系統參數動態調節,故無法簡單使用CUDA_VISIBLE_DEVICES。

一種方式是全局使用tf.device函數生成的域,但設備號需要在繪制Graph前指定,仍然不夠靈活。

查閱文檔發現config的GPUOptions中的visible_device_list可以定義GPU編號從visible到virtual的映射,即可以設置tensorflow可見的GPU device,從而全局設置了tensorflow可見的GPU編號。代碼如下:

?
1
2
3
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與邏輯分區

TensorFlow 設置程序可見GPU(多GPU情況)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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的 邏輯分區

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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的 邏輯分區
 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 = []
 
# 循環遍歷當前邏輯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編號操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/silent56_th/article/details/81319597

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色诱亚洲精品久久久久久 | 欧美性受xxx黑人xyx性爽 | 综合精品一区 | 国产中出视频 | 亚洲va久久久噜噜噜久久男同 | 国人精品视频在线观看 | 经典三级av在线 | 免费一级特黄欧美大片勹久久网 | 亚洲精品午夜国产va久久成人 | 成人一区二区三区在线 | 午夜视频久久久 | 日韩黄色av网站 | 91网页在线观看 | 亚洲va国产va | a一级黄色大片 | 日本成年免费网站 | 污片视频网站 | 美国黄色毛片女人性生活片 | 精品国产一区二区三区久久久 | 伊人二本二区 | 九九视频精品在线 | 欧美一区二区三区四区电影 | 免看黄大片aa | 日韩三级伦理在线观看 | 爱逼爱操综合网 | 综合国产在线 | www国产成人免费观看视频 | 久久久经典视频 | 久久久久久久久久久一区 | 亚洲国产网址 | 国产午夜亚洲精品午夜鲁丝片 | 久久精品久 | 一本在线高清码电影 | 久久精品a一级国产免视看成人 | 国产69精品久久99不卡免费版 | 亚洲成人免费网站 | 91色一区二区三区 | 视频在线色 | 鲁人人人鲁人人鲁精品 | 国产精品亚洲激情 | 成人短视频在线观看免费 |