How to delete a model

2,485 views
Skip to first unread message

emmanuel chappat

unread,
Jul 19, 2018, 3:47:58 AM7/19/18
to TensorFlow.js Discussion
I am programatically interacting in the browser with the Tfjs API and for my use case I would need to frequently rebuild the model from it's `getConfig` object and `setWeights` its weights. I am worried that this would cause some memory leaks. Is there a proper way to delete a model from memory ?


David Soergel

unread,
Jul 19, 2018, 10:37:22 AM7/19/18
to emmanuel...@gmail.com, TensorFlow.js Discussion
Yep, great question!  Please call tf.disposeVariables() to just delete everything TF.js-related from GPU memory.

Anything in main memory will get garbage-collected by the normal JavaScript mechanism once you're not holding a reference to it anymore.

For a general overview of memory management in TF.js, see https://js.tensorflow.org/tutorials/core-concepts.html.

-ds

On Thu, Jul 19, 2018 at 3:48 AM emmanuel chappat <emmanuel...@gmail.com> wrote:
I am programatically interacting in the browser with the Tfjs API and for my use case I would need to frequently rebuild the model from it's `getConfig` object and `setWeights` its weights. I am worried that this would cause some memory leaks. Is there a proper way to delete a model from memory ?


--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfjs/5e7596f5-d6d3-4176-9a65-90360b68ac59%40tensorflow.org.

emmanuel chappat

unread,
Jul 20, 2018, 2:31:19 AM7/20/18
to TensorFlow.js Discussion, emmanuel...@gmail.com
Thanks David. That's exactly what I was looking for !

Shanqing Cai

unread,
Jul 20, 2018, 1:10:05 PM7/20/18
to TensorFlow.js Discussion, emmanuel...@gmail.com
We have discussed whether we should provide a `tf.disposeModel()` method to automatically free up the variables of a `tf.Model`.

We haven't made the decision to do it yet, because
1) Layers can be shared among different model instances. So disposing a model may cause other models to be unusable. We need
to think about how to prevent that sort of surprising behavior.
2) If we add the method, it will be a deviation from the Keras API, as Keras doesn't provide a way to dispose models in memory
(even though users can always reset Sessions in Keras with the TensorFlow backend).

Neither of these reasons completely rule out `tf.disposeModel()` in my opinion. We may actually do it if there are enough users
asking for it.

Shanqing

Nikhil Thorat

unread,
Jul 20, 2018, 1:16:36 PM7/20/18
to Shanqing Cai, TensorFlow.js Discussion, emmanuel chappat
I've heard a few users asking for this, so I think we should do it.

Re 1: I think we can solve this by using a layers refcount. When a layer is used in multiple models, increase the refcount. When disposing a layer, decrease refcount. If refcount is 0, remove memory.

Re 2: I don't think diverging from Keras is a big deal here. Python has finalization, and JS doesn't, so there's a fundamental difference we need to solve.

Regarding the API, I think it should be "model.dispose()".

Shanqing Cai

unread,
Jul 20, 2018, 1:40:55 PM7/20/18
to TensorFlow.js Discussion, ca...@google.com, emmanuel...@gmail.com
OK, I created issue https://github.com/tensorflow/tfjs/issues/533 to track this. 

We can discuss the naming of the method (e.g., Model.dispose() vs. tf.disposeModel()). But I agree that such a method will be useful in general.

Kavita Krishnaswamy

unread,
Dec 26, 2019, 7:06:26 PM12/26/19
to TensorFlow.js Discussion
After a model is trained and built, how can rebuild the model without creating a new model? 

Edoh Kodjo

unread,
Dec 27, 2019, 2:31:34 AM12/27/19
to Kavita Krishnaswamy, TensorFlow.js Discussion
You can answer your question on stackoverflow.com. But What do you mean by rebuilding a model ?

El vie., 27 de diciembre de 2019 01:06, Kavita Krishnaswamy <ka...@umbc.edu> escribió:
After a model is trained and built, how can rebuild the model without creating a new model? 

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.

Kavita Krishnaswamy

unread,
Jan 5, 2020, 8:04:51 PM1/5/20
to TensorFlow.js Discussion, ka...@umbc.edu
thanks for your message! I suppose I should've formulated my question with an example.

When we look at the transfer learning with the WebCam in the Pac-Man demo, after we have collected samples and train the model and I can move Pac-Man by playing the game, how can I re-collect new samples creating a new model without refreshing the webpage?

I already tried to dispose the model and the model still continues with old collecting samples. The same challenge occurs when I want to change a different WebCam source to re-collect new samples to train a new model.

Any suggestions with examples please? Is this related to releasing memory?


On Friday, December 27, 2019 at 2:31:34 AM UTC-5, edkeveked wrote:
You can answer your question on stackoverflow.com. But What do you mean by rebuilding a model ?

El vie., 27 de diciembre de 2019 01:06, Kavita Krishnaswamy <ka...@umbc.edu> escribió:
After a model is trained and built, how can rebuild the model without creating a new model? 

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tf...@tensorflow.org.
Reply all
Reply to author
Forward
0 new messages