网络编程
位置:首页>> 网络编程>> Python编程>> Keras设定GPU使用内存大小方式(Tensorflow backend)

Keras设定GPU使用内存大小方式(Tensorflow backend)

作者:mjiansun  发布时间:2021-12-20 22:15:12 

标签:Keras,GPU,内存

通过设置Keras的Tensorflow后端的全局变量达到。


import os
import tensorflow as tf
import keras.backend.tensorflow_backend as KTF

def get_session(gpu_fraction=0.3):
'''Assume that you have 6GB of GPU memory and want to allocate ~2GB'''

num_threads = os.environ.get('OMP_NUM_THREADS')
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction)

if num_threads:
 return tf.Session(config=tf.ConfigProto(
  gpu_options=gpu_options, intra_op_parallelism_threads=num_threads))
else:
 return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

使用过程中显示的设置session:

import keras.backend.tensorflow_backend as KTF
KTF.set_session(get_session())

补充知识:限制tensorflow的运行内存 (keras.backend.tensorflow)

我就废话不多说了,大家还是直接看代码吧!


import tensorflow as tf
from keras.backend.tensorflow_backend import set_session

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5 #half of the memory
set_session(tf.Session(config=config))

来源:https://blog.csdn.net/u013066730/article/details/77510033

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com