100% found this document useful (4 votes)
52 views

Full Artificial Neural Networks With TensorFlow 2: ANN Architecture Machine Learning Projects Poornachandra Sarang Ebook All Chapters

Networks

Uploaded by

ruznicgaideu41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
52 views

Full Artificial Neural Networks With TensorFlow 2: ANN Architecture Machine Learning Projects Poornachandra Sarang Ebook All Chapters

Networks

Uploaded by

ruznicgaideu41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Download the full version of the textbook now at textbookfull.

com

Artificial Neural Networks with TensorFlow 2:


ANN Architecture Machine Learning Projects
Poornachandra Sarang

https://textbookfull.com/product/artificial-
neural-networks-with-tensorflow-2-ann-
architecture-machine-learning-projects-
poornachandra-sarang/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Applied Neural Networks with TensorFlow 2: API Oriented


Deep Learning with Python Orhan Gazi Yalç■n

https://textbookfull.com/product/applied-neural-networks-with-
tensorflow-2-api-oriented-deep-learning-with-python-orhan-gazi-yalcin/

textbookfull.com

MATLAB Deep Learning: With Machine Learning, Neural


Networks and Artificial Intelligence 1st Edition Phil Kim

https://textbookfull.com/product/matlab-deep-learning-with-machine-
learning-neural-networks-and-artificial-intelligence-1st-edition-phil-
kim/
textbookfull.com

Deep Learning Projects Using TensorFlow 2: Neural Network


Development with Python and Keras 1st Edition Vinita
Silaparasetty
https://textbookfull.com/product/deep-learning-projects-using-
tensorflow-2-neural-network-development-with-python-and-keras-1st-
edition-vinita-silaparasetty/
textbookfull.com

Theoretical Approaches to Multi-Cultural Positive


Psychological Interventions Llewellyn Ellardus Van Zyl

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

Psychology 5th Edition Saundra K. Ciccarelli

https://textbookfull.com/product/psychology-5th-edition-saundra-k-
ciccarelli/

textbookfull.com

Cultivating Behavioral Change in K 12 Students Team Based


Intervention and Support Strategies 1st Edition Marty
Huitt Gail Tolbert
https://textbookfull.com/product/cultivating-behavioral-change-
in-k-12-students-team-based-intervention-and-support-strategies-1st-
edition-marty-huitt-gail-tolbert/
textbookfull.com

Principles of Animal Research Ethics 1st Edition Tom L


Beauchamp David Degrazia

https://textbookfull.com/product/principles-of-animal-research-
ethics-1st-edition-tom-l-beauchamp-david-degrazia/

textbookfull.com

Train Your Mind To Be Successful 1st Edition Sayra Montes

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

ISBN-13 (pbk): 978-1-4842-6149-1 ISBN-13 (electronic): 978-1-4842-6150-7


https://doi.org/10.1007/978-1-4842-6150-7

Copyright © 2021 by Poornachandra Sarang


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole
or part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical
way, and transmission or information storage and retrieval, electronic adaptation, computer
software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a
trademark symbol with every occurrence of a trademarked name, logo, or image we use the
names, logos, and images only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms,
even if they are not identified as such, is not to be taken as an expression of opinion as to
whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the
date of publication, neither the authors nor the editors nor the publisher can accept any
legal responsibility for any errors or omissions that may be made. The publisher makes no
warranty, express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Aaron Black
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201)
348-4505, e-mail [email protected], or visit www.springeronline.com. Apress
Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business
Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for
reprint, paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our
Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub via the book’s product page, located at www.apress.com/
978-1-4842-6149-1. For more detailed information, please visit http://www.apress.com/
source-code.
Printed on acid-free paper
Table of Contents
About the Author�������������������������������������������������������������������������������xix

About the Technical Reviewer�����������������������������������������������������������xxi

Acknowledgments���������������������������������������������������������������������������xxiii

Preface��������������������������������������������������������������������������������������������� xxv

Chapter 1: TensorFlow Jump Start�������������������������������������������������������1


What Is TensorFlow 2.0?���������������������������������������������������������������������������������������3
TensorFlow 2.x Platform����������������������������������������������������������������������������������3
Training������������������������������������������������������������������������������������������������������������4
Model Saving���������������������������������������������������������������������������������������������������9
Deployment���������������������������������������������������������������������������������������������������10
What TensorFlow 2.x Offers?������������������������������������������������������������������������������12
The tf.keras in TensorFlow�����������������������������������������������������������������������������12
Eager Execution���������������������������������������������������������������������������������������������12
Distribution����������������������������������������������������������������������������������������������������15
TensorBoard��������������������������������������������������������������������������������������������������15
Vision Kit��������������������������������������������������������������������������������������������������������17
Voice Kit���������������������������������������������������������������������������������������������������������17
Edge TPU�������������������������������������������������������������������������������������������������������17
Pre-trained Models for AIY Kits���������������������������������������������������������������������18
Data Pipelines������������������������������������������������������������������������������������������������18

