Comparison between PyTorch and TensorFlow

Sidra Naseem
DiveDeepAI
Published in
5 min readDec 13, 2021

Introduction

In recent decades deep learning has become standard because of its high accuracy and efficiency. With increasing usage of deep learning techniques in different areas of life, a variety of deep learning frameworks have been introduced and developed. A few well-known frameworks are PyTorch, TensorFlow, Keras, Caffe, and Chainer. In this post, i am going to discuss PyTorch and TensorFlow, which are most popular among all. Before starting the main content, it is necessary to have some insights about both frameworks.

  • Both TensorFlow and PyTorch make history by providing benchmark results in application of many deep learning models.
  • Both frameworks have a gigantic community and developers that use them in research labs or incubation centers for simulations to bring uniqueness in results using respected frameworks.
  • PyTorch is known to be used in research labs or academic centers while TensorFlow is popular among industry professionals.

PyTorch started as a tool majorly contributed by developers at Facebook and TensorFlow by Google, and they both bring uniqueness to what can be achieved by these frameworks. Rest of the blog briefly describes the Computational Graphs and Eager Execution. After this the reasoning is provided to justify the upgrade of TensorFlow 1.0 to TensorFlow 2.0 version.

Computational Graphs

Computational Graphs represent machine learning algorithms in TensorFlow. In TensorFlow, it is a type of directed graph whose nodes work like operations and edges contain data that flows in between different operations. Model development via computational graphs is also known as dataflow programming. These graph models can be used to achieve correspondence and Distributed execution through multiple devices such as; CPUs, GPUs, and TPUs. These graph based models can also provide automatic differentiation and graph optimization. Once a graph is created after the graph execution, it can be stored, run, and restored efficiently without having the need of original python code. This feature of computational graphs makes them more flexible for cross-platform applications. The stored graph can be used to embed anywhere, even though python is not available.

Source: TensorFlow website

Eager execution

Eager execution is used for model building instantly and visualizing results instantly. It is the best option for naïve developers or beginners to use eager execution because it is easy to test and intuitive. For research and experimentation, it is a better option because it reduces the need for cyclical boilerplate code segments. Since, the interface of eager execution is intuitive with regular python code and data structures, the debugging process with eager execution is easier. It also supports additional processing units like GPU and TPU acceleration.

Why did TensorFlow adopt Eager Execution? (TensorFlow 1.0 vs TensorFlow 2.0.)

The libraries of TensorFlow are enriched with tensors that stream through a graph which results in computational graphs containing operations and variables. For beginners who do not have enough knowledge to interpret computational graphs since everything is part of graphs. Development of a TensorFlow model requires you to write a complete code before running and especially when it comes to debugging your code and visualizing partial results. However, TensorFlow allows you to get results while running the operations in eager execution mode and you don’t need to wait for completing and connecting the code. Let us take a look at this very basic example with and without Tensorflow eager mode to understand the difference between graph computations and eager execution.

After executing the first cell in the above code, the results are not shown because evaluation has not been triggered yet in TensorFlow 1.0. To look at results we have to wrap our code into a tf.session which means it has to wait for complete evaluation of code until the graph building is completed. Now let us take a look at the same code snippet with tensorflow eager mode. It can be observed in the following code that tensorflow eager mode helps displays results instantly.

TensorFlow vs PyTorch

Graph execution was preferred by TensorFlow before the 2.0 version because graph execution provides quick, efficient, and compliant results. However, all difficulties faced by experienced programmers whilst implementing tensorflow codes were like trade-offs. On the other hand, PyTorch provides the facility of dynamic computation of graphs which is quite similar to eager execution and therefore, it got popular among beginners of AI programmers and researchers. One of the main reasons why Pytorch became famous is due to its dynamic execution feature since it provides an instinctive edge instead of graph interpretations and it also made partial results evaluation easy and simple. However, it is not as efficient and quick as graph execution in TensorFlow 1.0.

TensorFlow replaces the default execution method from graph to eager execution after realizing the increase in popularity of PyTorch and graph execution becomes an optional mode in TensorFlow. So similar to PyTorch, Tensorflow sets eager execution as default and you can also use graph execution if you are deliberate to use a static computational graph that is more efficient anyhow. By changing the default execution mode to eager execution TensorFlow turned into a novice-friendly framework which increased its popularity among beginners and became a competitor of Pytorch and becoming first choice against Pytorch. Nevertheless, PyTorch is also a great substitute for beginners to develop and train deep learning models.

Conclusion

Typically, software development life cycle lives in the trial and error process which includes executing some code, running into some errors and implementing few modifications accordingly. So the sooner you can test your code and see the errors, the more efficient and effective you can be. Since both Tensorflow and Pytorch now provide visualization of partial results and mutable graph implementations, It is now feasible for beginners to start off their machine learning projects with either of the frameworks and analyze their outcomes on an intuitive interface.

--

--