来源:​​https://medium.com/aimluae/tensorflow-vs-pytorch-which-will-be-the-top-deep-learning-framework-in-2022-a3488635029​


原作者观点(本人也赞同):如果你是深度学习领域的初学者,那么我的建议是先从Tensorflow开始;但是,一旦你对端到端的深度学习管道和不同的模型架构足够强大,最好开始尝试使用PyTorch。


Introduction

PyTorch and Tensorflow are two of the most popular deep learning libraries available today. PyTorch was developed by ​​Facebook’s AI Research Lab​​ to make deep neural networks easier to build and use. TensorFlow was initially authored by ​Google Brain Team​ which offers a flexible representation of data, allowing you to build custom machine learning models that range from linear regression to deep neural networks. So it’s common to hear people say.

“Use [Deeplearning Library] because it’s faster or it’s more flexible.” But which one should you pick?

History

​TensorFlow ​​was developed for internal Google use in research and production. The initial version was released in November 2015. Then Google released the updated version of TensorFlow, named TensorFlow 2.0, in September 2019 with official inbuilt Keras support.

​Keras​​ is an open-source software library that provides a higher-level API for other deep learning libraries. Keras was developed as part of the research effort of project ONEIROS (Open-ended Neuro-Electronic Intelligent Robot Operating System) in March 2015. Up until version 2.3, Keras supported multiple backends, including TensorFlow, Microsoft Cognitive Toolkit, Theano, and PlaidML. As of version 2.4, Keras acts as an interface for the TensorFlow library. its primary author and maintainer is ​​François Chollet​​, a Google engineer.

​PyTorch​​ was started in October 2016, as an internship project by Adam Paszke In Facebook’s AI Research lab (FAIR). Along with Sam Gross, Soumith Chintala, Gregory Chanan.

Popularity & Adoption

Github star is an indicator of popularity among the developer community and ​​Tensorflow ​​has got 3x more GitHub stars than ​​PyTorch​​ while this writing.

Tensorflow 165K ⭐️ | PyTorch 56.4k ⭐️

The PyTorch framework has been gaining popularity in recent years. Google Trends data confirms that interest in PyTorch is growing rapidly and its overtaken TensorFlow & Keras. If you look at the chart below, then you can see that it’s been growing in popularity at the time of this writing.

A vast majority of State Of The Art (SOTA) models in ​​HuggingFace​​ are in PyTorch. And most of the research papers published in recent years are used PyTorch as their de facto framework.

In terms of job listing in different platforms, Tensorflow is leading.

API Level Comparison

Tensorflow is more beginner friendly than PyTorch, In PyTorch, you can define neural network layers by subclassing, which means you have to put all the different layers of the neural network inside a class so you can make use of the python class features whereas in Tensorflow you can either use the sequential API which is more beginner friendly and easy to write each layer of the neural network just line by line then finally train the model just using a single command model.fit() with relevant parameters or you can also use subclassing which gives more flexibility to write inside a class like PyTorch which is recommended to use if you are an advance level user.

PyTorch is like Numpy but optimized for GPUs so it is more pythonic, while TensorFlow is less Pythonic because it was a C++-based library ported to Python and it is also optimized for GPU. PyTorch supports dynamic auto-differentiation (autodif) which means you can perform partial derivative calculus on any kind of object very easily and have more flexibility to change your model architecture at any point during training.

Earlier TensorFlow 1.0 and Keras were based on the static graph which made less flexibility to change the model architecture during training, but the TensorFlow 2.0 with Keras integrated also supports dynamic graphs using eager execution. In PyTorch, you can define neural network layers using subclassing whereas in Tensorflow, Keras you can either use the sequential API or subclassing.

Ecosystem & Tools

Tensorflow is widely used at the production level in Industry while PyTorch is more popular in the research community. Both TensorFlow and PyTorch provide a set of tools that include:

Tensorflow

​TensorFlow Serving​​ is for deploying TensorFlow models on servers, be them in-house or on the cloud, and is used within the ​​TensorFlow Extended​​ (TFX) end-to-end Machine Learning platform. Serving makes it easy to serialize models into well-defined directories with model tags and select which model is used to make inference requests while keeping server architecture and APIs static.

​TensorFlow Extended​​ is TensorFlow’s end-to-end platform for model deployment. You can load, validate, analyze, and transform data; train and evaluate models; deploy models using Serving or Lite, and then track artifacts and their dependencies.

​TensorFlow Lite​​ (TFLite) is for use when deploying TensorFlow models on mobile or IoT/embedded devices. TFLite compresses and optimizes models for these devices, and more widely addresses 5 constraints for on-device Artificial Intelligence — latency, connectivity, privacy, size, and power consumption.

​Tensorflow Hub ​​contains TensorFlow, TensorFlow Lite, and TensorFlow.js models for different use cases, with models available for image, video, audio, and text problem domains.

​Model Garden​​ is a repository that makes the source code for SOTA models available.

​Vertex AI​​ is Google Cloud’s unified Machine Learning platform. which is to unify services on ​​GCP​​, ​​AI Platform​​, and ​​AutoML​​ into one platform. Vertex AI can help you automate, monitor, and govern Machine Learning systems by orchestrating workflows in a serverless manner.

​MediaPipe​​ is a framework for building multimodal, cross-platform applied Machine Learning pipelines which can be used for face detection, multi-hand tracking, object detection, and more.

​Google Coral​​ was created to address local AI rather than cloud-based in many industries and is a complete toolkit to build products with local AI.

​TensorFlow.js​​ is a JavaScript library for Machine Learning that allows you to train and deploy models both in the browser and server-side with Node.js.

​TensorFlow Cloud​​ is a library that allows you to connect your local environment to Google Cloud.

​Playground​​ is a small but polished educational tool for understanding the basics of neural networks. It offers a simple dense network visualized inside a clean UI.

PyTorch

​PyTorch Hub​​ — a research-oriented platform for sharing repositories with pre-trained models. Hub has a wide range of models, including those for Audio, Vision, and NLP.

​TorchServe​​ which is an open-source deployment framework resulting from a collaboration between AWS and Facebook and was released in 2020. It has basic features like endpoint specification, model archiving, and observing metrics.

​PyTorch Live​​ was released at the beginning of December 2020 to build upon Mobile. It uses JavaScript and React Native to create cross-platform iOS and Android AI-powered apps with associated UIs.

​TorchVision​​ is PyTorch’s official Computer Vision library. It includes everything you need for your Computer Vision projects, including ​​model architectures​​ and popular ​​datasets​​.

If your area of expertise is Natural Language Processing rather than Computer Vision, then you might want to check out ​​TorchText​​. It contains datasets frequently seen in the NLP domain, as well as data processing utilities to operate on these and other datasets.

​TorchAudio​​ — PyTorch’s official audio library. TorchAudio includes popular audio ​​models​​ like DeepSpeech and Wav2Vec, and provides ​​walkthroughs​​ and ​​pipelines​​ for ASR and other tasks.

​SpeechBrain​​ — an open-source speech toolkit for PyTorch. SpeechBrain supports ASR, speaker recognition, verification and diarization.

​TorchElastic​​ was released in 2020 and is the result of collaboration between AWS and Facebook. It is a tool for distributed training which manages worker processes and coordinates restart behaviors so that you can train models on a cluster of compute nodes which can change dynamically without affecting training.

​PyTorch Lightning​​ is sometimes called the Keras of PyTorch. Lightning is a useful tool for simplifying the model engineering and training processes in PyTorch.

Conclusion

PyTorch has a lot more features, even improved upon Tensorflow. PyTorch serves as Tensorflow serving in Tensorflow, PyTorch Live for Tensorflow Lite & PyTorch Hub as for Tensorflow Hub, and every time a new feature is released on one framework will be available on the other within a short period. So it’s again a difficult choice to make.

It all depends on the use case that you are having. If you are a beginner in the deep learning world then my suggestion is to get started with Tensorflow first and once you are strong enough with the end-to-end deep learning pipelines and different model architectures it’s better to try PyTorch.