iii
Table of Contents

Installation����������������������������������������������������������������������������������������������������������18
Installation�����������������������������������������������������������������������������������������������������19
Docker Installation�����������������������������������������������������������������������������������������20
No Installation�����������������������������������������������������������������������������������������������20
Testing����������������������������������������������������������������������������������������������������������������20
Summary������������������������������������������������������������������������������������������������������������23

Chapter 2: A Closer Look at TensorFlow���������������������������������������������25


A Trivial Machine Learning Application���������������������������������������������������������������25
Creating Colab Notebook�������������������������������������������������������������������������������26
Imports����������������������������������������������������������������������������������������������������������28
Setting Up Data���������������������������������������������������������������������������������������������30
Defining Neural Network�������������������������������������������������������������������������������32
Compiling Model��������������������������������������������������������������������������������������������34
Training Network�������������������������������������������������������������������������������������������35
Examining Training Output�����������������������������������������������������������������������������36
Predicting������������������������������������������������������������������������������������������������������40
Full Source Code�������������������������������������������������������������������������������������������40
Binary Classification in TensorFlow��������������������������������������������������������������������44
Setting Up Project������������������������������������������������������������������������������������������45
Imports����������������������������������������������������������������������������������������������������������45
Mounting Google Drive����������������������������������������������������������������������������������46
Loading Data�������������������������������������������������������������������������������������������������47
Data Preprocessing���������������������������������������������������������������������������������������50
Defining ANN�������������������������������������������������������������������������������������������������55
Model Training�����������������������������������������������������������������������������������������������58
Full Source Code�������������������������������������������������������������������������������������������66
Summary������������������������������������������������������������������������������������������������������������70

iv
Table of Contents

Chapter 3: Deep Dive in tf.keras���������������������������������������������������������71


Getting Started����������������������������������������������������������������������������������������������������71
Functional API for Model Building�����������������������������������������������������������������������72
Sequential Models�����������������������������������������������������������������������������������������73
Model Subclassing����������������������������������������������������������������������������������������76
Predefined Layers������������������������������������������������������������������������������������������78
Custom Layers�����������������������������������������������������������������������������������������������78
Saving Models�����������������������������������������������������������������������������������������������������81
Whole-Model Saving�������������������������������������������������������������������������������������81
Convolutional Neural Networks���������������������������������������������������������������������������86
Image Classification with CNN����������������������������������������������������������������������������89
Creating Project���������������������������������������������������������������������������������������������90
Image Dataset�����������������������������������������������������������������������������������������������90
Loading Dataset���������������������������������������������������������������������������������������������92
Creating Training/Testing Datasets����������������������������������������������������������������93
Preparing Data for Model Training�����������������������������������������������������������������95
Model Development���������������������������������������������������������������������������������������99
Defining Models�������������������������������������������������������������������������������������������107
Saving Model�����������������������������������������������������������������������������������������������129
Predicting Unseen Images���������������������������������������������������������������������������130
Summary����������������������������������������������������������������������������������������������������������132

Chapter 4: Transfer Learning������������������������������������������������������������133


Knowledge Transfer������������������������������������������������������������������������������������������134
TensorFlow Hub������������������������������������������������������������������������������������������������135
Pre-trained Modules������������������������������������������������������������������������������������137
Using Modules���������������������������������������������������������������������������������������������139

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

Visualizing Test Results�������������������������������������������������������������������������������177


Predicting an Unknown Image��������������������������������������������������������������������181
Training with Smaller Datasets��������������������������������������������������������������������183
Saving/Reloading Model������������������������������������������������������������������������������186
Submitting Your Work����������������������������������������������������������������������������������������187
Further Work�����������������������������������������������������������������������������������������������������187
Summary����������������������������������������������������������������������������������������������������������188

Chapter 5: Neural Networks for Regression�������������������������������������189


Regression��������������������������������������������������������������������������������������������������������190
Definition�����������������������������������������������������������������������������������������������������190
Applications�������������������������������������������������������������������������������������������������191
Regression Problem������������������������������������������������������������������������������������191
Regression Types�����������������������������������������������������������������������������������������192
Regression in Neural Networks������������������������������������������������������������������������193
Setting Up Project����������������������������������������������������������������������������������������194
Extracting Features and Label���������������������������������������������������������������������195
Defining/Training Model������������������������������������������������������������������������������196
Predicting����������������������������������������������������������������������������������������������������196
Wine Quality Analysis����������������������������������������������������������������������������������������197
Creating Project�������������������������������������������������������������������������������������������198
Data Preparation�����������������������������������������������������������������������������������������������198
Downloading Data���������������������������������������������������������������������������������������199
Preparing Dataset����������������������������������������������������������������������������������������199
Creating Datasets����������������������������������������������������������������������������������������200
Scaling Data������������������������������������������������������������������������������������������������201

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

