argh max soft
max statquest
hello i'm josh starmer and welcome to
statquest
today we're going to talk about neural
networks part 5
arg max and softmax
note this stat quest assumes that you
already understand the main ideas behind
neural networks
the main ideas behind back propagation
and how neural networks work with
multiple input and output nodes
if not check out the quests the links
are in the description below
in the statquest on neural networks with
multiple input and output nodes
we had a fancy neural network that used
petal and sepal widths
to predict iris species the neural
network made predictions with crinkled
surfaces for setosa
versicolor and virginica
then we plugged in the petal and sepal
widths from an iris we found in the
woods
and ran the numbers through the neural
network
and predicted that this iris is
versicolor because that
output value 0.86 is closest
to 1. bam now let's see what happens
when petal width equals 0
and sepal width equals 1.
when we run the numbers through the
neural network
the output values are 1.43 for setosa
negative 0.4 for versicolor
and 0.23 for virginica
so one thing we notice is that the raw
output values are not always values
between
0 and 1. sometimes a raw output value
can be greater than 1
like in the case for setosa with 1.43
and sometimes a raw output value can be
less than zero
like the case reversi-color with
negative 0.4
this broad range of values makes the raw
output harder to interpret than it needs
to be
and this is one of the reasons that when
there is more than one output
like we have here the raw output values
are sent to either an
arg max layer or a soft max layer before
a final decision is made
argmax which sounds like something a
pirate might say
arg max simply sets the largest value to
1
and all of the other values to 0.
in this example setosa has the largest
value
1.43 so the arg max function
sets the final output value for setosa
to 1.
and the final output values for
versicolor and virginica
to zero thus when we use arg max
the neural network's prediction is
simply the output with a 1 in it
and that makes the output super easy to
interpret
bam the only problem with argmax
is that we can't use it to optimize the
weights and biases in the neural network
this is because the output values from
arg max are constants
0 and 1. to see why this is a problem
let's plot the second largest output
value 0.23
on a graph since this is the second
largest value
arg max will output 1 for any other
output value that is greater than 0.23
and it will output 0 for any other
output value that is less than 0.23
because the slopes of these two lines
are both zero
their derivatives are also zero
and that means if we wanted to find the
optimal value for any of the weights and
biases in the neural network
then we would end up plugging zero into
the chain rule for the derivative of arg
max and then the whole derivative would
be zero
and if we plug zero into gradient
descent we won't step
towards the optimal parameter values
and that means we can't use arg max for
back propagation
[Music]
so that leads us to the soft max
function
when people want to use arg max for
output they often use softmax for
training
however first let me say that softmax
sounds like a brand of toilet paper but
since we're already using a roll of
toilet paper to represent the soft
plus activation function let's use a
picture of a soft teddy bear to
represent the soft max function
now let's see the soft max function in
action
first let's solve for the soft max
output for setosa
we raise e to the raw output value for
setosa
and divide by the sum of e raised to
each raw
output value so
in this case we plug 1.43
in for setosa negative 0.4
for versicolor and 0.23
for virginica and when we do the math
we get 0.69
as the soft max output value for setosa
so let's put 0.69 here so we don't
forget it
now let's calculate the softmax output
value for versicolor
when we calculate the softmax value for
versicolor
the only thing that changes is the
numerator
now instead of e raised to the raw
output value for setosa
we are using the raw output value for
versicolor
so let's plug in the raw output values
do the math and we get 0.1
as the soft max output value for
versicolor
lastly let's calculate the soft max
output value for virginica
just like before the only thing that
changes is the numerator
now e is raised to the raw output value
for virginica
so let's plug in the raw output values
do the math and we get 0.21
as the softmax output value for
virginica
bam now let's take a moment to look at
the three soft max output values
first notice that the largest raw output
value
1.43 for setosa is paired with the
largest softmax
output value 0.69
likewise the second largest raw output
value
0.23 for virginica
is paired with the second largest
softmax output value
0.21 lastly
the lowest raw output value negative 0.4
for versicolor is paired with the lowest
softmax
output value 0.1
so we see that the softmax function
preserves the original order
or ranking of the raw output values
bam second notice that all three soft
max output values are between zero
and one this is something that the soft
max function ensures
regardless of how many raw output values
there are
the soft max output values will always
be between
zero and one double bam
third notice that if we add up all of
the softmax output values
then the total is one that means that as
long as the output values are mutually
exclusive
then the soft max output values can be
interpreted
as predicted probabilities
note i put the word probabilities in
quotes
because you should not put a lot of
trust in their accuracy
the reason you should not put a lot of
trust in the accuracy of these predicted
probabilities
is that they are in part dependent on
the weights and biases in the neural
network
and the weights and biases in turn
depend on the randomly selected initial
values
and if we change the initial values we
can end up with different weights and
biases
that give us a neural network that is
just as good at classifying the data
but give us different raw output values
and different raw output values give us
different soft max
output values and that means that
different
randomly selected initial values for the
weights and biases
result in different predicted
probabilities
in other words the predicted
probabilities don't just depend on the
input values
but also on the random initial values
for the weights
and biases so don't put a lot of trust
in the accuracy of these predicted
probabilities small bam
now let's go back to the original neural
network with the original predicted
probabilities
and look at the general form of the
softmax equation
this i refers to an individual raw
output value
for example when i equals 1 then we are
talking about the raw output value for
setosa
and that means we put setosa here
and we put the raw output value for
setosa in the numerator
in the denominator we just have the sum
of e
raised to each raw output value
now remember that we started by talking
about arg max
which is easy to interpret but cannot be
used for back propagation because its
derivative
is totally lame in contrast
soft max has a derivative that can be
used for back propagation
for example if we have the soft max
function for setosa
then the derivative of the predicted
probability with respect to the raw
output value for setosa is the predicted
probability for setosa
times 1 minus the predicted probability
for setosa
where in this case the predicted
probability for setosa
equals 0.69 and so when we do the math
we get 0.21 note
some of you may wonder where this
derivative came from
some of you may not for those who want
to know
i've created a video that walks you
through every single step
so check it out bam
now because the raw output values for
versicolor and virginica
play a role in the soft max output value
for setosa
we also need the derivatives of the
predicted probability for setosa
with respect to versicolor and virginica
the derivative of the predicted
probability with respect to the raw
output value for versicolor is the
negative predicted probability for
setosa
times the predicted probability for
versicolor
where the predicted probability for
setosa equals 0.69
and the predicted probability for
versicolor equals 0.10
and when we do the math we get negative
0.07
lastly the derivative with respect to
virginica is
the negative predicted probability for
setosa
times the predicted probability for
virginica
and when we do the math we get negative
0.15
thus unlike the arg max function which
has a derivative equal to 0
or it's undefined the derivative of the
soft max function
is not always zero and we can use it for
gradient ascent
so we see why neural networks with
multiple outputs
often use softmax for training and use
argmax
which has super easy to understand
output to classify new
observations triple bam
now before we go i need to mention that
way back in the stack quest on back
propagation main ideas
we use the sum of the squared residuals
to determine how well the neural network
fit the data
however when we use the soft max
function
because the output values are predicted
probabilities between 0
and one we often use something called
cross entropy to determine how well the
neural network fits the data
and we'll talk about cross entropy in
the next stack quest in this series
bam now it's time for some
shameless self-promotion if you want to
review statistics and machine learning
offline check out the statquest study
guides at statquest.org
there's something for everyone hooray
we've made it to the end of another
exciting stack quest
if you like this stat quest and want to
see more please subscribe
and if you want to support statquest
consider contributing to my patreon
campaign
becoming a channel member buying one or
two of my original songs or a t-shirt or
a hoodie
or just donate the links are in the
description below
alright until next time quest on