0% found this document useful (0 votes)
2 views

Difference between Numpy Arrays & Tensorflow Tensors _ Python in Plain English

Uploaded by

wpaenator
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Difference between Numpy Arrays & Tensorflow Tensors _ Python in Plain English

Uploaded by

wpaenator
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

11/21/24, 10:08 AM Difference between Numpy Arrays & Tensorflow Tensors | Python in Plain English

HEAD 2 HEAD

What is the Difference Between


NumPy Arrays and Tensorflow
Tensors?
What sets them apart, what they share in common, what their
intended use is — explained with coding examples.

Konstantinos Giorgas · Follow


Published in Python in Plain English · 5 min read · Jan 5, 2022

83 1

https://python.plainenglish.io/numpy-arrays-vs-tensorflow-tensors-95a9c39e1c17 1/11
11/21/24, 10:08 AM Difference between Numpy Arrays & Tensorflow Tensors | Python in Plain English

Image by source https://unsplash.com/photos/nc8C8Ns4mM0, edited with permission by the author.

Introduction
So, long story short, when I came across Tensors practicing this elusive
discipline called “Data Science”, I found it hard to understand what sets them
apart from Arrays. To help any aspiring practitioners out there facing the
same problem I did in the (not so distant) past, I will provide a head to head
comparison between those 2 data structures following this 3 part format:

1. Definitions & Differences

2. Intended Uses

3. Coding Comparison

Having said that, this article will not dive deep into the mathematical
properties of tensors but rather provide a comprehensive beginner-friendly
description of the differences between Arrays & Tensors in the context of Data
Science.

Definitions & Differences


Since a picture is worth a thousand words…

Image by author

A NumPy array is a grid of values belonging to the same numerical type.


This homogeneity allows mathematical operations to be more efficient and
https://python.plainenglish.io/numpy-arrays-vs-tensorflow-tensors-95a9c39e1c17 2/11
11/21/24, 10:08 AM Difference between Numpy Arrays & Tensorflow Tensors | Python in Plain English

reduces memory usage in data storage in contrast to other data structures


like python lists and pandas data frames. An array can be one-dimensional
or multi-dimensional. We can think of a multi-dimensional array as a
container of arrays of the same type and size.

import numpy as np
np.array([1, 4, 9, 5])

Image by author

np.array([[1, 4, 9, 5],
[4, 1, 6, 2]])

Image by author

https://python.plainenglish.io/numpy-arrays-vs-tensorflow-tensors-95a9c39e1c17 3/11
11/21/24, 10:08 AM Difference between Numpy Arrays & Tensorflow Tensors | Python in Plain English

A TensorFlow tensor is actually an array on steroids sharing most of the


array’s functionalities and more. Much like arrays, tensors can be one-
dimensional and multi-dimensional or have 0 to n axis as we can more
accurately define. More specifically, a tensor with zero axis contains only a
single value and is called a “scalar”, a tensor with one axis contains a list of
values and is called a “vector”, a tensor with 2 axes is called a “matrix” etc. In
the case of multi-dimensional tensors, each item must be of the same size
just like arrays. Yet, there are types of tensors that can still handle different
item shapes (Ragged Tensors, Sparse Tensors).

Having gotten the basics out of the way, I will now focus on some of their
differences.

To begin with, the main selling point of tensors is, since being part of the
TensorFlow library, using GPU accelerated computing. Simply put, without
any change in code, TensorFlow allows you to parallelize tensor calculations
across the cores of a GPU greatly improving speed against its NumPy array
counterpart. There are still ways to reap the benefits of the beloved GPU
computing with arrays but are not supported natively with the NumPy
library.

Another difference between the 2, responsible for multiple error messages


in IDEs (and dare I say frustration) across the globe is the immutable nature
of tensors. Guys do not try to append, insert, delete or change a value to a
tensor. Once you create a tensor then there is nothing you can do to update
it. You need to create a new one. Of course, that is not the case with NumPy
arrays, showcasing several helper functions to achieve the above forsaken by
tensors functionalities. And the reason behind this is simple. A tensor is not
actually a grid of values but the computations that lead to those values. Thus

https://python.plainenglish.io/numpy-arrays-vs-tensorflow-tensors-95a9c39e1c17 4/11
11/21/24, 10:08 AM Difference between Numpy Arrays & Tensorflow Tensors | Python in Plain English

it does not allow updates in values because it does not store explicitly those
values in the first place. Crazy right?

What is not crazy though is the next difference (my apologies for the cheesy
transition). Tensors, unlike NumPy arrays, can handle other types of values
like strings. Yet, unlike normal python strings where a string is a list of
characters, string tensors treat them as indivisible values.

Last but not least, tensors support automatic differentiation. If you are not
familiar with the term it is much simpler and far sexier than it sounds. What
this basically means is that, given a smooth function y = f(x), what is the
expected change in y(dy) in respect to a change in x (dx) or df(x)/dx. This
notion is used primarily for parameter tuning of neural networks or in
layman’s terms, they help a network fit the data at hand.

Intended Uses

Image by author

Functionalities like GPU computing and automatic differentiation make


tensors ideal data structures to handle the complexity of deep learning
Open in app Sign up Sign in
https://python.plainenglish.io/numpy-arrays-vs-tensorflow-tensors-95a9c39e1c17 5/11
11/21/24, 10:08 AM Difference between Numpy Arrays & Tensorflow Tensors | Python in Plain English
Open in app Sign up Sign in
models, whether you prefer TensorFlow or PyTorch as your go-to
framework. Search Write

In any other case, NumPy arrays will be your everyday driver due to their
ease of use and setup. Generally speaking, one can consider NumPy arrays
as an optimized version of python lists. So apart from using them as the
input data format to machine learning models, they should also be your go-
to structure for any type of scientific computing with Python.

Coding Comparison

https://python.plainenglish.io/numpy-arrays-vs-tensorflow-tensors-95a9c39e1c17 6/11
11/21/24, 10:08 AM Difference between Numpy Arrays & Tensorflow Tensors | Python in Plain English

As you can see most of the commands have identical naming across the 2
libraries and the truth is that they can be used interchangeably as well.
When working with tensors though we prefer using the commands of the
TensorFlow library since they are much more optimized for such operations.
Another thing to consider is the fact that the result of TensorFlow operations
is not an array or a number but a tensor itself.

Conclusion
https://python.plainenglish.io/numpy-arrays-vs-tensorflow-tensors-95a9c39e1c17 7/11
11/21/24, 10:08 AM Difference between Numpy Arrays & Tensorflow Tensors | Python in Plain English

To sum up, comparing NumPy arrays to TensorFlow tensors would be like


comparing apples to oranges. By no means was the scope of this article to
pinpoint the best data structure between the 2, but rather create a clear case
of the purpose of each one and their specifics. Feel free to share any
opinions you might have in the comments.

More content at plainenglish.io. Sign up for our free weekly newsletter. Get
exclusive access to writing opportunities and advice in our community Discord.

Python Numpy TensorFlow Programming Data Science

Written by Konstantinos Giorgas Follow

9 Followers · Writer for Python in Plain English

📈
Machine Learning Engineer | Deep Learning Researcher 👨🏻‍💻 |
https://www.linkedin.com/in/konstantinosgiorgas/

More from Konstantinos Giorgas and Python in Plain English

https://python.plainenglish.io/numpy-arrays-vs-tensorflow-tensors-95a9c39e1c17 8/11

You might also like