Linear Regressin & Gradient Descent#
Hypothsis function ๊ฐ์ค ํจ์#
Hypothesis Definition
To Describe the supervised learining problem slightly more formally, our goal is, given a training set, to learn a function \(h : X \mapsto Y\) that \(h(x)\) is a โgoodโ predictor for the corresponding value of \(y\). For historical reasons, this function \(h\) is called a hypothesis.
hypothesis๋ input(feature, x)์ output(targer,label, y)์ ๊ด๊ณ๋ฅผ ๋ํ๋ด๋ ํจ์๋ฅผ ๋งํ๋ค. input์ ๋ง์ด ๊ณ ๋ คํ ์๋ก ๋ ์ ์๋ฏธํ ๊ฐ์ค์ ๋ฝ์๋ผ ํ๋ฅ ์ด ๋์์ง๊ฒ ์ง๋ง, ๊ทธ์ ๋ฐ๋ผ ์ง์์ ์ผ๋ก ์ฐ์ฐ๋์ด ๋์ด๋๋ฉฐ ์ฌ์ค์ ๊ทธ๋ ๊ฒ ํ ์ ์๋ค. ๋ฐ๋ผ์ ์ ์๋ฏธํด ๋ณด์ด๋ feature๋ฅผ selectionํ๊ณ ๊ทธ๊ฒ์ ๊ฐ์ง๊ณ ๊ฐ์ค์ ์ธ์์ผ ํ๋ค. ์ฐ์ ์ ํ ๊ฐ์ ์ข ์๋ณ์(x)์ ํ ๊ฐ์ ๋ ๋ฆฝ๋ณ์(y)๋ฅผ ๊ฐ์ง๊ณ linear ์ ํ ํจ์๋ก ๊ฐ์ค์ ์ธ์๋ณด์.
์์ ๋ณด์ด๋ ๊ฒ์ด ์์ ์ ์์ ํ์ํ ์ํ์ ๊ธฐํธ์ ์ ์๋ค์ด๋ค.
์ฐ๋ฆฌ๋ ์ฃผ์ด์ง ๋ฐ์ดํฐ๋ก \(h_{\theta}\)์ ์ค์ ๊ฐ์ง๊ณ ์๋ target๊ฐ์ธ \(y\)์ ์ฐจ์ด ์ฆ \(error = h_{\theta}(x) - y\)๊ฐ ๊ฐ์ฅ ์์ \(\theta_{0}\)์ \(\theta_{1}\)์ ์ฐพ๊ณ ์ถ์ ๊ฒ์ด๋ค. ์ด error,loss,cost,์์ฐจ ๋ผ๊ณ ํต์นญํ๋ ์ด๊ฒ์ ์ต์ํํ๋ ํจ์๋ฅผ cost function์ด๋ผ๊ณ ํ๋ค.
Cost function ๋ชฉ์ ํจ์#
์์์ ๋ณด์๋ฏ์ด ์ฐ๋ฆฌ๋ ๊ฐ์คํจ์ \(h_{\theta}(x)\)์ ์ค์ ๊ฐ y์์ ๋น์ฉ(์์ฐจ error loss)๋ฅผ ์ต์ํํ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋จธ์ ์ ํ์ต์ํค๋ ๊ฒ์ด ๋ชฉํ์ด๋ค. ๊ฒฐ๊ตญ ์ด cost function์ \(x\) input์ ๋ฃ์์ ๋ ๋์ค๋ ๊ฐ \(J(\theta)\)์ ์ต์ํํ๋ ๊ฒ์ด learning algorithm(์ฌ๊ธฐ์๋ supervised learning, linear regression)์ ์ฑ๋ฅ์ ๋์ด๋ ๊ฒ์ด๋ผ๊ณ ๋ณผ ์ ์๋ค.
๊ธฐ๋ณธ์ ์ธ cost function์ LSE(least squared error)์ด๋ค. error์ ์ ๊ณฑ์ ํด์ฃผ๊ณ ๊ทธ๊ฒ์ ํฉ์ ๋ํด์ค์ ์ต์๊ฐ์ ์ฐพ๋ ๋ฐฉ์์ด๋ค. ์ฌ๊ธฐ์ 1/m์ ํด์ฃผ๋ฉด mean squared error(MSE)๊ฐ ๋๋ค.
์ m์ด ์๋๋ผ 2m์ธ๊ฐ?
ํ๊ท ์ด๋ผ๋ฉด m ๋ฐ์ดํฐ์ ๊ฐฏ์๋๋ก ๋๋ ์ค์ผํ๋๊ฒ ์๋๊ฐ ํ๋ ์๋ฌธ์ด ๋ค ์ ์๋๋ฐ, ๋ค์ ๋ฏธ๋ถ์ ํ๊ฒ ๋๋ฉด ^2๊ฐ ์์ผ๋ก ํ์ด๋์ 1/2์ ์์๋์ด 1์ด ๋๊ธฐ ๋๋ฌธ์ด๋ค. \(\theta\)๋ฅผ ์ด๋ค ์์๋ก ๋๋ ๋ ์๊ด์๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ๋ค.
๊ทธ๋ฅ ๋จ์ํ๊ฒ w = w + learning_rate * error * x
๋ฅผ ์ฌ์ฉํด์ updateํด๊ฐ๋ฉด์ theta๋ค์ ์ง์ ์ฐพ์๊ฐ๊ณ cost function์ ๊ธฐ์ธ๊ธฐ๋ฅผ ๊ตฌํ์ง ์์๋ ๋๊ธดํ๋ค.
Gradient Descent ๊ฒฝ์ฌํ๊ฐ๋ฒ#
์ ๊ฒฝ์ฌํ๊ฐ๋ฒ์ ์ฌ์ฉํ๋๊ฐ?
๊ฐ ๋ฐ์ดํฐ ์ํ ํ๋ํ๋ ๋ง๋ค ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฐ์ดํธ๋ฅผ ํด๊ฐ๋ฉฐ cost function์ ์ต์ ํํ๋ ๋ฐฉ์์ ์๊ฐ์ด ์ค๋๊ฑธ๋ฆฌ๊ณ ๋ฐ์ดํฐ ์ฌ์ด์ฆ๊ฐ ์ปค์ง๋ฉด mini-batch๋ก ๋ช ๊ฐ์ dataset example๋ง๋ค iterativeํ๊ฒ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฐ์ดํธ ํ๋ ๊ฒ์ด dataset์ด ๊ฑฐ๋ํด์ง ๊ฒฝ์ฐ์ ๊ณ์ฐ๋ ์ธก๋ฉด์ด๋ค ์ต์ ํ ์๊ฐ ์ธก๋ฉด์์ ์ ์ฉํ ์ ์๋ค.
closed form solution์ด ์๋ ๊ฒฝ์ฐ์๋ ์ฌ์ฉํ ์ ์๋ค.
non linearity ํจ์๋ ๋ฏธ๋ถ๊ณ์์ ๊ทผ์ ๊ณ์ฐํ๊ธฐ ์ด๋ ค์ด ๊ฒฝ์ฐ์๋ ์ฌ์ฉํ ์ ์๋ค.
Gradient Descent ๊ฒฝ์ฌํ๊ฐ๋ฒ์ 1์ฐจ ๋ฏธ๋ถ๊ณ์๋ฅผ ์ด์ฉํด ํจ์์ ์ต์๊ฐ์ ์ฐพ์๋ด๋ iterativeํ ๋ฐฉ๋ฒ์ด๋ค. Steepest Descent๋ผ๊ณ ๋ ๋ถ๋ฆฐ๋ค. ์์ด ํ๋๋ ์๋ณด์ด๊ณ ์ด๋๊ฐ ์์ด๊ณ ์ด๋๊ฐ ์๋์ธ์ง๋ง ๋ณด์ด๋ ์ํฉ์์ ํ ๋ฐ์ฉ ์๋๋ก ๋ด๋ ค๊ฐ๋ ๊ฒ๊ณผ ๋น์ทํ๊ธฐ ๋๋ฌธ์ด๋ค. ํ์ฐ์ ๋ชฉํ๊ฐ ์ฐ์ ๋งจ ๋ฐ์ด๋ฏ cost function์ ์ต์๊ฐ์ ์ฐพ๋ ๊ฒ์ด ๋ชฉํ์ด๊ณ , cost function์ ์ต์๊ฐ์ ์ง์ ์ ๊ณง hypothesis function์ ํ๋ผ๋ฏธํฐ \(\theta\)์ ์ต์ ๊ฐ์ด ๋๋ค.
(Gradient Descent Algorithm)
start with some \({\theta}_0\), \({\theta}_1\)
keep changing \({\theta}_0\), \({\theta}_1\) to reduce \(J({\theta}_0, {\theta}_1)\) until we hopefully end up at a minimum
repeat until convergence{
\({\theta}_j := {\theta}_j - {\alpha}{\frac \partial {\partial{\theta}_j} } J({\theta}_0, {\theta}_1)\space\text {for j=0,j=1}\)
}
์์ gradient descent ์๊ณ ๋ฆฌ์ฆ์ ์ฐ๋ฆฌ์ cost function์ ์ ์ฉํด๋ณด์.
๋ค์ ํํํ๋ฉด