brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Oct 12. 2020

앤드류 응의 머신러닝 (4-7):정규 방정식의 비가역성

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


Linear Regression with Mutiple Variables

다변수 선형회귀


Computing Parameters Analytically 

(분석적 파라미터 연산)  


Normal Equation Noninvertibility (정규 방정식 비가역성)


   In this video I want to talk about the Normal equation and non-invertibility. This is somewhat more advanced concept, but it's something that I've often been asked about. And so I want to talk it here and address it here. But this is a somewhat more advanced concept, so feel free to consider this optional material. And there's a phenomenon that you may run into that may be somewhat useful to understand, but even if you don't understand the normal equation and linear progression, you should really get that to work okay.


   이번 강의는 정규 방정식에서 역행렬이 없는 경우를 설명합니다. 고급 과정으로 다소 어렵지만 부담 없이 들을 수 있습니다. 옵션 과정이므로 전체 과정을 이해하는 데 문제가 없습니다. 이번 강의를 이해하지 못해도 정규 방정식과 선형 회귀 문제를 해결하는 것에 전혀 어려움이 없기 때문입니다.  



   Here's the issue. For those of you there are, maybe some are more familiar with linear algebra, what some students have asked me is, when computing this Theta equals X transpose X inverse X transpose Y. What if the matrix X transpose X is non-invertible? So for those of you that know a bit more linear algebra you may know that only some matrices are invertible and some matrices do not have an inverse we call those non-invertible matrices. Singular or degenerate matrices. The issue or the problem of x transpose x being non invertible should happen pretty rarely. And in Octave if you implement this to compute theta, it turns out that this will actually do the right thing. 


   선형 대수에 익숙한 학생들은 θ = (X'X)^-1X'y를 계산할 때, 행렬 X'X의 역행렬이 없으면 어떻게 되는 지를 묻습니다. 선형대수를 배운 사람들은 역행렬을 구할 수 있는 행렬은 한정되어 있고 역행렬이 없는 행렬도 있다는 것을 압니다. 역행렬을 구할 수 없는 행렬을 수학적으로 비가역 행렬, 특이 행렬 또는 degenerate matrix라고 부릅니다. 사실 X'X의 역행렬이 없는 경우는 거의 없고, 옥타브 프로그램은 이런 경우에도 파라미터 θ에 대한 올바른 값을 구합니다. 


   I'm getting a little technical now, and I don't want to go into the details, but Octave hast two functions for inverting matrices. One is called pinv, and the other is called inv. And the differences between these two are somewhat technical. One's called the pseudo-inverse, one's called the inverse. But you can show mathematically that so long as you use the pinv function then this will actually compute the value of data that you want even if X transpose X is non-invertible. The specific details between inv. What is the difference between pinv? What is inv? That's somewhat advanced numerical computing concepts, I don't really want to get into. But I thought in this optional video, I'll try to give you little bit of intuition about what it means for X transpose X to be non-invertible. For those of you that know a bit more linear Algebra might be interested. 


   수학적인 내용을 너무 깊게 설명할 생각은 없습니다. 옥타브 프로그램은 역행렬을 구하는 2 개의 함수 또는 명령어를 제공합니다. 하나는 pinv(), 다른 하나는 inv()입니다. 두 함수는 엄밀하게 차이점이 있습니다. 하나는 pseudo-inverse라 부르고, 하나는 inverse라 부릅니다. pinv 함수는 X'X의 역행렬이 없더라도 수학적으로 정확히 θ값을 계산합니다. inv와 pinv의 자세한 차이점은 어려운 수치 계산에 관한 개념이므로 다루지 않습니다. 



   I'm not gonna prove this mathematically but if X transpose X is non-invertible, there usually two most common causes for this. 


   하지만, 이번 강의에서 X'X의 역행렬이 없다는 의미가 무엇인지 설명합니다. 선형대수를 아는 사람들에게 매우 흥미로운 주제입니다.  




   The first cause is if somehow in your learning problem you have redundant features. Concretely, if you're trying to predict housing prices and if x1 is the size of the house in feet, in square feet and x2 is the size of the house in square meters, then you know 1 meter is equal to 3.28 feet Rounded to two decimals. And so your two features will always satisfy the constraint x1 equals 3.28 squared times x2. And you can show for those of you that are somewhat advanced in linear Algebra, but if you're explaining the algebra you can actually show that if your two features are related, are a linear equation like this. Then matrix X transpose X would be non-invertable.


   수학적으로 증명하지 않겠지만 X'X가 역행렬이 없는 이유는 대체로 두 가지 경우입니다. 첫 번째 경우는 학습 문제에 불필요한 피처가 있을 때입니다. 예를 들면, 주택 가격을 예측하기 위한 피처 x1은 주택 크기로 평방피트 단위이고, 피처 x2는 주택 크기로 제곱미터 단위입니다. 두 자릿수까지 반올림하여 1 m = 3.28 feet입니다.  두 개의 Feature는 x1 = (3.28)^2 * X2의  관계를 가집니다. 선형대수 전문가들은 두 Feature가 서로 연관된 같은 선형 방정식이라는 것이 행렬 X'X 역행렬이 없다는 것을 증명합니다.   


   The second thing that can cause X transpose X to be non-invertable is if you are training, if you are trying to run the learning algorithm with a lot of features. Concretely, if m is less than or equal to n. For example, if you imagine that you have m = 10 training examples that you have n equals 100 features then you're trying to fit a parameter back to theta which is, you know, n plus one dimensional. So this is 101 dimensional, you're trying to fit 101 parameters from just 10 training examples. This turns out to sometimes work but not always be a good idea. Because as we'll see later, you might not have enough data if you only have 10 examples to fit you know, 100 or 101 parameters. We'll see later in this course why this might be too little data to fit this many parameters. But commonly what we do then if m is less than n, is to see if we can either delete some features or to use a technique called regularization which is something that we'll talk about later in this class as well, that will kind of let you fit a lot of parameters, use a lot features, even if you have a relatively small training set. But this regularization will be a later topic in this course.


   X'X가 역행렬이 없는 두 번째 경우는 너무 많은 피처를 가진 학습 알고리즘을 다룰 때입니다. 예를 들면,  m이 n보다 작거나 같을 때입니다. 즉, 학습 데이터 셋의 개수 m = 10이고, 피처의 개수 n = 100일 때 , 파라미터 벡터 θ는 (n+1) 차원이므로 101차원입니다. 10개의 학습 데이터로 101개의 파라미터를 표현하는 것은 가끔 잘 동작하더라도 좋은 상황은 아닙니다. 데이터가 충분하지 않습니다.  왜 데이터가 적으면 많은 파라미터를 표현할 수 없는지는 뒤에서 다루겠습니다. m이 n보다 작을 때는 피처를 줄이거나 정규화(regularization)를 합니다. 정규화는 많은 피처를 사용하지만 파라미터의 값을 줄여서 적은 학습 데이터 셋으로 알고리즘이 잘 동작하게 만들어 줍니다. 정규화는 이 과정의 후반에 배울 것입니다. 


   But to summarize if ever you find that x transpose x is singular or alternatively you find it non-invertable, what I would recommend you do is first look at your features and see if you have redundant features like this x1, x2. You're being linearly dependent or being a linear function of each other like so. And if you do have redundant features and if you just delete one of these features, you really don't need both of these features. If you just delete one of these features, that would solve your non-invertibility problem. And so I would first think through my features and check if any are redundant. And if so then keep deleting redundant features until they're no longer redundant. And if your features are not redundant, I would check if I may have too many features. And if that's the case, I would either delete some features if I can bear to use fewer features or else I would consider using regularization. Which is this topic that we'll talk about later.


   정리하면, X'X가 특이 행렬이거나 역행렬이 없을 경우 불필요한 피처가 있는지 찾아봅니다. 선형적으로 종속된 피처들은 제거합니다. 예를 들어, 주택의 크기를 평방피트로 표시한 것과 제곱미터로 표시한 데이터가 있다면 하나를 지우면 역행렬이 없는 문제를 해결할 수 있습니다. 피처가 많다면 불필요한 피처를 지우거나 정규화를 고려합니다. 정규화는 나중에 다루겠습니다.


   So that's it for the normal equation and what it means for if the matrix X transpose X is non-invertable but this is a problem that you should run that hopefully you run into pretty rarely and if you just implement it in octave using P and using the P n function which is called a pseudo inverse function so you could use a different linear out your alive in Is called a pseudo-inverse but that implementation should just do the right thing, even if X transpose X is non-invertable, which should happen pretty rarely anyways, so this should not be a problem for most implementations of linear regression.


   지금까지 정규 방정식에서 행렬 X'X가 역행렬이 없다는 것이 무엇을 의미하는지를 설명했습니다. 하지만, 역행렬이 없는 경우는 거의 없습니다. 옥타브 프로그램은 pinv()라는 pseudo-inverse 함수가 있습니다. 역행렬이 없는 경우에도 pseudo-inverse 함수를 사용하면 해결할 수 있습니다. 왜냐하면 pinv 함수는 X'X의 역행렬이 없어도 올바르게 계산을 하기 때문입니다. 사실 그런 경우가 거의 일어나지 않기 때문에 선형 회귀 구현에서 크게 문제가 되지는 않을 것입니다.


앤드류 응의 머신 러닝 동영상 강의



정리하며 - 정규 방정식의 비가역성

   정규 방정식은 J(θ)의 최소값에 해당하는 파라미터 θ를 한 번에 구합니다. 공식은 다음과 같습니다. 


     hθ(X) = θ0x0 + θ1x1 + θ2x2 + θ3x3 + θ4x4 +... 일 때

                 = θ^T*X  


   이때, θ를 구하는 공식은 다음과 같다

             θ = (X^T *X)^-1 * X^T * y 


   θ 를 구하는 공식에서 행렬 (X'X)의 역행렬이 없는 경우가 있습니다. 

    1) 불필요한 Feature가 많을 경우 (두 Feature가 선형 방정식으로 연결될 때)

    2) Feature의 개수가 훈련용 데이터의 개수보다 훨씬 많은 경우  


   옥타브 프로그램에서 역행렬을 구하는 함수는 두 pinv()와 inv()가 있습니다. pinv() 함수는 역행렬이 없는 행렬을 가지고도 정확하게  θ 의 값을 구합니다.


             pinv(X'*X)*X'*y


매거진의 이전글 앤드류 응의 머신러닝 (4-6) : 정규 방정식
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari