最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Tensorflow全局設(shè)置可見GPU編號操作

 更新時間:2020年06月30日 09:29:33   作者:silent56_th  
這篇文章主要介紹了Tensorflow全局設(shè)置可見GPU編號操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

筆者需要tensorflow僅運行在一個GPU上(機器本身有多GPU),而且需要依據(jù)系統(tǒng)參數(shù)動態(tài)調(diào)節(jié),故無法簡單使用CUDA_VISIBLE_DEVICES。

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

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

config = tf.ConfigProto()
config.gpu_options.visible_device_list = str(device_num)
sess = tf.Session(config=config)

參考 多卡服務器下隱藏部分 GPU 和 TensorFlow 的顯存使用設(shè)置,還可以通過os包設(shè)置全局變量CUDA_VISIBLE_DEVICES,代碼如下:

os.environ["CUDA_VISIBLE_DEVICES"] = "2"

補充知識:TensorFlow 設(shè)置程序可見GPU與邏輯分區(qū)

TensorFlow 設(shè)置程序可見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的個數(shù)
gpus = tf.config.experimental.list_physical_devices("GPU") 

if len(gpus) >= 1:
 # 設(shè)置第幾個GPU 當前程序可見
 tf.config.experimental.set_visible_devices(gpus[0], "GPU")
 
print("物理GPU個數(shù):", len(gpus))

# 獲取邏輯GPU的個數(shù)
logical_gpus = tf.config.experimental.list_logical_devices("GPU") 
print("邏輯GPU個數(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的個數(shù)
gpus = tf.config.experimental.list_physical_devices("GPU") 

if len(gpus) >= 1:
 # 設(shè)置第幾個GPU 當前程序可見
 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個數(shù):", len(gpus))

# 獲取邏輯GPU的個數(shù)
logical_gpus = tf.config.experimental.list_logical_devices("GPU") 
print("邏輯GPU個數(shù):", len(logical_gpus))

TensorFlow 手動設(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) 

# 自動指定處理設(shè)備
tf.config.set_soft_device_placement(True)

# 獲取物理GPU的個數(shù)
gpus = tf.config.experimental.list_physical_devices("GPU") 
for gpu in gpus:
 # 設(shè)置內(nèi)存自增長方式
 tf.config.experimental.set_memory_growth(gpu, True) 
print("物理GPU個數(shù):", len(gpus))

# 獲取邏輯GPU的個數(shù)
logical_gpus = tf.config.experimental.list_logical_devices("GPU") 
print("邏輯GPU個數(shù):", len(logical_gpus))

c = []

# 循環(huán)遍歷當前邏輯GPU
for gpu in logical_gpus:
 print(gpu.name)

 # 手動設(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))

# 手動設(shè)置處理GPU
with tf.device("/GPU:0"):
 matmul_sum = tf.add_n(c)

print(matmul_sum)

以上這篇Tensorflow全局設(shè)置可見GPU編號操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python?使用?pyc?解決明文密鑰問題記錄

    Python?使用?pyc?解決明文密鑰問題記錄

    pyc 是 Python 經(jīng)過 compile 后的文件類型,一段 Python 代碼執(zhí)行前會先將 .py 文件編譯成 .pyc 文件它是一種字節(jié)碼 byte code,然后由 Python 虛擬機執(zhí)行,這篇文章主要介紹了Python使用pyc解決明文密鑰問題,需要的朋友可以參考下
    2023-07-07
  • Python如何給函數(shù)庫增加日志功能

    Python如何給函數(shù)庫增加日志功能

    這篇文章主要介紹了Python如何給函數(shù)庫增加日志功能,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-08-08
  • 用Python實現(xiàn)簡單的人臉識別功能步驟詳解

    用Python實現(xiàn)簡單的人臉識別功能步驟詳解

    這篇文章主要介紹了用Python實現(xiàn)簡單的人臉識別功能步驟詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • 如何通過pycharm實現(xiàn)對數(shù)據(jù)庫的查詢等操作(非多步操作)

    如何通過pycharm實現(xiàn)對數(shù)據(jù)庫的查詢等操作(非多步操作)

    這篇文章主要介紹了如何通過pycharm實現(xiàn)對數(shù)據(jù)庫的查詢等操作(非多步操作),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python 復平面繪圖實例

    Python 復平面繪圖實例

    今天小編就為大家分享一篇Python 復平面繪圖實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • linux系統(tǒng)使用python監(jiān)測網(wǎng)絡接口獲取網(wǎng)絡的輸入輸出

    linux系統(tǒng)使用python監(jiān)測網(wǎng)絡接口獲取網(wǎng)絡的輸入輸出

    這篇文章主要介紹了linux系統(tǒng)使用python監(jiān)測網(wǎng)絡接口獲取網(wǎng)絡的輸入輸出信息,大家參考使用吧
    2014-01-01
  • Python操作Jira庫常用方法解析

    Python操作Jira庫常用方法解析

    這篇文章主要介紹了Python操作Jira庫常用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Python模板的使用詳細講解

    Python模板的使用詳細講解

    Django 模板是使用 Django 模板語言標記的一個文本文檔或Python字符串。模板引擎可以識別和解釋一些構(gòu)造。主要是變量和標簽。模板是通過上下文來渲染的。渲染用變量的值替換變量,變量的值在上下文中查找,并執(zhí)行標簽。其他的一切都按原樣輸出
    2022-10-10
  • Python寫一個字符串數(shù)字后綴部分的遞增函數(shù)

    Python寫一個字符串數(shù)字后綴部分的遞增函數(shù)

    這篇文章主要介紹了Python寫一個字符串數(shù)字后綴部分的遞增函數(shù),寫函數(shù)之前需要Python處理重名字符串,添加或遞增數(shù)字字符串后綴,下面具體過程,需要的小伙伴可以參考一下
    2022-03-03
  • Python中Json使用示例詳解

    Python中Json使用示例詳解

    這篇文章主要介紹了Python中Json使用,主要介紹一下python?中?json的使用?如何把dict轉(zhuǎn)成json?、object?轉(zhuǎn)成json?、以及json轉(zhuǎn)成對象,需要的朋友可以參考下
    2022-07-07

最新評論

横峰县| 马龙县| 陇南市| 衡阳县| 苏尼特右旗| 阜南县| 林周县| 包头市| 临清市| 文登市| 镇康县| 黔南| 闽清县| 中牟县| 绵竹市| 榆中县| 洛浦县| 葵青区| 彰化市| 观塘区| 岑溪市| 赤壁市| 信阳市| 芦溪县| 灌云县| 繁峙县| 环江| 石棉县| 双牌县| 武川县| 南部县| 商水县| 沙河市| 红安县| 九龙城区| 长沙市| 三河市| 蒲城县| 衡东县| 陕西省| 万载县|