Professional Documents
Culture Documents
Andre Ye
Modern Deep Learning Design and Application Development: Versatile Tools to Solve
Deep Learning Problems
Andre Ye
Redmond, WA, USA
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
vi
Table of Contents
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
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
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