Perceptrons and Logistic Regression
Transcript
[INTERPOSING VOICES] PROFESSOR: Hi, everyone. Welcome to lecture 21 of CS 188. Today we'll cover perceptrons and logistic regression. Couple of announcements. Your project four is due on Friday at 4:00 PM. Your homework 10 will be released soon. Next Monday is a holiday so your homework is going to be due on Tuesday exceptionally rather than Monday. And then your midterm two is coming up. That's on Thursday. So please prepare for that. Any questions about logistics? Yes. STUDENT: What materials are covered for--
PROFESSOR: What materials will be covered for midterm two? It will be everything from the very beginning of the semester all the way through naive Bayes, which was last lecture and the current homework. What we cover today and today onwards will be part of the final but not part of midterm two. And midterm two will have more emphasis on materials covered after midterm one than before but it'll be a mix of both materials. STUDENT: How much is it? PROFESSOR: It's hard to know ahead of time.
We just tried to put more emphasis on it. Then some questions come out better than others, and that affects which questions end up on the exam. But the kind of mindset ahead of time is more emphasis on materials that came later. But sometimes it'll be a mix. So it'll be about materials that came later and earlier all in one question. And you need to understand both to be able to solve those kind of questions. Yes. STUDENT: Will there be a review session? PROFESSOR: Yes. So we'll release a midterm two prep page probably on Thursday evening. There will be review sessions in the first half of next week, and there will be a practice midterm two that we'll probably do on Monday or Tuesday, a couple of days before the midterm. Similar to how midterm one was.
So today we'll look at perceptrons and logistic regression. And before we do that, let's take a quick step back to what you covered last lecture, as well as the lectures before that. So the recent five, six, seven, eight lectures was about probabilistic models. And if we train a probabilistic model over all variables that we might care about, we can then reuse that probabilistic model to make decisions for specific queries. For example, what's the probability of a class label y given some feature values f1 through fn? And that was one way you could reuse those models. And that's what we saw with naive Bayes. For a specific type of Bayes net, you can use it to answer conditional queries about a class label. Today we're going to look at, again, solving that kind of problem, where we have a set of features and we want to output the decision on what class might be represented by those features. But we're not going to learn a full probabilistic model.
We're going to focus on just the decision of going from features to what the class label might be and ignore learning a full probabilistic model, which has some pros and cons. If you want a full probabilistic model you're not going to get it with the methods from today. But if all you care about is that decision, today's method will zone in more on the particulars of that decision. So what we'll look at is something called linear classifiers. We'll have input-- for example, email, like you've seen as a running example in previous lecture. Then the email gets turned into a feature vector. They'll still do the same thing. In this case, the feature vector has a account of how often the word "free" occurs, how often your name occurs, how often a misspelling occurs, whether it's from a friend or not, and so forth. The result of that is a real vector that is what we use to then make a decision from.
And then it might be spam or ham depending on whether we like this email or not. What we'll focus on today is this part over here-- how to go from the feature vectors to a decision of what the class label might be. This part is very important too, going from x to f of x. Traditionally, it requires a lot of art in that you think carefully about what might be a good way to characterize text. Maybe occurrence of words. Maybe some words should be considered synonyms and then you count them together.
Maybe you should have some special things, like does it come from a friend as a feature and so forth. Those are an art to figure out. That's still sometimes how it's done. What we'll also see in the next next lecture is how to do this transition from x to f of x in a way that it's also learned. But for now we'll assume f of x is available and go from f of x to y. Or it could be images. Input is an image. That image gets turned into a feature vector that represents the image. In this case, the feature vector is a bunch of digits here. Is there a pixel value that's on or off at each location? So 1 0 values. You could also use grayscale. If it was a grayscale image, it would be a number between 0 and 1 maybe, or maybe sometimes from 0 to 255 if you have byte representation of images. And then you might have more advanced features, like counting the number of loops in an image, and make that one of your features. And again, today is all about going
from once you have those feature vectors to making a decision of what's in the image. And in the future, we'll look at this part. Today's approach will be somewhat inspired by biology and how the brain works. Now, nobody really knows how the brain works so don't take this too seriously, but it's a loose inspiration of roughly some ideas of how the brain might work that we're going to use here. So your brain consists of a bunch of neurons. Maybe, if you're pretty lucky, you
have 100 billion neurons or something. And we're going to zone in on one of those neurons. So this is just one of them out of, if it was a human, about 100 billion. So you have a neuron here. What does a neuron do? Well, it has a bunch of inputs. Stuff comes in here, here, here. They're called dendrites. That's where the inputs come in. This could be coming maybe from your retina, where photons hit your eye, or it could be coming from other neurons sitting
in another part of your brain that send signals over. These signals come in. Then the nucleus here, the core of the neuron, based on what comes in, in some sense makes a decision. It decides whether or not it's going to send out a signal over the output channel, which is called the axon. And the axon will then branch out and go reach many other neurons where this will become essentially the input to other neurons that come later. We're not going to worry about the network of neurons for now.
We're just going to worry about one of these. And so we're going to have maybe you can think of it as f1 coming in here, f2 coming in here, feature 3 coming in here, and feature n coming in here. And then here we have a decision y that's coming out. Representing as a circuit diagram you have, essentially, a bunch of inputs that are wires. Those wires could have a high signal or low signal. If they're very active they might influence more what comes out on the other side.
And then there will be weightings here-- see the little weights here-- that corresponds to the strength of the connections. With some neurons you'll be more strongly connected than with other ones. Stronger connections will mean that you pay more attention to what's coming from that input. And then somehow the combination of inputs is resulting in a output signal, which would, in our case, be a class label. So the inputs are the feature values. Each feature has a weight, and the sum is the activation. So take a weighted sum of feature values. That's the activation, and we feed that out. Let's put that in mathematics. So the activation coming out is a weighted sum of features. So this might be f1 of x. This here, small font-- I can't even see what the number is. Actually, looks like the 1 is on top. f1 of x comes in here. This one here is w1. And we compute w1 times f1 of x. That together gets fed in here, summed together
with all the other weighted feature values, or in a dot product notation, which we'll use a lot going forward, w inner product with feature vector f of x. If the activation is positive the output will call a positive 1, and we'll have the positive class. If the output is negative, we'll say it's a negative class. You might wonder, how can it be negative? Well, these weights could be negative. And then if a positive feature value comes in and the weight is negative, you get a negative output if that dominates, or you could have a feature value that's negative and a weight vector that has a positive entry and together, it becomes a negative output. Any questions about this? Because this the basic calculation we're going to be doing for the first half of lecture here.
Represented as a network-- and we'll see more of these drawings in the future-- we have features coming in. In this case, three feature values. Imagine an image with just three pixels that could be on or off. Three feature values coming in. They're being weighted, summed together, and then a decision is made whether the resulting activation is bigger or smaller than 0. So let's look at an example. We have a weight vector, w, and this weight vector w has entries, just like feature vectors of entries. Just as many entries. And it corresponds. So maybe the weight for "free" is 4, meaning you pay a lot of attention to how often the word "free" occurs. The weight for your name is negative 1, meaning if your name occurs, the activation goes down. In this case, it's supposed be positive when it's spam, negative when it's not spam. So when your name occurs, you want the probability to go down.
Then misspelled is 1, because maybe spam emails more often have a misspelling. From a friend is negative 3, meaning that you want it to be a negative output if it's from a friend and you have no other signal. And this is our weight vector drawn here in 2D space. But if there's four features, really they would be living in a four-dimensional space. And the dots here mean that, in principle, there's even more features. So it'd be living in a high dimensional space that we can't draw on a slide. Yes. STUDENT: Is there a bias term? PROFESSOR: Is there a bias term? You can. So that's a good question. You could start off with a bias here, which is an entry that would be always 1. And what that would do is, let's say you have no information. If you think most often it's one or the other class, the bias term could push you in that direction. And it would essentially set a higher bar-- if the bias term is positive, it would set a higher bar to be able to conclude that it is not spam.
Or if the bias term is negative, that would mean that you think, by default, it's not going to be spam. But if there is enough indication for spam, you can get it over zero and classify as spam. And so let's say we assume-- maybe we assume almost everything is spam, which, behind the scenes probably is true, and a lot of it we don't see. Then maybe the bias term would be something like 100 in that by default you think things are going to be spam. But if there's enough evidence otherwise, then you might be able to get this below zero. I haven't told you how we find these weights. For now, just assume that they're there. It's like your brain's already been trained. It's fixed. It's going to make decisions. But we haven't talked about how we actually train it. So this is the weight vector. And the dot product determines whether you're positive or negative. So it's very easy to see, if I draw a vector corresponding to some incoming email, I compute the feature vector.
If I draw it here, well, it's in the same direction. That's a positive, so that's spam. If I draw it in this direction that would be misaligned. That's a negative. That's not spam. And really what you want to look at is, in some sense, the orthogonal plane to that vector. And if you lie on one side of the plane you have a positive inner product, on the other side, a negative inner product. And that's how decisions get made. So here's one vector which is pretty spammy. A lot of free.
Not your name anywhere. Things are misspelled frequently. It's not from a friend. And so it's pretty well aligned with this w. But then here's another one that looks like maybe a more benign email, and the feature vector points in the other direction. Negative inner product. And this one will be the negative class, and this one will end up being in the positive class according to this vector w. If you change w, your decision process will change.
So how do we get these decision rules? We have a binary decision rule in the space of feature vectors. The examples are all points. Any weight vector corresponds to a hyperplane. What does that mean? A weight vector is a hyperplane. It's the hyperplane orthogonal to that weight vector. And so let's draw this out. Here is a weight vector. It mostly points towards free and a little bit towards money. So it would maybe roughly point in this direction. Then we have a bias term. What does that mean?
It means that if we did not have a bias term-- let's say for now that this was 0-- then our hyperplane would be right here going through zero. Well, it's not a perfect drawing. It should be orthogonal. So maybe this way, going through 0. And this would be the positive class. This would be the negative class. But we actually do have a bias term, and the bias term seems to favor the negative class. So that means this hyperplane is actually shifted over and will be-- it's not drawn to scale but will be, let's say, qualitatively somewhere here. And then everything on this side is negative and on this side is positive. So you could have a point that lies out here. And you would look at the inner product with the weight vector. But then there's also the bias. If this one's far enough out it would be positive. But then, if it's only out here, well, it's not far enough out with the weight vector. It would be negative class.
Let's do this in a more typeset drawing. So we have a decision boundary due to this weight vector. Corresponds to where this equation holds true. One side is negative. Other side is positive. That's how we make decisions. How do we find w? It's going to be a little different from most things we've seen so far. We're going to have an iterative algorithm that doesn't just look at the data and says, here is the result, but will loop over the data, look at all the data multiple times until finally it's found something it's happy with. So it'll do an update, update, update, till finally it converges. A bit like the local search we did in [INAUDIBLE],, where you wouldn't right away find
a solution but try to improve it over time till finally you find something good. So we start with all the weights it's equal to 0, let's say. That's a reasonable thing to do. Then, for each training instance, we classify with the current weights. So you run the classifier with the current weights. If this is your current classifier and these are data points, then that should be the case that it will be correct. You feed in a data point or made the correct decision. y star means the correct decision. y is the decision the perceptron is making. They're equal, will be no change needed, and you go on to the next data point. On the other hand, if it's wrong, like you might have in this drawing over here, and if you're looking at this data point, then an adjustment will be made to the weight vector to try to make it more compatible with the data point that we just saw. And then this process repeats until ultimately, hopefully,
it's compatible with all data points that are in our data set. Let's look at this a little bit more concretely. We classify with the current weights. So we have a weight vector. We have a feature vector. We compute the dot product, w times f of x. And we check if it's positive or negative. That'll tell us whether it's correct or incorrect. Now if it's correct, if the true label was positive in our algorithm, we have to do nothing because it was predicted as positive, the true label is positive, no update. But what if the true label is negative? So what if y star is negative 1? What will happen? Well, we'll make the wrong decision here because the inner product is positive and we should be negative. So you want to adjust the weight vector. f we cannot adjust here. f is just a description of the input, and that's how we capture what we're getting. w is our decision making process, and we can change that and try to improve it. So how can we change it?
Well, if y star is negative 1, so it's wrong, what we can do is we can say, well, we'd love for w to be not as aligned with f. So what does that mean, not as aligned with f? Well, in some sense that means more aligned with the negative of f. So we can make w more aligned with the negative of f. That will make it less aligned with f. So how do we do that? We can just essentially add y star times f. In this picture what it would look like is we have y star times f or y star negative,
pointing the opposite way. We can add it to w, and we end up over here with our new w. Let's say w prime, which is the one we get after the update. This new w, w prime, will be less aligned with f, which is what we want because you want it to be a negative inner product. How do we know this for sure? We can do some math. So let's call this w prime. After the update we have w prime inner product with f is equal to, well, the original w plus-- in this case, y star was negative 1
but let's just say y star for now, times f inner product with f, which is w inner product with f plus y star times f inner product with itself. This is the original, and this is the correction. So after we did the correction, the inner product has changed. How has it changed? f times f is always bigger than 0. So when y star is positive, the inner product will have gone up. When y star is negative, the inner product will have gone down. That's exactly what we want.
So it's a very, very simple update rule that ensures that the inner product moves in the direction we want it to move. Pictorially what it means, if y star is negative, we see the weight vector rotate away from f. And if y star is positive, we'll see the weight vector rotate closer to f. Any questions about this? This is our first algorithm. This is the perceptron.
Yes? STUDENT: Is there a way to control the rate of convergence? PROFESSOR: So the question is, look at this-- I mean, it gets one example, and it seems to be swept all the way over. And maybe that's overfitting. It's paying too much attention to this one example. And indeed, learning rates is what you would use to make the steps of the size that is more appropriate. We're going to look at that in next lecture. Not today. It does turn out that, under certain assumptions, this works just fine, even without having a learning rate. So it's not necessarily wrong to do it without a learning rate but definitely some more sophisticated approaches. We'll do an update where they will multiply this with some alpha, just like we did in Q-learning, to make the adjustment more gradual. There was another question. Yes. STUDENT: Could you relate it to the feature-based? PROFESSOR: Is it related to-- STUDENT: The feature-based Q-learning?
PROFESSOR: Is this related to feature-based Q-learning? In many ways it is. In feature-based Q learning we had notion of the Q value is weight vector times feature vector. In Q-learning, we wanted the result of that inner product to be a Q value. Here, the way we use the resulting inner product is to make a decision as to whether it's positive or negative, which indicates which class we are in. But there's definitely a lot of similarity. But the learning is very different because in Q-learning, we learn with an approximation to the Baumann equation based on samples, and here we learn essentially through supervision of whether something should be positive or negative. So we get a lot more signal here. In Q-learning you have to bootstrap. And you were doing it based on some estimate of what the Q value would be at the next state. That was kind of approximate. Whereas here somebody tells you, this data point, this f of x, should have a positive label, or they tell you, this f of x should have a negative label. So a lot more signal here to learn from.
That was the binary perceptron. In a separable case-- meaning the data points you have are laid out in a way that there exists a hyperplane, or in two dimensions a line that separates them apart in the class categories-- this is how it could play out. Here's your separable data set. You look at one example. It changes your hyperplane. You look at another example. Shifts around again. As we said, it might jump a little much. But then over time, it'll stop jumping around.
It will be right in the middle here-- not exactly in the middle but separating the two classes. When we now cycle to the next data point it will be correct, which will mean no update. We go to the next data point, it'll be already correct, which means no update. And the algorithm has converged. How about multiple classes? It's pretty exceptional you would only have two classes to decide between. Let's say we have three or more classes, and we'll use three as a running example. How to go about that. One way to do it is to have a weight vector per class. So instead of having w times f of x positive versus negative determines the class, for each class, we have a wy. For example, three class classification problem, we have a w1, a w2, w3 corresponding to each of the three classes. And each will point in the direction that that class's data points live. You compute the activation for a class y, wy times f of x. And then you can see which one has the highest activation.
And that's your decision. It's not identically matched with a two class. Like if you said, oh, what if instead of three we had two classes? If you use this approach, you'd have a weight vector for the positive class and another weight vector for the negative class. In the two class case you can also simplify it to only have one weight vector. That's what we did. In the multi-class case, you can't really do it that way, and we'll have one weight vector per class.
And so here's what it pictorially could look like. You have three weight vectors pointing in their own directions, and then the regions in that direction would correspond to those classes. It's a little more subtle than that because we're looking at the inner product. So if the vector is really, really long-- let's say this w3 was super, super long, out of the slide, way out there-- then this decision boundary might look more like this. And it might take up more. Because it's so long, its inner product will be larger. Similarly if this w3 was really short and only came up to here, then this would shrink a little bit and maybe only be this big. If they're all the same size, then it's just dividing up the space based on the angles.
So that's how we can make decisions into multi-class. How about training this thing? We can use the same procedure, actually, with a very, very minor modification. We're going to start with all weights equal to zero which, now, if we had a three class problem, means three weight vectors, each of them having all equal to 0 with some tie-breaking happening in the beginning. We pick up training examples one by one. And we predict with the current weights. We see what the prediction is. If the prediction is correct, then our weights are good as far as this training example is concerned and we don't have to do any update. However, if the prediction is incorrect-- let's say, for this feature vector we would predict class y but the class label is actually y star, then we need to somehow rotate wy and wy star, such than wy star has a higher inner product with f than wy. How are we going to do this? Well, we can use essentially the same trick
as we did before, by adding or subtracting the feature vector to the weight vector. So wy we want lower inner product because we didn't want the outcome to be y. So we're going to subtract out f. This here is negative f. It's being added on to wy. We have a new wy prime. And for wy star we want a higher inner product, so we add it on. And we end up with a new wy star prime. And same thing. If we look at the inner products, we look at wy prime with f of x, which
is wy minus f of x times f of x, which is wy times f of x minus f of x inner product with f of x. This is the original. This part, inner product of vector with itself, is always bigger than 0. We subtract it out. So the resulting weight vector wy prime has a lower inner product than the original one, which is what we want. And for wy star we can do the same calculation. There's a positive here, so this negative would be a positive. And we would find that the inner product has increased.
Does it mean that after this update wy star will win? We don't know that. That's not guaranteed. But we know that at least it'll have a higher inner product than before and wy will have a lower inner product than before. So we nudge things in the right direction. Do we nudge them far enough? Too far? We don't know. But we at least nudged in the right direction.
OK. Let's take a look at this in action in a demo. So let's reset this. We have a bunch of data points. There are three classes-- there's a blue class, a red class, and a green class. We have random initial weight vectors-- a red one pointing this way, a blue one pointing this way, and a green one pointing this way. It's kind of interesting here. The red one actually points in the blue region. How is that even possible that the red is not surrounded by red? That's because the blue one is longer.
And because it's longer, the inner product is higher and it can take over, in some sense, some of that angular space of the red one, which only has this region over here. So first data point. So we cycle through the algorithm. We're training. We get a data point. It's a blue one. It should be classified as red. So first step is compute the label. It'll be red. Then-- oops, this is going very fast. Let me reset this.
This is going too fast. OK. Let's finish training. Reset. It's on autopilot for some reason. OK. Stopped. So we have a current data point here, which is green. It computed the label. It's going to check, is the computer label equal to the actual label? In this case it is equal because it will compute green and it is green. So when we step, it'll skip over this, look at the next training example. It'll compute the label for the next training sample, which is blue, which is correct. So again, it will skip over the--
oh, that's green. Didn't see that right. So this is green. Apologies. Green dot under the yellow arrow. So that's wrong in terms of it predicting blue. Then what it's going to do is it's going to change the way it vectors. It's going to change the weight vector for green and for blue. The green one should become more aligned with this data point here. So it's the first thing we do for the-- let's see. Computed label. Hold on. Let me reset this. We have a weight vector for blue,
which is the one over here, green over here. It computed blue because it's in a blue region. It's checking for the computed label, which is blue. It shouldn't be blue so it should subtract out this vector. So we're going to see this blue one, get this thing subtracted out in the next step here. Then it's looking at the green one. The green one should be more aligned with the yellow one, the data point. So we'll see the green one rotate towards the data point. And then it goes to the next one.
Next data point up here is blue. The computed label is red, so that's not good. It'll say, OK, I need to first make sure that I don't predict or predict less likely red. So red should point less in this direction. And we'll see red rotate away in this first update step here. And blue should point more in that direction, and we'll see it rotate towards that data point. And this process repeats. Right now we're here. It predicted green and it is green, so it'll skip over any updates.
Am I seeing the colors wrong again? It's rotating red away and rotating green towards. And this process just repeats. It has the next data point up here. It's going to compute it as blue as its decision. It actually is blue so it should skip over this. It skipped over it. Great. It goes to the next one. That one kind of falls out of the screen. Not sure which color it has. It's going to check, is the color predicted? It'll predict green. Is that equal to the actual label? The label looks like it's blue so it's not the same.
So it's going to rotate the blue one away from this and the green one towards it. It seems to have some bugs or my color ratings are off. Let's finish training. And after it's gone through all the examples till it doesn't change anymore we have all the green in the green region over here, all the blue in the blue region over here, and all the red in the red region over here. At this point, whenever it grabs a new data point, it will see that it already agrees with it and it will do no updates. And it's fully converged.
You can reset this and play with the app, and you'll see it cycle through a different versions of the data set and iteratively improved the weight vectors. So it's very different from most algorithms we've seen till today in that here there is not a one-time update. You don't just look once at the data and then you know the answer. You keep cycling through the data until, at some point, things stop changing. And that's when you declare success. Any questions about this?
Question over there. Yes. STUDENT: If the data is linearly separable, are we guaranteed that perceptrons will converge to the correct boundaries? Or is there a situation where you could ping-pong back and forth and never converge? PROFESSOR: So that's a good question. So are we guaranteed, if there is a solution, that we find it? And sometimes it's like completeness in the original lectures on search. If a solution exists do you find it? Do we have the same completeness guarantee for perceptron? In the binary perceptron, if data are linearly separable, then it might bounce around for a while. But ultimately it will find the decision boundary that separates your data. Otherwise, there is no guarantees.
This is an example that-- actually, let's take a break here. And let's work through this example after the break.
PROFESSOR: OK. Let's restart. Any questions about the first half? Let's do this example then. Multi-class perceptron. We have currently three weight vectors-- one for sports, one for politics, one for tech. And so we're trying to classify whether a sentence is about one of these three categories. We get in the first sentence "win the vote." What would be the weight vector for this first sentence? Well, it'll have a bias term. It has to "win." Oops, where did it go? It has "win," which is not a 1. Then it doesn't have "game." Oh, come on. Doesn't have "game."
It has "vote," and it has "the." So that's our feature vector for this sentence. If we inner product with sports we get-- inner product with this guy we get 1. Inner product with this guy we get 0. Inner product with this guy we get 0. So sports would win with our current settings of the weight vectors, which is not what we want. Let's say we want this to be about politics. So our y star would be politics. Then what happens? Well, we will subtract this weight vector
from the sports one to make sports less aligned with the weight vector. So we'll do-- oh, come on, screen. We'll do minus 1 minus 1 minus 0 minus 1 minus 1, which results in a negative 1, negative 1, 0, negative 1, negative 1 weight vector. And we'll add it to politics, which will result in-- this is all zeros, so this will result in a 1 1 0 1 1 weight vector. Then we go to our next example. So we did this one. We'll go to this one. "Win the election" was the weight vector for this one.
Bias is always 1. Then does "win" appear? Yes. That's a 1. Does "game" appear? No. That's a 0. Does "vote" appear? No. That's a 0. Does "the" appear? Yes. That's a 1. We inner product with each of these weight vectors. And what do we get for the first one? We get negative 1 plus negative 1 plus 0 plus 0 plus negative 1. So we get a negative 3. For politics we get a 1 plus 1 plus 0 plus 0 plus 1. We get a plus 3. And for technology we get a 0. So now the class predicted is politics,
which, let's say, we think is the right thing here, the correct label, so no changes to the weight vectors. We go on to the next one, "win the game." What's the weird vector for this one? Weight vector bias is always 1. There is a "win," there is a "game," there is no "vote," and there is a "the." Let's look at the inner products with each of our current weight vectors. For the first one, which is negative, negative, 0, negative, negative, we get a negative 3.
For the second one, politics, we get a plus 3. And for technology, we get 0 again. So politics wins. But we don't want politics to win this and we want sports to win, so we're going to subtract this feature vector from the politics one. So we're going to subtract out. There's our current weight vector. We're going to subtract out this one, which is 1 1 1 0 1, which results in a 0 0 negative 1 1 0. New weight vector for politics. And we're going to add it to the sports one, 1 1 1
0 1, which will give us a new weight vector for sports, which is 0 0 1 negative 1 0. What happens now? We go back to the first one, win the vote, and we keep cycling until we do a full pass through all the data points without any update, at which point no updates will ever happen anymore and we call it done. So some properties, tying back to the question earlier. Separability is a property of your data. Your data is separable if you can find a hyperplane that separates it into the different classes. Or if it's a multi-class problem, if there are weight vectors pointing in various directions, such that each can have their own region where all the data points from that class fall into that region, that's separable data. Convergence is a property of a run of an algorithm.
Your algorithm, when it is being run, could either converge or keep jumping around. Some algorithms will be guaranteed to always converge. That's not true for the perceptron in general. But if you have a binary perceptron with separable data, then it is guaranteed to converge, and it will find a decision boundary that separates the two classes. How many mistakes can it make along the way? It can make at most this many mistakes. So we're not proving this guarantee,
but let's get some intuition for this. What is this saying? We have number of mistakes, we have k, which refers to the number of features that we have-- so the size of the feature vector-- and delta squared, where delta is the separability measure. So if you look at this data here, you can find the direction such that along this there is a separation of delta between the two classes. So that's delta. If you find this kind of separation
that's non-zero between the two classical and parallel planes delta apart. If your data has non-zero delta, then this will be a finite number, assuming you have a finite number of features, and it means you'll make a finite number of mistakes after which you'll have converged. Now, let's get some intuiton for this. This shows that the more features you have, the longer it might take before it converges, which makes sense. The more features you have, the more you need to learn to understand what these features tell you before you converge to the right decisions. Also, the larger delta is, the less mistakes you're going to make. So the further your classes are apart, the easier the problem is and the less iterations you need to find a solution. So that's what this is saying over here.
Any questions about the positive properties? Yes. STUDENT: [INAUDIBLE] PROFESSOR: We don't have the proof here so I can't justify it without working through the proof. I can't do it on the spot here. But if you were to work through the proof, it would tell you that it's strictly less than, unless you made a typo. Now, there are some problems. Often, your data will not be perfectly separable. Just like when you run linear regression, your data points don't necessarily all fit on a line. They might deviate from that line, and you might still want to run linear regression. Same thing here. Even if your data is not perfectly separable, you might still want to find something that is a pretty good decision boundary that is usually correct.
So what to do then? Perceptron will just keep bouncing around. A new data point comes in, corrects for that, but then does another one wrong. Next time it has to correct for that one. And it just keeps happening over and over and over. Another thing that can happen is that you stop when you don't want to stop. Imagine you end up finding this decision boundary over here. Yeah. Everything's separated, but that's not a very smart decision, because there are some data points very close to that decision boundary. And that means, well, what if a data point now actually is right over here? Wouldn't you prefer that one to be also blue when it classifies because it's closer to the blue ones? But with that decision boundary on top, it would actually classify it as red because it stopped. It found something where it doesn't make mistakes anymore. So how to make it be a little more wary about being close to making mistakes and maybe shifting that boundary over a bit.
That would be nice if you could do that. Another issue, which you've already seen with naive Bayes, is that you could have overfitting. That one's actually not too hard to fix. So as you iterate, meaning you loop through your training data, you keep updating your weight vectors. Your training accuracy will keep going up because you're looking at your training data and it's telling you what to change to be more accurate on your training data. But you'll have a separate stash of data, called hold out data, and another separate stash of data, called test data. The test data you can't touch. That's for when you're 100% done you report out on. So it's representative of data you'll see in the future. But your hold out data you use during training to monitor your training and see what's happening. So as you're training you'll see on your hold out data accuracy will keep going up too for a while,
but then it'll start saturating and go back down. And you want to stop training right here. Once this starts going down again, that's the moment your updates are overfitting-- that is, memorizing your training data-- rather than fitting to the general pattern that you want to capture. If you stop there, then you'd hope, since you used your hold out data only for this one thing-- you only use it to decide where to stop-- that very likely whatever you had on your hold out data is pretty representative of what you'll also get on your test data. So this one's easily fixable. Early stopping. You stop training when you see hold out accuracy go down. How about the first two? How do we deal with those? Well, let's improve the perceptron and look at a new algorithm called logistic regression that will resolve those two issues. Let's get some intuition first. So let's say we have data, blue and red, and we want to separate it. Red crosses, blue circles.
But these data is not separable so there's no perfect decision boundary here if we have to use a line. You might say, well, maybe you want to use not a line. Maybe you want to use some decision boundary that looks more squiggly, like this? That might be OK in some situations. But you might argue also that this would be overfitting, that you don't really want to loop through in detail. And in general, it'll always be the case that if you get perfect score in your training data by making your things squiggly enough, it's unlikely to do well on your test data. So it's likely that altering your training data, you're not going to get 100%. So here's a scenario. We want to separate this by a line. It's not separable. What will your perceptron do? It'll keep bouncing back and forth between different decision boundaries and never converge. What if we think about the problem a little differently? Instead of thinking of it as one side is positive,

Ask anything
We've read this whole study set, so questions about it get the best answers.