brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Oct 09. 2020

앤드류 응의 머신러닝 (4-2) : 다변수의 경사 하강

   온라인 강의 플랫폼 코세라의 창립자인 앤드류 응 (Andrew Ng) 교수는 인공지능 업계의 거장입니다. 그가 스탠퍼드 대학에서 머신 러닝 입문자에게 한 강의를 그대로 코세라 온라인 강의 (Coursera.org)에서 무료로 배울 수 있습니다. 이 강의는 머신러닝 입문자들의 필수코스입니다. 인공지능과 머신러닝을 혼자 공부하면서 자연스럽게 만나게 되는 강의입니다. 


Linear Regression with Mutiple Variables

다변수 선형회귀


Multivarate Linear Regression (다변수 선형 회귀) 


Gradient Descent For Multiple Variables (다변수의 경사 하강법)


   In the previous video, we talked about the form of the hypothesis for linear regression with multiple features or with multiple variables. In this video, let's talk about how to fit the parameters of that hypothesis. In particular let's talk about how to use gradient descent for linear regression with multiple features. 


   지난 강의에서 여러 개의 피처(Feature) 또는 다수의 변수를 가진 선형 회귀 가설을 배웠습니다. 이번 강의에서 학습 데이터에 최적화된 가설의 파라미터를 맞추는 방법을 설명합니다. 특히, 다수의 피처를 가진 선형 회귀에 경사 하강법을 사용하는 법을 다룹니다. 



   To quickly summarize our notation, 


   빠르게 표기법을 정리합니다.  



   This is our formal hypothesis in multivariable linear regression where we've adopted the convention that x0=1. The parameters of this model are theta0 through theta n, but instead of thinking of this as n separate parameters, which is valid, I'm instead going to think of the parameters as theta where theta here is a n+1-dimensional vector. So I'm just going to think of the parameters of this model as itself being a vector. Our cost function is J of theta0 through theta n which is given by this usual sum of square of error term. But again instead of thinking of J as a function of these n+1 numbers, I'm going to more commonly write J as just a function of the parameter vector theta so that theta here is a vector. 


   이것은 다변수 선형 회귀의 가설 공식이고, x0 = 1입니다. 파라미터는 θ0, θ1,..., θn까지 있습니다. 벡터화 구현을 할 경우 파라미터 벡터 θ = [θ0, θ1,..., θn]입니다. 0-인덱스드 벡터이므로 n+1차원 벡터입니다. 파라미터는 벡터 그 자체입니다. 비용 함수 J(θ0, θ1,..., θn)는 오차의 제곱에 대한 합계입니다. 비용 함수 J(θ0, θ1,..., θn)를 벡터화 구현으로 비용 함수 벡터 J(θ)로 표현할 수 있습니다. 파라미터 벡터 θ는 벡터이므로 비용 함수 J(θ)는 벡터입니다. 



   Here's what gradient descent looks like. We're going to repeatedly update each parameter theta j according to theta j minus alpha times this derivative term. And once again we just write this as J of theta, so theta j is updated as theta j minus the learning rate alpha times the derivative, a partial derivative of the cost function with respect to the parameter theta j. 


   여기 경사 하강법이 있습니다.


   경사 하강법 알고리즘은 'θj = θj - α*미분항'을 반복해서 업데이트합니다. 파라미터 θ0,θ1,... , θn를 벡터화 구현으로 파라미터 벡터 θ = [θ0,θ1,... , θn]으로 표현하면, J(θ0,θ1,... , θn)을 벡터화 구현으로 J(θ)로 표현할 수 있습니다. 그래서 'θj - α * 미분항'은 파라미터 θj에 대응하는 비용 함수의 편미분입니다. 



   Let's see what this looks like when we implement gradient descent and, in particular, let's go see what that partial derivative term looks like. Here's what we have for gradient descent for the case of when we had n=1 feature. We had two separate update rules for the parameters theta0 and theta1, and hopefully these look familiar to you. And this term here was of course the partial derivative of the cost function with respect to the parameter of theta0, and similarly we had a different update rule for the parameter theta1.There's one little difference which is that when we previously had only one feature, we would call that feature x^(i) but now in our new notation we would of course call this x^(i) 1to denote our one feature. So that was for when we had only one feature.


   여기 n=1 일 때 경사 하강법 공식이 있습니다. n=1이므로 피처의 개수는 하나입니다. 파라미터 θ0와 θ1 각각에 대한 경사 하강 업데이트 공식입니다. 경사 하강법 공식은 이미 배웠습니다. 



   이 식은 파라미터 θ0에 대한 비용 함수 J(θ)의 편미분입니다. 



   이 식은 파라미터 θ1에 대한 업데이트 공식입니다. 단일 피처를 표기하기 위해 새로운 표기법이 필요합니다. 새로운 표기법 x^(i)1입니다. 이것은 단지 하나의 특징만을 가진 경우입니다. 




   Let's look at the new algorithm for we have more than one feature, where the number of features n may be much larger than one. We get this update rule for gradient descent and, maybe for those of you that know calculus, if you take the definition of the cost function and take the partial derivative of the cost function J with respect to the parameter theta j, you'll find that that partial derivative is exactly that term that I've drawn the blue box around. And if you implement this, you will get a working implementation of gradient descent for multivariate linear regression.


   오른쪽에 하나 이상의 피처를 가진 경사 하강 알고리즘이 있습니다. 'n >= 1'인 경우입니다. 미적분을 아는 사람들은 비용 함수의 정의와 비용 함수 J(θ)의 편미분을 찾을 수 있습니다. 

   다변수 선형 회귀에 대한 경사 하강법입니다. 




    The last thing I want to do on this slide is give you a sense of why these new and old algorithms are sort of the same thing or why they're both similar algorithms or why they're both gradient descent algorithms. Let's consider a case where we have two features or maybe more than two features, so we have three update rules for the parameters theta0, theta1, theta2 and maybe other values of theta as well. If you look at the update rule for theta0, what you find is that this update rule here is the same as the update rule that we had previously for the case of n = 1. And the reason that they are equivalent is, of course, x^(i) 0 = 1 convention, which is why these two term that I've drawn the magenta boxes around are equivalent. Similarly, if you look the update rule for theta 1, you find that this term here is equivalent to the term we previously had, or the equation or the update rule we previously had for theta1, x1 to denote our first feature, and now that we have more than one feature we can have similar update rules for the other parameters like theta2 and so on. 


   마지막으로 설명할 것은 새로운 알고리즘과 기존 알고리즘이 같다는 것입니다. 두 알고리즘은 모두 경사 하강 알고리즘입니다. 두 개 이상의 특징을 가진 가설과 경사 하강법 알고리즘을 정리합니다.  



   가설 함수 h(x) = θ0x0 +  θ1x1 + θ2x2  (x0=1)입니다. 따라서, 파라미터  θ0, θ1, θ2에 대한 세 가지 업데이트 공식이 있습니다. 피처가 더 있다면 θ의 다른 값들도 있을 수 있습니다. n>1 일 때 θ0 업데이트 공식은 기존의 n=1일 때의 θ0 업데이트 공식과 같습니다. 그 이유는 'x^(i) 0 = 1'이기 때문입니다. 이것이 분홍색 박스가 동일한 이유입니다. 이와 유사하게, θ1의 업데이트 공식은 기존 n=1 일 때 θ1의 업데이트 공식과 동일합니다. 단지 첫 번째 피처를 표기하는 x^(i)1의 차이입니다. 두 번째 피처를 표기하는 x^(i)2 도 마찬가지입니다. 


   There's a lot going on on this slide so I definitely encourage you if you need to to pause the video and look at all the math on this slide slowly to make sure you understand everything that's going on here. But if you implement the algorithm written up here then you have a working implementation of linear regression with multiple features.


   이번 강의에서 많은 것을 다뤘습니다. 잠시 영상을 멈추고 수학 공식을 천천히 살펴보면서 이해의 시간을 갖는 것이 좋습니다. 만약 여기의 알고리즘을 이해한다면, 다변수의 선형 회귀의 구현이 동작하는 환경을 구축할 수 있습니다. 