Estimator Input Function�����������������������������������������������������������������������������244


Creating Estimator Instance������������������������������������������������������������������������246
Model Training���������������������������������������������������������������������������������������������247
Model Evaluation�����������������������������������������������������������������������������������������248
Predicting Unseen Data�������������������������������������������������������������������������������250
Experimenting Different ANN Architectures�������������������������������������������������251
Project Source���������������������������������������������������������������������������������������������252
LinearRegressor for Regression������������������������������������������������������������������������256
Project Description��������������������������������������������������������������������������������������256
Creating Project�������������������������������������������������������������������������������������������256
Loading Data�����������������������������������������������������������������������������������������������257
Features Selection���������������������������������������������������������������������������������������257
Data Cleansing��������������������������������������������������������������������������������������������259
Creating Datasets����������������������������������������������������������������������������������������263
Building Feature Columns���������������������������������������������������������������������������266
Defining Input Function�������������������������������������������������������������������������������269
Creating Estimator Instance������������������������������������������������������������������������269
Model Training���������������������������������������������������������������������������������������������270
Model Evaluation�����������������������������������������������������������������������������������������270
Project Source���������������������������������������������������������������������������������������������271
Custom Estimators��������������������������������������������������������������������������������������������276
Creating Project�������������������������������������������������������������������������������������������276
Loading Data�����������������������������������������������������������������������������������������������276
Creating Datasets����������������������������������������������������������������������������������������277
Defining Model��������������������������������������������������������������������������������������������278
Defining Input Function�������������������������������������������������������������������������������278
Model to Estimator��������������������������������������������������������������������������������������279

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

Chapter 7: Text Generation���������������������������������������������������������������291


Recurrent Neural Networks������������������������������������������������������������������������������292
Simple RNN�������������������������������������������������������������������������������������������������294
Vanishing and Exploding Gradients�������������������������������������������������������������295
LSTM – A Special Case��������������������������������������������������������������������������������295
Text Generation�������������������������������������������������������������������������������������������������301
Model Training���������������������������������������������������������������������������������������������301
Inference�����������������������������������������������������������������������������������������������������303
Model Definition������������������������������������������������������������������������������������������304
Generating Baby Names�����������������������������������������������������������������������������������304
Creating Project�������������������������������������������������������������������������������������������305
Downloading Text����������������������������������������������������������������������������������������305
Processing Text��������������������������������������������������������������������������������������������307

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

Chapter 8: Language Translation������������������������������������������������������343


Introduction�������������������������������������������������������������������������������������������������������343
Sequence-to-Sequence Modeling���������������������������������������������������������������������344
Encoder/Decoder�����������������������������������������������������������������������������������������345
Shortcomings of seq2seq Model�����������������������������������������������������������������348

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

Chapter 9: Natural Language Understanding������������������������������������405


Introduction�������������������������������������������������������������������������������������������������������405
Transformer�������������������������������������������������������������������������������������������������405
Transformer�������������������������������������������������������������������������������������������������������407
Downloading Data���������������������������������������������������������������������������������������408
Creating Datasets����������������������������������������������������������������������������������������408
Data Preprocessing�������������������������������������������������������������������������������������409
Tokenizing Data�������������������������������������������������������������������������������������������409
Preparing Dataset for Training���������������������������������������������������������������������413
Transformer Model��������������������������������������������������������������������������������������414
Multi-Head Attention�����������������������������������������������������������������������������������416

xii
Table of Contents

Function for Scaled Dot-Product Attention��������������������������������������������������422


Encoder Architecture�����������������������������������������������������������������������������������425
Encoder�������������������������������������������������������������������������������������������������������430
Decoder Architecture�����������������������������������������������������������������������������������434
Decoder Layer���������������������������������������������������������������������������������������������436
Decoder�������������������������������������������������������������������������������������������������������440
Transformer Model��������������������������������������������������������������������������������������442
Creating Model for Training�������������������������������������������������������������������������445
Loss Function����������������������������������������������������������������������������������������������446
Optimizer�����������������������������������������������������������������������������������������������������447
Compiling����������������������������������������������������������������������������������������������������448
Training��������������������������������������������������������������������������������������������������������448
Inference�����������������������������������������������������������������������������������������������������448
Testing���������������������������������������������������������������������������������������������������������449
Full Source��������������������������������������������������������������������������������������������������450
What’s Next?�����������������������������������������������������������������������������������������������������469
Summary����������������������������������������������������������������������������������������������������������469

