Full Artificial Neural Networks With TensorFlow 2: ANN Architecture Machine Learning Projects Poornachandra Sarang Ebook All Chapters
Full Artificial Neural Networks With TensorFlow 2: ANN Architecture Machine Learning Projects Poornachandra Sarang Ebook All Chapters
com
https://textbookfull.com/product/artificial-
neural-networks-with-tensorflow-2-ann-
architecture-machine-learning-projects-
poornachandra-sarang/
https://textbookfull.com/product/applied-neural-networks-with-
tensorflow-2-api-oriented-deep-learning-with-python-orhan-gazi-yalcin/
textbookfull.com
https://textbookfull.com/product/matlab-deep-learning-with-machine-
learning-neural-networks-and-artificial-intelligence-1st-edition-phil-
kim/
textbookfull.com
https://textbookfull.com/product/theoretical-approaches-to-multi-
cultural-positive-psychological-interventions-llewellyn-ellardus-van-
zyl/
textbookfull.com
Ronald J Fisher A North American Pioneer in Interactive
Conflict Resolution 1st Edition Ronald J. Fisher
https://textbookfull.com/product/ronald-j-fisher-a-north-american-
pioneer-in-interactive-conflict-resolution-1st-edition-ronald-j-
fisher/
textbookfull.com
https://textbookfull.com/product/psychology-5th-edition-saundra-k-
ciccarelli/
textbookfull.com
https://textbookfull.com/product/principles-of-animal-research-
ethics-1st-edition-tom-l-beauchamp-david-degrazia/
textbookfull.com
https://textbookfull.com/product/train-your-mind-to-be-successful-1st-
edition-sayra-montes/
textbookfull.com
Uncommon Causes of Stroke 3rd Edition Louis Caplan
https://textbookfull.com/product/uncommon-causes-of-stroke-3rd-
edition-louis-caplan/
textbookfull.com
Artificial Neural
Networks with
TensorFlow 2
ANN Architecture Machine
Learning Projects
—
Poornachandra Sarang
Artificial Neural
Networks with
TensorFlow 2
ANN Architecture Machine
Learning Projects
Poornachandra Sarang
Artificial Neural Networks with TensorFlow 2: ANN Architecture
Machine Learning Projects
Poornachandra Sarang
Mumbai, India
Acknowledgments���������������������������������������������������������������������������xxiii
Preface��������������������������������������������������������������������������������������������� xxv
iii
Table of Contents
Installation����������������������������������������������������������������������������������������������������������18
Installation�����������������������������������������������������������������������������������������������������19
Docker Installation�����������������������������������������������������������������������������������������20
No Installation�����������������������������������������������������������������������������������������������20
Testing����������������������������������������������������������������������������������������������������������������20
Summary������������������������������������������������������������������������������������������������������������23
iv
Table of Contents
v
Table of Contents
ImageNet Classifier�������������������������������������������������������������������������������������������140
Setting Up Project����������������������������������������������������������������������������������������140
Classifier URL����������������������������������������������������������������������������������������������141
Creating Model��������������������������������������������������������������������������������������������142
Preparing Images����������������������������������������������������������������������������������������143
Loading Label Mappings�����������������������������������������������������������������������������146
Displaying Prediction�����������������������������������������������������������������������������������146
Listing All Classes����������������������������������������������������������������������������������������148
Result Discussions��������������������������������������������������������������������������������������149
Dog Breed Classifier�����������������������������������������������������������������������������������������149
Project Description��������������������������������������������������������������������������������������150
Creating Project�������������������������������������������������������������������������������������������151
Loading Data�����������������������������������������������������������������������������������������������151
Setting Up Images and Labels���������������������������������������������������������������������154
Preprocessing Images���������������������������������������������������������������������������������158
Processing Image����������������������������������������������������������������������������������������159
Associating Labels to Images����������������������������������������������������������������������161
Creating Data Batches���������������������������������������������������������������������������������161
Display Function for Images������������������������������������������������������������������������164
Selecting Pre-trained Model������������������������������������������������������������������������165
Defining Model��������������������������������������������������������������������������������������������166
Creating Datasets����������������������������������������������������������������������������������������169
Setting Up TensorBoard�������������������������������������������������������������������������������170
Model Training���������������������������������������������������������������������������������������������172
Examining Logs�������������������������������������������������������������������������������������������173
Evaluating Model Performance�������������������������������������������������������������������175
Predicting on Test Images���������������������������������������������������������������������������175
vi
Table of Contents
vii
Table of Contents
Model Building��������������������������������������������������������������������������������������������������206
Visualization Function for Metrics���������������������������������������������������������������206
Small Model�������������������������������������������������������������������������������������������������207
Medium Model���������������������������������������������������������������������������������������������211
Large Model�������������������������������������������������������������������������������������������������215
Fixing Overfitting�����������������������������������������������������������������������������������������218
Result Discussion����������������������������������������������������������������������������������������223
Loss Functions��������������������������������������������������������������������������������������������������224
Mean Squared Error������������������������������������������������������������������������������������225
Mean Absolute Error������������������������������������������������������������������������������������225
Huber Loss���������������������������������������������������������������������������������������������������226
Log Cosh������������������������������������������������������������������������������������������������������227
Quantile�������������������������������������������������������������������������������������������������������227
Optimizers���������������������������������������������������������������������������������������������������������228
Summary����������������������������������������������������������������������������������������������������������229
Chapter 6: Estimators�����������������������������������������������������������������������231
Introduction�������������������������������������������������������������������������������������������������������231
Estimator Overview�������������������������������������������������������������������������������������������232
API Stack�����������������������������������������������������������������������������������������������������232
Estimator Benefits���������������������������������������������������������������������������������������234
Estimator Types�������������������������������������������������������������������������������������������235
Workflow for Estimator-Based Projects�������������������������������������������������������237
Premade Estimators������������������������������������������������������������������������������������������242
DNNClassifier for Classification������������������������������������������������������������������������242
Loading Data�����������������������������������������������������������������������������������������������243
Preparing Data���������������������������������������������������������������������������������������������244
viii
Table of Contents
ix
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Table of Contents
Model Training���������������������������������������������������������������������������������������������279
Evaluation����������������������������������������������������������������������������������������������������280
Project Source���������������������������������������������������������������������������������������������280
Custom Estimators for Pre-trained Models�������������������������������������������������������283
Creating Project�������������������������������������������������������������������������������������������283
Importing VGG16������������������������������������������������������������������������������������������283
Building Your Model�������������������������������������������������������������������������������������284
Compiling Model������������������������������������������������������������������������������������������286
Creating Estimator���������������������������������������������������������������������������������������287
Processing Data������������������������������������������������������������������������������������������287
Training/Evaluation��������������������������������������������������������������������������������������288
Project Source���������������������������������������������������������������������������������������������288
Summary����������������������������������������������������������������������������������������������������������290
x
Table of Contents
Defining Model��������������������������������������������������������������������������������������������312
Compiling����������������������������������������������������������������������������������������������������314
Creating Checkpoints����������������������������������������������������������������������������������314
Training��������������������������������������������������������������������������������������������������������315
Prediction����������������������������������������������������������������������������������������������������315
Full Source – TextGenerationBabyNames����������������������������������������������������317
Saving/Reusing Model���������������������������������������������������������������������������������324
Advanced Text Generation���������������������������������������������������������������������������������324
Creating Project�������������������������������������������������������������������������������������������325
Loading Text������������������������������������������������������������������������������������������������326
Processing Data������������������������������������������������������������������������������������������327
Defining Model��������������������������������������������������������������������������������������������329
Creating Checkpoints����������������������������������������������������������������������������������330
CustomCallback Class���������������������������������������������������������������������������������330
Model Training���������������������������������������������������������������������������������������������333
Results���������������������������������������������������������������������������������������������������������333
Training Continuation�����������������������������������������������������������������������������������334
Some Observations�������������������������������������������������������������������������������������335
Full Source��������������������������������������������������������������������������������������������������337
Further Work�����������������������������������������������������������������������������������������������������341
Summary����������������������������������������������������������������������������������������������������������342
xi
Table of Contents
Attention Model�������������������������������������������������������������������������������������������������348
English to Spanish Translator����������������������������������������������������������������������������351
Creating Project�������������������������������������������������������������������������������������������352
Downloading Translation Dataset����������������������������������������������������������������353
Creating Datasets����������������������������������������������������������������������������������������353
Data Preprocessing�������������������������������������������������������������������������������������355
Glove Word Embedding��������������������������������������������������������������������������������364
Defining Encoder�����������������������������������������������������������������������������������������369
Defining Decoder�����������������������������������������������������������������������������������������370
Attention Network���������������������������������������������������������������������������������������371
Defining Model��������������������������������������������������������������������������������������������379
Model Training���������������������������������������������������������������������������������������������381
Inference�����������������������������������������������������������������������������������������������������382
Full Source��������������������������������������������������������������������������������������������������392
Summary����������������������������������������������������������������������������������������������������������404
xii
Table of Contents
xiii
Table of Contents
xiv
Table of Contents
Evaluation����������������������������������������������������������������������������������������������������540
Predicting Next Data Point���������������������������������������������������������������������������543
Predicting Range of Data Points������������������������������������������������������������������545
Full Source��������������������������������������������������������������������������������������������������548
Multivariate Time Series Analysis���������������������������������������������������������������������555
Creating Project�������������������������������������������������������������������������������������������556
Preparing Data���������������������������������������������������������������������������������������������556
Checking for Stationarity�����������������������������������������������������������������������������558
Exploring Data���������������������������������������������������������������������������������������������559
Preparing Data���������������������������������������������������������������������������������������������561
Creating Model��������������������������������������������������������������������������������������������563
Training��������������������������������������������������������������������������������������������������������563
Evaluation����������������������������������������������������������������������������������������������������565
Predicting Future Point��������������������������������������������������������������������������������566
Predicting Range of Data Points������������������������������������������������������������������567
Full Source��������������������������������������������������������������������������������������������������570
Summary����������������������������������������������������������������������������������������������������������576
xv
Table of Contents
Do It Yourself�����������������������������������������������������������������������������������������������������589
VGG16 Architecture��������������������������������������������������������������������������������������590
Creating Project�������������������������������������������������������������������������������������������591
Downloading Images�����������������������������������������������������������������������������������592
Displaying Images���������������������������������������������������������������������������������������593
Preprocessing Images���������������������������������������������������������������������������������594
Model Building���������������������������������������������������������������������������������������������596
Content Loss������������������������������������������������������������������������������������������������598
Style Loss����������������������������������������������������������������������������������������������������598
Total Variation Loss��������������������������������������������������������������������������������������599
Computing Losses for Content and Style����������������������������������������������������600
Evaluator Class��������������������������������������������������������������������������������������������601
Generating Output Image����������������������������������������������������������������������������602
Displaying Images���������������������������������������������������������������������������������������603
Full Source��������������������������������������������������������������������������������������������������604
Summary����������������������������������������������������������������������������������������������������������611
xvi
Table of Contents
xvii
Table of Contents
AutoEncoder������������������������������������������������������������������������������������������������������676
Loading Data�����������������������������������������������������������������������������������������������677
Creating Training/Testing Datasets��������������������������������������������������������������680
Preparing Training Dataset��������������������������������������������������������������������������681
Defining Model��������������������������������������������������������������������������������������������682
Model Training���������������������������������������������������������������������������������������������686
Testing���������������������������������������������������������������������������������������������������������686
Inference on an Unseen Image��������������������������������������������������������������������689
Full Source��������������������������������������������������������������������������������������������������691
Pre-trained Model as Encoder��������������������������������������������������������������������������698
Project Description��������������������������������������������������������������������������������������699
Defining Model��������������������������������������������������������������������������������������������699
Extracting Features�������������������������������������������������������������������������������������701
Defining Network�����������������������������������������������������������������������������������������701
Model Training���������������������������������������������������������������������������������������������703
Inference�����������������������������������������������������������������������������������������������������704
Inference on an Unseen Image��������������������������������������������������������������������706
Full Source��������������������������������������������������������������������������������������������������707
Summary����������������������������������������������������������������������������������������������������������714
Index�������������������������������������������������������������������������������������������������715
xviii
About the Author
Poornachandra Sarang started his IT career way back in the late 1980s.
During this long career, he had an opportunity to work on a wide variety
of technologies. He has taught Computer Science/Engineering at the
University of Notre Dame and the University of Mumbai. He has been a
Ph.D. advisor for Computer Science and is currently on a Thesis Advisory
Committee for students pursuing Ph.D. in Computer Engineering. His
current research interests are in machine/deep learning. He has several
papers and journal articles to his credit and has presented in several
conferences.
xix
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
About the Technical Reviewer
Vishwesh Ravi Shrimali graduated from BITS Pilani in 2018, where he
studied mechanical engineering. Since then, he has worked with Big
Vision LLC on deep learning and computer vision and was involved in
creating official OpenCV AI courses. Currently, he is working at Mercedes-
Benz Research and Development India Pvt. Ltd. He has a keen interest
in programming and AI and has applied that interest in mechanical
engineering projects. He has also written multiple blogs on OpenCV and
deep learning on Learn OpenCV, a leading blog on computer vision. He
has also authored Machine Learning for OpenCV (2nd edition) by Packt.
When he is not writing blogs or working on projects, he likes to go on long
walks or play his acoustic guitar.
xxi
Acknowledgments
I would first like to thank my past student Prof. Abhijit Gole, who is
currently a faculty member in the Department of Computer Science at
an autonomous college affiliated to the University of Mumbai. Abhijit
initiated me into delivering a postgraduate course in machine learning to
his students. This triggered my interest in guiding students in machine/
deep learning for their projects at the master’s level and in guiding those
pursuing Ph.D.
My sincere thanks to my four intern students for their contribution
in developing this book. Mukul Rawat graduated this year (2020), and
Karan Aryan, Chandrakant Sharma, and Udit Dashore will be graduating
next year (2021). All four are brilliant students, and without their help in
developing the projects in this book, I would have taken much longer time
in completing this book. All of them took painstaking efforts in reviewing
the first draft of this book for technical discrepancies.
I would like to thank Mr. Aaron Black, Senior Editor, Apress, who gave
me the opportunity to author this book, taking quick decisions on the
book’s outline and providing me the desired flexibility in finalizing the
outline as I kept developing the contents. I would like to thank Jessica
Vakili, Coordinating Editor, for her excellent coordination on various
fronts, especially on tech review. I would also like to thank the entire
Apress team for bringing this book out quickly to market.
Lastly, but not least importantly, I would like to thank Vijay Jadhav
who put in lots of effort in running all the project sources to bring out
the discrepancies in writing and the code. His efforts in formatting the
manuscript as per Apress authoring guidelines require a special mention.
xxiii
Preface
With recent advances in deep learning and the ever-growing popularity
of TensorFlow, you see several domains where machine learning models
have caused a tremendous impact. For a developer, it gives a great
opportunity to apply their skills in real-world projects across a wide range
of domains. Having a knowledge of what is available in the industry today
becomes an important asset. This book attempts to provide you with a
wide range of real-world applications based on deep neural networks
(DNN) and the latest version of TensorFlow. It provides a comprehensive
coverage of various deep learning architectures. It is fully project-oriented
and contains about 25 fully implemented real-life projects.
Chapter 1 starts with an introduction to TensorFlow 2, describing why
it is considered a complete platform for machine learning. It describes
the various additions to TensorFlow such as tf.keras integration, eager
execution, distributed training, deployment on edge devices, and the
use of data pipelines. At the end, it provides guidance for setting up the
development environment for running projects in the book.
Chapter 2 goes deeper into TensorFlow 2. As is the convention, we
start with a trivial “Hello World” kind of application introducing you to
the complete machine learning development cycle. The chapter then
deals with a binary classification problem that is a typical application
taught at the beginning of any machine learning course. Here, you are
introduced to data preprocessing, defining a dense neural network using
tf.keras API, training the model, and evaluating its performance using
TensorBoard metrics and the traditional confusion matrix. Rather than
dealing with only the classification, the project teaches you how to load
data at runtime into your development environment, how to preprocess it,
xxv
Preface
how to develop a machine learning model, and finally how to evaluate the
model’s performance using TensorBoard analytics and other traditional
techniques.
Chapter 3 goes deeper in the tf.keras module. It introduces you to
the functional API for model building. It talks about object orientation in
TensorFlow and shows how to create a class hierarchy for your models and
how to create custom layers by subclassing. You will learn how to save the
model using TensorFlow’s new SavedModel interface and several ways
of saving partial or full models. You will then do a project on a multiclass
classification using predefined Convolutional Neural Network (CNN)
layers in tf.keras. Again, unlike the traditional ways of teaching, I show you
how to experiment with different network architectures to optimize model
performance.
As training deep neural networks using multiple CNN layers is
resource hungry, in Chapter 4 I show you how to take advantage of
somebody else’s training. This chapter deals with transfer learning
where the knowledge of a domain expert and somebody else’s model
development efforts are used to your advantage almost effortlessly. The
chapter deals with two projects. The first project shows you how to use
a pre-trained ImageNet classifier to classify your own set of images. The
second project shows you how to construct your own classifier based on a
pre-trained model.
Any machine learning training starts with classification and regression
problems. In the previous chapters, we tackled classification to a great
depth. The question arises: Can we use deep neural networks for
regression analysis where there are several successful statistical-based
techniques already available for our use? I show you how to use DNN
for regression in Chapter 5 with the help of three different dense neural
network architectures.
xxvi
Preface
xxvii
Random documents with unrelated
content Scribd suggests to you:
I STOODbeside a pleasant stream,
Where spicy boughs were wreathing;
Its gentle ripples came and went
Like sleeping infants breathing.
I asked, "Is that 'Littell' you have?" but I knew only too
well
The answer which I should receive, that it was not
"Littell;"
And had he read my wishes, and offered me the "Book,"
I would not have accepted; but I love the first, fresh look.
DESCRIPTION OF DIAGRAMS.
(See next page.)
Fig. 1.—Front of body as shown in the engraving.
Fig. 2.—Back of body, by placing the letters a a.
Fig. 3.—The cape sommet worn for evening dress. Place the
letters b b, the upper part of the cape forming an epaulette over the
shoulder.
Fig. 4.—The sleeve, showing the muslin sleeve underneath, as in
the engraving, à Watteau shape, fastened by a bow of ribbon. We
have given the pattern of the whole sleeve.
DIAGRAMS OF THE DRESS SHOWN ON PAGE 453.
COTTAGE FURNITURE.
The above figure is a handsome pattern for a sofa. The castors are
sunk in the legs.
EMBROIDERY.—DRESS UNDERSLEEVE.