1. Home
  2. Explore
  3. Computer Science
  4. Perceptrons and Logistic Regression

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Perceptrons and Logistic Regression

Speaker 1Speaker 2

Perceptrons and Logistic Regression

12:56
0:00-12:56
  1. Maria

    Welcome back. Today we are covering perceptrons and logistic regression, two approaches for turning feature vectors into class predictions. The earlier lectures focused on probabilistic models such as naive Bayes; today we will focus directly on the decision from features to a label, without learning a full probability model over every variable.

  2. Michael

    And a quick course note before we begin: project four is due Friday at 4:00 PM, homework ten is coming soon, and because next Monday is a holiday, that homework will be due Tuesday. Midterm two covers material from the beginning of the semester through naive Bayes and the current homework, while perceptrons and logistic regression belong to the final rather than midterm two. There will also be a preparation page, review sessions, and a practice midterm before the exam.

  3. Maria

    Let's begin with the general classification setup. An input, such as an email or an image, is converted into a feature vector, often written as f of x. For an email, features might count occurrences of words like “free,” record whether your name appears, detect misspellings, or indicate whether the message came from a friend. For an image, features might be binary pixel values, grayscale values, or more advanced measurements such as the number of loops in a digit.

  4. Michael

    The feature-engineering step is important and traditionally involves a lot of design judgment. You decide which characteristics are useful, whether related words should be grouped together, and whether details like the sender should become features. In this lecture, we assume the feature vector is already available and concentrate on the next step: mapping f of x to a class label y.

  5. Maria

    The first model is loosely inspired by a biological neuron. A neuron receives signals through inputs called dendrites, combines those signals in its core, and may send an output through an axon. We will not model a whole neural network here. We are focusing on one unit whose inputs are feature values and whose output is a classification decision.

  6. Michael

    Each feature has an associated weight that represents how strongly the feature influences the decision. The model computes a weighted sum, or dot product: w inner product f of x. We call this value the activation. In the binary case, a positive activation predicts the positive class, usually represented by plus one, and a negative activation predicts the negative class, represented by minus one.

  7. Maria

    The weights can be positive or negative. A positive weight means that a larger feature value pushes the activation upward, while a negative weight pushes it downward. For a spam classifier, the feature “free” might have weight four, the presence of your name might have weight negative one, a misspelling might have weight one, and coming from a friend might have weight negative three.

  8. Michael

    A bias term can also be included. We treat the bias as another weight attached to a feature that is always one. This lets the classifier favor one class by default. For example, a negative bias means that an input needs enough positive evidence to cross zero and be classified as spam, while a positive bias shifts the decision in the other direction.

  9. Maria

    Geometrically, the weight vector defines a decision boundary. Without a bias, the boundary passes through the origin and consists of points whose dot product with w is zero. The boundary is orthogonal to w. One side has positive inner products and is assigned to the positive class; the other side has negative inner products and is assigned to the negative class.

  10. Michael

    Changing the weight vector changes both the orientation and, with a bias term, the position of that boundary. In a two-dimensional drawing, a feature vector aligned with w produces a positive score, while a vector pointing in the opposite direction produces a negative score. In reality, the feature space may have many dimensions, even though we can only draw two or three.

  11. Maria

    The binary perceptron learns its weights iteratively. It starts with all weights equal to zero, examines training examples one at a time, and classifies each example using the current weights. If the prediction is correct, no update is made. If the prediction is wrong, the weights are adjusted to make the current example more compatible with its true label.

  12. Michael

    The update is especially simple: add the true label y star times the feature vector to the weight vector. In symbols, the new weights are w prime equals w plus y star times f of x. If the true label is positive, we add the feature vector, making w more aligned with it. If the true label is negative, we subtract the feature vector, making w less aligned with it.

  13. Maria

    The dot-product calculation explains why this works. After the update, the new activation on that same example is the old activation plus y star times the inner product of the feature vector with itself. That self-inner-product is nonnegative, so a positive true label raises the score and a negative true label lowers it. The update moves the score in the correct direction, although it does not guarantee that the example will immediately be classified correctly.

  14. Michael

    This resembles feature-based Q-learning because both methods use a weight vector and a feature vector in an inner product. The difference is what that value means and how it is learned. In Q-learning, the inner product estimates a Q-value and learning relies on a Bellman-style target, while the perceptron receives direct supervision telling it whether the example should be positive or negative.

  15. Maria

    For linearly separable binary data, the perceptron is guaranteed to converge eventually. Separable means that some hyperplane can place all examples of one class on one side and all examples of the other class on the other side. The algorithm may move the boundary repeatedly at first, but once it reaches a separating boundary, every later example is classified correctly and no further updates occur.

  16. Michael

    There is also a mistake bound for separable data. If there are k features and the classes have a positive separation measure delta, the number of mistakes is bounded in terms of k divided by delta squared. The intuition is that more features can require more learning, while a larger gap between the classes makes the problem easier and reduces the number of mistakes.

  17. Maria

    The perceptron extends to multiple classes by assigning one weight vector to each class. For class y, compute the score w sub y inner product f of x, and predict the class with the largest score. Unlike the binary case, where one weight vector can represent the two sides of a boundary, the multiclass version keeps a separate vector for every class.

  18. Michael

    During multiclass training, suppose the true class is y star but the model predicts y. We subtract the feature vector from the incorrectly predicted class's weight vector and add the feature vector to the true class's weight vector. This lowers the incorrect class's score and raises the correct class's score on that example.

  19. Maria

    For example, imagine classifying sentences as sports, politics, or technology using features for a bias term and words such as “win,” “game,” “vote,” and “the.” If “win the vote” is supposed to be politics but sports currently receives the highest score, subtract that sentence's feature vector from the sports weights and add it to the politics weights. Then continue cycling through the examples until a full pass produces no updates.

  20. Michael

    It is useful to distinguish separability from convergence. Separability is a property of the data: whether an appropriate set of boundaries exists. Convergence is a property of a particular algorithm run. A binary perceptron with separable data converges, but the perceptron has no general convergence guarantee when the data is not separable.

  21. Maria

    Nonseparable data creates the first major problem. If no line can classify every training point correctly, the perceptron may keep correcting one mistake and then creating another, bouncing among different boundaries forever. A second problem is that even when the data is separable, the first boundary with zero training mistakes may place some examples extremely close to the boundary, which can make the classifier fragile.

  22. Michael

    Overfitting is another concern. Training accuracy can keep increasing as the weights repeatedly respond to the training examples, while performance on unseen data eventually gets worse. A holdout set can be monitored during training, and training should stop when holdout accuracy reaches its best point and then begins to decline. The separate test set is kept untouched until the final evaluation.

  23. Maria

    The next model, logistic regression, addresses the first two perceptron issues by making probabilistic rather than purely deterministic decisions. Instead of saying one side of a line is definitely positive and the other side definitely negative, it assigns probabilities that change gradually across the boundary. A point on the boundary might receive a fifty-fifty probability, while points farther away receive increasingly confident probabilities.

  24. Michael

    Start with the same activation, z equals w inner product f of x. This value can range from negative infinity to positive infinity, so we transform it with the sigmoid function: one divided by one plus e to the negative z. The sigmoid always lies between zero and one, approaches zero for very negative z, approaches one for very positive z, and equals one-half when z is zero.

  25. Maria

    We can interpret the sigmoid as the probability of the positive label. The probability of the negative label is one minus that value. This probabilistic view allows some training examples to lie on the “wrong” side of the boundary without causing the algorithm to chase a perfect but impossible separation.

  26. Michael

    To choose the weights, logistic regression uses maximum likelihood. We select the weight vector that maximizes the sum of the log probabilities of the observed labels given their feature vectors. Unlike a full generative model, this objective focuses on predicting y given x and does not also try to model the distribution of the inputs themselves.

  27. Maria

    That distinction connects logistic regression to discriminative classification. A generative approach, such as naive Bayes, models the joint distribution of labels and inputs, which can be written as a conditional term plus a term for the inputs. A discriminative approach focuses only on the conditional label probability because that is the part directly needed for classification.

  28. Michael

    There is a trade-off between the two approaches. If the only goal is classification and there is abundant data, focusing directly on the conditional distribution can be useful. With a small amount of data, modeling the input distribution as well can sometimes act as a form of regularization and moderate the choice of weights, potentially reducing overfitting.

  29. Maria

    Logistic regression also prefers a sensible margin between the data and the decision boundary. Suppose two lines both classify every training example correctly, but one passes much closer to the examples. The perceptron treats both as equally successful once it stops making mistakes. Logistic regression assigns probabilities, so the boundary that makes the observed labels more confidently likely receives the higher log-likelihood.

  30. Michael

    For multiple classes, we again give each class its own weight vector and compute a score z sub y equal to w sub y inner product f of x. Instead of choosing only the largest score, we convert all the scores into probabilities using the softmax function. The probability of class y is e to the z sub y divided by the sum of e to the z values over every possible class.

  31. Maria

    Softmax works because exponentiation turns every score into a positive number, and dividing by the total makes the probabilities sum to one. A class with the highest score receives the highest probability because exponentials grow quickly. If two scores are equal, their contributions are equal; if one score is much larger, it dominates the resulting distribution.

  32. Michael

    Multiclass logistic regression then chooses the weight vectors that maximize the summed log probability of the correct labels given the feature vectors. This is the same maximum-likelihood idea, now using softmax probabilities instead of a binary sigmoid. It can maintain meaningful uncertainty when the classes overlap rather than endlessly correcting impossible mistakes.

  33. Maria

    So the main contrast is this: the perceptron makes a hard decision from the sign of an activation, while logistic regression turns activations into probabilities and optimizes their likelihood. The perceptron can converge to any separating boundary and may not prefer a safer margin. Logistic regression can handle nonseparable data through graded probabilities and prefers parameters that make the observed labels confidently likely.

  34. Michael

    One final limitation of today's discussion is that we have defined what the best logistic-regression weights should be, but we have not yet explained how to find them efficiently. Checking every possible weight vector would require unrealistic computation, and taking derivatives does not produce a simple closed-form answer here. The next lecture will address practical optimization methods for finding the weights.

  35. Maria

    To recap: start with engineered feature vectors, compute weighted activations, and use the perceptron update to move weights toward correct binary or multiclass decisions. For separable binary data, the perceptron converges, but it can struggle with overlap and can stop at an unnecessarily narrow margin. Logistic regression replaces hard decisions with sigmoid or softmax probabilities, chooses weights by maximum likelihood, and directly focuses on the conditional classification task.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent