Tensorflow常用会话设置GPU&ConfigProto

深度学习中常用的会话设置,仅供参考!

模块ConfigProto

创建session时常用的配置参数,使用tf.ConfigProto进行参数配置

with tf.Graph().as_default():

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4)

    sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,log_device_placement=True))

    with sess.as_default():

        print "example"

tf.ConfigProto()的参数

log_device_placement=True 是否打印设备分配日志

allow_soft_placement=True 如果指定的设备不存在,允许TF自动分配设备

gpu_options = tf.GPUOptions() 控制GPU资源

tf.GPUOptions() 参数

使用allow_growth参数,开始分配少量GPU容量,然后按需慢慢的增加,由于不释放内存,所以会导致碎片

tf.GPUOptions(allow_growth = True)

设置每个GPU应该拿出多少容量给进程使用

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4)

GPU选择

方式一 CUDA_VISIBLE_DEVICES=0 python your.py  or   CUDA_VISIBLE_DEVICES=0,1 python your.py

方式二 os.environ['CUDA_VISIBLE_DEVICES'] = '0' #使用 GPU 0  or os.environ['CUDA_VISIBLE_DEVICES'] = '0,1' # 使用 GPU 0,1

你可能感兴趣的:(Tensorflow常用会话设置GPU&ConfigProto)