Lesson 05 TensorFlow
Lesson 05 TensorFlow
TensorFlow
Learning Objectives
Install and setup TensorFlow and TFLearn for building and testing
models
Gain hands-on experience with TensorFlow Playground to visualize
and understand the impact of hyperparameters on model performance
It is an effective and adaptable machine learning library that enables researchers to build innovative
ML models and quickly deploy applications.
Tools Libraries
Community
resources
TensorFlow
In TensorFlow, the data values are not stored as integers, floats, or strings but are encapsulated in an
object called a tensor.
Example of a tensor
It is a powerful AI tool that enables the creation of large-scale neural networks with complex layers.
Features of TensorFlow:
High-level APIs
TensorFlow operates on a system of data flow graphs, which allows for efficient computation and
parallel processing. It is essential for handling the heavy computational requirements of deep learning.
It provides a comprehensive and flexible framework for designing, training, and deploying
deep learning models.
TensorFlow: Dataflow Graph
Parallelism It is easy for the ML system to identify operations that can be executed in parallel.
Distributed It is possible for TensorFlow to distribute programs across multiple devices, CPUs,
execution and GPUs.
Portability It can be built in Python, stored in a saved model, and restored in a C++ program.
Categories of TensorFlow APIs
Flexibility
Parallel computation
Open-source platform
High-level abstraction
TensorFlow: Flexibility
Python API offers flexibility to create all sorts of computations for every
neural network architecture.
TensorFlow utilizes parallel computation techniques to efficiently process data and accelerate the
training of deep learning models.
TensorFlow: Multiple Environment Friendly
Linux
macOS
iOS
Android
Raspberry Pi
Windows
TensorFlow: Open-Source Platform
High-level abstractions in TensorFlow simplify the process of creating deep learning models by providing:
Video detection
6 Image recognition
1
Natural language
5
processing
Handwritten digit
2
classification
Word embedding 4
3 Recurrent neural
networks
Companies Using TensorFlow
Coca-Cola Dropbox
Companies Using TensorFlow
Google
Use-cases: Healthcare
TensorFlow's capabilities in processing and analyzing medical images can significantly aid in diagnosing
diseases, with more speed and precision.
TensorFlow is used to filter toxic chat messages in classrooms in a virtual learning platform.
It is also used to accurately identify a student’s current capabilities and help them decide the most
suitable course of action in the future.
Use-cases: Retail
Let us understand the concept of Tensors and training DNN with TensorFlow using Jupyter Notebooks.
• 5.02_Introduction to Tensors
• 5.03_Hands-on with TensorFlow: Part A
• 5.04_Training DNN Using TensorFlow
Note: Please refer to the Reference Material section to download the notebook files
corresponding to each mentioned topic
Installation of TensorFlow
Prerequisites for Installing TensorFlow
Windows 10 or higher
4 Extract the content from the downloaded zip file and copy it
Setting Up the System
6 Go to the CUDA folder and paste the cuDNN files to the folder v11.2
7 Go to the bin folder and copy the entire path to that location
9 Click on New
Once all the steps are completed, restart the computer before installing TensorFlow.
Installing TensorFlow
TensorFlow can be installed by a single line of code. Once installed, run the following command in the
Python interpreter to ensure successful installation.
Code:
pip install tensorflow
import tensorflow as tf
print(tf.__version__)
Output:
TensorFlow Playground
TensorFlow Playground
Step 2: Set the dataset for the ratio of training to test data, noise, and batch size
Hands-on with the TensorFlow Playground
Step 3: Set the features for the Epoch, Learning rate, Activation, Regularization, Regularization rate, and
Problem type
Hands-on with the TensorFlow Playground
Step 6: Click the play button to pause the timer once the test loss reaches its lowest value
Hands-on with the TensorFlow Playground
Result: It can be observed that within the 649 epoch, a decision boundary is built between two classes.
TFLearn
What Is TFLearn?
TFLearn is a modular and transparent deep learning library built on TensorFlow. It provides a high-
level API to facilitate and speed up experiments while maintaining full transparency and
compatibility with TensorFlow.
Features of TFLearn
The easiest way to install TFLearn is to run the following command using pip:
Layers are a core concept of TFLearn. TFLearn brings layers that represent an abstract set of
operations to make building neural networks more convenient.
File Layers
File Operations
activations linear, tanh, sigmoid, softmax, softplus, softsign, relu, relu6, leaky_relu, prelu, elu
Training functions are another core feature of TFLearn. TensorFlow has no prebuilt APIs to train a
network, so TFLearn integrates a set of functions that can easily handle any neural network
training, for any number of inputs, outputs, and optimizers.
Trainer, Evaluator, and Predictor
In TFLearn, the terms called trainer, evaluator, and predictor refer to specific roles or
functionalities within the framework that handles different aspects of working with neural
network models.
Input
• Any TensorFlow graph can be trained using the helpers functions
offered by TFLearn.
• By introducing real-time monitoring, batch sampling, moving
Train averages, TensorBoard logs, and other data input methods, it is
possible to significantly enhance the convenience of the training
Evaluate process.
• It accepts any quantity of inputs, outputs, and optimization
Predict operations.
Trainer, Evaluator, and Predictor
Syntax:-
import tensorflow as tf
import tflearn
TrainOps can be fed into a Trainer class that will handle the whole training process, considering
all TrainOps together as a whole model.
Syntax:-
#Train the model
model.fit(feed_dicts={input_placeholder: X, target_placeholder: Y},
n_epoch=10, batch_size=128, show_metric=True)
Trainer, Evaluator, and Predictor
TFLearn models are useful for more complex models to handle multiple optimization.
Syntax:-
#Train the model with different feed dictionaries for each training operation
feed_dict1 = {in1: X1, label1: Y1}
feed_dict2 = {in2: X2, in3: X3, label2: Y2}
model.fit(feed_dicts=[feed_dict1, feed_dict2])
Trainer, Evaluator, and Predictor
For prediction, TFLearn implements an Evaluator class that works the same as the
trainer. It takes a parameter and returns the predicted value.
Syntax:-
#Create Evaluator
model = tflearn.Evaluator(network)
#Make predictions
predictions = model.predict(feed_dict={input_placeholder: X})
Trainer, Evaluator, and Predictor
The Trainer class in TFLearn utilizes the is_training boolean variable to handle network behavior
during training, testing, and prediction.
Syntax:-
To make it easy, TFLearn implements functions to retrieve that variable or change its value.
Syntax:-
TFLearn handles the creation and management of logs for training metrics without requiring
manual setup. Currently, it supports a verbose level to automatically manage summaries like:
Visualizing loss and accuracy aids in analyzing and optimizing neural network training.
Epochs Epochs
http://tflearn.org/img/loss_acc.png
Visualization: Layers
Visualizing the layers between convolutional operations and convolutional weight layers provides
insights into feature extraction and transformations within a convolutional neural network.
Epochs Epochs
http://tflearn.org/img/layer_visualization.png
Visualization: Layers
Visualizing the layers between convolutional weight gradients and convolutional bias layers
unveils the influence of biases on feature extraction within a convolutional neural network.
Epochs Epochs
http://tflearn.org/img/layer_visualization.png
Introduction to Keras
Keras
Keras is a high-level deep learning API that simplifies the process of building and training neural network models,
providing a user-friendly interface and extensive support for various deep learning tasks. It is:
Fast
Easy to implement
Modular in nature
What Is Keras?
Keras is closely tied to the TensorFlow library and acts as an interface for it.
It also allows the user to define and train neural network models with only a few lines of code.
Framework Supported by Keras
Theano PlaidML
TensorFlow MXNet
CNTK
Keras: Features
Additionally, it enables the user to define and train neural network models with very little code.
Keras: Backends
Keras
03
Advantages
Advantages of Keras
Keras provides clear and concise APIs. It enables quick experimentation, that allows for
easy and quick development of deep learning models.
Advantages of Keras
Keras is well-documented, with a wide range of tutorials and resources available. It also
has a huge and active community support with several open-source platforms.
Advantages of Keras
One can train a Keras model on one backend and test its results on another. Its
modular design lets a user plug together building blocks with limited restriction.
Advantages of Keras
Pretrained models
Keras provides access to various deep learning models with pretrained weights; one
can use these to make predictions or extract features.
Advantages of Keras
Keras allows one to train the model on a single GPU or use multiple GPUs. It also
provides built-in support for data parallelism and can process a large amount of data.
Keras API Components: Layers
The tf.keras.layers.Layer class is the fundamental abstraction in Keras. A layer encapsulates a state
(weights) and some computation.
tf.keras.Model.fit
It uses a predetermined number of
epochs to train the model.
tf.keras.Model.evaluate tf.keras.Model.predict
It provides the model's loss It predicts the output
and metrics values, which for the input samples.
were set using the
tf.keras.Model.compile
method.
Sequential and Functional API in Keras
Keras framework provides two primary ways to build models: Sequential API and Functional API.
Sequential API: The Sequential API is the simpler of the two and is best suited for models
that have a single input tensor and output tensor. It allows one to create models layer-by-
layer in a linear stack.
Functional API: It is more flexible and powerful, allowing the creation of complex models, such
as multi-input or multi-output models, models with shared layers, and models with non-
sequential data flows.
Sequential API
It is more straightforward to use, making it ideal for building linear stacks of layers where each layer
has one input and output tensor.
Example:
model = Sequential([
Dense(64, input_shape=(input_dim,),
activation='relu'),
Dense(10, activation='softmax')
])
Functional API
It provides more flexibility and allows you to create models with complex architectures, including
multiple inputs and outputs, shared layers, and skip connections.
Example:
input_layer = Input(shape=(input_dim,))
hidden_layer = Dense(64, activation='relu')(input_layer)
output_layer = Dense(10,
activation='softmax')(hidden_layer)
Supports complex
Complexity Limited to simple models
architectures
Let us understand the concept of sequential and functional APIs in TensorFlow and Keras using Jupyter
Notebooks
• 5.09_Sequential_APIs_in_TensorFlow
• 5.10_Functional_APIs_in_TensorFlow
Note: Please refer to the Reference Material section to download the notebook files
corresponding to each mentioned topic
Creating a Keras Model
Step 1 Step 2
Step 3 Step 4
Compile the loss function and evaluate Fit the model using backpropagation
a set of weights and weight optimization with input
data
Creating a Keras Model
Step 5 Step 6
Import statements are used to import specific classes, load the dataset, and set image dimensions.
Syntax:-
# Image dimensions
img_width, img_height = x_train.shape[1], x_train.shape[2]
Step 2: Create the Model
Syntax:-
# Model definition
model = Sequential()
model.add(Convolution2D(16, (5, 5), activation='relu', input_shape=(img_width,
img_height, 3)))
model.add(MaxPooling2D(2, 2))
model.add(Convolution2D(32, (5, 5), activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(10, activation='softmax'))
The above-mentioned code creates a sequential model in Keras with convolutional, pooling, and
dense layers, ultimately forming a network for image classification with 10 classes.
Step 3: Compile the Model
Syntax:-
This syntax compiles the model with binary cross-entropy loss, Adam optimizer, and
accuracy as the metrics for evaluation.
Step 4: Fit the Model
This syntax trains the model on the training data, displays the progress, and
evaluates the performance using validation data.
Syntax:-
# Train the model
model.fit(x_train, y_train,
batch_size=32, • Executes a model for some data
epochs=10, • Trains and iterates data in batches
verbose=1,
validation_data=(x_test, y_test))
Output:
Step 5: Evaluate the Model
This syntax evaluates the trained model on the test data and prints the test loss and test accuracy.
Syntax:-
The syntax makes predictions on the test data, x_test, using the trained model and returns the
predicted classes.
Syntax:-
classes=model.predict(x_test,batch_size=128) Output:
Implementation of Loss Function
The loss function’s task is to estimate the model's error or loss and change the weights in the hidden
layers. This reduces the loss in the next assessment.
Loss functions in TensorFlow are implemented using TensorFlow's computational graph and automatic
differentiation capabilities.
How Are Loss Functions Implemented in TensorFlow?
To implement a loss function, a choice of loss function should first be made so that it fits the framing of
the specific predictive modeling issue.
Syntax:
model.compile(optimizer=tf.optimizers.Adam(),
loss='mae',
metrics='mean_absolute_error')
Assisted Practice
Let us understand the topics of TensorFlow and Keras, using Jupyter Notebook.
Note: Please refer to the Reference Material section to download the notebook files
corresponding to each mentioned topic
Key Takeaways
Keras is a high-level deep learning API designed for easy and fast
model development, built on top of TensorFlow.
Knowledge Check
Knowledge
Check
Which of the following tasks can be performed using TensorFlow?
1
A. Image recognition
C. Video detection
A. Image recognition
C. Video detection
TensorFlow can be used for image recognition, natural language processing, and video detection.
Knowledge
Check
Which of the following defines TensorFlow?
2
TensorFlow is a flexible open-source library for machine learning and artificial intelligence.
Knowledge
Check
What is the sequential API in TensorFlow?
3