tensorflow 配置nvidia 显卡

本文详细介绍了如何在TensorFlow中管理GPU内存,包括设定虚拟设备配置以限制内存使用,动态增长GPU内存,以及如何通过代码指定可见GPU设备,确保GPU资源合理分配,避免训练过程中的资源冲突。
部署运行你感兴趣的模型镜像
import tensorflow as tf
import numpy as np
import os

os.environ['CUDA_VISIBLE_DEVICES']='0'
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction=0.6

with tf.Session(graph=...,config=config) as sess:
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
  # Restrict TensorFlow to only allocate 1GB of memory on the first GPU
  try:
    tf.config.experimental.set_virtual_device_configuration(
        gpus[0],
        [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])
    logical_gpus = tf.config.experimental.list_logical_devices('GPU')
    print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Virtual devices must be set before GPUs have been initialized
    print(e)

 根据需要配置显存,避免爆掉

import os
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"

 

import tensorflow as tf
import os
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)
os.environ['CUDA_VISIBLE_DEVICES']='2'

检测下面有效:其他的有时候有效,有时候无效

os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
#这句希望把GPU限定在10G范围内,避免死机,测试效果没多大用。nvidia-smi 查看GPU使用率,都超过了。
tf.config.experimental.VirtualDeviceConfiguration(memory_limit=10240)

strategy = tf.distribute.MirroredStrategy()  

 


下面引用:https://blog.youkuaiyun.com/zkbaba/article/details/104101584

通过 tf.config.experimental.set_visible_devices ,可以设置当前程序可见的设备范围(当前程序只会使用自己可见的设备,不可见的设备不会被当前程序使用)。例如,如果在上述 4 卡的机器中我们需要限定当前程序只使用下标为 0、1 的两块显卡(GPU:0 和 GPU:1),可以使用以下代码:

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
tf.config.experimental.set_visible_devices(devices=gpus[0:2], device_type='GPU')

 


指定训练GPU:确实可行的方案。

#方法一,指定训练GPU
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
tf.config.experimental.set_visible_devices(gpus[1], device_type='GPU')
print("gpus",gpus)

方法二,指定训练GPU
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "1"



#配置限制GPU使用大小,避免GPU崩溃 ,接在方法一,或者方法二代码后面即可
tf.config.experimental.set_virtual_device_configuration(
   gpus[0],
    [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=10240)])
tf.config.experimental.set_virtual_device_configuration(
   gpus[1],
    [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024*10)])

logical_gpus = logical_gpus = tf.config.experimental.list_logical_devices('GPU')
print("logical_gpus",logical_gpus)

 

 

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值