Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow2.0 training: model.compile vs GradientTape

I am starting to learn Tensorflow2.0 and one major source of my confusion is when to use the keras-like model.compile vs tf.GradientTape to train a model.

On the Tensorflow2.0 tutorial for MNIST classification they train two similar models. One with model.compile and the other with tf.GradientTape.

Apologies if this is trivial, but when do you use one over the other?

like image 871
piccolo Avatar asked May 09 '19 17:05

piccolo


1 Answers

This is really a case-specific thing and it's difficult to give a definite answer here (it might border on "too opinion-based). But in general, I would say

  • The "classic" Keras interface (using compile, fitetc.) allows for quick and easy building, training & evaluation of standard models. However, it is very high-level/abstract and as such doesn't give you much low-level control. If you are implementing models with non-trivial control flow, this can be hard to accommodate.
  • GradientTape gives you full low-level control over all aspects of training/running your model, allowing easier debugging as well as more complex architectures etc., but you will need to write more boilerplate code for many things that a compiled model will hide from you (e.g. training loops). Still, if you do research in deep learning you will probably be working on this level most of the time.
like image 141
xdurch0 Avatar answered Sep 29 '22 13:09

xdurch0