# PyTorch vs TensorFlow for Your Python Deep Learning Project :

**PyTorch vs TensorFlow for Your Python Deep Learning Project**

**by:**

*blow post content copied from Real Python*

click here to view original post

click here to view original post

PyTorch vs TensorFlow: What’s the difference? Both are open-source Python libraries that use graphs to perform numerical computations on data in deep learning applications. Both are used extensively in academic research and commercial code. Both are extended by a variety of APIs, cloud computing platforms, and model repositories.

If they’re so similar, then how do you decide which one is best for your project?

**In this tutorial, you’ll learn:**

- What the differences are between
**PyTorch**and**TensorFlow** - What
**tools**and**resources**are available for each - How to choose the
**best option**for your specific use case

You’ll start by taking a close look at both platforms, beginning with the slightly older TensorFlow. Then, you’ll explore PyTorch and some considerations to help you determine which choice is best for your project. Let’s get started!

**Get Your Decision Guide:** Click here to download the free decision guide that will help you choose between PyTorch and TensorFlow for your Python deep learning project.

## What Is TensorFlow?

TensorFlow was developed by Google and released as open-source in 2015. It grew out of Google’s homegrown machine learning software, which was refactored and optimized for use in production.

The name “TensorFlow” describes how you organize and perform operations on data. The basic data structure for both TensorFlow and PyTorch is a tensor. When you use TensorFlow, you perform operations on the data in these tensors by building a stateful dataflow graph, kind of like a flowchart that remembers past events.

### Who Uses TensorFlow?

TensorFlow has a reputation for being a production-grade deep learning library. It has a large and active user base and a proliferation of official and third-party tools and platforms for training, deploying, and serving models.

After PyTorch was released in 2016, TensorFlow declined in popularity. But in late 2019, Google released TensorFlow 2.0—a major update that simplified the library and made it more user-friendly, leading to renewed interest among the machine learning community.

### Code Style and Function

In TensorFlow 2.0, you can use eager execution, which is how Python normally works. Eager execution evaluates operations immediately, so you can write your code using Python control flow rather than graph control flow. To see this in action, you’ll take a look at how you would multiply two tensors using both Tensorflow 1.0 and 2.0.

To start, here’s an example of how to multiply tensors using TensorFlow 2.0. With eager execution, all you need is `tf.math.multiply()`

:

```
>>> import tensorflow as tf
>>> x = [[2., 4., 6.]]
>>> y = [[1.], [3.], [5.]]
>>> m = tf.math.multiply(x, y)
>>> m
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[ 2., 4., 6.],
[ 6., 12., 18.],
[10., 20., 30.]], dtype=float32)>
```

In this code, you declare your tensors using Python’s list notation, and `tf.math.multiply()`

executes the **element-wise multiplication** immediately when you call it.

Before TensorFlow 2.0, you had to manually stitch together an abstract syntax tree by making `tf.*`

API calls. You then had to pass a set of output tensors and input tensors to a `session.run()`

call and manually compile the model.

A `Session`

object is a class for running TensorFlow operations. It contains the environment in which `Tensor`

objects are evaluated and `Operation`

objects are executed, and it can own resources like `tf.Variable`

objects. The most common way to use a `Session`

is as a context manager.

To see how a `Session`

is used in this way, here’s an example of how you multiply two tensors using the old TensorFlow 1.0 method:

```
>>> import tensorflow as tf
>>> tf.compat.v1.disable_eager_execution()
>>> x = tf.compat.v1.placeholder(tf.float32, name = "x")
>>> y = tf.compat.v1.placeholder(tf.float32, name = "y")
>>> multiply = tf.math.multiply(x, y)
>>> with tf.compat.v1.Session() as session:
... m = session.run(
... multiply, feed_dict={x: [[2., 4., 6.]], y: [[1.], [3.], [5.]]}
... )
... print(m)
[[ 2. 4. 6.]
[ 6. 12. 18.]
[10. 20. 30.]]
```

This code uses TensorFlow 2.x’s `tf.compat`

API to access TensorFlow 1.x methods and disable eager execution.

You first declare the input tensors `x`

and `y`

using `tf.compat.v1.placeholder`

tensor objects. Then you define the operation to perform on them. Note that nothing has been calculated at this point.

Next, using the `tf.Session`

object as a context manager, you create a container to encapsulate the runtime environment and do the multiplication by feeding real values into the placeholders with a `feed_dict`

. Finally, still inside the session, you `print()`

the result.

**Note:** Keep in mind that because TensorFlow 1.0 has been deprecated, it probably isn’t the best option for your future projects, and you should stick with using TensorFlow 2.0.

## Read the full article at https://realpython.com/pytorch-vs-tensorflow/ »

*[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]*

May 08, 2024 at 07:30PM

Click here for more details...

=============================

The original post is available in Real Python by

this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.

The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.

============================

## Post a Comment