如何在基於tensorflow的深度學習框架keras中指定GPU記憶體使用大小
阿新 • • 發佈:2019-01-10
set_gpu.py
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))
呼叫set_gpu.py中的get_session
from set_gpu.py import get_session
import keras.backend.tensorflow_backend as KTF
KTF.set_session(get_session())