ChatGPT解决这个技术问题 Extra ChatGPT

What do I need K.clear_session() and del model for (Keras with Tensorflow-gpu)?

What I am doing I am training and using a convolutional neuron network (CNN) for image-classification using Keras with Tensorflow-gpu as backend.

What I am using - PyCharm Community 2018.1.2 - both Python 2.7 and 3.5 (but not both at a time) - Ubuntu 16.04 - Keras 2.2.0 - Tensorflow-GPU 1.8.0 as backend

What I want to know In many codes I see people using

from keras import backend as K 

# Do some code, e.g. train and save model

K.clear_session()

or deleting the model after using it:

del model

The keras documentation says regarding clear_session: "Destroys the current TF graph and creates a new one. Useful to avoid clutter from old models / layers." - https://keras.io/backend/

What is the point of doing that and should I do it as well? When loading or creating a new model my model gets overwritten anyway, so why bother?

Did you discover anything related to your question? I'm trying to find the same answers. The 1 answer provided doesn't quite clear up my thoughts.
The model does not get overwritten as far as I know the TF graph will just add the new model to the old model TF graph if you don't clear it. Del the model just saves memory so the cpu/gpu won't have to hold extra stuff in it.
If you're using the Sequential method in Keras and you create a new model by starting with model=Sequential(), then I agree that your previous model should be overwritten. I couldn't find confirmation in the docs, however.
actually your question itself help me a lot and thanks a lot i was searching for this for past 6 hours thanks again wish you a good luck

u
user5305519

K.clear_session() is useful when you're creating multiple models in succession, such as during hyperparameter search or cross-validation. Each model you train adds nodes (potentially numbering in the thousands) to the graph. TensorFlow executes the entire graph whenever you (or Keras) call tf.Session.run() or tf.Tensor.eval(), so your models will become slower and slower to train, and you may also run out of memory. Clearing the session removes all the nodes left over from previous models, freeing memory and preventing slowdown.

Edit 21/06/19:

TensorFlow is lazy-evaluated by default. TensorFlow operations aren't evaluated immediately: creating a tensor or doing some operations to it creates nodes in a dataflow graph. The results are calculated by evaluating the relevant parts of the graph in one go when you call tf.Session.run() or tf.Tensor.eval(). This is so TensorFlow can build an execution plan that allocates operations that can be performed in parallel to different devices. It can also fold adjacent nodes together or remove redundant ones (e.g. if you concatenated two tensors and later split them apart again unchanged). For more details, see https://www.tensorflow.org/guide/graphs

All of your TensorFlow models are stored in the graph as a series of tensors and tensor operations. The basic operation of machine learning is tensor dot product - the output of a neural network is the dot product of the input matrix and the network weights. If you have a single-layer perceptron and 1,000 training samples, then each epoch creates at least 1,000 tensor operations. If you have 1,000 epochs, then your graph contains at least 1,000,000 nodes at the end, before taking into account preprocessing, postprocessing, and more complex models such as recurrent nets, encoder-decoder, attentional models, etc.

The problem is that eventually the graph would be too large to fit into video memory (6 GB in my case), so TF would shuttle parts of the graph from video to main memory and back. Eventually it would even get too large for main memory (12 GB) and start moving between main memory and the hard disk. Needless to say, this made things incredibly, and increasingly, slow as training went on. Before developing this save-model/clear-session/reload-model flow, I calculated that, at the per-epoch rate of slowdown I experienced, my model would have taken longer than the age of the universe to finish training.

Disclaimer: I haven't used TensorFlow in almost a year, so this might have changed. I remember there being quite a few GitHub issues around this so hopefully it has since been fixed.


Do I need to create a new model, when I run K.clear_session(). Also, can you explain adding of nodes to the graph part and the model becoming slower and running out of memory.
This answer might not apply to thet current version of TensorFlow as I haven't used it since last year. Assuming the situation hasn't changed; yes, you need to either create a new model or save and reload the old one to continue training. I've edited my answer to answer the second part of your question.
By performing K.clear_session() I assume the current graph is deleted and every future Operation relying on the existence of that graph (such as adding new layers, or running model.fit()) has no graph to link to anymore.
@mathlover Marcus is correct, if you delete the graph and create a new one then your existing tensors have a dangling reference. This is why you should save the model before deleting the graph and then reload it. That way, you get a new graph containing all the necessary data.
@Chris Swinchatt and all: after crashing out with OOM today I can confirm this still applies! I discovered it by checking my Tensorboard logs: the logs files got bigger in the order they were created, and the graphs in Tensorboard were more and more complex as the different models' graphs were chained together. It didn't seem to affect model results (which is as one would hope) but can anyone confirm for sure that it doesn't harm later models? (aside from obvious speed/memory issues).
佚名

del will delete variable in python and since model is a variable, del model will delete it but the TF graph will have no changes (TF is your Keras backend). This said, K.clear_session() will destroy the current TF graph and creates a new one. Creating a new model seems to be an independent step, but don't forget the backend :)


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now