Difference between Numpy Arrays & Tensorflow Tensors _ Python in Plain English
Difference between Numpy Arrays & Tensorflow Tensors _ Python in Plain English
HEAD 2 HEAD
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
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:
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.
Image by author
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
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.
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
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
More content at plainenglish.io. Sign up for our free weekly newsletter. Get
exclusive access to writing opportunities and advice in our community Discord.
📈
Machine Learning Engineer | Deep Learning Researcher 👨🏻💻 |
https://www.linkedin.com/in/konstantinosgiorgas/
https://python.plainenglish.io/numpy-arrays-vs-tensorflow-tensors-95a9c39e1c17 8/11