Chapter 10: Image Captioning����������������������������������������������������������471


Project Description��������������������������������������������������������������������������������������������474
Creating Project�������������������������������������������������������������������������������������������474
Downloading Data���������������������������������������������������������������������������������������475
Parsing Token File���������������������������������������������������������������������������������������477
Loading InceptionV3 Model�������������������������������������������������������������������������481
Preparing Dataset����������������������������������������������������������������������������������������482
Extracting Features�������������������������������������������������������������������������������������483
Creating Vocabulary������������������������������������������������������������������������������������484

xiii
Table of Contents

Creating Input Sequences���������������������������������������������������������������������������484


Creating Training Datasets��������������������������������������������������������������������������486
Creating Model��������������������������������������������������������������������������������������������487
Creating Encoder�����������������������������������������������������������������������������������������488
Creating Decoder�����������������������������������������������������������������������������������������488
Encoder/Decoder Instantiations������������������������������������������������������������������497
Defining Optimizer and Loss Functions�������������������������������������������������������498
Creating Checkpoints����������������������������������������������������������������������������������502
Training Step Function���������������������������������������������������������������������������������503
Model Training���������������������������������������������������������������������������������������������504
Model Inference�������������������������������������������������������������������������������������������505
Full Source��������������������������������������������������������������������������������������������������509
Summary����������������������������������������������������������������������������������������������������������521

Chapter 11: Time Series Forecasting������������������������������������������������523


Introduction�������������������������������������������������������������������������������������������������������523
What Is Time Series Forecasting?���������������������������������������������������������������523
Concerns of Forecasting������������������������������������������������������������������������������525
Components of Time Series�������������������������������������������������������������������������526
Univariate vs. Multivariate���������������������������������������������������������������������������527
Univariate Time Series Analysis������������������������������������������������������������������������527
Creating Project�������������������������������������������������������������������������������������������528
Preparing Data���������������������������������������������������������������������������������������������528
Creating Training/Testing Datasets��������������������������������������������������������������533
Creating Input Tensors���������������������������������������������������������������������������������538
Building Model���������������������������������������������������������������������������������������������539
Compiling and Training��������������������������������������������������������������������������������540

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

Chapter 12: Style Transfer����������������������������������������������������������������577


Introduction�������������������������������������������������������������������������������������������������������577
Fast Style Transfer��������������������������������������������������������������������������������������������578
Creating Project�������������������������������������������������������������������������������������������579
Downloading Images�����������������������������������������������������������������������������������579
Preparing Images for Model Input���������������������������������������������������������������582
Performing Styling���������������������������������������������������������������������������������������584
Displaying Output����������������������������������������������������������������������������������������584
Some More Results�������������������������������������������������������������������������������������585
Full Source��������������������������������������������������������������������������������������������������587

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

Chapter 13: Image Generation����������������������������������������������������������613


GAN – Generative Adversarial Network�������������������������������������������������������������613
How Does GAN Work?���������������������������������������������������������������������������������������614
The Generator���������������������������������������������������������������������������������������������������615
The Discriminator����������������������������������������������������������������������������������������������616
Mathematical Formulation��������������������������������������������������������������������������������616
Digit Generation������������������������������������������������������������������������������������������������618
Creating Project�������������������������������������������������������������������������������������������618
Loading Dataset�������������������������������������������������������������������������������������������618
Preparing Dataset����������������������������������������������������������������������������������������620

xvi
Table of Contents

Defining Generator Model����������������������������������������������������������������������������620


Testing Generator����������������������������������������������������������������������������������������624
Defining Discriminator Model����������������������������������������������������������������������625
Testing Discriminator�����������������������������������������������������������������������������������627
Defining Loss Functions������������������������������������������������������������������������������627
Defining Few Functions for Training������������������������������������������������������������629
Full Source��������������������������������������������������������������������������������������������������636
Alphabet Generation�����������������������������������������������������������������������������������������644
Downloading Data���������������������������������������������������������������������������������������644
Creating Dataset for a Single Alphabet��������������������������������������������������������646
Program Output�������������������������������������������������������������������������������������������647
Full Source��������������������������������������������������������������������������������������������������648
Printed to Handwritten Text������������������������������������������������������������������������������655
Color Cartoons��������������������������������������������������������������������������������������������������656
Downloading Data���������������������������������������������������������������������������������������656
Creating Dataset������������������������������������������������������������������������������������������656
Displaying Images���������������������������������������������������������������������������������������658
Output����������������������������������������������������������������������������������������������������������659
Full source���������������������������������������������������������������������������������������������������660
Summary����������������������������������������������������������������������������������������������������������669

