Practical MATLAB Deep Learning: A Projects-Based Approach, 2nd Edition Michael Paluszek instant download
Practical MATLAB Deep Learning: A Projects-Based Approach, 2nd Edition Michael Paluszek instant download
https://ebookmass.com/product/practical-matlab-deep-
learning-a-projects-based-approach-2nd-edition-michael-
paluszek/
https://ebookmass.com/product/matlab-machine-learning-recipes-a-
problem-solution-approach-3rd-edition-michael-paluszek/
https://ebookmass.com/product/beginning-anomaly-detection-using-
python-based-deep-learning-2nd-edition-suman-kalyan-adari/
https://ebookmass.com/product/practical-radiology-a-symptom-based-
approach-1st-edition/
https://ebookmass.com/product/neuroanesthesia-a-problem-based-
learning-approach-david-e-traul/
https://ebookmass.com/product/go-crazy-a-fun-projects-based-approach-
to-golang-programming-1st-edition-nicolas-modrzyk/
https://ebookmass.com/product/practical-graphql-learning-full-stack-
graphql-development-with-projects-1st-edition-nabendu-biswas/
Stephanie Thomas
Princeton, NJ, USA
Eric Ham
Princeton, NJ, USA
This work is subject to copyright. All rights are solely and exclusively
licensed 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.
The publisher, the authors, and the editors are safe to assume that the
advice and information in this book are believed to be true and accurate
at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the
material contained herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
Stephanie Thomas
is Vice President of Princeton Satellite
Systems, Inc. in Plainsboro, New Jersey.
She received her bachelor’s and master’s
degrees in Aeronautics and Astronautics
from the Massachusetts Institute of
Technology in 1999 and 2001. Ms.
Thomas was introduced to the PSS
Spacecraft Control Toolbox for MATLAB
during a summer internship in 1996 and
has been using MATLAB for aerospace
analysis ever since. In her 20 years of
MATLAB experience, she has developed
many software tools including the Solar
Sail Module for the Spacecraft Control
Toolbox; a proximity satellite operations
toolbox for the Air Force; collision
monitoring Simulink blocks for the Prisma satellite mission; and launch
vehicle analysis tools in MATLAB and Java. She has developed novel
methods for space situation assessment such as a numeric approach to
assessing the general rendezvous problem between any two satellites
implemented in both MATLAB and C++. Ms. Thomas has contributed to
PSS’ Spacecraft Attitude and Orbit Control textbook, featuring examples
using the Spacecraft Control Toolbox, and written many software users’
guides. She has conducted SCT training for engineers from diverse
locales such as Australia, Canada, Brazil, and Thailand and has
performed MATLAB consulting for NASA, the Air Force, and the
European Space Agency. Ms. Thomas is the coauthor of MATLAB
Recipes, MATLAB Machine Learning, and MATLAB Machine Learning
Recipes: A Problem-Solution Approach, published by Apress. In 2016,
Ms. Thomas was named a NASA NIAC Fellow for the project “Fusion-
Enabled Pluto Orbiter and Lander.”
Eric Ham
is an Electrical Engineer and Computer
Scientist at Princeton Satellite Systems in
Plainsboro, New Jersey. He has a BS in
Electrical Engineering with certificates
in Applications of Computing and
Robotics and Intelligent Systems from
Princeton University, 2019. At PSS, Mr.
Ham is working on developing neural
networks for terrain relative navigation
for a lunar lander under a NASA contract.
He is simultaneously working as a
research specialist with the Hasson Lab
at Princeton University’s Princeton
Neuroscience Institute. He is involved in
the design and testing of temporal
convolutional neural networks (TCNs) to
model semantic processing in the brain.
He developed a pipeline for automatic transcription of audio data using
Google’s speech-to-text API. He assisted in the development of a
method for sequence prediction that was inspired by Viterbi’s
algorithm.
His undergraduate research was on implementing an SDRAM for a
novel neuro-computing chip. Mr. Ham did a summer internship at
Princeton University in 2018, in which he worked on a novel path
selection algorithm to improve the security of the Tor onion router. He
worked at the Princeton Plasma Physics Laboratory in 2017 on a high-
efficiency Class-E RF amplifier for nuclear fusion plasma heating.
About the Technical Reviewers
Dr. Joseph Mueller
specializes in control systems and
trajectory optimization. For his doctoral
thesis, he developed optimal ascent
trajectories for stratospheric airships.
His active research interests include
robust optimal control, adaptive control,
applied optimization and planning for
decision support systems, and intelligent
systems to enable autonomous
operations of robotic vehicles. Prior to
joining SIFT in early 2014, Dr. Joseph
worked at Princeton Satellite Systems for
13 years. In that time, he served as the
principal investigator for eight Small Business Innovation Research
contracts for NASA, Air Force, Navy, and MDA. He has developed
algorithms for optimal guidance and control of both formation flying
spacecraft and high-altitude airships and developed a course of action
planning tool for DoD communication satellites. In support of a
research study for NASA Goddard Space Flight Center in 2005, Dr.
Joseph developed the Formation Flying Toolbox for MATLAB, a
commercial product that is now used at NASA, ESA, and several
universities and aerospace companies around the world. In 2006, he
developed the safe orbit guidance mode algorithms and software for
the Swedish Prisma mission, which has successfully flown a two-
spacecraft formation flying mission since its launch in 2010. Dr. Joseph
also serves as an adjunct professor in the Aerospace Engineering and
Mechanics Department at the University of Minnesota, Twin Cities
campus.
© The Author(s), under exclusive license to APress Media, LLC, part of Springer
Nature 2022
M. Paluszek et al., Practical MATLAB Deep Learning
https://doi.org/10.1007/978-1-4842-7912-0_1
Abstract
Deep learning is a subset of machine learning which is itself a subset of
artificial intelligence and statistics. Artificial intelligence research began
shortly after World War II [35]. Early work was based on the knowledge of
the structure of the brain, propositional logic, and Turing’s theory of
computation. Warren McCulloch and Walter Pitts created a mathematical
formulation for neural networks based on threshold logic. This allowed
neural network research to split into two approaches: one centered on
biological processes in the brain and the other on the application of neural
networks to artificial intelligence. It was demonstrated that any function
could be implemented through a set of such neurons and that a neural net
could learn to recognize patterns.
2. Support-vector machines
3. Adaptive control
4. System identification
(1.4)
The exponential one is normalized and offset from zero so it ranges from
−1 to 1. The last one, which simply passes through the value of y, is called
the linear activation function. The following code in the script
OneNeuron.m computes and plots these three activation functions for an
input q. Figure 1.4 shows the three activation functions on one plot.
Solution
Historically, the first neuron was the perceptron. This is a neuron with an
activation function that is a threshold. Its output is either 0 or 1. This is not
useful for many real-world problems. However, it is well suited for simple
classification problems. We will use a single perceptron in this example.
How It Works
Suppose our input is a light level measured by a photocell. If you weight the
input so that 1 is the value defining the brightness level at noon, you get a
sunny day detector.
This is shown in the script, SunnyDay.m. The solar flux is modeled
using cosine and scaled so that it is 1 at noon. Any value greater than 0 is
daylight.
Figure 1.7 shows the detector results. The set(gca,...) code sets
the x-axis ticks to end at exactly 24 hours. This is a really trivial example but
does show how classification works.
If we had multiple neurons with thresholds set to detect sunlight levels
within bands of solar flux, we would have a neural net sun clock.
Solution
The XOR problem impeded the development of neural networks for a long
time before “deep learning” was developed. Look at Figure 1.8. The table on
the left gives all possible inputs A and B and the desired outputs C.
“Exclusive or” just means that if the inputs A and B are different, the output
C is 1. The figure shows a single-layer network and a multi-layer network, as
in Figure 1.1, but with the weights labeled as they will be in the code. You
can implement this in MATLAB easily, in just seven lines:
Figure 1.7 The daylight detector. The top plot shows the input data, and the bottom plot
shows the perceptron output detecting daylight.
Figure 1.8 Exclusive or (XOR) truth table and possible solution networks.
This type of logic was embodied in medium-scale integrated circuits in
the early days of digital systems and vacuum tube–based computers even
earlier than that. Try as you might, you cannot pick two weights and a bias
on the single-layer network to reproduce the XOR. Minsky created proof
that it was impossible.
The second neural net, the deep neural net, can reproduce the XOR. We
will implement and train this network.
How It Works
What we will do is explicitly write out the backpropagation algorithm that
trains the neural net from the four training sets given in Figure 1.8, that is,
(0,0), (1,0), (0,1), or (1,1). We’ll write it in the script XORDemo.m. The point
is to show you explicitly how backpropagation works. We will use the tanh
as the activation function in this example. The XOR function is given in
XOR.m as follows:
There are three neurons, y1, y2, and y3. The activation function for the
hidden layer with neurons y1 and y2 is the hyperbolic tangent. The
activation function for the output layer y3 is linear. In addition to the
weights depicted in Figure 1.8, each neuron also has a bias input, numbered
w7, w8, and w9:
(1.7)
(1.8)
(1.9)
Now we will derive the backpropagation routine. The hyperbolic
activation function is
(1.10)
Its derivative is
(1.11)
In this derivation, we are going to use the chain rule. Assume that F is a
function of y which is a function of x. Then
(1.12)
The error is the square of the difference between the desired output and the
output. This is known as a quadratic error. It is easy to use because the
derivative is simple and the error is always positive, making the lowest
error the one closest to zero.
(1.13)
(1.14)
(1.15)
(1.17)
(1.18)
(1.19)
(1.20)
(1.21)
(1.22)
(1.23)
(1.24)
where
(1.25)
(1.26)
(1.27)
(1.28)
(1.29)
(1.30)
You can see from the derivation how this could be made recursive and
applied to any number of outputs or layers. Our weight adjustment at each
step will be
(1.31)
where η is the update gain. It should be a small number. We only have four
sets of inputs. We will apply them multiple times to get the XOR weights.
Our backpropagation trainer needs to find the nine elements of w. The
training function XORTraining.m is as follows:
The first two arguments to PlotSet are the data and are the minimum
required. The remainder is parameter pairs. The leg value has legends for
the two plots, as defined by ’plot set’. The first plot uses the first nine
data points, in this case, the weights. The second plot uses the last data
point, the mean of the error. leg is a cell array with two strings or string
arrays. The ’plot set’ is two arrays in a cell. A plot with only one value
will not generate a legend.
The demo script XORDemo.m starts with the training data, which is the
complete truth data for this simple function, and randomly generated
weights. It iterates through the inputs 25,000 times, with a training weight
of 0.001.
The results of the neural network with random weights and biases, as
expected, are not good. After training, the neural network reproduces the
XOR problem very well, as shown in the following demo output. Now, if you
change the initial weights and biases, you may find that you get bad results.
This is because the simple gradient method implemented here can fall into
local minima from which it can’t escape. This is an important point about
finding the best answer. There may be many good answers, which are
locally optimal, but there will be only one best answer. There is a vast body
of research on how to guarantee that a solution is globally optimal.
Figure 1.9 shows the weights and biases converging and also shows the
mean output error over all four inputs in the truth table going to zero. If you
try other starting weights and biases, this may not be the case. Other
solution methods, such as Genetic Algorithms [14], Electromagnetism based
[4], and Simulated Annealing [32], are less susceptible to falling into local
minima but can be slow. A good overview of optimization specifically for
machine learning is given by Bottou [5].
In the next chapter, we will use the MATLAB Deep Learning Toolbox to
solve this problem.
You can see how this compares to a set of linear equations. If we remove
the activation functions, we get
(1.32)
This reduces to just three independent coefficients:
(1.33)
Figure 1.9 Evolution of weights during exclusive or (XOR) training.
One is a constant, and the other two multiply the inputs. Writing the four
possible cases in matrix notation, we get
(1.34)
(1.35)
This makes three out of four equations correct. There is no way to make all
four correct with just three coefficients. The activation functions separate
the coefficients and allow us to reproduce the XOR. This is not surprising
because the XOR is not a linear problem.
The array form implies a structure for the data. The same number of
points could be organized into a single vector using reshape:
The numbers are the same, they are just organized differently.
Convolutional neural networks, described in the next section, are often used
for image structured data. We might also have a vector:
Appendix.
To make a sun-dial, procure a circular piece of zinc, about ⅛ inch
thick, and say twelve inches in diameter. Have a “style” or “gnomon”
cast such that the angle of its edge equals the latitude of the place
where the sun-dial is to be set up. This for London will be equal to
51° 30´´. A pattern may be made for this in wood; it should then be
cast in gun-metal, which is much better for out-of-door exposure
than brass. On a sheet of paper draw a circle A B C with centre O.
Make the angle B O D equal to the latitude of the place for London =
51° 30´´. From A draw A E parallel to O B to meet O D in E, and
with radius O E describe another circle about O. Divide the inner
circle A B C into twenty-four parts, and draw radii through them from
O to meet the larger circle. Through any divisions (say that
corresponding to two o’clock) draw lines parallel to O B, O C,
respectively to meet in a. Then the line O a is the shadow line of the
gnomon at two o’clock. The lines thus drawn on paper may be
transferred to the dial and engraved on it, or else eaten in with acid
in the manner in which etchings are done.
Fig. 18.
The centre O need not be in the centre of the zinc disc, but may
be on one side of it, so as to give better room for the hours, etc. A
motto may be etched upon the dial, such as “Horas non numero nisi
serenas,” or “Qual ’hom senza Dio, son senza sol io,” or any suitable
inscription, and the dial is ready for use. It is best put up by turning
it till the hour is shown truly as compared with a correctly timed
watch. It must be levelled with a spirit level. It must be remembered
that the sun does not move quite uniformly in his yearly path among
the fixed stars. This is because he moves not in a circle, but in an
ellipse of which the earth is in one of the foci. Hence the hours
shown on the dial are slightly irregular, the sun being sometimes in
advance of the clock, sometimes behind it. The difference is never
more than a quarter of an hour. There is no difference at
midsummer and midwinter.
Fig. 19.
But the first great step in science which Galileo made when quite
a young professor at Pisa was the refutation of Aristotle’s opinion
that heavy bodies fell to the earth faster than light ones. In the
presence of a number of professors he dropped two balls, a large
and a small one, from the parapet of the leaning tower of Pisa. They
fell to the ground almost exactly in the same time. This experiment
is quite an easy one to try. One of the simplest ways is as follows:
Into any beam (the lintel of a door will do), and about four inches
apart, drive three smooth pins so as to project each about a quarter
of an inch; they must not have any heads. Take two unequal
weights, say of 1 lb. and 3 lbs. Anything will do, say a boot for one
and pocket-knife for the other; fasten loops of fine string to them,
put the loops over the centre peg of the three, and pass the strings
one over each of the side pegs. Now of course if you hitch the loops
off the centre peg P the objects will be released together. This can
be done by making a loop at the end of another piece of string, A,
and putting it on to the centre peg behind the other loops. If the
string be pulled of course the loop on it pulls the other two loops off
the central peg, and allows the boot and the knife to drop. The boot
and the knife should be hung so as to be at the same height. They
will then fall to the ground together. The same experiment can be
tried by dropping two objects from an upper window, holding one in
each hand, and taking care to let them go together.
This result is very puzzling; one does not
understand it. It appears as though two
unequal forces produced the same effect. It
is as though a strong horse could run no
faster than a weaker one.
The professors were so irritated at the
result of this experiment, and indeed at the
general character of young Professor
Fig. 22. Galileo’s attacks on the time-honoured ideas
of Aristotle, that they never rested till they
worried him out of his very poorly paid chair
at Pisa. He then took a professorship at Padua.
Let us now examine this result and see why it is that the ideas we
should at first naturally form are wrong, and that the heavy body will
fall in exactly the same time as the light one.
We may reason the matter in this way. The heavy body has more
force pulling on it; that is true, but then, on the other hand there is
more matter which has got to be moved. If a crowd of persons are
rushing out of a building, the total force of the crowd will be greater
than the force of one man, but the speed at which they can get out
will not be greater than the speed of one man; in fact, each man in
the crowd has only force enough to move his own mass. And so it is
with the weights: each part of the body is occupied in moving itself.
If you add more to the body you only add another part which has
itself to move. A hundred men by taking hands cannot run faster
than one man.
But, you will say, cannot a man run faster than a child? Yes,
because his impelling power is greater in proportion to his weight
than that of a child.
If it were the fact that the attraction of gravity due to the earth
acted on some bodies with forces greater in proportion to their
masses than the forces that acted on other bodies, then it is true
that those different bodies would fall in unequal time. But it is an
experimental fact that the attractive force of gravity is always exactly
proportional to the mass of a body, and the resistance to motion is
also proportional to mass, hence the force with which a body is
moved by the earth’s attraction is always proportional to the
difficulty of moving the body. This would not be the case with other
methods of setting a body in motion. If I kick a small ball with all my
might, I shall send it further than a kick of equal strength would
send a heavier ball. Why? Because the impulse is the same in each
case, but the masses are different. But if those balls are pulled by
gravity, then, by the very nature of the earth’s attraction (the reason
of which we cannot explain), the small ball receives a little pull, and
the big ball receives a big pull, the earth exactly apportioning its pull
in each case to the mass of the body on which it has to act. It is to
this fact, that the earth pulls bodies with a strength always in each
case exactly proportional to their masses, that is due the result that
they fall in equal times, each body having a pull given to it
proportional to its needs.
The error of the view of Aristotle was not only demonstrated by
Galileo by experiment, but was also demonstrated by argument. In
this argument Galileo imitated the abstract methods of the
Aristotelians, and turned those methods against themselves. For he
said, “You” (the Aristotelians) “say that a lighter body will fall more
slowly than a heavy one. Well, then, if you bind a light body on to a
heavy one by means of a string, and let them fall together, the light
body ought to hang behind, and impede the heavy body, and thus
the two bodies together ought to fall more slowly than the heavy
body alone; this follows from your view: but see the contradiction.
For the two bodies tied together constitute a heavier body than the
heavy body alone, and thus, on your own theory, ought to fall more
quickly than the heavy body alone. Your theory, therefore,
contradicts itself.”
The truth is that each body is occupied in moving itself without
troubling about moving its neighbour, so that if you put any number
of marbles into a bag and let them drop they all go down
individually, as it were, and all in the time which a single marble
would take to fall. For any other result would be a contradiction. If
you cut a piece of bread in two, and put the two halves together,
and tie them together with a thread, will the mere fact that they are
two pieces make each of them fall more slowly than if they were
one? Yet that is what you would be bound to assert on the
Aristotelian theory. Hold an egg in your open hand and jump down
from a chair. The egg is not left behind; it falls with you. Yet you are
the heavier of the two, and on Aristotelian principles you ought to
leave the egg behind you. It is true that when you jump down a
bank your straw hat will often come off, but that is because the air
offers more resistance to it than the air offers to your body. It is the
downward rush through the air that causes your hat to be left
behind, just as wind will blow your hat off without blowing you away.
For since motion is relative, it is all one whether you jump down
through the air, or the air rushes past you, as in a wind. If there
were no air, the hat would fall as fast as your body.
This is easy to see if we have an airpump and are thus enabled to
pump out almost all the air from a glass vessel. In that vessel so
exhausted, a feather and a coin will fall in equal times. If we have
not an airpump, we can try the experiment in a more simple way.
For let us put a feather into a metal egg-cup and drop them
together. The cup will keep the air from the feather, and the feather
will not come out of the cup. Both will fall to the ground together.
But if the lighter body fall more slowly, the feather ought to be left
behind. If, however, you tie some strings across a napkin ring so as
to make a sort of rough sieve, and put a feather in it, and then drop
the ring, then as the ring falls the air can get through the bottom of
the ring and act on the feather, which will be left floating as the ring
falls.
Let us now go on to examine the second fallacy that was derived
from the Aristotelians, and that so long impeded the advance of
science, namely, that the earth must be at rest.
The principal reason given for this was that if bodies were thrown
up from the earth they ought, if the earth were in motion, to remain
behind. Now, if this were so, then it would follow that if a person in
a train which was moving rapidly threw a ball vertically, that is
perpendicularly, up into the air, the ball, instead of coming back into
his hand, ought to hit the side of the carriage behind him. The next
time any of my readers travel by train he can easily satisfy himself
that this is not so. But there are other ways of proving it. For
instance, if a little waggon running on rails has a spring gun fixed in
it in a perpendicular position, so arranged that when the waggon
comes to a particular point on the rails a catch releases the trigger
and shoots a ball perpendicularly upwards, it will be found that the
ball, instead of going upwards in a vertical line, is carried along over
the waggon, and the ball as it ascends and descends keeps always
above the waggon, just as a hawk might hover over a running
mouse, and finally falls not behind the waggon, but into it.
So, again, if an article is dropped out of the window of a train, it
will not simply be left behind as it falls, but while it falls it will also
partake of the motion of the train, and touch the ground, not behind
the point from which it was dropped, but just underneath it.
The reason is, that when the ball is dropped or thrown it acquires
not only the motion given to it by the throw, or by gravity, but it
takes also the motion of the train from which it is thrown. If a ball is
thrown from the hand, it derives its motion from the motion of the
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.
ebookmasss.com