AI Unit 5
AI Unit 5
UNIT-5: TENSORFLOW
1. FEATURES OF TENSORFLOW
• Flexibility: TensorFlow supports a broad range of tasks, from simple linear regression to
complex deep neural networks, making it suitable for both research and production.
• Scalability: It can execute computations on various platforms, including multiple CPUs,
GPUs, mobile devices, and embedded systems, enabling efficient large-scale processing.
• Ecosystem: TensorFlow offers a rich set of tools and libraries:
o TensorBoard: Visualization tool for model debugging and performance analysis.
o TensorFlow Lite: Lightweight version for mobile and embedded devices.
o TensorFlow.js: Enables machine learning in web browsers.
• Community Support: As an open-source project, it benefits from a large, active community
that contributes to its development, documentation, and troubleshooting.
• Integration: Seamlessly integrates with popular Python libraries like NumPy, Pandas, and
Keras (now a core part of TensorFlow), enhancing its usability in data science workflows.
• A tensor is just a fancy word for data in any shape — a number, a list, a table, an image
(everything can be seen as a tensor).
• Flow means moving that data through different steps (mathematical operations) to get a result.
In the world of deep learning and artificial intelligence, tensors are the fundamental
units of data representation.
Whether you are dealing with images, sounds, videos, or text, in the background,
everything is represented and manipulated as tensors.
Thus, understanding how to create, modify, transform, and operate on tensors is
an essential skill for any aspiring machine learning practitioner.
1 By kuber kr jha
Unit 5
In simple words, a tensor is just a multi-dimensional container that stores
elements of the same data type (for example, integers or floating-point numbers).
Every tensor has certain important properties that define its structure and behavior:
(A) RANK
Thus, the higher the rank, the more complex the tensor structure becomes.
(B) SHAPE
The shape of a tensor describes the size of the tensor along each of its
dimensions.
For example, a tensor with shape (2, 3) has two rows and three columns.
2. By kuber kr jha
Unit 5
Shape is important because many operations (like addition or multiplication) require
tensors to have compatible shapes.
(C) SIZE
The size of a tensor is the total number of elements stored inside it.
It is calculated by multiplying the numbers in the shape.
For example, a tensor of shape (3, 4) contains 3 × 4 = 12 elements.
Each tensor has a data type, called dtype, which specifies the kind of data it holds.
Common data types include integers (int32), floating-point numbers (float32), booleans,
or even strings.
Handling dtype correctly is critical because mismatched data types can cause errors
during operations
3. CREATING TENSORS
Tensors can be created in several ways, depending on the nature of your data and your
application requirements.
• Converting Python lists or NumPy arrays into tensors using libraries like
TensorFlow.
• Using built-in functions to create tensors filled with zeros, ones, random
values, or sequences.
3. By kuber kr jha
Unit 5
Example in TensorFlow:
import tensorflow as tf
# From list
tensor1 = tf.constant([1, 2, 3])
# From nested list (matrix)
tensor2 = tf.constant([[1, 2, 3], [4, 5, 6]])
# Tensor of all zeros
tensor_zeros = tf.zeros([2, 3])
# Tensor of all ones
tensor_ones = tf.ones([3, 3])
# Random tensor
tensor_random = tf.random.uniform([2, 2], minval=0, maxval=1)
These methods make it extremely easy to generate data structures required for
machine learning models.
Handling tensors effectively means being able to modify their shape, access their
elements, and combine them in different ways.
Let's explore the main manipulation techniques in detail:
Reshaping is critical when preparing data batches for training deep learning models.
4. By kuber kr jha
Unit 5
(B) SLICING AND INDEXING TENSORS
Just like you slice lists in Python, you can slice tensors to access specific elements,
rows, columns, or blocks.
Example:
Sometimes, you need to add or remove dimensions to make tensor shapes compatible
for operations.
These operations are especially important in deep learning models, where input shapes
must match exactly.
transposed = tf.transpose(matrix)
You can combine tensors or split one tensor into multiple smaller ones.
5. By kuber kr jha
Unit 5
• Concatenation merges tensors along a specified axis:
combined = tf.concat([tensor_a, tensor_b], axis=0)
These operations are vital for handling batches and parallel computations.
Examples include:
# Element-wise addition
result = tf.add(tensor1, tensor1)
# Element-wise multiplication
result = tf.multiply(tensor1, tensor1)
# Matrix multiplication
matmul_result = tf.matmul(tensor2, tf.transpose(tensor2))
Learning these operations is crucial because deep learning heavily relies on matrix and
tensor mathematics.
For instance, adding a tensor of shape (3,) to a tensor of shape (2, 3) is possible because
the smaller tensor is broadcasted (copied across rows) automatically.
6. By kuber kr jha
Unit 5
7. DATA TYPE CASTING
At times, you might need to change the data type of a tensor, such as converting an
integer tensor to a floating-point tensor.
This is useful when different operations or neural networks expect tensors of specific
types.
with tf.device('/GPU:0'):
tensor_on_gpu = tf.constant([1.0, 2.0, 3.0])
Efficient device management becomes important for large models and faster
computations.
4. TENSORBOARD VISUALIZATION
TensorBoard is a powerful tool that provides visualizations and insights into your
machine learning models.
It helps you monitor metrics like loss and accuracy, visualize the model graph, examine
histograms, analyze images, and much more.
• Scalars: Plot graphs for loss, accuracy, learning rate over time.
• Histograms: Track how weights and biases change during training.
• Graphs: Visualize the entire structure of the computation graph.
• Images, Audio, Text: Visualize model inputs and outputs.
• Projector: Visualize high-dimensional data like embeddings.
Typical workflow:
7. By kuber kr jha
Unit 5
2. Run a TensorBoard server pointing to the log directory.
3. Open the TensorBoard dashboard in a browser.
Example:
import tensorflow as tf
from tensorflow import summary
log_dir = "logs/"
writer = tf.summary.create_file_writer(log_dir)
@tf.function
def train_step(x, y):
with writer.as_default():
tf.summary.scalar('loss', 0.5, step=1)
tensorboard --logdir=logs/
8. By kuber kr jha
Unit 5
5. TENSORS, VARIABLES, AND AUTOMATIC
DIFFERENTIATION
(A) WHAT ARE TENSORS?
At the heart of TensorFlow (and many other deep learning libraries) lies the concept of
a Tensor.
import tensorflow as tf
# Scalar Tensor
scalar = tf.constant(42)
# Vector Tensor
vector = tf.constant([1, 2, 3])
9. By kuber kr jha
Unit 5
# Matrix Tensor
matrix = tf.constant([[1, 2], [3, 4]])
# 3D Tensor
tensor_3d = tf.constant([[[1], [2]], [[3], [4]]])
• During training, the model's parameters (weights and biases) are constantly
updated to minimize error.
• These parameters must be stored in a way that supports updating — hence,
Variables.
Creating a Variable:
# Create a variable
my_var = tf.Variable([[1, 2], [3, 4]])
10. By kuber kr
jha
Unit 5
(C) AUTOMATIC DIFFERENTIATION
WHY DO WE NEED DIFFERENTIATION IN MACHINE LEARNING?
Example:
# Create a variable
x = tf.Variable(3.0)
# Start recording
with tf.GradientTape() as tape:
y = x ** 2 + 5 * x + 3 # Some computation
11. By kuber kr
jha
Unit 5
dy_dx = tape.gradient(y, x)
print(dy_dx.numpy()) # Output: 2*x + 5 = 2*3 + 5 = 11
Explanation:
• We created a function y = x² + 5x + 3.
• TensorFlow automatically differentiates it and gives the slope (11) at x=3.
✏ IMAGINE THIS:
z = (x + y) * (x - y)
TensorFlow doesn't immediately calculate the result. Instead, it creates a graph like
this:
x ----+
|--> + -->
y ----+ \
* --> z
x ----+ /
|--> - -->
y ----+
12. By kuber kr
jha
Unit 5
Key ideas:
Thus, the whole model is built as a graph structure, not just line-by-line code
execution.
Benefit Explanation
Optimization TensorFlow can rearrange the graph to improve performance (e.g.,
parallelize operations).
Portability Graphs can run on different hardware (CPU, GPU, TPU) without
changing code.
Serialization You can save the graph and reuse it later (deploy it to production
easily).
Efficiency Graph execution can be faster because TensorFlow understands the
"whole picture" and can optimize memory, compute usage.
13. By kuber kr
jha
Unit 5
In short, graphs allow TensorFlow to be smart and efficient behind the scenes.
Example:
x = tf.constant(2)
y = tf.constant(3)
print(x + y) # Output is immediately calculated as 5
But eager execution is slower because TensorFlow cannot optimize the sequence of
operations.
Usage:
@tf.function
def my_function(x, y):
return x * x + y
14. By kuber kr
jha
Unit 5
(C) UNDER THE HOOD OF TF.FUNCTION
1. TensorFlow traces the Python function, meaning it watches what operations you
are using.
2. It builds a computation graph from those operations.
3. It optimizes the graph (for speed, memory usage).
4. It executes the graph in highly efficient TensorFlow runtime (not slow Python).
• You have functions that are called many times, especially inside training
loops.
• You want to improve speed significantly.
• You are building models that will be deployed in production.
print(simple_add(tf.constant(2), tf.constant(3)))
@tf.function
def simple_add(x, y):
return x + y
15. By kuber kr
jha
Unit 5
print(simple_add(tf.constant(2), tf.constant(3)))
Now TensorFlow creates and optimizes a graph internally, leading to faster execution.
In deep learning, a model is typically composed of several layers, each responsible for
performing certain operations like activation, transformation, or regularization. Each
of these layers needs to manage its own weights and computations, which is where
modules come into play.
In TensorFlow, the base class tf.Module can be used to define custom modules:
import tensorflow as tf
class SimpleModule(tf.Module):
def __init__(self, name=None):
super().__init__(name=name)
self.weight = tf.Variable([[1.0, 2.0], [3.0, 4.0]]) # Trainable variable
16. By kuber kr
jha
Unit 5
• The SimpleModule has a trainable variable weight, and the computation is defined
in the __call__ method.
• You can call this module just like a function, passing inputs to compute the result.
(B) LAYERS IN TENSORFLOW
TYPES OF LAYERS
17. By kuber kr
jha
Unit 5
HOW LAYERS WORK TOGETHER IN A MODEL
A neural network is typically built by stacking these layers. Each layer transforms its
input and passes it to the next layer.
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu', input_shape=(784,)),
tf.keras.layers.Dense(10, activation='softmax')
])
• The first layer is a Dense layer with 128 units and ReLU activation.
• The second layer is another Dense layer with 10 units, typically used for
classification (10 classes, hence softmax activation).
Each layer has its trainable weights and will adjust them during training via
backpropagation.
A model in TensorFlow refers to the overall architecture that defines how input data
flows through the network and how it produces output. A model is composed of layers
and handles the training process.
TensorFlow provides several ways to build models, but the most common approach is
using the Keras API, which simplifies the process.
1. Sequential Model: A linear stack of layers. Each layer has one input and one
output.
18. By kuber kr
jha
Unit 5
Example: tf.keras.Sequential([...])
o
2. Functional API: More flexible and allows for multiple inputs and outputs,
shared layers, and non-linear architectures.
o Example:
inputs = tf.keras.Input(shape=(784,))
x = tf.keras.layers.Dense(64, activation='relu')(inputs)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
You can build models using either the Sequential API or Functional API, but the
Keras Model class is the base class for all models.
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
tf.keras.layers.Dense(10, activation='softmax')
])
Once you've built a model, it’s time to train it. Training a model involves the following
steps:
1. Forward Pass: Feed the data into the model to compute predictions.
2. Loss Calculation: Calculate how far off the predictions are from the true labels.
3. Backpropagation: Compute gradients using automatic differentiation.
4. Weight Update: Apply the gradients to update the model’s weights.
19. By kuber kr
jha
Unit 5
TRAINING LOOP
Here:
Once the model is trained, it can be evaluated using evaluation functions or used for
making predictions.
• The evaluate() function computes the test loss and accuracy on unseen data.
MAKING PREDICTIONS
predictions = model.predict(test_data)
• provides the predicted outputs (e.g., class probabilities or values) for the
predict()
given input data.
(F) SAVING AND LOADING MODELS
TensorFlow makes it easy to save your models and load them later.
• The model, including its architecture, weights, and training configuration, can be
saved and reused.
20. By kuber kr
jha
Unit 5
8. TRAINING LOOPS IN TENSORFLOW
(A) WHAT IS A TRAINING LOOP?
A training loop is the sequence of operations that you repeat during the training
process of a machine learning model. It typically consists of:
1. Forward Propagation: Pass the inputs through the model to get the
predictions.
2. Loss Calculation: Compare the predictions to the true labels to compute the
loss (how "wrong" the model is).
3. Backpropagation: Compute the gradients using automatic differentiation
to understand how to update the weights.
4. Weight Update: Use the optimizer to adjust the weights in a direction that
reduces the loss.
5. Iteration: Repeat the process for a certain number of iterations or epochs.
THE TRAINING LOOP IS REPEATED MULTIPLE TIMES OVER ALL THE DATA IN THE
DATASET AND ACROSS SEVERAL EPOCHS TO ENSURE THE MODEL LEARNS WELL.
(B) STEPS IN A TRAINING LOOP
1. INPUT DATA: FEEDING DATA TO THE MODEL
At the start of each training iteration, data (inputs) is fed into the model. This data is
typically batched to ensure more efficient computation.
The input data is passed through the layers of the neural network (from input to
output). Each layer performs its specific computation, and the final output (prediction)
is produced.
# Forward pass
predictions = model(batch_inputs)
The model's output is compared to the ground truth labels (true labels), and the
loss is computed. The loss function measures how well the predictions match the
actual labels.
21. By kuber kr
jha
Unit 5
3. LOSS CALCULATION: MEASURING THE ERROR
The loss quantifies how far off the model’s predictions are from the actual outputs. For
example, in a classification task, this could be cross-entropy loss; for regression, it
could be mean squared error.
# Loss computation
loss = loss_function(batch_labels, predictions)
Backpropagation involves computing the gradients of the loss function with respect to
the model’s trainable parameters (weights). TensorFlow’s Gradient Tape is used
for automatic differentiation to compute these gradients.
Once the weights are updated, the loop continues to the next iteration or epoch,
repeating the process for more batches or epochs. Each pass through the data helps the
model learn better representations and makes the predictions more accurate.
22. By kuber kr
jha
Unit 5
(C) HOW TO IMPLEMENT A TRAINING LOOP
USING TENSORFLOW’S MODEL.FIT()
In TensorFlow, you can use the built-in fit() method to handle the training loop for
you. This abstracts away much of the complexity of the manual training loop. Here's
how you can use it:
This function:
• Iterates over the entire dataset for the specified number of epochs.
• Performs forward propagation, loss calculation, backpropagation, and weight
update automatically.
Advantages:
If you need more control over the training process (for example, custom gradient
updates, adding callbacks, or managing different types of metrics), you can write a
custom training loop:
# Backpropagation
gradients = tape.gradient(loss, model.trainable_variables)
# Optimizer update
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
23. By kuber kr
jha
Unit 5
BENEFITS OF CUSTOM TRAINING LOOPS:
• Flexibility: You can easily change how you compute losses, apply custom
learning rates, or modify the optimizer.
• Control: You can add features like logging, custom callbacks, or experimenting
with different training techniques.
• Learning Rate Schedulers: Adjust the learning rate during training (e.g.,
reduce learning rate on plateau).
24. By kuber kr
jha
Unit 5
5. GRADIENT CLIPPING: SOMETIMES GRADIENTS CAN BECOME VERY LARGE, CAUSING
THE MODEL TO DIVERGE. GRADIENT CLIPPING IS A TECHNIQUE USED TO CLIP
GRADIENTS DURING BACKPROPAGATION TO PREVENT THEM FROM EXCEEDING A
CERTAIN THRESHOLD.
6. REGULARIZATION: TECHNIQUES LIKE L2 REGULARIZATION OR DROPOUT ARE
OFTEN APPLIED DURING TRAINING TO PREVENT THE MODEL FROM OVERFITTING.
(E) MONITORING TRAINING PROGRESS
1. METRICS:
2. CALLBACKS:
Callbacks are functions or methods that are called during certain points in the training
loop. Common callbacks include:
3. VISUALIZATION:
You can visualize the training progress using TensorBoard or by plotting the loss and
accuracy curves over time.
TensorFlow Playground is an interactive web tool that allows you to visually explore
and experiment with the basic concepts of neural networks. It provides an easy-to-use
interface where you can adjust different parameters, train a simple neural network, and
25. By kuber kr
jha
Unit 5
observe how it behaves as you modify the architecture and hyperparameters. It is an
excellent tool for understanding the mechanics of neural networks and
Data refers to the input that will be fed into the neural network for training and
testing. TensorFlow Playground allows you to choose different datasets, including
simple 2D datasets and more complex ones with features and labels.
TYPES OF DATA:
26. By kuber kr
jha
Unit 5
4. Gaussian Data: Randomly generated data points, often used for testing the
robustness of the model.
CUSTOMIZING DATA:
In TensorFlow Playground, you can control how the data is split into training and
test datasets. This is crucial for evaluating the generalization ability of the neural
network.
• If too much data is used for testing, the model may not learn enough from the
training data, leading to underfitting.
• Conversely, if too little data is used for testing, the model might not be able to
generalize to unseen examples, leading to overfitting.
3. FEATURES
In the context of neural networks, features refer to the inputs to the model that are
used to make predictions.
27. By kuber kr
jha
Unit 5
HOW FEATURES IMPACT MODEL PERFORMANCE:
In TensorFlow Playground, you can experiment with the number of features, their
interactions, and how they influence the model's ability to make accurate predictions.
4. HIDDEN LAYERS
The hidden layers are intermediate layers between the input and output layers in a
neural network. These layers play a key role in learning complex patterns from the
data.
• Single Hidden Layer: For simpler problems, a single hidden layer might be
sufficient.
• Multiple Hidden Layers: For more complex problems (like spiral or XOR
problems), more layers are required to capture the non-linear relationships
between input features.
KEY FACTORS TO CONSIDER:
• Too Few Layers: The network might not be able to capture the complexity of
the data (underfitting).
• Too Many Layers: The network might overfit the training data and perform
poorly on unseen data (overfitting).
28. By kuber kr
jha
Unit 5
5. EPOCHS
An epoch refers to one complete pass of the entire dataset through the model during
training. In TensorFlow Playground, you can adjust the number of epochs to see how
the model's performance improves over time.
• The number of epochs determines how long the model will train. With too few
epochs, the model might not learn enough from the data (underfitting).
• With too many epochs, the model may start memorizing the training data
(overfitting) and fail to generalize to new, unseen data.
In TensorFlow Playground, you can observe how the model's error decreases over each
epoch and how it stabilizes or increases as training continues.
6. LEARNING RATE
The learning rate controls how much the model's weights are updated with respect to
the computed gradient during training. It is a hyperparameter that significantly affects
the performance of the training process.
• High Learning Rate: The model may converge too quickly and overshoot the
optimal solution, resulting in poor performance.
• Low Learning Rate: The model may converge very slowly and might get stuck
in local minima, making the training process inefficient.
In TensorFlow Playground, you can adjust the learning rate to see how it influences the
model's ability to converge during training.
7. ACTIVATION FUNCTION
The activation function introduces non-linearity into the model, allowing it to learn
more complex patterns from the data. TensorFlow Playground allows you to choose
from several activation functions.
29. By kuber kr
jha
Unit 5
COMMON ACTIVATION FUNCTIONS:
• The activation function in the hidden layers determines the network's ability
to learn complex relationships between input features.
• Choosing the right activation function is important for model performance, as
each has different properties, such as how quickly they learn and whether they
suffer from problems like vanishing gradients.
8. REGULARIZATION
TYPES OF REGULARIZATION:
30. By kuber kr
jha
Unit 5
2. Multi-class Classification: More than two classes (e.g., classifying images into
categories).
3. Regression: Predicting continuous values (e.g., predicting house prices).
HOW PROBLEM TYPE AFFECTS NETWORK DESIGN:
31. By kuber kr
jha