CHAPTER 06
Intermediate
TensorFlow Basics and Tensors
Updated: May 16, 2026
6 min read
# CHAPTER 6
TensorFlow Basics and Tensors
1. Introduction
The name "TensorFlow" describes exactly what the framework does: It takes multi-dimensional data structures called Tensors and "flows" them through a graph of mathematical operations (a Neural Network). If you want to master TensorFlow, you must understand what a Tensor is, how it differs from a standard Python list or a NumPy array, and how to manipulate it. In this chapter, we get our hands dirty with core TensorFlow code.2. Learning Objectives
By the end of this chapter, you will be able to:- Define what a Tensor is.
- Create TensorFlow Constants and Variables.
- Perform mathematical operations on Tensors.
- Check Tensor shapes and data types.
- Explain the concept of the Computation Graph.
3. What are Tensors?
A Tensor is a multi-dimensional array of numbers. It is almost identical to a NumPy array, with two major differences:- 1. Tensors can be processed on a GPU (Graphics Processing Unit) for massive speedups.
- 2. Tensors automatically keep track of the mathematical operations applied to them, allowing TensorFlow to automatically calculate calculus gradients for Backpropagation (known as AutoGraph).
Tensor Dimensions (Ranks):
-
0D Tensor (Scalar): A single number. (e.g.,
5)
-
1D Tensor (Vector): An array of numbers. (e.g.,
[1, 2, 3])
- 2D Tensor (Matrix): A grid of numbers. (e.g., A grayscale image).
- 3D Tensor: A cube of numbers. (e.g., A color image with Red, Green, Blue channels).
- 4D Tensor: A batch of color images.
4. TensorFlow Constants
Atf.constant is a Tensor whose value cannot be changed once it is created.
python
5. TensorFlow Variables
While Constants never change, Neural Networks need to constantly update their Weights and Biases during training. We usetf.Variable for this.
python
6. Tensor Operations
TensorFlow has built-in functions for all standard mathematical operations. These operations are heavily optimized for GPU execution.
python
7. Mini Project: Tensor Operations Playground
Let's generate some random tensors and inspect them, a very common task when initializing neural network weights.
python
*Notice .numpy()? If you just want the raw Python number out of a complex Tensor object, call .numpy()!*
8. The Computation Graph
Historically (in TF 1.x), you had to build a massive "Computation Graph" of operations and then "run a session" to execute it. Modern TensorFlow 2.x uses Eager Execution. This means the code runs line-by-line instantly, just like standard Python. However, behind the scenes, TensorFlow is still silently building a highly-optimized C++ graph so that when you train a model, it runs blazingly fast on the GPU.9. Common Mistakes
-
Shape Mismatch in Matrix Multiplication: You cannot use
tf.matmulon two tensors unless the inner dimensions match. E.g., A(3, 2)matrix can multiply with a(2, 5)matrix, but NOT a(3, 5)matrix. This is a strict rule of linear algebra, not just TensorFlow!
-
Mixing Data Types: Trying to add an integer tensor
[1, 2]to a float tensor[1.0, 2.0]will crash. TensorFlow is strictly typed. You must cast them usingtf.cast(tensor, dtype=tf.float32).
10. Best Practices
-
Use
tf.float32: Deep learning relies on decimals (gradients, weights). Ensure your input tensors are usuallyfloat32. Usingfloat64takes up twice as much RAM on your GPU and rarely improves accuracy.
11. Exercises
-
1.
Create a
tf.constant2D matrix filled entirely with the number 1 (Hint: researchtf.ones).
-
2.
Write code to cast an integer tensor
tf.constant([10, 20])into a float tensor.
12. MCQ Quiz with Answers
Question 1
What is the primary difference between a tf.constant and a tf.Variable?
Question 2
Why does TensorFlow use Tensors instead of standard NumPy arrays for deep learning?
13. Interview Questions
- Q: Explain what "Rank" means in the context of a Tensor and give an example of a Rank-2 Tensor.
- Q: Explain what an Eager Execution environment is compared to a static Graph execution environment.
14. FAQs
Q: Should I manually write tensor math for my neural networks? A: No! While understanding tensor math is important, the Keras API (tf.keras) handles all of the complex matrix multiplications and additions for you. You will rarely write raw tf.matmul code unless you are building custom, bleeding-edge architectures.