使用 tensorflow 后端时在 Keras 中使用 tensorflow.GPUOptions

use tensorflow.GPUOptions within Keras when using tensorflow backend

在tensorflow中,我可以在创建会话时做这样的事情:

tf.GPUOptions(per_process_gpu_memory_fraction=0.333,allow_growth=True)

有没有办法在带有 tensorflow 后端的 keras 中做同样的事情?

您可以使用 keras.backend.tensorflow_backend.set_session() 设置 Keras 全局 tensorflow 会话:

import tensorflow as tf
import keras.backend.tensorflow_backend as ktf


def get_session(gpu_fraction=0.333):
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction,
                                allow_growth=True)
    return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))


ktf.set_session(get_session())