Chapter 14: Image Translation���������������������������������������������������������671


AutoEncoders����������������������������������������������������������������������������������������������������671
Color Spaces�����������������������������������������������������������������������������������������������������672
Network Configurations������������������������������������������������������������������������������������674
Vanilla Model�����������������������������������������������������������������������������������������������674
Merged Model���������������������������������������������������������������������������������������������675
Merged Model Using Pre-trained Network��������������������������������������������������675

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

Though the use of pre-trained models eases your custom model


development, there are many instances where you cannot find a pre-
trained model that fits your purpose. The model development from scratch
requires lots of plumbing; the Estimators in TensorFlow come to your
help in these situations. Chapter 6 deals with estimators. It contains four
projects. The first two projects describe how to use a predefined estimator
for classification and then a regression. The third project talks about how
to create a custom estimator from scratch, and the last one talks about
creating a custom estimator for a pre-trained model.
Chapter 7 talks about text generation techniques. It describes the
traditional techniques of RNNs (recurrent neural networks) and LSTM
(long short-term memory) for text generation and shows you how to
use LSTM for a trivial application of generating baby names. The next
project in the chapter uses some advanced text generation techniques
to create text matching the linguistics of a famous novel by Tolstoy. As
training a text generation model is a very lengthy process, I will show some
techniques of performing such long-duration training by showing you how
to continue upon an interrupted training, which may be an intentional or
unintentional interrupt.
Chapter 8 takes you further in the text generation where I describe
the seq2seq model, encoder/decoder architecture, and a recent attention
model for language translation. The chapter gives an in-depth coverage
of the English to Spanish translator that uses glove word embedding. It
shows you how to define your own machine learning model with encoder,
decoder, and attention layers.
Chapter 9 describes the state-of-the-art technology for text generation.
It describes the latest transformer-based architecture for natural language
processing. A complete in-depth project teaches you how to construct
a transformer model on your own in Python, and not to depend fully
on just the pre-trained models such as BERT (Bidirectional Encoder
Representations from Transformers).

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.

The lily press'd its dewy cheek


Upon the kissing billow,
And slumber'd like a summer bride
Upon her nuptial pillow.

Yet, by this stream a dark rock tower'd


Like fane in forest waving;
Deep furrows shown within its side,
Wrought by the ripples laving!

I gazed upon the sunny stream,


And thought of sunny faces,
And wonder'd how such gentle waves
Could leave such angry traces.

Again I stood within the hall


Where Wealth her glow was shedding;
The spacious dome seem'd lighted up
For some grand princely wedding.

The moon look'd down on golden spires,


As if to give a greeting;
One would have thought, amid the show,
'Twas Pleasure's natal meeting.

Yet there, within that hall, that night


I saw the discontented;
I saw pale faces mark'd with care,
Like spirits unrepented.

I gazed upon the princely hall


Wh lth h d bl h b bbl
Where wealth had blown her bubble,
And wonder'd how, amid such show,
There could be aught of trouble.

And thus, I said, amid Life's glare—


Amid this world of hurry—
'Tis true that "tongues we find in trees,
And sermons in the quarry!"

Our life is like yon little stream,


Where ripples are retreating;
And Pleasure, though array'd in smiles,
Hath spots where Care is eating.

Our life is like a summer stream


That lulls us into slumber;
We dream we're happy for a while,
While waves in countless number,

Though gentle in their ceaseless flow,


Are every day and morrow,
Still chafing in the shores of Life
Some secret marks of sorrow!
BRIGHT FLOWERS FOR HER I
LOVE.
BY WILLIAM RODERICK LAWRENCE.
BRIGHT flowers for her I love
Yes, flowers rich and rare,
The rose-bud and the violet
To grace her golden hair;
Yet nature's gems—though beautiful
And pure and bright they be—
Are not so fair as she I love,
Or beautiful to me.

Rare gems for her I love!


All sparkling in their light,
A diadem to grace that brow
So beautifully bright;
Yet earthly crowns must fade—
Immortal crowns above
Alone are worthy to be sought
By her I fondly love.

Music for her I love!


Melodiously low,
Breathed soft from harps whose golden strings
With songs of rapture glow;
Such music as the angels make
In worlds of light above—
Such music would I have to cheer
The heart of her I love.

And peace for her I love!


The peace religion brings,
Renouncing fleeting, transient joys
For bright and heavenly things;
Let happiness be hers,
And heaven her rest above;
May this, my prayer, accepted rise
For her I truly love.
STANZAS.
BY HELEN HAMILTON.

THOUGH thou art dying, yet I may not weep


Such grief I leave to those who part for years;
We only part for days; it may be—hours;
We have no need of tears.

Ere thy last kiss is cold upon my lips,


