使用tensorflow后端时在Keras中使用tensorflow.GPUOptions [英] use tensorflow.GPUOptions within Keras when using tensorflow backend

查看:77
本文介绍了使用tensorflow后端时在Keras中使用tensorflow.GPUOptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在tensorflow中,我可以在创建会话时执行以下操作:

In tensorflow I can do something like this when creating a session:

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

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

Is there a way to do the same in keras with the tensorflow backend?

推荐答案

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

You can set the Keras global tensorflow session with keras.backend.tensorflow_backend.set_session():

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())

这篇关于使用tensorflow后端时在Keras中使用tensorflow.GPUOptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