TensorFlow 设置程序可见GPU与逻辑分区

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的 逻辑分区

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

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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值