Thy dying clasp is loosened from my hand;
I will be with thee—thou but goest before
Into the better land.

When thou hast reached Heav'n's golden portal, pause


And cast one look adown Death's shadowy road;
I will be near, nor tremble as I walk
The road thou first hast trod.

Would that together we might pass away!


Would that one sound might ring our passing knell!
Yet soon we'll meet where partings are unknown;
For the last time—farewell.
SONNET.—NATURE.
BY WM. ALEXANDER.

NATURE! Well hath the Poet said: "Who can


Paint like to thee?" Inimitably fine
Seem all the hues and colorings of thine,
Though microscopic eye may closely scan:
Close search but lifts the mystic veil that hides
Thy scenes of beauty. In the tiny cup
Of thy fair flowers, what wonders open up!
Lo! a whole insect nation there resides,
Clad in rich vests of fine embroidery,
Or coats of living purple, green, and gold.
Such fairy scenes, so constantly unrolled,
Declare design most manifest to be;
And the least path Omnipotence hath trod
Exhibits footprints of our glorious God.
TO ONE WHO RESTS.
BY WINNIE WOODFERN.
I THOUGHT my heart had cast away
Each memory of its early day;
I thought nor grief, nor change, nor fear,
Could teach these eyes to shed a tear;
And yet, a very child I be,
Alas, I still remember thee!

I often gaze with heart unmoved


On lips that smile like thine, beloved;
I often catch a deep low tone,
That bears the music of thine own;
Yet pass, without a tear or smile,
My pulses calm and cool, the while.

Thou, dearest, hast been linked to me


By things which never more can be;
By memories of that lovely place,
That village, quiet in its grace,
Like lilies, in the summer air,
That stir not; knowing they are fair.

And those who trod its mossy walks,


And shared with me those woodland talks,
'Till our hearts, hungry for the pain
Of loving, to be loved again,
Learned the deep meaning of a word
Which had been better never heard.

Thou, and thy love, were of that time


When life was but a passion—rhyme;
When I knew not that care might come
Even to that sweet mountain home;
When stars and streams and flowers were part
Of this, then calmly beating heart.

So, when the martyr's cross was mine,


I chose another love than thine;
Our hearts, but not our souls, were mates,
Our love the same, but not our fates;
And he who, in these later years,
Seeks me, seeks also scorching tears.

'Tis long since I have breathed thy name!


It once could turn my heart to flame;
But now, so changed and cold am I,
I only speak it with a sigh,
That dreams, whose proper home is Heaven,
To hearts o'ertasked with Earth, are given!

Oh, long forsaken! no fond dream,


Floating (like flowers on a stream),
Down the wild current of my mind,
Counts o'er the joys I've left behind,
A little thing has drawn these tears,
For thee, and for our early years!

A moment since I cast a look


Within the pages of a book
Which thou to me hast often read,
Thy shoulder pillowing my head;
A faint, sweet perfume thence arose;
There lay thy gift—a faded rose!

It was as if an altar burned


With sacrifices, and I turned—
Beloved, do not think me weak!
Tears, wild with grief, fled down my cheek,
And to my lips arose a prayer
That I might die while pausing there!

My song is o'er; 'twill only tell,


To some who know and love me well,
At times, within my inmost soul,
t t es, t y ost sou ,
Are thoughts I cannot quite control,
Because they breathe and speak of thee,
Who can be nothing now to me!
THEY SAY THAT SHE IS
BEAUTIFUL.
BY MARY GRACE HALPING.
THEY say that she is beautiful;
They praise that speaking eye,
That fair and softly rounded cheek,
Its bright and changeful dye,
That pure and polished brow that towers
Like ivory temple high.

But is that radiant being fair


The light and joy of home?
Doth from its loving inmates there
Her heart forget to roam?
Oh, is she not as false and fair
As ocean's snowy foam?

They say, unlike the tones of earth


Rings out that music free;
But only from the halls of mirth
Are heard those tones of glee;
They say that she is beautiful—
She is not so to me.

I've seen that sweet and smiling lip


Give back a stern reply;
I've seen the cloud of passion dim
That proudly glorious eye,
And on that pure transparent brow
The shade of anger lie.

I know that outward beauty sits


Upon that queenly brow;
Before its proud and gorgeous shrine
Doth man admiring bow,
While she, with false, capricious smile,
Repays each idle vow.

I know with seeming truth doth flash


That darkly radiant eye,
Yet beauty oft will sell for cash
What love can never buy,
Aside a loving heart will dash
That time and change defy.

Upon the thickly crowded street,


I many a form have past,
Whom grace gave not proportion meet
From beauty's model cast,
To whom the soul a glory lends
A radiance that will last,
When beauty's tender floweret bends
Before time's wintry blast.

