TensorFlow is hands down one of my all-time favorite deep learning tools! I was going to call it an AI tool, but let’s be real, ‘AI tools’ these days make people think of image generators, essay writers, or tools to generate one of those weird videos of Will Smith eating spaghetti. Before the ‘AI Boom’ days of 2018–2019, calling something an AI tool meant it was a coding framework to build AI applications. Funny how the meaning has shifted over the years, huh? But that is not what I am writing about today. Today we will learn how to create tensors using TensorFlow in Python.
This TensorFlow in Python guide is primarily for people who are new to deep learning or machine learning frameworks, but I still expect you to have a decent knowledge of Python programming.
Read: The Ultimate Python Cheat Sheet – An Essential Reference for Python Developers
And if you are a complete beginner to the world of Machine Learning, then here are some of the articles that you may like:
- What is Machine Learning? – Supervised & Unsupervised
- KNN Algorithm Using Scikit-Learn – Classifying Iris Species (Tutorial)
We have a lot of material to cover today, so let’s get started.
What is Deep Learning?
Before we understand deep learning. Let’s understand what is machine learning.
Machine learning is the process of writing code that allows machines or computers to learn from observational data and make predictions based on that.
The best part is we don’t have to hand-feed machines every single instruction to make predictions. Past data or observational data is the main ingredient we need. The computer figures out the patterns on its own and gets smarter with experience.
Now, here’s where things get interesting. Deep learning is a subset of machine learning, but it cranks things up a notch.
While traditional machine learning works with structured data and requires you to select the important features manually, deep learning takes a more hands-off approach.
It uses artificial neural networks, a fancy term for algorithms inspired by how our brains work to automatically find patterns and features through data. ANN or artificial neural networks are capable of learning from data that is unstructured or unlabeled.
Why use Deep Learning?
Deep learning isn’t just a buzzword. It’s the engine behind the current AI boom that we are going through. It’s driving innovations from large language models that feel eerily human to self-driving cars that actually stay in their lane.
But why is deep learning stealing the show?
The Traditional Approach: Long Lists of Rules
Once upon a time, solving problems with computers meant creating endless lists of rules. Want to identify whether an image contains a cat? You’d have to manually code rules like:
- “If it has pointy ears and whiskers, it’s probably a cat.”
- “If it meows, chances are high.”
- “If it scratches your sofa, definitely a cat.”
Sounds exhausting, right? And that’s just one problem. What happens when you want to expand your rules to dogs, birds, or, heaven forbid, giraffes? The traditional approach collapses under its own complexity.
Deep learning eliminates this hassle. Instead of you crafting every rule, the model learns the rules by itself from data.
Deep Learning Adapts to New Challenges
The world isn’t static, and neither are the problems we need to solve. That’s where deep learning truly shines. Unlike traditional methods, which struggle with changing environments, deep learning models can be retrained or updated as new data rolls in.
Let’s say you’ve trained a model to recognize fruit. Now, someone asks you to identify not just apples and oranges, but 101 different kinds of food. Creating rules manually for all that? No thanks. With deep learning, you feed the model new data, and it does the job for us.
Discovering Insights in Massive Data Collections
Here’s the thing, we’re drowning in data. Text, images, videos, you name it. Hidden in those terabytes are valuable insights waiting to be uncovered. But imagine trying to manually sift through all that information to find patterns. You’d go gray before you get anywhere.
Deep learning, on the other hand, thrives on this kind of challenge. Whether it’s detecting diseases in medical images or identifying customer trends, deep learning can handle the heavy lifting. It’s like having a detective who never sleeps and gets smarter with every case.
Real-World Applications That Prove the Point
If you’re still wondering why deep learning is worth the hype, just look around:
- Voice Assistants: Siri and Alexa don’t magically understand your accent, they rely on deep learning.
- Self-Driving Cars: Deep learning helps them recognize stop signs, avoid pedestrians, and navigate changing traffic conditions.
- Personalized Recommendations: Ever wondered how Netflix knows what you want to watch next? Yep, deep learning.
These systems wouldn’t work nearly as well, or maybe at all with traditional methods.
Whether you’re dealing with complex problems, mountains of data, or rapidly changing environments, deep learning offers solutions that traditional approaches simply can’t match.
When not to Use Deep learning?
Deep learning is the superstar of the AI world, but it’s not always the right choice. Believe it or not, there are times when you should put the deep learning hype aside and go for a simpler solution.
When You Need Explainability
Deep learning models learn patterns that are often uninterpretable by humans. For instance, if you’re in a field like healthcare or finance, where decisions must be transparent and explainable, deep learning can be a dealbreaker.
Like seriously can you imagine telling a patient, “The AI says you need this treatment, but I can’t tell you why.”. Not something I would want to hear as a patient.
When a Traditional Approach Works Just Fine
If you are a fan of a show known as The Office, you may heard Michael Scott saying to Dwight, keep it simple stupid or aka K.I.S.S. And this mantra goes well for deep learning. You don’t have to overcomplicate things. If you can solve your problems with a basic rule-based system, why bother with deep learning?
For example, if you’re sorting emails based on whether they contain the word “urgent,” a few lines of rule-based code will do the trick. Seriously, no need to unleash a neural network on such a straightforward task.
When Errors Are Not an Option
Although deep learning models are powerful, they aren’t perfect.
Their outputs can be unpredictable, and mistakes are bound to happen. That’s why its important to use a more predictable approach for tasks that are mission-critical.
When You Don’t Have Much Data
This is probably my favorite one. No data? then no AI.
AI, machine learning, or deep learning heavily depends on data. Especially deep learning. It is hungry for data, like a lot of data. So, without enough data or training examples, these models tend to underperform, overfit, or just simply fail.
If you’re working with a small dataset, you’re better off sticking to traditional machine learning or even statistical methods. Deep learning’s real strength comes into play with large datasets where it can learn complex patterns.
When to Ask, “Is Deep Learning Worth It?”
- Before diving headfirst into deep learning, ask yourself these questions:
- Do I need explainable results? If yes, look elsewhere.
- Is my problem complex enough to justify it? If not, save yourself the trouble.
- Can I afford some errors? If no, steer clear.
- Do I have enough data? If not, don’t even think about it.
Knowing when to use fancy tools like deep learning is great, but knowing when not to use them is even better. Sometimes, keeping things simple isn’t only just smart, but it can save you from disastrous outcomes.
What are Neural Networks?
We can’t really start coding neural networks unless we know what they are. I won’t be diving into the specifics of neural networks in this guide.
Related: Neural Network and Deep Learning Foundation – Introduction to Neural Network
But for this tutorial, let’s at least understand the basic concepts and workflow that you need to know.
By definition, a neural network is a fancy term for algorithms mimicking the biological neurons of a human brain. These algorithms are the heart of any deep learning model and their purpose is to figure out complex patterns.
But how exactly do they work?
Turning Data Into Numbers
Before you can feed the data into a neural network, we need to translate the data into numerical values that the model understands.
For example, converting a picture of a flower into numerical pixel values. Or transforming a sentence into embeddings or tokenized numbers. And even sound waves can become a series of numerical amplitudes.
These numbers, representing your data, are fed into the neural network, which learns patterns, features, and relationships to solve the problem at hand. Whether it’s recognizing objects in images, understanding text, or converting audio to text, the process starts with data becoming numbers.
How Does Neural Network Work?
To keep things simple, I want you to think of it as a three-step process. We can talk about the specifics of these steps later.
But here is the breakdown:
- Feed the Data: Once the data is turned into numbers, it’s fed into the neural network.
- Learn Representations: The neural network identifies patterns or features in these numbers, like colors, edges, or shapes in an image.
- Produce Outputs: It transforms the patterns into representation outputs. These might not make sense to us yet. But they’re also just numbers representing what the network has learned.
Turning Outputs Into Human-Readable Information
The neural network’s output is a numerical representation of the patterns it found. It’s up to us to convert this into something meaningful.
For example, you feed in a picture of a flower. The neural network processes it, discovers patterns like petal shapes and colors, and produces a representation output (e.g., “It’s 85% likely to be a rose”). Then you can interpret that output and label it as rose.
Types of Learning in Neural Networks
Neural networks can learn in different ways depending on how much information you give them about the data.
1. Supervised Learning
What it is: The neural network learns from labeled data or data already paired with the correct answer or label.
Example: You have images of flowers labeled with their names, like “rose” or “tulip.” The network learns to predict labels based on the patterns in the images.
2. Semi-Supervised Learning
What it is: The network has a mix of labeled and unlabeled data.
Example: You have 10,000 flower images, but only 1,000 are labeled. The network learns from the labeled images and uses that knowledge to make predictions on the unlabeled ones.
3. Unsupervised Learning
What it is: The network only has data and no labels. It finds patterns or structures on its own.
Example: You give the network a dataset of flower images. It might group them into clusters based on similarities (e.g., grouping roses together) without knowing their names.
4. Transfer Learning
What it is: The network takes what it learned from one task and applies it to a new, similar task.
Example: Training a neural network to recognize animals in photos can be adapted to identify flowers with much less training data.
Why Are Neural Networks So Powerful?
Neural networks shine because they can automatically learn patterns and relationships in data. Especially data that are complex, unstructured, or huge in size. They’re adaptable to new problems, can generalize across tasks, and make life easier by handling data that would be technically impossible for humans to analyze manually.
Anyway, let’s talk about Tensorflow. A powerful deep learning library that I just can’t get enough of.
TensorFlow in Python: The Toolkit for Deep Learning Greatness
Now that we’ve talked about neural networks and how they work their magic, let’s dive into the real MVP of deep learning, which is TensorFlow.
I have been using TensorFlow in Python since 2018 and it has been a regular tool in my arsenal for deep learning because it combines simplicity with power. This library is a game changer for building, training, and deploying deep learning models. If you are not using TensorFlow yet, you are missing out on one of the best software libraries in the AI world.
What Is TensorFlow?
TensorFlow is a complete machine learning platform that takes raw data and transforms it into a fully deployed deep learning model. It supports coding in Python and other languages while enabling you to run your models on specialized hardware like GPUs and TPUs.
The library also includes TensorFlow Hub, a collection of pre-built models that you can easily integrate into your projects.
Whether you are solving tasks in computer vision or natural language processing, TensorFlow provides a head start by offering models we can utilize for specific use cases.
Why TensorFlow?
TensorFlow comes with tons of features that make it stand out from the crowd. Here’s why it’s a favorite for both beginners and experts:
1. Easy Model Building
TensorFlow simplifies the process of creating complex neural network architectures. Its Keras API allows you to design and train models with minimal effort while maintaining flexibility for customization.
2. Scalable Production
It supports deploying models across multiple platforms, including mobile devices, web applications, and even edge devices. For example, training a computer vision model to recognize objects in a smartphone app to identify items in real-time.
3. Powerful Experimentation
Researchers use TensorFlow in Python for its flexibility and capability to test experimental architectures. Moreover, TensorFlow provides robust tools for iterating on models and fine-tuning them for specific tasks.
4. Cost Efficiency
TensorFlow optimizes training by leveraging GPUs and TPUs, significantly reducing the time and computational cost required for deep learning tasks. Additionally, being open source makes TensorFlow in Python an accessible tool for anyone, regardless of budget.
Hardware Support: GPUs and TPUs
TensorFlow utilizes special hardware that accelerates deep learning workflows. Here is a quick overview of GPUs and TPUs:
The primary use of GPUs were originally for rendering graphics, but they’re insanely good at crunching numbers. When it comes to deep learning, GPUs can perform thousands of numerical calculations simultaneously, making them perfect for training neural networks. And, TPUs are even faster than GPUs when handling TensorFlow tasks and are often used for large-scale machine learning projects. Google created TPUs specifically for TensorFlow. These are advanced chips optimized for AI workloads.
Tensors in Neural Networks
In the neural network section we discussed about inputs such as images or text and how we turned those inputs into numerical encodings, fed them into the neural network, and then got outputs that we could interpret.
Well, here’s the thing, both those numerical encodings (inputs) and the representational outputs (outputs) are actually tensors. Sounds easy right?
A Tensor is simply a numerical representation of data. It’s that simple. It is how we encode information into a form that a neural network can process. Whether that information is an image, text, audio or any other type of data. I like to think of tensors as the universal language of deep learning.
How Tensors Work in Neural Networks
Here’s a little break down on how the whole process works:
1. Input as a Tensor: When we encode raw input data (like an image of a flower) into numbers, we are essentially turning it into a tensor.
2. Processing the Tensor: The tensor passes through the neural network. The network analyzes the numbers, learns patterns, and figures out what matters most in those numbers.
3. Output as a Tensor: The network outputs another tensor, which represents the patterns it has learned from the original input tensor.
4. Interpreting the Output: Finally, we take the output tensor and convert it into something that we humans can understand. For instance, like a label (“this is a rose”).
Tensors are how deep learning models see the world. They are the numerical form of data, allowing neural networks to analyze and learn from inputs. Whether it is finding patterns in an image or interpreting the meaning of a sentence, tensors make it possible.
Creating Tensors with TensorFlow in Python
Now that we understand what tensors are, let’s jump into creating them using TensorFlow. For this tutorial I will be using Google Colab.
So open your browser and head over to https://colab.research.google.com/
Colab is a free cloud-based Jupyter Notebook that lets you run Python code and store your projects on Google Drive.
The best part? TensorFlow already comes with it, and you can even use free GPUs. It is perfect if you want to skip the hassle of setting up TensorFlow on your local machine.
If a window pops up, go ahead and click on New Notebook on the bottom left corner.
After that name your notebook and click the Connect button at the top right to start coding.
Creating Tensors with tf.constant()
First, we need to import TensorFlow in Python. Run the following:
import tensorflow as tf
We are using the alias tf, which is the standard convention for TensorFlow in Python, but you can choose any alias you like.
Now that TensorFlow is ready to go, let’s create our first tensor.
Read: NumPy Tutorial for Beginners – Arrays
Creating a Scalar Tensor
Let’s start with the simplest tensor, scalar:
scalar = tf.constant(4)
scalar
When you run the code, you get:
<tf.Tensor: shape=(), dtype=int32, numpy=4>
Here’s what the output tells us:
- tf.Tensor: It’s a tensor object.
- shape=(): It’s a scalar, so there’s no shape (empty).
- dtype=int32: The data type is 32-bit integers.
- numpy=4: The actual value of the tensor.
Now I want to check the number of dimensions (or ndim) of our scaler:
scalar.ndim
Output: 0
A scalar has no dimensions, which is why the ndim is 0.
Creating a Vector Tensor
Now let’s move to a vector, which has one dimension.
Here we will pass a Python list into tf.constant() to create it:
vector = tf.constant([8, 8])
vector
Output:
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([8, 8], dtype=int32)>
Here’s what’s different from the scalar we saw earlier:
- shape=(2,): The vector has two elements.
- numpy=array([8, 8]): A list of two numbers.
Also read: List Comprehension in Python – The Ultimate Tutorial for Beginners
Now let’ts check the dimensions:
vector.ndim
A vector has one dimension, the shape (2,) indicates.
Creating a Matrix Tensor
A matrix has two dimensions. Let’s create one:
matrix = tf.constant([[4, 5], [5, 4]])
matrix
Output:
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[4, 5],
[5, 4]], dtype=int32)>
The shape (2, 2) means we have two rows and two columns.
To check the dimensions:
matrix.ndim
Output:
2
Our matrix here has two dimensions.
Specifying Data Types
By default, tensors created with tf.constant() have the dtype=int32.
Let’s specify a different data type:
newer_matrix = tf.constant([[4., 5.], [5., 4.], [6., 7.]], dtype=tf.float16)
newer_matrix
Output:
<tf.Tensor: shape=(3, 2), dtype=float16, numpy=
array([[4., 5.],
[5., 4.],
[6., 7.]], dtype=float16)>
The dtype=float16 means the tensor uses 16-bit precision, which takes up less memory than int32.
If you ever encounter a data type error in the future, you can adjust it using the dtype parameter.
Creating a 3D Tensor
Let’s level up and create a 3-dimensional tensor:
three_dimension_arr = tf.constant([[[1, 2, 3],
[2, 3, 4],
[4, 5, 6]]])
three_dimension_arr
Output:
<tf.Tensor: shape=(1, 3, 3), dtype=int32, numpy=
array([[[1, 2, 3],
[2, 3, 4],
[4, 5, 6]]], dtype=int32)>
The shape (1, 3, 3) indicates one array containing three rows and three columns.
Checking the dimensions:
3
A three-dimensional tensor has three dimensions.
Relationship between Shapes and Dimensions
Let’s summarize the relationship between shapes and dimensions:
- Scalar: A single number with no dimensions (shape=() and ndim=0).
- Vector: A one-dimensional array (shape=(n,) and ndim=1).
- Matrix: A two-dimensional array (shape=(n, m) and ndim=2).
- Tensor: An n-dimensional array, where n can be 0, 1, 2, or higher.
When I first started learning about tensors, thinking of tensors as levels also helped me. This is an approach that I followed:
- 0D Tensor: Scalar (a single number).
- 1D Tensor: Vector (a list of numbers, like car speed and direction).
- 2D Tensor: Matrix (rows and columns).
- 3D Tensor: 3D Array (layers of matrices).
Now that you know how to to create tensors with tf.constant(), we will explore another way of creating tensors, tf.Variable()
Creating Tensors with tf.Variable()
So far we’ve covered tf.constant(). These are unchangeable tensors, let’s look at its sibling, tf.Variable().
This one’s a bit different because it allows you to create changeable tensors. These types of tensors are perfect for when you need flexibility in your deep learning models.
What Exactly Is tf.Variable()?
A tf.Variable() tensor is mutable, meaning you can change its values after it’s creation. This sets it apart from tf.constant(), which creates unchangeable tensors.
tensor1 = tf.Variable([4, 5])
tensor2 = tf.constant([4, 5])
print("Tensor 1: ", tensor1)
print("Tensor 2: ", tensor2)
Output:
Tensor 1: <tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([4, 5], dtype=int32)>
Tensor 2: tf.Tensor([4 5], shape=(2,), dtype=int32)
At first glance, both look pretty similar. But notice how Tensor 1 uses tf.Variable, while Tensor 2 uses tf.constant. The difference becomes clear when we try to modify their values.
Modifying a tf.Variable() Tensor
Let’s start by indexing Tensor 1:
tensor1[0]
Output:
<tf.Tensor: shape=(), dtype=int32, numpy=4>
This gives us the value 4 at index position 0.
Now, let’s try to change the value at index 0:
tensor1[0] = 8
tensor1
Output:
---------------------------------------------------------------------------
TypeError: 'ResourceVariable' object does not support item assignment
Oops! That didn’t work. Directly assigning a value doesn’t work for tf.Variable objects. But don’t worry, there’s another way.
Using .assign() to Modify Tensors
To change the value of a tf.Variable tensor, you need to use the .assign() method. Here’s how:
tensor1[0].assign(8)
tensor1
Output:
<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([8, 5], dtype=int32)>
Success! We replaced the value 4 with 8 using .assign().
Using .assign() on tf.constant()
Let’s try modifying Tensor 2, created with tf.constant(), using .assign() to see what happens:
tensor2[0].assign(8)
tensor2
Output:
---------------------------------------------------------------------------
AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'assign'
Thus, we get an attribute error. That’s because tf.constant() creates immutable tensors, so their values can’t be changed after creation.
When to Use tf.Variable() vs tf.constant()
- Use tf.Variable() when your tensor needs to change during computation, such as in training models where weights and biases are updated.
- Use tf.constant() for fixed data, like configuration values or inputs that don’t need to change.
Conclusion
If you are interested in working with deep learning, then TensorFlow in Python isn’t just another library you’ll occasionally use. It is your trusted resource for building, experimenting, and deploying AI models. And the best part is, Google made it absolutely free and open source for us to use.
You may like:
- How to Become a Machine Learning Engineer
- How to Become a Data Scientist – The Sexiest Job of 21st Century
In short, tensors are the backbone of deep learning, and understanding them is the first step to unlocking TensorFlow’s full potential. So go ahead, try out the examples, play with the dimensions, and experiment with what you’ve learned. The more you practice, the closer you’ll get to TensorFlow wizardry (okay, maybe not wizardry, but close enough).
TensorFlow is everchanging and evergrowing. Despite using it for the last 6 years, I am constantly learning new things about TensorFlow in Python. What we just learned here is probably the tip of an iceberg. There is so much to learn about TensorFlow and I plan to write about them on my blog as much as i can. So stay tuned and make sure to check out my other guides.