Tensorflow 2.0 with Keras (TF2) has been popular for deploying production-level machine learning applications, and PyTorch has been the favorite in classroooms. But PyTorch has been gaining ground on TensorFlow, which to use, and do you have to choose?
Firstly, you can't "mix" in TF2 to PyTorch programs because neither framework is purely python code, and they each have their own compiled C++ backends. Rather, pick one framework or the other for an application, to avoid having problematic conflicts in the CUDA environment.
Otherwise, a quick search on these keywords turn up almost too many comparisons; here's one way to look at it:
- PyTorch:
- More pythonic
- Arguably easier to debug
- Extremely customizable (e.g., can control the order of items in the train loop like forward and backward propagation)
- Arguably faster (because of customizability let's you turn things off as needed)
- Arguably easier to install (because the TensorFlow evolution makes it hard to track which versons work with which)
- Great, academic community
- Terrific documentation
- TorchServe, PyTorch Mobile for deployment
- Many companies (Tesla, OpenAI, Microsoft) are adopting PyTorch as official framework of choice or committing to support it.
- TensorFlow:
- Keras abstracts away a lot of the code that PyTorch makes you write (although, Module.nn parallels Keras quite a bit, and PyTorch Lightening alleviates the repetition even further)
- TensoFlow is arguably easier to prototype with, thanks to Keras
- The dynamic graphs ("eager execution") are slower
- Documentation and community comments can be difficult to traverse due to the TF1 to TF2 migration
- Arguably more mature for deployment (1 year older than PyTorch) - "Tensorflow Serving", Tensorflow.js
Several members of an excellent community, "Zero To Mastery", have weighed in with the following advice "know both, choose what is best suited for you for a specific use-case."
The good news is, things have come a long way since 2015, when there were many frameworks, and now there are basically just these two. Both are open source (with links directly to the github repos from the doc), with TensorFlow from Google and PyTorch from Facebook. As of this writing, PyTorch and TF2 are very close in terms of adoption and "popularity". TF1 had static graphs, but TF2 has dynamic graphs (same as PyTorch), which is great but leaves a lot of "residue" online in terms of finding documentation and support.
To summaryize, TF2 has the advantage of inertia and but the weight of legacy technical debt, while PyTorch has the eye of researchers and future entrepreneurs.
Resources:
- ZTM academay community
- https://youtu.be/NuJB-RjhMH4
- https://neptune.ai/blog/moving-from-tensorflow-to-pytorch