You are on page 1of 14

Modern Deep Learning

Design and Application


Development
Versatile Tools to Solve Deep
Learning Problems

Andre Ye
Modern Deep Learning Design and Application Development: Versatile Tools to Solve
Deep Learning Problems
Andre Ye
Redmond, WA, USA

ISBN-13 (pbk): 978-1-4842-7412-5 ISBN-13 (electronic): 978-1-4842-7413-2


https://doi.org/10.1007/978-1-4842-7413-2

Copyright © 2022 by Andre Ye


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: Celestin Suresh John
Development Editor: James Markham
Coordinating Editor: Aditee Mirashi
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza,
Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, 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 booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
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-­7412-­5. For more
detailed information, please visit http://www.apress.com/source-­code.
Printed on acid-free paper
For my mother Fang and my father Quinn
Table of Contents
About the Author����������������������������������������������������������������������������������������������������� xi

About the Technical Reviewer������������������������������������������������������������������������������� xiii

Acknowledgments���������������������������������������������������������������������������������������������������xv

Introduction�����������������������������������������������������������������������������������������������������������xvii

Chapter 1: A
 Deep Dive into Keras���������������������������������������������������������������������������� 1
Why Keras?����������������������������������������������������������������������������������������������������������������������������������� 2
Installing and Importing Keras������������������������������������������������������������������������������������������������������ 3
The Simple Keras Workflow���������������������������������������������������������������������������������������������������������� 4
Step 1: Define Architecture������������������������������������������������������������������������������������������������������ 4
Step 2: Compile��������������������������������������������������������������������������������������������������������������������� 11
Step 3: Fit and Evaluate��������������������������������������������������������������������������������������������������������� 27
Visualizing Model Architectures�������������������������������������������������������������������������������������������������� 30
Functional API����������������������������������������������������������������������������������������������������������������������������� 33
Translating a Sequential to a Functional Model�������������������������������������������������������������������� 35
Building Nonlinear Topologies����������������������������������������������������������������������������������������������� 36
Dealing with Data������������������������������������������������������������������������������������������������������������������������ 41
TensorFlow Dataset from Loaded Data���������������������������������������������������������������������������������� 42
TensorFlow Dataset from Image Files����������������������������������������������������������������������������������� 43
Automatic Image Dataset from Directory������������������������������������������������������������������������������ 44
ImageDataGenerator������������������������������������������������������������������������������������������������������������� 45
Key Points����������������������������������������������������������������������������������������������������������������������������������� 47

v
Table of Contents

Chapter 2: P
 retraining Strategies and Transfer Learning��������������������������������������� 49
Developing Creative Training Structures������������������������������������������������������������������������������������� 50
The Power of Pretraining������������������������������������������������������������������������������������������������������� 51
Transfer Learning Intuition���������������������������������������������������������������������������������������������������� 56
Self-Supervised Learning Intuition���������������������������������������������������������������������������������������� 60
Transfer Learning Practical Theory��������������������������������������������������������������������������������������������� 65
Transfer Learning Models and Model Structure�������������������������������������������������������������������� 66
Changing Pretrained Model Architectures����������������������������������������������������������������������������� 73
Neural Network “Top” Inclusivity������������������������������������������������������������������������������������������� 78
Layer Freezing����������������������������������������������������������������������������������������������������������������������� 79
Implementing Transfer Learning������������������������������������������������������������������������������������������������� 81
General Implementation Structure: A Template��������������������������������������������������������������������� 81
No Architecture or Weight Changes��������������������������������������������������������������������������������������� 82
Transfer Learning Without Layer Freezing����������������������������������������������������������������������������� 85
Transfer Learning with Layer Freezing���������������������������������������������������������������������������������� 88
Accessing PyTorch Models���������������������������������������������������������������������������������������������������� 89
Implementing Simple Self-Supervised Learning������������������������������������������������������������������������ 91
Case Studies������������������������������������������������������������������������������������������������������������������������������� 95
Transfer Learning Case Study: Adversarial Exploitation of Transfer Learning����������������������� 95
Self-Supervised Learning Case Study: Predicting Rotations����������������������������������������������� 102
Self-Supervised Learning Case Study: Learning Image Context and Designing Nontrivial
Pretraining Tasks����������������������������������������������������������������������������������������������������������������� 108
Key Points��������������������������������������������������������������������������������������������������������������������������������� 112

Chapter 3: T he Versatility of Autoencoders���������������������������������������������������������� 115


Autoencoder Intuition and Theory��������������������������������������������������������������������������������������������� 116
The Design of Autoencoder Implementation����������������������������������������������������������������������������� 121
Autoencoders for Tabular Data�������������������������������������������������������������������������������������������� 121
Autoencoders for Image Data���������������������������������������������������������������������������������������������� 127
Image Data Shape Structure and Transformations�������������������������������������������������������������� 127
Convolutional Autoencoder Without Pooling������������������������������������������������������������������������ 133

vi
Table of Contents

Convolutional Autoencoder Vector Bottleneck Design��������������������������������������������������������� 137


Convolutional Autoencoder with Pooling and Padding�������������������������������������������������������� 140
Autoencoders for Other Data Forms����������������������������������������������������������������������������������������� 145
Autoencoder Applications��������������������������������������������������������������������������������������������������������� 145
Using Autoencoders for Denoising�������������������������������������������������������������������������������������� 146
Using Autoencoders for Pretraining������������������������������������������������������������������������������������� 156
Using Autoencoders for Dimensionality Reduction�������������������������������������������������������������� 161
Using Autoencoders for Feature Generation������������������������������������������������������������������������ 164
Using Variational Autoencoders for Data Generation����������������������������������������������������������� 172
Case Studies����������������������������������������������������������������������������������������������������������������������������� 188
Autoencoders for Pretraining Case Study: TabNet��������������������������������������������������������������� 189
Denoising Autoencoders Case Study: Chinese Spelling Checker���������������������������������������� 193
Variational Autoencoders Case Study: Text Generation������������������������������������������������������� 197
Key Points��������������������������������������������������������������������������������������������������������������������������������� 201

Chapter 4: M
 odel Compression for Practical Deployment������������������������������������ 205
Introduction to Model Compression������������������������������������������������������������������������������������������ 206
Pruning�������������������������������������������������������������������������������������������������������������������������������������� 210
Pruning Theory and Intuition����������������������������������������������������������������������������������������������� 210
Pruning Implementation������������������������������������������������������������������������������������������������������ 215
Pruning in Theoretical Deep Learning: The Lottery Ticket Hypothesis��������������������������������� 226
Quantization������������������������������������������������������������������������������������������������������������������������������ 229
Quantization Theory and Intuition���������������������������������������������������������������������������������������� 229
Quantization Implementation���������������������������������������������������������������������������������������������� 232
Weight Clustering���������������������������������������������������������������������������������������������������������������������� 236
Weight Clustering Theory and Intuition������������������������������������������������������������������������������� 237
Weight Clustering Implementation�������������������������������������������������������������������������������������� 238
Collaborative Optimization�������������������������������������������������������������������������������������������������������� 240
Sparsity Preserving Quantization���������������������������������������������������������������������������������������� 243
Cluster Preserving Quantization������������������������������������������������������������������������������������������ 245
Sparsity Preserving Clustering�������������������������������������������������������������������������������������������� 246

vii
Table of Contents

Case Studies����������������������������������������������������������������������������������������������������������������������������� 248


Extreme Collaborative Optimization������������������������������������������������������������������������������������ 248
Rethinking Quantization for Deeper Compression��������������������������������������������������������������� 251
Responsible Compression: What Do Compressed Models Forget?������������������������������������� 254
Key Points��������������������������������������������������������������������������������������������������������������������������������� 257

Chapter 5: A
 utomating Model Design with Meta-optimization���������������������������� 259
Introduction to Meta-optimization�������������������������������������������������������������������������������������������� 260
General Hyperparameter Optimization�������������������������������������������������������������������������������������� 264
Bayesian Optimization Intuition and Theory������������������������������������������������������������������������ 264
Hyperopt Syntax, Concepts, and Usage������������������������������������������������������������������������������� 270
Hyperas Syntax, Concepts, and Usage�������������������������������������������������������������������������������� 284
Neural Architecture Search������������������������������������������������������������������������������������������������������� 289
NAS Intuition and Theory����������������������������������������������������������������������������������������������������� 290
Auto-Keras��������������������������������������������������������������������������������������������������������������������������� 297
Case Studies����������������������������������������������������������������������������������������������������������������������������� 311
NASNet�������������������������������������������������������������������������������������������������������������������������������� 312
Progressive Neural Architecture Search������������������������������������������������������������������������������ 317
Efficient Neural Architecture Search����������������������������������������������������������������������������������� 320
Key Points��������������������������������������������������������������������������������������������������������������������������������� 323

Chapter 6: S
 uccessful Neural Network Architecture Design�������������������������������� 327
Nonlinear and Parallel Representation������������������������������������������������������������������������������������� 330
Residual Connections���������������������������������������������������������������������������������������������������������� 332
Branching and Cardinality��������������������������������������������������������������������������������������������������� 346
Case Study: U-Net���������������������������������������������������������������������������������������������������������������� 351
Block/Cell Design���������������������������������������������������������������������������������������������������������������������� 357
Sequential Cell Design��������������������������������������������������������������������������������������������������������� 359
Nonlinear Cell Design���������������������������������������������������������������������������������������������������������� 370
Case Study: InceptionV3������������������������������������������������������������������������������������������������������ 373
Neural Network Scaling������������������������������������������������������������������������������������������������������������ 380
Input Shape Adaptable Design�������������������������������������������������������������������������������������������� 382
Parametrization of Network Dimensions����������������������������������������������������������������������������� 388
viii
Table of Contents

Case Study: EfficientNet������������������������������������������������������������������������������������������������������ 392


Key Points��������������������������������������������������������������������������������������������������������������������������������� 399

Chapter 7: R
 eframing Difficult Deep Learning Problems�������������������������������������� 401
Data Representation: DeepInsight��������������������������������������������������������������������������������������������� 403
Corrupted Data: Negative Learning with Noisy Labels�������������������������������������������������������������� 414
Limited Data: Siamese Networks���������������������������������������������������������������������������������������������� 427
Key Points and Epilogue����������������������������������������������������������������������������������������������������������� 438

Index��������������������������������������������������������������������������������������������������������������������� 441

ix
About the Author
Andre Ye is a deep learning researcher and writer
working toward making deep learning more accessible,
understandable, and responsible through technical
communication. He is also a cofounder at Critiq, a machine
learning platform facilitating greater efficiency in the
peer-review process. In his spare time, Andre enjoys
keeping up with current deep learning research, reading up
on history and philosophy, and playing the piano.

xi
About the Technical Reviewer
Manohar Swamynathan is a data science practitioner and
an avid programmer, with over 14+ years of experience
in various data science-related areas that include data
warehousing, Business Intelligence (BI), analytical tool
development, ad hoc analysis, predictive modeling, data
science product development, consulting, formulating
strategies, and executing analytics programs. He’s had
a career covering the life cycle of data across different
domains, such as US mortgage banking, retail/ecommerce,
insurance, and industrial IoT. He has a bachelor’s degree
with a specialization in physics, mathematics, and
computers and a master’s degree in project management.
He’s currently living in Bengaluru, the Silicon Valley of India.

xiii
Acknowledgments
This book could not have been developed and published without the direct and indirect
help of so many. I want to express my thanks to Celestin Suresh John, who first made me
aware of this opportunity; Aditee Mirashi, the coordinating editor who was always there
to assist me and provide help to my countless inquiries; Jim Markham, the development
editor, and Manohar Swamynathan, the technical reviewer, both of whom provided
helping suggestions and comments in revision; and the editing team broadly that made
writing this book such a wonderful experience. I also would like to thank the authors and
coauthors of the over 20 deep learning papers used in this book as case studies for their
cooperation and help.
Throughout writing the book, my family has always been there for me – my father
Quinn, my mother Fang, and my brother Aiden. It was my father’s early work in neural
networks and support that spurred much of my initial interest in deep learning, and my
family has been incredibly welcoming of conversation and discussion on the subject and
for emotional support more generally. I also want to appreciate Andy Wang, who has
been my go-to person for deep learning ideas, chatting, and project collaboration, and
my friends – Krishna, Mark, Ayush, and Vivek – for their unwavering support, friendship,
and encouragement.
Lastly, I would like to thank the Robinson Center – a truly wonderful accelerator
for intellectual development – and the amazing faculty and staff there that exposed
me to new and profound ideas and perspectives of thinking in my formative years.
Their involvement, I believe, has made me an improved thinker and communicator of
knowledge, technical and otherwise.

xv
Introduction
Deep learning is almost universally regarded as falling under the field of computer
science, with some intersections in statistics and mathematics broadly, like most
computer science disciplines. Thus, upon first impression, it may be perceived to be
a strict, exacting science – a rigid process of adjusting architectures and methods in a
rote fashion to squeeze out a few more percentage points of accuracy on some dataset.
While this is arguably a necessary component and period of deep learning’s trajectory of
growth and development, if embraced too wholeheartedly, it becomes damaging. Such
a perspective is lethal toward innovation in deep learning, for many – and, possibly,
most – of the major advances in deep learning were and are not derived through a strict,
rigorous scientific process of adjusting but rather through wild and free ideas.
Deep learning, as will be further explicated in this book, has always been somewhat
of an experiment-first explanation-after field. That is, the success of a method is almost
always explained in fuller detail after the method itself has been empirically observed to
be successful. The method itself usually emerges from a hunch or intuition developed
through a long series of experimentation and revision rather than a carefully constructed
system in which a strong and rational explanation for a method’s success precedes its
empirical success. This is not, of course, to suggest that to make strides in deep learning
one must be messy; it is only to propose that deep learning is a study in a stage guided
primarily by the spirit of freedom in thought and experimentation.
Part of a different but seemingly more popular perspective of deep learning as rigid
and more or less a continuation of the “find the best model” paradigm within machine
learning, undoubtedly, arises from an immense focus on validation metrics that was
spurred in the relatively early development of more advanced and powerful deep
learning architectures and methods in the early to middle 2010s and the continuation of
this spirit in popular deep learning competitions and competition platforms.
Beyond this phase, though, deep learning is often presented in a way that encourages
this problematic perspective. Books, courses, online articles, and other mediums explain
deep learning through a series of concrete examples, packaging their primary value as
lying within the implementation of said examples. Concrete examples are important and
come at little cost in a digital-based field, which is likely why they have become so prized
in the computer science context.
xvii
Introduction

The trouble here is that the focus is often too narrowly placed upon specific
examples rather than broader concepts. It is very much reflective of the “give a man a
fish and you feed him for a day, teach him how to fish and you feed him for his lifetime”
aphorism. Rather than feeding single fish to readers, teaching motion and freedom –
the act of fishing for new ideas and applications – must be a key component in the
presentation of deep learning. Examples and implementations that are shown should be
anchor points that guide broader conceptual understandings rather than being the sole
occupants of the reader’s understanding.
Think of this in relation to the overfitting paradigm introduced early to machine
learning practitioners. If a simple curve fitting model places too high a weight on
passing through each point in its dataset, it models the data perfectly but fails to model
the underlying phenomena – the concept – that the data represents. This is overfitting.
The model is thus prone to difficulty generalizing the ideas that the data represents,
failing to be useful in new applications and contexts. Many of the measures used in
classical machine learning as well as deep learning seek to maximize the model’s
performance with respect to modeling the concepts underlying the data rather than
the data in and of itself. Likewise, the presentation of deep learning will inevitably be
heavily tied to specific examples and developments, but should not consist just of these
specific examples, for someone who has learned deep learning this way will likely have
difficulty using deep learning in a context outside the narrow range that they learned
those examples in. Moreover, they will find it challenging to innovate new ideas and
approaches – a skill incredibly valuable in such a quickly evolving field.
The goal of this book is to take a step toward this concept-prioritizing vision of deep
learning communication, to communicate the ideas of deep learning design, rather
than to develop the reader’s knowledge of deep learning in a manner consisting only of
specific architectures and methods with no understanding of how to generalize and find
innovative freedom within deep learning.
Make no mistake – this goal does not mean that this book is lacking in examples
or implementation. The first chapter, “A Deep Dive into Keras,” is dedicated entirely
toward developing a deep understanding of Keras, the deep learning library we will
use to realize concepts as implementations and applications. The remainder of the
book is organized generally in an intuition/theory-first, implementation-second
bipartite structure; the concept is introduced first with examples, analogy, and
necessary instruments of theory and intuition, followed by a walk-through of code
implementations and variations. Additionally, each of the six chapters following the

xviii
Introduction

first contains three case studies, each of which covers a deep learning paper from recent
deep learning research in a perspective relevant to the chapter’s topic, some of which
contain code walk-throughs. The objective of the case studies is multifold: to explore
the diversity of topics explored in current deep learning research, to provide empirical
evidence on the success of certain methods, and to provide examples of deep learning
innovations that push the boundaries and correspondingly advance the field’s state.
The six chapters following the first each cover an important set of concepts in
modern deep learning. It is important to note that these chapters assume a basic
knowledge of neural network operation (feed-forward, gradient descent, standard,
and convolutional layers) and general machine learning principles. The chapters
successively build upon these foundations to explore transfer learning and self-­
supervised learning in Chapter 2, “Pretraining Strategies and Transfer Learning”;
autoencoders in Chapter 3, “The Versatility of Autoencoders”; model compression in
Chapter 4, “Model Compression for Practical Deployment”; Bayesian optimization
and Neural Architecture Search in Chapter 5, “Automating Model Design with Meta-­
optimization”; modern architecture design in Chapter 6, “Successful Neural Network
Architecture Design”; and problem-solving in Chapter 7, “Reframing Difficult Deep
Learning Problems.” Readers, especially those familiar with other deep learning
resources, will find that these chapters contain many undiscussed topics in other
books and tutorials, including self-supervised learning; model compression methods
like pruning, quantization, and weight clustering; and Neural Architecture Search
methods. Broadly, the book attempts a novel approach at communicating both core
and boundary-pushing aspects of deep learning in a light that emphasizes intuition,
variation, and generalization across concepts and methods.
The hope is that this book not only gives the reader a strong, deep understanding of
the topics explored in each chapter but also intellectual freedom to propose and pursue
new deep learning ideas addressing the great expanse of difficult problems we face today
and will face tomorrow.

xix

You might also like