Welcome to Exploring Computing.
The topic of this video is how AI is actually done.
The goal here is to demystify AI by giving you
a concrete example of how it works.
I'll do this by first showing you an example of an AI spam
classifier and the data needed to train the classifier.
I'll then go into how it works by breaking down
the theory behind logistic regression,
and generalizing that into an explanation of neural networks.
Finally, I'll talk about the role of AI engineers
and how AI is done in practice.
So let's look at a spam classifier.
I've previously mentioned email spam classifiers.
But the one we're breaking down today
is going to be for text messages.
Recall that the goal of a spam classifier
is to take in a message and determine
whether it is spam or not spam.
What we're looking at here right now
is the data that the AI is going to be trained on.
It consists of many text messages, as well as
whether they are spam or not spam.
Each enumerated item here is a training sample
that consists of the text message,
as well as a classification of whether it's spam or ham.
In this case, ham means it's not spam.
For example, for the 10th item, we
have the message had your mobile 11 months or more, et cetera,
et cetera.
And it's classified as spam because it's not
from your friend.
It's just from someone or some corporation that's
trying to sell you something.
Now, on the other hand, we have an example
here where it says, I've been searching for the right words
to thank you for this breather, there I
promise I won't take your help for granted,
so on and so forth.
And that's classified as ham because that
is a legitimate text message from someone
that you might know.
Now, this is just the training data,
which is why we have the right answers like ham or spam
already in place.
However, the goal is that we will
use this data to train the AI.
And eventually, will be able to just put in the text message,
and the I will tell us whether it thinks it's ham or spam.
And the I should be able to do this, not just for the messages
that it has already seen.
But for any message.
So in order to train the AI successfully,
it needs to see a lot of samples.
So just to give you an idea of the scope of how much data
it needs let's scroll down and just look at all of the data.
All of these have been labeled.
And all of these are messages.
And you'll see that there's about 5,000 of them.
This might seem like a lot of data,
but honestly, it's considered a relatively small data set.
And the reason we can get away with using something that's
relatively small is because this is a really simple problem,
more complicated AI problems, such as face ID, which
takes in a video and determines whether it's
the right person or not required training data on the scale
of hundreds of thousands to even millions of training samples.
Since the message that we have contains both the message as
well as the correct classification,
we can model this as a supervised learning problem.
In this case, the X's would be the messages.
And the Y's would be whether it's spam or ham.
And so, I just built a really simple logistic regression
classifier, which I then trained on the data.
And so, let's see it in action right now.
So first, I need to load the model that I just
trained by doing import spam.
And then, I type in what I want to classify.
So I just use the model that I wrote writing spam,
and I'd write, classify.
And I put in a message that I just sent to my roommate
a few days ago.
So I said, "Also, I made some coconut pudding.
Feel free to take the one in the glass yogurt container."
In the model should tell me whether or not it thinks
it's spam or not spam.
Once I hit Enter.
And as you can see, it thinks it's not spam.
Now I'll do the same with the message
that my roommate replied to me.
And she wrote, "Oh, thank you."
And as you can see the model thinks it's not spam.
Now let's test this model on a text message that is spam.
So I received this message from a while back from a barbershop.
And as you can see, it is a spam message.
It's not from someone that I actually know.
And as you can see, the message classifies it
as spam, which is what we expected.
Of course, this model isn't perfect,
it sometimes makes mistakes too, but it actually
works pretty well.
So let's break down how this thing actually works.
And we'll do that by starting with a very simple example.
And I promise it'll generalize very easily.
Suppose you're working for a car insurance company,
and you want to assign a good driver score bonus to drivers
applying for insurance.
You want to based the score off of the number of years
of driving experience as well as the number of accidents
a person has been in.
So the inputs to this model would
be the number of years of driving experience, which we'll
call E, as well as the number of accidents that a person has
been in, which we'll call A. And we want our model
to predict a good driver score, which we will denote as Y.
And note that this should be positive if the driver was
a good driver and negative if the driver was a bad driver.
And the data that we have, will be a past record
of how scores were previously assigned based on the inputs.
And this might look something like this.
So the horizontal axis corresponds
to E, which is then years of experience a driver has.
And the vertical axis corresponds
to A, which is the number of accidents.
So, for example, this point over here
might correspond to someone who has 10 years of experience
and only one accident.
Whereas this point over here might correspond to someone
with one year of driving experience and having
had 10 accidents.
And for each of these points, we've
color-coded and marked them so that they
correspond to the driver score that they had received.
So, for example, this person who had driven 10 years with only
one accident was a good driver, which
is why they received a positive score, and it's red.
Whereas this person who had only driven one year
but had 10 accidents must have been a terrible driver.
And so he got a negative score as well
and that's why it's marked as blue.
Now, the goal of the learning algorithm
is to figure out, based off of all
of this data and past experiences,
if for example, it received a point here,
which might correspond to someone who had driven for nine
years and got into two accidents,
whether or not it should be a positive score
or a negative score.
And as humans, were able to kind of tell immediately off the bat
that this should be a positive score.
But we want the computer to be able to figure that out, too.
Now, as you can tell from the title of this page,
the technique that we're going to use
is called Linear/Logistic Regression.
The difference between linear and logistic
is not super important.
But just know that the reason why
we call it Linear Regression is because it's linear,
which means line.
We can draw a line between all of the points
that we're trying to separate.
So, in this case, we can see that the line
would look something like that.
And there is that one point that falls
on the wrong side of the line.
But that's OK.
The model that we train is not always perfect.
And perhaps even the person assigning that score
might have messed up.
It doesn't matter since our model would be working
most of the time anyways.
Now let's do a quick refresher on some math
that you might not have thought about in a really long time.
So remember how a long time ago, you
were once taught that the line is represented by y
equals mx plus b.
And we were told that M represents
the slope of the line, and B is equal to the y-intercept
of the line.
And so what you would do with this equation is
you would plug-in an X, and you would get in y.
Well, now we want to find a similar equation, except now,
instead of just plugging in a single X,
we actually have two things to input, an E as well as an A.
And so what we want to do is we want to modify this equation
so that it can take in two different things.
So, in this case, M and B are constants.
And so just because we'll be adding in more constants,
let's rename them into C0, and C1.
So we can rewrite this Y as y is equal to C0 plus C1 times x.
I kind of switched the order between the two terms, right,
this mx, which is now C1x.
I moved to the second whereas this B, which is now C0,
I moved to the first.
But also notice that there is no variable that will
input that's actually called X, right?
We only have E and A.
So let's replace this X as E instead.
In addition to that, our formula also
needs to take into account A. And so we'll
just add that into the equation by giving it
an additional constant, which we will call C2, which
is multiplied by A. And so the goal of linear regression
is to figure out what these constants C0, C1, and C2 should
be so that we obtain this nice line over here.
And the way that this is done is first by randomly picking
values of CO, C1, C2, and then changing them a little bit.
And checking to see if it does better.
And if it does better, then you keep on changing it
in that direction.
And if it does worse, then you change it
in the other direction.
So, for example, if we first started with a guess
that C0 was supposed to be 200, and C1 was 100, and C2 was 0.
And then, you would plug-in all of the data points
that you already have to test if it works well.
And if it doesn't, then you might tweak it.
So that C0 is now 201.
And C1 is maybe 101.
And C2 might be 1 instead of 0.
And you would just repeatedly try different combinations
of C0, C1, and C2 by changing them a little bit at a time
until you get a nice line that looks something like that, that
splits up all of the points that you already have from your data
nicely.
And because computers are so fast
you're able to try out a bunch of different combinations
of C0, C1, and C2.
And so eventually you'll be able to find something really nice.
So in this case, the computer might have picked 0 for C0,
50 for C1, and negative 100 for C2
as what would have best fit the data that we had.
And so the resulting equation would be y
is equal to 0 plus 50E minus 100A.
So let's check how well this model fits
the data that we trained it on.
So let's look at this point over here, which
corresponded to a driver who had 10 years of experience and one
accident.
So E would be 10.
And A would be 1.
And so plugging that into the formula that we have,
we would get 0 plus 50 times 10 minus 100,
times 1, which is equal to 0 plus 500,
minus 100, which is equal to 400.
And that fits our model because 400 is positive,
and this point was positive as well.
So let's try it with a not-so-good driver.
So remember this point over here, where E was 1,
and A was 100.
Let's see how that fits into the model.
So plugging those numbers into the equation,
we have 0 plus 50 times 1 minus 100
times 10, which is equal to 0 plus 50 minus 1,000,
which is equal to negative 950.
And that matches our model as well
because negative 950 is negative,
which is exactly what this point was supposed to be.
And now we can apply this model to points
that wasn't even in our data, to begin with.
So do you remember this point over here,
which wasn't in our data, and we were just
wondering what it should be?
Well, now we can plug it into our model.
So this corresponds to E being 9 and A being 2.
So let's plug it into our model and see what happens.
So the equation would be 0, plus 50 times 9 minus 100 times
2, which is equal to 0 plus 450 minus 200,
which is equal to 250.
And this number is positive, which
means that this should be positive as well.
As a disclaimer, these numbers, 0, 50 and negative 100,
might not actually be the most optimal numbers.
But the goal of Logistic/Linear Regression
would be to find what these actually should be.
And just for the sake of this example,
these worked pretty good.
And so that's why I just assume that this
is what linear or logistic regression would find.
But they can be really fine-tuned
and even find numbers like 0.002 and 52.1.
And that would probably give a better fit.
And so I actually highly recommend
playing around with changing around these numbers
over here, just to see how it might actually
affect this line over here.
But as a summary, basically what linear regression does
is it tweaks these constants over here
until you get a nice line that fits your data.
And because computers are so fast and powerful,
it's able to try out a whole bunch of values.
So that the values that you do get are very optimal.
Now, in the previous example, we only had two variables,
A and E. And so those would have been the X's.
But the model that you just learned
can actually be generalized to any number of variables.
So let's say we had the variables
X1, X2, X3, so on and so forth, Xn.
Then the linear regression model would basically
find the constants for all of these variables.
And so the resulting model would look something
like Y is equal to C0 plus C1 times X1, plus C2
times X2, plus C3 times X3, so on and so forth,
plus Cn times Xn.
And the goal of the model would be
to find the C0, C1, C2, C3, C4, so on and so forth,
up until Cn.
That is the most optimal.
And so, as you can tell, the more complicated
a model is, the more Cs or constants
you would have to find.
And typically, a more complicated model
would require more data just because there's more constants
that need to be found.
And finally, the last thing to note
is that logistic regression actually
adds an additional component called the sigmoid function.
And so, instead of directly using
this value that we have obtained,
we actually pass this we through what
we call a sigmoid function.
The sigmoid function, which we'll call G over here,
is given by the formula, 1 over 1 plus e
to the negative x power, where e is Euler's number
which is approximately 2.718.
And so for logistic regression, the prediction
that you make is actually given by the formula y
is equal to g with the value that we have up here passed in.
So it would be g with c0 plus c1 x1, plus c2 x2,
plus so on and so forth, passed in.
And that's equal to 1 over 1 plus e
to the negative c0 plus c1x1, plus so on and so forth.
And the important thing to know about the sigmoid function
is that it looks something like that.
If we were to graph it out, and this was x,
and this was the sigmoid function,
then it would actually decrease to 0
for values that are very small, so very negative numbers,
and it would asymptotically increase to 1,
for values that are very large.
So let's just draw out that asymptotic line.
This would be 1.
So for, example, if this sum over here
was a very large number like 5,000,
then it would probably be something like 0.99,
where it's very close to one.
Whereas if this sum over here became
was a very high magnitude negative number,
like negative 200,000, then it would be very, very close
to zero.
And the reason why we would want to use the sigmoid function
is because it limits everything to between 0 and 1.
And that's very helpful when we're
talking about probabilities.
And so if the problem was something
like, how likely is this message a spam message?
Then, we would be able to use the sigmoid function
to get a number between 0 and 1 that represents the likelihood
that the message is spam.
So that's cool.
Linear regression or logistic regression
seems to allow us to take in any number of inputs
and give an output that seems to fit well.
So why would we ever need neural networks?
I encourage you to pause this video for a few seconds
and take a guess.
I hope you had a chance to do that.
Well, the answer is sometimes linear regression doesn't work.
For example, what if we can't draw
a straight line or a linear line between the
positive and negative scores?
So let's consider this following problem.
It's a similar problem to earlier.
We're still trying to figure out the good driver score.
And we also want to consider the years of experience
of the driver, which we call E. But now, instead
of the number of accidents that the driver has been in we
are considering the age of the vehicle which
we're calling V. Is the data that we're working with.
And it looks a little bit different from earlier
because we want a negative score for experienced drivers
with a newer car, because newer cars are expensive,
and wrecks are more expensive.
But we also want a negative score
for newer drivers with older cars
since older cars have less safety features.
And so you'll see that we have a bunch of positive scores here.
But we have these two groups of negative scores.
And so you'll notice that no matter how you draw the line,
whether it's like this, or like this, or like this,
you can't cleanly split these two groups of points apart.
So let's backtrack for a second.
Remember how we're able to express the logistic regression
model as the formula y is equal the sigmoid function with C0
plus C1 e plus C2 v passed in, where we have e and v as inputs
and y as an output?
Well, if you remember from the previous video,
we can actually represent this model as a picture as well.
So we would have the inputs, E and v,
and it would get passed into the function, which
we would express as a square, and it would output y.
And this function over here is exactly g, C0 plus C1, e,
plus C2 v.
And we also talked about how this unit over here
looks like a neuron, where this part is the dendrites, which
takes in the outputs of other neurons.
And this part would correspond to the axon,
which passes on the output of this entire neuron
to other neurons.
So we can use that idea to change a bunch of these units
together.
And so one way that we can chain this together
would look like this.
We have three similar units that are chained together.
And the units in the first layer all look very similar to this
because they both take in e and v. And this one also takes an e
and v. But this one's a little bit different
because it's in the second layer.
So instead of taking in either e or v,
it takes in the outputs of this one and this one.
And this gets passed in through a similar logistic function
that looks like that and outputs our final answer.
So zooming in to the picture a little bit.
We have three neurons, one, two, and three.
And they're laid out into two layers.
In the first layer each of these neurons have the input e and v.
And you can see these over here.
And they each have their own logistic function.
So here we have the sigmoid function
that has C0 per c1e per c2v passed into it.
And here we have a different logistic function
where the sigmoid function has C3 plus C4e plus C5e passed in.
Note that these constants are different.
Right.
Here it's C0, C1, C2.
Here at C3, C4, C5.
And then, in the second layer, we
have its own logistic function.
But now, instead of having e's and v's, we
have Y1 and Y2, which is what was
passed into this second layer over here.
And in this logistic function, we
have the constant C6, C7, and C8.
And so what neural networks do is
they do the same tweaking that we've mentioned earlier,
where it kind of changes the values of all the constants
until it finds something that is optimal.
But it has way more than what it needs to do before.
Right?
It needs to figure out C0, C1, C2, C3, C4, C5, C6, C7, and C8.
And as a consequence of that, we need a lot more training data
to figure out all of these constants.
So the pros of using a neural network
is it allows you to chain together
multiple logistical functions.
And so, by doing so, you actually
obtain enough flexibility that your model no longer
has to just be a straight line across all of the data.
And that's extremely powerful.
However, the to that is you actually
need a lot more data to train something that's
a little bit more complex.
And finally, we mentioned before that each logistical unit
can actually take in any number of inputs, right?
We were able to take in X1, X2, all the way to Xn,
where n is any number.
And so what that means is we can actually
chain together all of these things in any way we want.
And so let's say we had two inputs, E and V, which
are the same as before.
But let's say we wanted to have five different neurons
in the first layer.
And so we could easily do that where each one of those circles
represents a logistical function.
And we would just pass it in to every single one.
And let's say, if in the second layer we wanted three,
then we would pass in the five outputs from the previous layer
into each one of the three neurons of the next layer.
And I'll just do dot dot dot because that's a lot of lines.
And we could go back to four, and so on and so forth.
And so, this is actually an extremely flexible structure,
which is why this has been such a big deal.
So to see a neural network in action,
I highly recommend you checking out the website
playground.tensorflow.org.
TensorFlow is a library that allows you
to implement neural networks.
And this page allows you to visualize
how a neural network works.
And so you'll see over here, we have a neural network
that has two layers.
So, in this case, the first layer has four neurons
and the second layer has two neurons.
And this is the data that is being trained on.
And since it hasn't been trained yet,
the background here is all hazy because it doesn't know what
the right answer should be.
And over here, we have the different data
sets that you can train your neural network on.
Let's start with this one because it's
the one that's the most similar to what we had earlier.
And let's begin by visualizing logistic regression.
So remember how logistic regression
can be represented as a neural network with just one
hidden layer and only one neuron in that layer?
So once we hit that play button, it starts training it.
And as you can see, it was trained really quickly,
because this graph on the right stopped changing as much.
And as well as this training graph over here got really low,
and it stopped changing.
And so you'll see over here that these clusters of points
over here were our original data set
that we trained the model on.
And this shaded background is what the model is now learned.
So, in this case, all of this area over here
that's shaded orange will be predicted
to be orange by the model, whereas everything over here
would be predicted to be blue.
And so you can see over here that it actually
perfectly predicts all of these orange points should be orange.
And it perfectly predicts that all of these blue points
should be blue.
And it works for points that you haven't seen, too, right.
So, for example, if you saw a point that
was like somewhere over there, it
would also be considered orange.
But now, let's see what happens if he tried this
on a more complicated data set.
So let's reset the model.
And you'll see that on the right over here
it basically stopped improving.
And so that means that it's done being trained.
But you'll see that this model that's being shown
does not accurately predict this data over here.
And notice that this data is very
similar to that second example that I showed you.
And so now, the model for all of this area
will predict orange with somewhat high confidence
because it's a brighter orange.
Whereas over here it would predict blue
with somewhat lower confidence.
And so this model doesn't do very well
on any of these points because it
would have predicted blue when it
should have predicted orange.
And so, as we've mentioned in the lecture earlier,
in order to remedy this, we can use a neural network
by making this more complicated.
So will increase the number of neurons
in the first hidden layer.
And this isn't quite the same model
that I explained earlier since that model actually had
an additional layer to this.
But we can see what happens when we just have one hidden
layer with two neurons.
And you'll see that it has converged.
And in this model, it would predict
all of these points to be orange and all of these points
to be orange, and all of these points in the middle
to be blue.
And it does pretty well, except for these little small areas
over here it actually does really well.
However, the cool thing is you can actually
mouse-over these points.
And you can see what each of these neurons are doing
and how they're contributing to the understanding
of this entire model.
And so you see that this first one
helps figure out whether or not the points are over here.
And the second one helps figure out
whether the points are over here.
And so, with the two combined, we get the model that we want.
Now, once we see what these two neurons are actually
doing, what AI engineer might think is OK--
so they're trying to figure out which corner
is each set of points on.
And so, what might make sense is to actually use
four neurons so that each corner can be covered.
And so that's what I will do right now,
increase the number of neurons, reset the model.
And once I start training it, you'll
see that it does exactly what we want it to do.
Each neuron tries to determine which corner,
then data point is on.
And this is great.
But we'll see that on some data sets that
are even more complicated, this just simply isn't enough.
So now, let's play with a more complicated data
set that looks all spirally.
And once we hit the train, you'll
see that it just doesn't work.
And we can even increase the number of neurons and layers.
And it still doesn't work very well.
And the reason why this is happening
is because there's just simply not enough information
for the model to figure out what to do.
So it's like, no matter how smart
you are, with just these two inputs
of X1 and X2, that's just not enough.
And so one thing that we can do is actually
increase the number of inputs that we have in general.
So they call it features here.
But basically, what they are different inputs.
If you give it a little bit more information, for example,
x1 squared x2 squared, as well as the sine transformations
of the data, you'll be able to do a lot better.
So since this is much more complicated.
It takes a much longer time to train.
As you can see over here.
It's still doing a lot of changes.
And it's changing somewhat slowly.
But you kind of see that it's forming
that little spiral over there.
And now it's more prominent.
And eventually.
It does a somewhat decent job of figuring out
what the points should be.
Anyways, I highly recommend you checking out this website.
Literally at the top of the page,
it says tinker with the neural network
right here in your browser.
Don't worry.
You can't break it, we promise.
And so, don't be intimidated.
Just mess around with it.
It's really cool.
So going back to the spam classifier that we
were talking about earlier.
What it does is it uses logistic regression.
And the inputs are basically the words
that appear in the message, as well as their frequency.
So for every single word that appears in the training data,
it will correspond to a specific X.
So, for example, if we had the sentence, "goodnight
room, goodnight moon."
And the word "goodnight" corresponded to x2,
and the word "room" corresponded to x4,
and the word "moon" corresponded to x1,
then the input would look something
like this, where everything else would be zero.
The model doesn't consider the meaning
of the words or the order they appear et cetera, et cetera.
And the output would be the probability
that something is spam.
Now, as I mentioned earlier, the model isn't perfect.
And so, the role of an AI engineer
would be to perfect this model.
So some things that they could potentially
do to improve the model would be potentially pre-process
the inputs so that synonyms will map to the same input value.
So we could achieve that by incorporating a thesaurus
into the model that we have.
Or we could use sentiment classification to first map
words to their meanings.
We could also change the input so
that it considers groups of words instead
of just words on their own.
So the word not bad would then be
considered a single positive phrase
instead of two negative words.
Alternatively, we could also use a more complex model.
And so, these are the kinds of things
that AI engineers think about.
They think about the features.
So the inputs.
What features do we have.
How should we modify them?
What features should we add?
Which ones are useful, which ones not?
How to scale the features so that they're equally or not
equally considered?
And what to do if the features aren't
giving the right results.
And we see a lot of that earlier in the example
where we had to transform the Xs so that we
had x1 squared x2 squared as well as their sign
transformations.
And they also look a lot at data.
So how do they collect it?
What do they collect?
How much data do they need?
Does it match their actual use case?
So, for example, the spam classifier
didn't work perfectly because the spam messages
it was trained on usually involved dating sites,
whereas the spam messages that I receive
are very different since I'm not the target
audience of "date single local woman you" type of messages.
And finally, there's also questions
like, what model do we use, and many more.
So, in conclusion, I really hope that the last few lectures were
interesting and that they really helped demystify
the feel of AI.
I really don't like how the term AI has now become a buzzword,
and everyone thinks that it's something super complicated,
but in just the last 40 minutes, I
was able to explain to you exactly how it works.
And now you understand the theory behind it too.
Thanks again for watching.
And I hope you enjoy the rest of the quarter.