Week 3 Lecture ML : Classification and Representation
mcdn2020. 8. 7. 13:38
반응형
ML:Logistic Regression
Now we are switching from regression problems toclassification problems. Don't be confused by the name "Logistic Regression"; it is named that way for historical reasons and is actually an approach to classification problems, not regression problems.
To attempt classification, one method is to use linear regression and map all predictions greater than 0.5 as a 1 and all less than 0.5 as a 0. However, this method doesn't work well because classification is not actually a linear function.
The classification problem is just like the regression problem, except that the values we now want to predict take on only a small number of discrete values. For now, we will focus on thebinary classificationproblemin 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, thenx^{(i)}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. Hence, y∈{0,1}. 0 is also called the negative class, and 1 the positive class, and they are sometimes also denoted by the symbols “-” and “+.” Givenx^{(i)}x(i), the correspondingy^{(i)}y(i)is also called the label for the training example.
보충 설명 :
만약 classifcation문제에서 앞의 regression 문제처럼 linear regression을 사용하게 되면 0,1 만 나뉘어지는 자료에서 0,1외의 수가 나오는 것은 물론이고 위의 예처럼 완전 동떨어진 예가 있다면 그 중간 (평균)이 지나치게 치우쳐지는 문제가 일어난다. 따라서 classifcation 문제에서는 sigmoid function 즉 지수함수를 이용해서 h(x)를 표현하게 되고 그 결과 logistic regression 함수가 나오게 된다. classifcation 문제이지만 logistic 'regression'이름인것은 가볍게 인정하도록 하자.
Binary Classification
Instead of our output vector y being a continuous range of values, it will only be 0 or 1.
y∈{0,1}
Where 0 is usually taken as the "negative class" and 1 as the "positive class", but you are free to assign any representation to it.
We're only doing two classes for now, called a "Binary Classification Problem."
One method is to use linear regression and map all predictions greater than 0.5 as a 1 and all less than 0.5 as a 0. This method doesn't work well because classification is not actually a linear function.
Hypothesis Representation
Our hypothesis should satisfy:
0 \leq h_\theta (x) \leq 10≤hθ(x)≤1
Our new form uses the "Sigmoid Function," also called the "Logistic Function":
hθ(x)=g(θTx)z=θTxg(z)=11+e−z
The function g(z), shown here, maps any real number to the (0, 1) interval, making it useful for transforming an arbitrary-valued function into a function better suited for classification. Try playing with interactive plot of sigmoid function : (https://www.desmos.com/calculator/bgontvxotm).
We start with our old hypothesis (linear regression), except that we want to restrict the range to 0 and 1. This is accomplished by plugging\theta^TxθTxinto the Logistic Function.
h_\thetahθwill give us theprobabilitythat our output is 1. For example,h_\theta(x)=0.7hθ(x)=0.7gives us the probability of 70% that our output is 1.
Our probability that our prediction is 0 is just the complement of our probability that it is 1 (e.g. if probability that it is 1 is 70%, then the probability that it is 0 is 30%).
So, so long as we're given my parameter vector theta,
that defines the decision boundary, which is the circle.
But the training set is not what we use to define the decision boundary.
The training set may be used to fit the parameters theta.
We'll talk about how to do that later.
But, once you have the parameters theta,
that is what defines the decisions boundary.
Decision Boundary
In order to get our discrete 0 or 1 classification, we can translate the output of the hypothesis function as follows:
hθ(x)≥0.5→y=1hθ(x)<0.5→y=0
The way our logistic function g behaves is that when its input is greater than or equal to zero, its output is greater than or equal to 0.5:
g(z)≥0.5whenz≥0
Remember.-
z=0,e0=1⇒g(z)=1/2z→∞,e−∞→0⇒g(z)=1z→−∞,e∞→∞⇒g(z)=0
So if our input to g is\theta^T XθTX, then that means:
hθ(x)=g(θTx)≥0.5whenθTx≥0
From these statements we can now say:
θTx≥0⇒y=1θTx<0⇒y=0
Thedecision boundaryis the line that separates the area where y = 0 and where y = 1. It is created by our hypothesis function.
Example:
θ=⎡⎣5−10⎤⎦y=1if5+(−1)x1+0x2≥05−x1≥0−x1≥−5x1≤5
In this case, our decision boundary is a straight vertical line placed on the graph wherex_1 = 5x1=5, and everything to the left of that denotes y = 1, while everything to the right denotes y = 0.
Again, the input to the sigmoid function g(z) (e.g.\theta^T XθTX) doesn't need to be linear, and could be a function that describes a circle (e.g.z = \theta_0 + \theta_1 x_1^2 +\theta_2 x_2^2z=θ0+θ1x12+θ2x22) or any shape to fit our data.
Cost Function
We cannot use the same cost function that we use for linear regression because the Logistic Function will cause the output to be wavy, causing many local optima. In other words, it will not be a convex function.
Instead, our cost function for logistic regression looks like:
J(θ)=1m∑i=1mCost(hθ(x(i)),y(i))Cost(hθ(x),y)=−log(hθ(x))Cost(hθ(x),y)=−log(1−hθ(x))if y = 1if y = 0
When y = 1, we get the following plot forJ(\theta)J(θ)vsh_\theta (x)hθ(x):
Similarly, when y = 0, we get the following plot forJ(\theta)J(θ)vsh_\theta (x)hθ(x):
The more our hypothesis is off from y, the larger the cost function output. If our hypothesis is equal to y, then our cost is 0:
Cost(hθ(x),y)=0 if hθ(x)=yCost(hθ(x),y)→∞ if y=0andhθ(x)→1Cost(hθ(x),y)→∞ if y=1andhθ(x)→0
If our correct answer 'y' is 0, then the cost function will be 0 if our hypothesis function also outputs 0. If our hypothesis approaches 1, then the cost function will approach infinity.
If our correct answer 'y' is 1, then the cost function will be 0 if our hypothesis function outputs 1. If our hypothesis approaches 0, then the cost function will approach infinity.
Note that writing the cost function in this way guarantees that J(θ) is convex for logistic regression.
Simplified Cost Function and Gradient Descent
We can compress our cost function's two conditional cases into one case:
Notice that when y is equal to 1, then the second term(1-y)\log(1-h_\theta(x))(1−y)log(1−hθ(x))will be zero and will not affect the result. If y is equal to 0, then the first term-y \log(h_\theta(x))−ylog(hθ(x))will be zero and will not affect the result.
We can fully write out our entire cost function as follows:
"Conjugate gradient", "BFGS", and "L-BFGS" are more sophisticated, faster ways to optimize θ that can be used instead of gradient descent. A. Ng suggests not to write these more sophisticated algorithms yourself (unless you are an expert in numerical computing) but use the libraries instead, as they're already tested and highly optimized. Octave provides them.
We first need to provide a function that evaluates the following two functions for a given input value θ:
J(θ)∂∂θjJ(θ)
We can write a single function that returns both of these:
1
2
3
4
function [jVal, gradient] = costFunction(theta)
jVal = [...code to compute J(theta)...];
gradient = [...code to compute derivative of J(theta)...];
end
Then we can use octave's "fminunc()" optimization algorithm along with the "optimset()" function that creates an object containing the options we want to send to "fminunc()". (Note: the value for MaxIter should be an integer, not a character string - errata in the video at 7:30)
We give to the function "fminunc()" our cost function, our initial vector of theta values, and the "options" object that we created beforehand.
Multiclass Classification: One-vs-all
Now we will approach the classification of data into more than two categories. Instead of y = {0,1} we will expand our definition so that y = {0,1...n}.
In this case we divide our problem into n+1 (+1 because the index starts at 0) binary classification problems; in each one, we predict the probability that 'y' is a member of one of our classes.
We are basically choosing one class and then lumping all the others into a single second class. We do this repeatedly, applying binary logistic regression to each case, and then use the hypothesis that returned the highest value as our prediction.