参考博客:[1] Pytorch 1.8 vs TensorFlow 2.5(2021)[2] PyTorch vs TensorFlow in 2022

我第一次接触深度学习的时候,只知道 PyTorch 和 TensorFlow 两种深度学习框架,对于两者的区别,听的最多的一句话就是“PyTorch 支持动态计算图,TensorFlow则是静态计算图”。
但实际上,Google 在2017年十月就引入了 Eager,实现了 TensorFlow 中的动态图机制。
那么2022年1月5日的今天,PyTorch 最新版本已经到了1.10,TensorFlow 也到了 2.7,那么两者相比,应如何做选择?
这篇博客主要参考上述两篇博客,从 机器学习工程师自身角度、两个框架的使用数据、生态 这些角度做一个总结,作为选择的参考一句。


文章目录

  • 1. 先说结论
  • 2. 博客[1]——一个机器学习从业者的看法
  • 3. 博客[2]——模型可用性、模型的部署、框架生态
  • 3.1 模型可用性(Model Availability):
  • 3.2 模型部署(Deployment Infrastructure):
  • 3.2.1 TensorFlow
  • 3.2.2 PyTorch
  • 3.3 生态(Ecosystems):
  • 3.3.1 PyTorch
  • 3.3.2 TensorFlow



1. 先说结论

时至今日,PyTorch 与 TensorFlow 已经都是较成熟的深度学习框架,两者均有优秀的文档、大量的学习资源、活跃的社区,但两者优势/侧重不同。
PyTorch 在研究领域采用度很高,大家更倾向于用 PyTorch 训练 STOA 的模型;而 TensorFlow 作为工业界的行业框架,在模型部署和生态上更优。


作者认为在性能上,两者相差不大。两者的选择更多要考虑以下三个方面:

  • 代码风格上,Pytorch 是面向对象的风格,经常会把代码弄得很长;TensorFlow / Keras 则代码更短,需要就个人喜好选择。
  • 待解决问题出发,是 Pytorch 相关的现有资源、提供的支持更多,还是 TensorFlow 的更多。
  • API 层级:Pytorch 中包含了更多的 底层细节(low-level details),一方面这给新手提供了足够详细的信息,但另一方面太多细节会使逻辑混乱;而 TensorFlow 则包含了更多的顶层接口 (high-level API)。


3.1 模型可用性(Model Availability):

很多 SOTA(State-Of-The-Art) 模型的训练需要巨大的算力和时间,从头开始并不现实。因此一个 pre-trained 模型非常关键,然后在此预训练模型上做 迁移学习、微调、out-of-the-box inference。

  • HuggingFace (AI Community, Build, train and deploy state of the art models powered by the reference open source in machine learning.) 上的模型分布:


pytorch和tensorflow的区别 pytorch和tensorflow选择_tensorflow


pytorch和tensorflow的区别 pytorch和tensorflow选择_pytorch_02


  • 已发表文章中 两者的比较:


pytorch和tensorflow的区别 pytorch和tensorflow选择_tensorflow_03


下面这张图是从2018到2019研究人员使用框架的改变情况:(数据来自The Gradient => The Gradient is a digital magazine covering research and trends in artificial intelligence and machine learning. We provide accessible and technically informed overviews of the what’s going on AI, as well as a platform for perspectives on recent developments and long-term trends. In short, The Gradient points in the direction of the field.)

pytorch和tensorflow的区别 pytorch和tensorflow选择_Google_04

  • Papers With Code (a website whose mission it is to create a free and open resource with Machine Learning papers, code, datasets, etc.) 上的模型分布:

在 Model Availability 这方面,从数据上看 PyTorch 相比 TensorFlow 更受欢迎。

3.2 模型部署(Deployment Infrastructure):

3.2.1 TensorFlow

  • TensorFlow Serving —— 在服务器(server)上部署 TensorFlow 模型,使用 REST API, TensorFlow RESTful APITensorFlow Serving 在 TensorFlow Extended(TFX) 端到端机器学习平台上使用,能在专门的 gRPC (gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any environment.) 服务器上部署。gRPC 旨在连接多样化的微服务生态系统,因此这些服务器非常适合模型部署。通过 Vertex AI 与 Google Cloud 紧密集成,并与 Kubernetes 和 Docker 集成。
  • TensorFlow Lite —— 在 移动终端、IoT设备、嵌入式设备 中部署 TensorFlow 模型
    TFLite 可用于 Android 和 iOS,以及微控制器(带有 Bazel 或 CMake 的 ARM)和嵌入式 Linux(例如 Coral 设备)。 TensorFlow 的 Python、Java、C++、JavaScript 和 Swift API(截至今年已归档)为开发人员提供了广泛的语言选择。

3.2.2 PyTorch

在 TorchServe 和 PyTorch Live发布之前, PyTorch 用户需要使用 Flask 或 Django 在模型之上构建 REST API

  • TorchServe
    AWS 和 FaceBook 2020 年合作发布的开源部署框架,支持 REST 和 gRPC API。但仍然不如 TensorFlow Serving。
  • PyTorch Live
    PyTorch 在 2019 年首次发布了 PyTorch Mobile,用于在 安卓、IOS和Linux 上端到端部署模型。
    Meta 在 2021年12月初 发布了 PyTorch Live,它使用 JavaScript 和 React Native 创建具有相关 UI 的跨平台 iOS 和 Android 人工智能驱动的应用程序。其 inference 仍旧是通过 PyTorch Mobile 实现。

在 模型部署上,TensorFlow 要优于 PyTorch。

A final note on the issues of model availability and deployment: For those who want to use the TensorFlow deployment infrastructure but want access to models that are only available in PyTorch, consider using ONNX to port the models from PyTorch to TensorFlow.

若想兼顾 Model Availbility 和 Depolyment,即一方面享受 PyTorch 易得到 SOTA 的模型,又想使用 TensorFlow 的部署结构,那么可以用 ONNX (Open Neural Network Exchange, The open standard for machine learning interoperability) 实现从 PyTorch 到 TensorFlow 的迁移。

3.3 生态(Ecosystems):

3.3.1 PyTorch

  • PyTorch Hub —— 可以理解为 PyTorch 官方的 ‘Hugging Face’
  • SpeechBrain —— PyTorch 官方语言处理工具包
  • Ecosystem Tools —— PyTorch 官方提供的库,有 fastai, PyTorch Metric Learning 以及一系列为 CV、NLP 提供的库
  • TorchElastic —— 2020 年由 AWS 和 FaceBook 联合发布,用于分布式训练
  • TorchX —— 用于快速构建和部署机器学习应用程序的 SDK
  • PyTorch Lightning —— 也被称为 ‘Keras of PyTorch’,2019 年发布,用于简化 PyTorch 中的模型工程和训练过程

3.3.2 TensorFlow

  • TensorFlow Hub —— 同 PyTorch Hub
  • Model Garden —— 如其名,“模型花园”,其中包含了 Google 官方、研究人员、社区维护的预训练模型的源代码
  • TensorFlow Extended(TFX) —— TensorFlow 用于模型部署的端到端平台,可使用 TF Sensing / TF Lite 实现在服务器/终端设备上的部署
  • Vertex AI —— Google Cloud 的统一机器学习平台
  • MediaPipe —— 用于构建多模式、跨平台应用机器学习管道的开源框架
  • Coral —— 为打造高效、私密的AI程序而提供的一系列用于原型设计、生产和传感的硬件产品
  • TensorFlow.js —— 用于机器学习的 JavaScript 库,允许用户使用 Node.js 在浏览器和服务器端训练和部署模型
  • TensorFlow Cloud —— 用于连接本地环境到 Google Cloud 的库
  • Google Colab —— 类似 Jupyter,可以在线训练,PyTorch 也可以使用
  • Playground —— 以可视化方式理解神经网络的教学工具
  • Datasets —— 由 Google 定期发布的数据集资源,PyTorch 也可以使用

Google 在各个深度学习相关领域都投入巨资确保产品的发布,在生态方面,TensorFlow 更优。