Yet there I see no loving heart,


No spirit pure and free,
Though like a whited sepulchre
An outward gloss may be;
They say that she is beautiful,
She is not so to me.
ODE TO THE AIR IN MAY.
BY NICHOLAS NETTLEBY.
AWAKE, O Muse! my trembling pen inspire!
Infuse my words with unpolluted song;
Touch every line with thine own sacred fire,
And bear me by thy impulses along!

To thee, sweet Air, that dost around me play,


Touching ethereally each silv'ry string
That vibrates in the golden Harp of May,
To thee I dedicate this offering.

Soft, gentle Air! unnumbered missions thine,


Missions of mercy, kindness, and of love;
Guardian to man thou art, almost divine,
Doing below as angel hosts above.

Thine is it, Air, at morn's first op'ning light,


To hang rich curtains in the eastern sky,
Which, casting back their own refulgence bright,
Proclaim to earth that glorious day is nigh.

Thine is the task, as heaven's all-wondrous orb


Fills the eternal arch that o'er us spans,
Within thyself its fiercest rays t' absorb,
And make its milder, softer radiance man's.

O'er the broad earth thou wingest; every day


Lighting bright smiles in mansions high and low;
Blessings uncounted strewing in thy way,
Bright'ning the eye, kindling the cheek with glow.

When burning fever mantles o'er the brow,


And dire disease foretells the angel Death,
Welcome is thy refreshing entrance. Now
Gold hath not there the sweetness of thy breath.

With flowers thou lov'st to sport in fondest glee,


Si i f l t th i i h f
Sipping from velvet cups their rich perfume;
Thou lov'st to dally with the old oak-tree,
And with its broad green crest sweetly commune.

That sombre cloud that far on high is seen,


Shading the earth from Sol's intensest rays,
Is upward borne by thee, a wondrous screen,
Which both thy goodness and thy power displays.

Within our path thy liquid waves are found,


Constant attendant upon every hour,
Bearing unto us many a moving sound,
And messages from each surrounding flower.

And if (when weary of a long repose),


Thou dost invite an earth-refreshing storm,
When three-tongued lightnings in the heavens disclose
Terrific thunderclouds of grandest form,

Mantling the sky in blackest robes of night,


And rushing onward in confusion dire,
While deep explosions cause the timid fright,
And heaven and earth are filled with lurid fire;

Then is it that thy majesty we love;


Then we behold the wonders of thy power;
Thou hold'st the elements that rage above,
And guidest them in that sublimest hour!

But over thee, sweet Air, another hand,


Higher and stronger, holier than thine own,
Presides. 'Tis He, who by a seraph band,
Is circled round. HE upon Heaven's Throne!
ANNOYANCE.
BY BEATA.
I WAS thinking of the "Godey;" that it was out I knew,
The month was just beginning, and the papers said so
too;
"A charming number," "brilliant," "a treat for ladies all,"
And I wished to see its contents, and read "Fashion" on
the fall.

A rainy afternoon it was—not a dashing, roaring rain,


With a trumpet-sounding wind, or a stirring hurricane;
It did not rattle 'gainst the glass a lively, merry chime,
But a dull and dreary drizzle, a stupid, yawning time.

I almost had a mind to venture on the street,


But I do detest the pavements, even when they're clean
and neat;
So I thought upon the "Godey," with its fresh and uncut
page,
And longed for something pretty, my moments to engage.

It struck me that some pleasant chat would restore a


cheerful tone,
And rising with a sigh (for I, musing, sat alone),
I gathered up my sewing and quickly took my way,
Where it always wears an aspect bright, despite a rainy
day.

But scarcely had I entered, ere there fell, distinct and


clear,
The sound of cutting pages upon my wondering ear;
There sat my quiet brother, this dismal afternoon,
With my number in his hand, as I perceived full soon.

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.

So I waited very patiently, and my reward was near;


I saw that he was pleased, though it cost me rather dear;
And when the day was closing, and the rain at last was
done,
I enjoyed the precious "Godey," and the glorious setting
sun.
'TIS O'ER.
BY I. J. STINE.

'TIS o'er! the tender tie is broke


Which bound my heart so close to thee;
Though painful, though severe the stroke,
I now can smile that I am free.
The grief, the sorrow, and the woe
That I was called to undergo,
The bitter pangs, the heartfelt pain,
All, all have ceased their tyrant reign.

'Twas but a moment's pain, 'tis gone;


