[英]How to control memory while using Keras with tensorflow backend?
I have created a wrapper class which initializes a keras.models.Sequential
model and has a couple of methods for starting the training process and monitoring the progress. 我创建了一个包装器类,该包装器类可初始化keras.models.Sequential
模型,并具有一些用于启动训练过程和监视进度的方法。 I instantiate this class in my main
file and perform the training process. 我在main
文件中实例化该类并执行培训过程。 Fairly mundane stuff. 平凡的东西。
My question is: 我的问题是:
How to free all the GPU memory allocated by tensorflow
. 如何释放由tensorflow
分配的所有GPU内存。 I tried the following with no luck: 我没有运气就尝试了以下方法:
import keras.backend.tensorflow_backend as K
with K.get_session() as sess:
K.set_session(sess)
import tensorflow as tf
from neural_net import NeuralNet
with tf.device('/gpu:0'):
nn = NeuralNet('config', train_db_path, test_db_path)
nn.train(1000, 1)
print 'Done'
K._SESSION.close()
K.set_session(None)
Even after the session has been closed and reset to None
, nvidia-smi
does not reflect any reduction in memory usage. 即使在关闭会话并将其重置为None
, nvidia-smi
也不会反映出内存使用的减少。 Any ideas? 有任何想法吗?
Idea 理念
Would it be meaningful to add a __exit__
method to my NeuralNet
class and instantiate it as: 难道是有意义的增加__exit__
方法我NeuralNet
类实例化它:
with NeuralNet() as nn:
nn.train(1000, 1)
How should I free up the resources of the keras model in this method? 我应该如何用这种方法释放keras模型的资源?
Test environment 测试环境
I'm using iPython Notebook on an Ubuntu 14.04 with 3 GTX 960 GPUs. 我在具有3个GTX 960 GPU的Ubuntu 14.04上使用iPython Notebook。
Reference: 参考:
The following works for me to reinitialize the state of Keras layers in my Jupyter notebook for every run: 对于我,以下工作可为每次运行重新初始化Jupyter笔记本中Keras层的状态:
from keras import backend as K
K.clear_session()
sess = tf.Session()
K.set_session(sess)
Also, the graph is named and reset every time the notebook runs using: 此外,每次使用笔记本计算机运行时,都会对图形进行命名和重置:
graphr = K.get_session().graph
with graphr.as_default():
#...graph building statements...
Note : I am still trying to wrap my head around the concepts of Keras and tensorflow ( I believe they are described poorly in documentation and examples ) but the above works. 注意:我仍在努力了解Keras和tensorflow的概念(我相信在文档和示例中对它们的描述很差),但是上述方法可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.