





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Let’s now talk about the classification problem. This is just like the regression problem, except that the values y we now want to predict take on only a small number of discrete values. For now, we will focus on the binary classification problem in which y can take on only two values, 0 and 1. (Most of what we say here will also generalize to the multiple-class case.) For instance, if we are trying to build a spam classifier for email, then x(i) may be some features of a piece of email, and y may be 1 if it is a piece of spam mail, and 0 otherwise. 0 is also called the negative class, and 1 the positive class, and they are sometimes also denoted by the symbols “-” and “+.” Given x(i), the corresponding y(i)^ is also called the label for the training example.
We could approach the classification problem ignoring the fact that y is discrete-valued, and use our old linear regression algorithm to try to predict y given x. However, it is easy to construct examples where this method performs very poorly. Intuitively, it also doesn’t make sense for hθ(x) to take values larger than 1 or smaller than 0 when we know that y ∈ { 0 , 1 }. To fix this, let’s change the form for our hypotheses hθ(x). We will choose
hθ(x) = g(θT^ x) =
1 + e−θT^ x^
where
g(z) =
1 + e−z
21
is called the logistic function or the sigmoid function. Here is a plot showing g(z):
−5^0 −4 −3 −2 −1 0 1 2 3 4 5
1
z
g(z)
Notice that g(z) tends towards 1 as z → ∞, and g(z) tends towards 0 as z → −∞. Moreover, g(z), and hence also h(x), is always bounded between 0 and 1. As before, we are keeping the convention of letting x 0 = 1, so that θT^ x = θ 0 +
∑d j=1 θj^ xj^. For now, let’s take the choice of g as given. Other functions that smoothly increase from 0 to 1 can also be used, but for a couple of reasons that we’ll see later (when we talk about GLMs, and when we talk about generative learning algorithms), the choice of the logistic function is a fairly natural one. Before moving on, here’s a useful property of the derivative of the sigmoid function, which we write as g′:
g′(z) =
d dz
(1 + e−z^ )^2
e−z^
(1 + e−z^ )
(1 + e−z^ )
= g(z)(1 − g(z)).
So, given the logistic regression model, how do we fit θ for it? Following how we saw least squares regression could be derived as the maximum like- lihood estimator under a set of assumptions, let’s endow our classification model with a set of probabilistic assumptions, and then fit the parameters via maximum likelihood.
Above, we used the fact that g′(z) = g(z)(1 − g(z)). This therefore gives us the stochastic gradient ascent rule
θj := θj + α
y(i)^ − hθ(x(i))
x( ji)
If we compare this to the LMS update rule, we see that it looks identical; but this is not the same algorithm, because hθ(x(i)) is now defined as a non-linear function of θT^ x(i). Nonetheless, it’s a little surprising that we end up with the same update rule for a rather different algorithm and learning problem. Is this coincidence, or is there a deeper reason behind this? We’ll answer this when we get to GLM models.
Remark 2.1.1. : An alternative notational viewpoint of the same loss function is also useful, especially for Section 7.1 where we study nonlinear models. Let `logistic : R × { 0 , 1 } → R≥ 0 be the logistic loss defined as
`logistic(t, y) , y log(1 + exp(−t)) + (1 − y) log(1 + exp(t)). (2.3)
One can verify by plugging in hθ(x) = 1/(1 + e−θ >x ) that the negative log- likelihood (the negation of `(θ) in equation (2.1)) can be re-written as
−(θ) =logistic(θ>x, y). (2.4)
Oftentimes θ>x or t is called the logit. Basic calculus gives us that
∂`logistic(t, y) ∂t
= y
− exp(−t) 1 + exp(−t)
1 + exp(−t)
= 1/(1 + exp(−t)) − y. (2.6)
Then, using the chain rule, we have that
∂ ∂θj
`(θ) = −
∂`logistic(t, y) ∂t
∂t ∂θj
= (y − 1 /(1 + exp(−t))) · xj = (y − hθ(x))xj , (2.8)
which is consistent with the derivation in equation (2.2). We will see this viewpoint can be extended nonlinear models in Section 7.1.
We now digress to talk briefly about an algorithm that’s of some historical interest, and that we will also return to later when we talk about learning
theory. Consider modifying the logistic regression method to “force” it to output values that are either 0 or 1 or exactly. To do so, it seems natural to change the definition of g to be the threshold function:
g(z) =
1 if z ≥ 0 0 if z < 0
If we then let hθ(x) = g(θT^ x) as before but using this modified definition of g, and if we use the update rule
θj := θj + α
y(i)^ − hθ(x(i))
x( ji ).
then we have the perceptron learning algorithn. In the 1960s, this “perceptron” was argued to be a rough model for how individual neurons in the brain work. Given how simple the algorithm is, it will also provide a starting point for our analysis when we talk about learning theory later in this class. Note however that even though the perceptron may be cosmetically similar to the other algorithms we talked about, it is actually a very different type of algorithm than logistic regression and least squares linear regression; in particular, it is difficult to endow the perceptron’s predic- tions with meaningful probabilistic interpretations, or derive the perceptron as a maximum likelihood estimation algorithm.
Consider a classification problem in which the response variable y can take on any one of k values, so y ∈ { 1 , 2 ,... , k}. For example, rather than classifying emails into the two classes spam or not-spam—which would have been a binary classification problem—we might want to classify them into three classes, such as spam, personal mails, and work-related mails. The label / response variable is still discrete, but can now take on more than two values. We will thus model it as distributed according to a multinomial distribution. In this case, p(y | x; θ) is a distribution over k possible discrete outcomes and is thus a multinomial distribution. Recall that a multinomial distribu- tion involves k numbers φ 1 ,... , φk specifying the probability of each of the outcomes. Note that these numbers must satisfy
∑k i=1 φi^ = 1. We will de- sign a parameterized model that outputs φ 1 ,... , φk satisfying this constraint given the input x. We introduce k groups of parameters θ 1 ,... , θk, each of them being a vector in Rd. Intuitively, we would like to use θ> 1 x,... , θ k> x to represent
It’s convenient to define the cross-entropy loss `ce : Rk^ × { 1 ,... , k} → R≥ 0 , which modularizes in the complex equation above:^1
`ce((t 1 ,... , tk), y) = − log
exp(ty) ∑k j=1 exp(tj^ )
With this notation, we can simply rewrite equation (2.13) as
`(θ) =
∑^ n
i=
`ce((θ> 1 x(i),... , θ k> x(i)), y(i)). (2.15)
Moreover, conveniently, the cross-entropy loss also has a simple gradient. Let t = (t 1 ,... , tk), and recall φi = ∑kexp(ti) j=1 exp(tj^ )^
. By basic calculus, we can derive
∂`ce(t, y) ∂ti
= φi − 1 {y = i} , (2.16)
where 1{·} is the indicator function, that is, 1{y = i} = 1 if y = i, and 1 {y = i} = 0 if y 6 = i. Alternatively, in vectorized notations, we have the following form which will be useful for Chapter 7:
∂`ce(t, y) ∂t
= φ − ey , (2.17)
where es ∈ Rk^ is the s-th natural basis vector (where the s-th entry is 1 and all other entries are zeros.) Using Chain rule, we have that
∂`ce((θ> 1 x,... , θ k> x), y) ∂θi
∂`(t, y) ∂ti
∂ti ∂θi
= (φi − 1 {y = i}) · x. (2.18)
Therefore, the gradient of the loss with respect to the part of parameter θi is
∂`(θ) ∂θi
∑^ n
j=
(φ( ij )− 1 {y(j)^ = i}) · x(j)^ , (2.19)
where φ( i j)= exp(θ i> x(j)) ∑k s=1 exp(θ>^ s^ x(j))^
is the probability that the model predicts item i
for example x(j). With the gradients above, one can implement (stochastic) gradient descent to minimize the loss function `(θ).
(^1) There are some ambiguity in the naming here. Some people call the cross-entropy loss
the function that maps the probability vector (the φ in our language) and label y to the final real number, and call our version of cross-entropy loss softmax-cross-entropy loss. We choose our current naming convention because it’s consistent with the naming of most modern deep learning library such as PyTorch and Jax.
−10 1 1.5 2 2.5 3 3.5 4 4.5 5
0
10
20
30
40
50
60
x
f(x)
−10 1 1.5 2 2.5 3 3.5 4 4.5 5
0
10
20
30
40
50
60
x
f(x)
−10 1 1.5 2 2.5 3 3.5 4 4.5 5
0
10
20
30
40
50
60
x
f(x)
Returning to logistic regression with g(z) being the sigmoid function, let’s now talk about a different algorithm for maximizing `(θ). To get us started, let’s consider Newton’s method for finding a zero of a function. Specifically, suppose we have some function f : R 7 → R, and we wish to find a value of θ so that f (θ) = 0. Here, θ ∈ R is a real number. Newton’s method performs the following update:
θ := θ −
f (θ) f ′(θ)
This method has a natural interpretation in which we can think of it as approximating the function f via a linear function that is tangent to f at the current guess θ, solving for where that linear function equals to zero, and letting the next guess for θ be where that linear function is zero. Here’s a picture of the Newton’s method in action: In the leftmost figure, we see the function f plotted along with the line y = 0. We’re trying to find θ so that f (θ) = 0; the value of θ that achieves this is about 1.3. Suppose we initialized the algorithm with θ = 4.5. Newton’s method then fits a straight line tangent to f at θ = 4.5, and solves for the where that line evaluates to 0. (Middle figure.) This give us the next guess for θ, which is about 2.8. The rightmost figure shows the result of running one more iteration, which the updates θ to about 1.8. After a few more iterations, we rapidly approach θ = 1.3. Newton’s method gives a way of getting to f (θ) = 0. What if we want to use it to maximize some function ? The maxima of correspond to points where its first derivative ′(θ) is zero. So, by letting f (θ) =′(θ), we can use the same algorithm to maximize `, and we obtain update rule:
θ := θ −
′(θ)′′(θ)
(Something to think about: How would this change if we wanted to use Newton’s method to minimize rather than maximize a function?)