Table Of ContentDeep Belief Nets in C++
and CUDA C: Volume 1
Restricted Boltzmann Machines and
Supervised Feedforward Networks
Timothy Masters
Deep Belief Nets in C++ and CUDA C: Volume 1: Restricted Boltzmann Machines and
Supervised Feedforward Networks
Timothy Masters
Ithaca, New York, USA
ISBN-13 (pbk): 978-1-4842-3590-4 ISBN-13 (electronic): 978-1-4842-3591-1
https://doi.org/10.1007/978-1-4842-3591-1
Library of Congress Control Number: 2018940161
Copyright © 2018 by Timothy Masters
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/9781484235904. For more
detailed information, please visit www.apress.com/source-code.
Contents
Chapter 1: Introduction���������������������������������������������������������������������������������������������1
Review of Multiple-Layer Feedforward Networks �������������������������������������������������������������������������2
What Are Deep Belief Nets, and Why Do We Like Them? ��������������������������������������������������������������5
Chapter 2: Supervised Feedforward Networks ���������������������������������������������������������9
Backpropagation of Errors ������������������������������������������������������������������������������������������������������������9
SoftMax Outputs for Classification ����������������������������������������������������������������������������������������13
Code for Gradient Calculation �����������������������������������������������������������������������������������������������������18
Weight Penalties �������������������������������������������������������������������������������������������������������������������������27
Multithreading Gradient Computation �����������������������������������������������������������������������������������������29
Gradient Computation with CUDA �����������������������������������������������������������������������������������������������36
Basic Architecture �����������������������������������������������������������������������������������������������������������������37
A Simple Example ������������������������������������������������������������������������������������������������������������������39
Initialization ���������������������������������������������������������������������������������������������������������������������������43
Hidden Neuron Activation ������������������������������������������������������������������������������������������������������47
Output Neuron Activation �������������������������������������������������������������������������������������������������������52
SoftMax Output ����������������������������������������������������������������������������������������������������������������������53
Output Delta ���������������������������������������������������������������������������������������������������������������������������54
Output Gradient ���������������������������������������������������������������������������������������������������������������������55
Gradient of the First Hidden Layer �����������������������������������������������������������������������������������������57
Gradient of Subsequent Hidden Layers ���������������������������������������������������������������������������������60
Fetching the Gradient ������������������������������������������������������������������������������������������������������������63
Mean Squared Error by Reduction �����������������������������������������������������������������������������������������65
Log Likelihood by Reduction ��������������������������������������������������������������������������������������������������70
Putting It All Together ������������������������������������������������������������������������������������������������������������71
Basic Training Algorithms ������������������������������������������������������������������������������������������������������������77
Simulated Annealing for Starting Weights �����������������������������������������������������������������������������78
Singular Value Decomposition for Optimal Output Weights ���������������������������������������������������80
Stochastic Gradient Descent �������������������������������������������������������������������������������������������������82
Conjugate Gradient Optimization �������������������������������������������������������������������������������������������84
Chapter 3: Restricted Boltzmann Machines �����������������������������������������������������������91
What Is a Restricted Boltzmann Machine? ���������������������������������������������������������������������������������91
Reconstruction Error��������������������������������������������������������������������������������������������������������������93
Maximum Likelihood Training, Sort Of ����������������������������������������������������������������������������������������94
Contrastive Divergence ���������������������������������������������������������������������������������������������������������97
Weight Penalties ������������������������������������������������������������������������������������������������������������������100
Encouraging Sparsity ����������������������������������������������������������������������������������������������������������101
Finding Initial Weights ���������������������������������������������������������������������������������������������������������������103
Hidden Neuron Bias �������������������������������������������������������������������������������������������������������������105
Visible Neuron Bias��������������������������������������������������������������������������������������������������������������106
Code for Reconstruction Error ���������������������������������������������������������������������������������������������107
Multithreading Initial Weight Selection ��������������������������������������������������������������������������������109
Stochastic Gradient Descent Basic Principles ��������������������������������������������������������������������������116
The Core Algorithm ��������������������������������������������������������������������������������������������������������������116
Dividing Epochs into Batches ����������������������������������������������������������������������������������������������117
Shuffling Epochs ������������������������������������������������������������������������������������������������������������������118
Updating the Learning Rate and Momentum �����������������������������������������������������������������������118
Determining Convergence ���������������������������������������������������������������������������������������������������120
Code for Multithreaded RBM Training ���������������������������������������������������������������������������������������121
CUDA Code for RBM Training �����������������������������������������������������������������������������������������������������141
Initialization and Cache Line Matching ��������������������������������������������������������������������������������142
Fetching Training Cases �������������������������������������������������������������������������������������������������������143
Visible-to-Hidden Layer �������������������������������������������������������������������������������������������������������144
Hidden-to-Visible Layer �������������������������������������������������������������������������������������������������������145
Gradient Length and Dot Product by Reduction �������������������������������������������������������������������146
Updating the Input Bias �������������������������������������������������������������������������������������������������������148
Updating the Hidden Neuron Bias ���������������������������������������������������������������������������������������149
Updating the Weights ����������������������������������������������������������������������������������������������������������151
Putting It All Together ����������������������������������������������������������������������������������������������������������153
Timing ����������������������������������������������������������������������������������������������������������������������������������160
Updating Weights Analysis ��������������������������������������������������������������������������������������������������161
Visible-to-Hidden Analysis ���������������������������������������������������������������������������������������������������166
Hidden-to-Visible Analysis ���������������������������������������������������������������������������������������������������169
Advanced Training and Future Versions �������������������������������������������������������������������������������172
Chapter 4: Greedy Training �����������������������������������������������������������������������������������173
Generative Sampling �����������������������������������������������������������������������������������������������������������������175
Chapter 5: DEEP Operating Manual ����������������������������������������������������������������������183
Menu Options ����������������������������������������������������������������������������������������������������������������������������183
File Menu Options ����������������������������������������������������������������������������������������������������������������183
Test Menu Options ���������������������������������������������������������������������������������������������������������������184
Display Menu Options ����������������������������������������������������������������������������������������������������������186
The “Read a database” Option ��������������������������������������������������������������������������������������������������186
The “Read MNIST image” Option ����������������������������������������������������������������������������������������������186
The “Read MNIST labels” Option ����������������������������������������������������������������������������������������������187
The “Write activation file” Option ����������������������������������������������������������������������������������������������187
The “Clear all data” Option �������������������������������������������������������������������������������������������������������187
Model Architecture ��������������������������������������������������������������������������������������������������������������������188
Database Inputs and Targets �����������������������������������������������������������������������������������������������������189
RBM Training Params ����������������������������������������������������������������������������������������������������������������189
Supervised Training Params �����������������������������������������������������������������������������������������������������193
Train ������������������������������������������������������������������������������������������������������������������������������������������196
Test �������������������������������������������������������������������������������������������������������������������������������������������198
Analyze��������������������������������������������������������������������������������������������������������������������������������������200
Receptive Field �������������������������������������������������������������������������������������������������������������������������201
Generative Sample ��������������������������������������������������������������������������������������������������������������������202
The DEEP�LOG File ���������������������������������������������������������������������������������������������������������������������206
Index ���������������������������������������������������������������������������������������������������������������������213
Introduction
This book is intended primarily for readers who already have at least a basic knowledge
of neural networks but are interested in learning about, experimenting with, and perhaps
even programming deep belief nets. The salient features of this book are the following:
• The book provides motivation for the deep belief net paradigm.
• It presents the most important equations for the most common deep
belief net components and justifies them to a modest degree.
• The book provides training, execution, and analysis algorithms for
common deep belief net paradigms in language-independent forms.
• This book serves as a detailed user’s manual for the DEEP program,
which is available as a free download from the author’s web site.
I describe the internal operations of the program in depth.
• The book provides C++ code for many essential deep belief net
algorithms. This includes versions for multiple-thread execution on
Windows-based computers, as well as CUDA C implementations
for using the supercomputer capabilities of NVIDIA CUDA-capable
GPU cards.
It must be noted that several items are not included in this book.
• I largely avoid detailed mathematical theory. If we want to
understand the quite advanced theory behind deep belief nets,
numerous papers are available on the Internet. I will identify a few of
the best later in this chapter.
• I present only those models I found to be of greatest practical,
real-world value in my own work. This does not imply that the
omitted models are inferior, only that I have not found them to be
outstandingly useful in my particular applications.
Chapter 1 IntroduCtIon
In summary, I have attempted to fill gaps in the public domain material on deep
belief nets. Rigorous theory is available in numerous papers, especially those of
Dr. Geoffrey Hinton and other pioneers in the field. Reproducing these excellent
discussions would be redundant. Also, general statements of basic algorithms are widely
available on the Internet, though these are generally devoid of the practical nuances
that make the difference between a useful algorithm and something suitable for only toy
problems. What appears to be lacking in the public domain are the specific, practical bits
of information needed by someone who wants to program deep belief nets and use them
to solve real-world problems. This book focuses on such practicalities.
Review of Multiple-Layer Feedforward Networks
A multiple-layer feedforward network is generally illustrated as a stack of layers of
“neurons,” similar to what is shown in Figures 1-1 and 1-2. The bottom layer is the input
to the network, what would be referred to as the independent variables or predictors in
traditional modeling literature. The layer above the input layer is the first hidden layer.
Each neuron in this layer attains an activation that is computed by taking a weighted
sum of the inputs and then applying a nonlinear function. Each hidden neuron in this
layer will have a different set of input weights.
Figure 1-1. A shallow network
2
Chapter 1 IntroduCtIon
Figure 1-2. A deep network
If there is a second hidden layer, the activations of each of its neurons are computed
by taking a weighted sum of the activations of the first hidden layer and applying a
nonlinear function. This process is repeated for as many hidden layers as desired.
The topmost layer is the output of the network. There are many ways of computing
the activations of the output layer, and several of them will be discussed later. For now
let’s assume that the activation of each output neuron is just a weighted sum of the
activations of the neurons in the prior layer, without use of a nonlinear function.
Figures 1-1 and 1-2 show only a small subset of the connections. Actually, every
neuron in every layer feeds into every neuron in the next layer above.
3
Chapter 1 IntroduCtIon
To be more specific, Equation 1-1 shows the activation of a hidden neuron,
expressed as a function of the activations of the prior layer. In this equation, x = {x , …, x }
1 K
is the vector of prior-layer activations, w = {w , …, w } is the vector of associated weights,
1 K
and b is a bias term.
æ K ö
a= f çb+åw x ÷ (1-1)
è k kø
k=1
It’s often more convenient to consider the activation of an entire layer at once. In
Equation 1-2, the weight matrix W has K columns, one for each neuron in the prior layer,
and as many rows as there are neurons in the layer being computed. The bias and layer
inputs are column vectors. The nonlinear activation function is applied element-wise to
the vector.
a= f (b+Wx) (1-2)
There is one more way of expressing the computation of activations, which is most
convenient in some situations. The bias vector b can be a nuisance, so it can be absorbed
into the weight matrix W by appending it as one more column at the right side. We then
augment the x vector by appending 1.0 to it: x = {x , …, x , 1}. The equation for the layer’s
1 K
activations then simplifies to the activation function operating on a simple matrix/vector
multiplication, as shown in Equation 1-3.
a= f (Wx) (1-3)
What about the activation function? Traditionally, the hyperbolic tangent function
has been used because it has some properties that make training faster. However, for
reasons that will become clear later, we will exclusively use the logistic function shown in
Equation 1-4 and graphed in Figure 1-3.
1
f (t)= (1-4)
1+e--t
4