정리하며

   일변수의 비용 함수 J(θ)에 대한 경사 하강법 업데이트 공식과 다변수의 비용 함수 J(θ0, θ1, θ2, θ3,... , θn)에 대한 경사 하강법 업데이트 공식은 동일합니다. 다변수의 경사 하강법은 처음부터 행렬 곱셈을 염두에 두고 생각합니다. 


          hθ(x)= θ0x0 + θ1x1 + θ2x2 + θ3x3 + θ4x4 (x0=1)

          hθ(x) θ^TX


   파라미터 θ0, θ1, θ2, θ3,... , θn 은 행렬 θ이고 변수 x0, x1, x2, x3,... , xn은 행렬 X입니다. 파라미터와 변수의 곱으로 가설 함수 hθ(x)를 표현하기 위해서 파라미터 행렬 θ 를 전치합니다. 따라서 hθ(x) = θ^TX로 단순화하여 표현할 수 있습니다. 


   가설 함수에 대한 비용 함수 J(θ)의 경사 하강법을 살펴봅니다. 경사 하강법 함수는  'θj - 학습 비율 α * 미분항'입니다. 일변수 가설 함수는 θ0와 θ1을 업데이트하고, 다변수 가설 함수는 θ0와 θ1 뿐만 아니라 모든 θn에 대한 업데이트가 발생합니다. 업데이트를 반복할 때 θ0의 업데이트 공식(Rule)에서 'x^(i) 0 = 1' 이므로 일변수 가설 함수의 경사 하강법과 결과값이 동일합니다. 


매거진의 이전글 앤드류 응의 머신러닝 옥타브 실습 1: 프로그램 설치
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari