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?
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
compile
, fit
etc.) 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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With