I'm happy, though unhappy now;
And Melancholy, meek and wan,
Sits peaceful on my thoughtful brow.
The world, with all its loss and gain,
Me neither pleasure gives nor pain;
With thee, false, heartless one, with thee
I lost all joy—all misery.
OUR PRACTICAL DRESS
INSTRUCTOR
DESCRIPTION OF THE ENGRAVING.
Headdress of the Lady on the Right.—Hair in bandeaux à la
Niobe; torsade of pearls. Moire dress, low body, with progressive
revers opening over a modestie of embroidered muslin edged with
lace; short open sleeves à la Watteau; undersleeves of embroidered
muslin; half-long gloves; bracelets of pearls, or more often worn
different, according to choice.
The other Figure (Lady seated).—Cap of tulle trimmed with lace
and ribbon. Low body, with revers open to waist; loose bell-shaped
sleeves, edged with a bouillonne; two skirts trimmed with the same;
modestie of embroidered muslin, edged with point de Venise; black
velvet bracelets, half-long gloves, and Venetian fan.

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.

WORK-TABLE FOR JUVENILES;


OR,

LITTLE MARY'S HALF-HOLIDAY.


"WELL, my dear, I am glad to see you ready for work again."
"Here are six yards of pink satin ribbon, about an inch wide,
some pink sarsnet, some card-board, black net, and plaited straw.
What kind of straw is it, mamma?"
BACK OF A WATCH-POCKET.
"It is called straw-beading, and is employed in the same way that
split straw was once used; but as it is flexible, and to be had in any
length, it is much more easily worked. Besides the things you have
mentioned, there is some pink sewing silk, and a little stiff muslin.
Now you have all your materials complete, and I have only to show
you how to use them. Begin by cutting out the shape for the back of
the watch-pocket. Here is a drawing of it. It is 7 inches long, and
4½ in the widest part. Mark the depth of three inches from the
bottom, and form this into a half round, then cut it into a point from
the widest part to the top. The lower part, which is for the pocket, is
thus three inches deep, and the top four inches. You will require two
pieces of card-board of this shape and size, which must be covered
on both sides with pink silk. Tack them together round the edges."
"These pieces are for the backs. Are the fronts of the pockets
made of card-board, too?"
"No; you will use the stiff muslin for them. They must be in the
half circle form, 3 inches deep, but 5½ inches wide at the top. They
must also be covered with silk on both sides. Now cut out two pieces
of black net, rather larger than the backs, and two more (also
allowing for turnings in) for the fronts. Do you remark anything
peculiar in the net?"
"It is like the imitation netting you brought from Paris, mamma;
is it not? The holes are perfect diamonds, and much larger than in
any English net."
"Yes, it is part of that I brought with me. Being so open, it is easy
to slip the straw through it. Take the end of the straw, pass it under
two threads, and over three, in one line. Cut it off close to the edge
of the net. Run in as many lines as you can in the same direction,
but with intervals of four holes, five threads between them. Cross
them with others in the same way, both straws passing under the
same hole when they cross. All the four pieces of net must be
worked in the same way, and then tacked on, to cover the silk on
one side. Now sew the fronts to the backs. The ribbon trimming
must now be prepared. It is to be quilled in the centre, in the way
called box quilling; that is, one plait must be to the right, and the
next to the left. Do enough for the top of each pocket separately,
and put it on, then a length to go completely round. Finish each
pocket with a knot of ribbon at the point, and a small loop to pin it
to the bed."
"Do you know, mamma, I was inclined to think you had not
matched the sarsnet and ribbon well? The sarsnet looked so much
the darker. Now they correspond perfectly. How is that?"
"You forget that the sarsnet is covered with net, which softens
the depth of the tint considerably. Had the covering been muslin, it
must have been still deeper, to correspond with the uncovered
ribbon. It is for want of the consideration of these small points that
there is so frequently a want of harmony in the tints of amateur
needlewomen."
"And now, mamma, what next? For I have a good deal of spare
time still."
"You said you would like to work papa a pair of slippers, so I
have contrived a design for you, which will use up all your remnants
of wool. We will call it the dice pattern. Of each color you may use,
you will require two shades with black and white. You can mark on
your canvas the outline of the slippers with a soft pen and ink; then
work from the drawing I have made, beginning at the toe. You may
use any number of colors, only let them be well chosen, and falling
in stripes. Do not put green and blue, or any other two colors which
do not blend well, close together. You may try the effect with shades
in the following order: violet, orange, green, crimson, blue. That part
which is quite white in the drawing is done in white wool, and there
are two spotted squares which are to be black. Then the upper side
of each die is in the darker shade of whatever color may be used,
and the under light. Fill it up with black. If you work on Penelope
canvas, you will find it much easier."
DICE PATTERN FOR SLIPPERS.

EMBROIDERY.—DRESS UNDERSLEEVE.

(See Brown Cut in front of Book.)


Materials.—Half a yard of fine Swiss muslin; embroidery cotton,
No. 100.

You might also like