MY MEMO

[MACHINE LEARNING] Classification and Simplified cost function & Gradient Descent 본문

MACHINE LEARNING/Stanford University

[MACHINE LEARNING] Classification and Simplified cost function & Gradient Descent

l_j_yeon 2017. 3. 29. 21:21

+) this post is based on the lecture and content in the coursera(https://www.coursera.org/) machine learning class 

(professor)

Classification

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 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


example : 


Email : Spam / not spam

Online Transactions: Fraudulent (Yes/No)?

Tumor : Malignant / Benign?


Hypothesis Representation


Our new form uses the "Sigmoid Function," also called the "Logistic Function":



The following image shows us what the sigmoid function looks like:

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.

hθ(x) will give us the probability that our output is 1. For example, hθ(x)=0.7 gives us a 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%).


hθ(x)=P(y=1|x;θ)=1−P(y=0|x;θ)

P(y=0|x;θ)+P(y=1|x;θ)=1


Decision Boundary


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:


+) 로그를 사용하는 이유는, 구불구불한 cost 함수를 매끈하게 펴기 위함이다. the reason why using log is you can make clear line of function.




When y = 1, we get the following plot for J(θ) vs hθ(x):

Similarly, when y = 0, we get the following plot for J(θ) vs hθ(x):

+)  

y가 1일 때 1을 예측(H(X)=1)했다는 것은 맞았다는 뜻이고, 이때의 비용은 0이다.
y가 1일 때 0을 예측
(H(X)=0)했다는 것은 틀렸다는 뜻이고, 이때의 비용은 무한대이다.
y가 0일 때 0을 예측(H(X)=0)했다는 것은 맞았다는 뜻이고, 이때의 비용은 0이다.
y가 0일 때 1을 예측(H(X)=1)했다는 것은 틀렸다는 뜻이고, 이때의 비용은 무한대이다.


예측의 결과는 0과 1이 나올 수도 있지만, 대부분은 0과 1 사이의 어떤 값이 된다. 가령, 0.7이 나왔으면 0.7에서 label에 해당하는 1 또는 0을 뺀 결과를 활용하는 것은 여전히 동일하다. 다만 Linear Regression에서 했던 것처럼 페널티를 주기 위한 제곱을 하진 않는다. log 자체에 페널티와 동일한 무한대로 수렴하는 값이 있기 때문에 제곱을 할 필요가 없기 때문이다. 결론적으로 분류(classification)에서도 지금까지와 동일하게 최소 cost가 되도록 W를 조절하는 것이 핵심이다.

출처: http://pythonkim.tistory.com/22 [파이쿵]


Simplified Cost Function and Gradient Descent

A vectorized implementation is:

Gradient Descent :We can work out the derivative part using calculus to get: 



A vectorized implementation is: 


θ:=θαmXT(g(Xθ)y⃗ )θ:=θαmXT(g()−y )


Comments