블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

'AI Deep Learning/ML by Andrew Ng'에 해당되는 글 3

  1. 2018.07.19 [Machine Learning - NG] Linear Algebra - 선형 대수학1
  2. 2018.07.19 [Machine Learning - NG] Gradient Descent
  3. 2018.07.16 [Machine Learning - NG] Supervised Learning

선형 대수학 강좌를 정리한다. 




Matrix vs Vector vs Scalar


Matrix = m * n = 행*렬 = row * column

Vector = m * 1 형태의 Matrix  = 크기와 방향을 가짐

Scalar = Matrix * 상수 = 크기만 가짐



Matrix Vector Multiplication (곱)


[ m * n ] [ n * 1 ] = [ m * 1 ]


가설 방정식을 이와 같이 표현할 수 있음. 예로 집값 예측 방정식을 수식으로 표현하면 4차원 벡터가 (4 dimensional vector) 나온다. 





Matrix Matrix Multiplication


matrix vector 곱의 결과를 모아 놓은 형태로 보면됨.



결과는 다음의 형태가 된다. 




각 가설을 다시 matrix matrix multiplication으로 만들면 각 가설의 결과값이 column으로 나온다.




Matrix properties (속성)


A, B가 일반 행렬이고, I 가 항등 행렬이라고 하면 


- 교환 법칙:  A * B != B * A  성립하지 않는다. 

- 행렬곱 주의: A * (B * C) = (A * B) * C

- 항등행렬: n*n 으로 행과 열이 같으면서 대각선은 1이고 나머지는 0인 행렬 (identity matrix),  A * I = I * A = A




Inverse matrix (역행렬)


Matrix을 inverse해서 역행렬하고 matrix과 곱하면 항등행렬을 얻는다. 이때 역행렬을 얻을 수 있는 것은 정방행렬(행과 열의 수가 같은 행렬)뿐이다. 



역행렬을 가질 수 없는 예로 0으로 채워진 행렬은 역행렬을 가질 수 없다. 이를 singular matrix 또는 degenerate matrix라 칭한다. 



Matrix transpose (전치행렬, transposition)


m * n 을 n * m으로 만들기





참조


- 앤드류응 교수의 선형대수학 강좌

posted by 윤영식

앤드류 응교수님의 ML 강좌를 정리한다.





Gradient Descent


hypothesis 함수를 적절한 parameter를 가지고 검증할 필요가 있다. 아래 그림과 같이 특정 파라미터 값에 따라 하강한 위치가 틀려 질 수 있다. cost 함수를 최소값을 찾는 것인데, 잘 못된 곳으로 내려가면 해당 값이 최소인것으로 오인할 수 있다. 


- 하강의 스텝(learning rate, 얼마의 보폭으로 하강하는지 정도)은 알파이다. 

- 하강의 방향(direction)-기울기-는 미분(derivative)으로 계산한다.

- starting point가 틀리면 도착한 지점이 틀리게 나왔다. 



- 수학적으로 Gradient descent algorithm을 사용한다. 

   알파: Learning rate * 미분계수

   := 대입기호, = truth assertion

   temp0, temp1을 구한다음 세타0, 세타1에 대입한다. 







미분계수를 이용한 Gradient Descent 알고리즘 이해 


함수의 tanzent값을 구함. 즉, 기울기를 구함. 


- 세타1이 최소값보다 클때 미분계수에 의한 탄젠트 기울기값은 양수이다. 

- 세타1이 최소값보다 작을때 미분계수에 의한 탄젠트 기울기값은 음수이다. 




- 알파(learning rate)에 따라 하강 step이 정해진다.

  + 알파값이 작으면 최소값을 찾는데 느리고

  + 알파값이 크면 최소값을 못 찾고 멀어진다. 




- 하강 기울기가 줄어들 때마다 세타1의 값이 점점 작아진다. 이것은 공식에서 세타1 - 알파*미분계수(기울기 slop값) 을 빼주기 때문에 가능하다. 알파값은 상수 유지 가능.

- J(세타1) 이라는 cost function 그래프에서 미분계수값이 최소가 되는 곳의 세타1의 값을 알 수 있다.

즉, 미분계수 0을 찾는게 목적이다. 


세타1 := 세타1 - 알파 * 0 







비용함수와 기울기 하강 함수를 같이 사용하기


Gradient Descent 알고리즘과 비용 함수 J(세타1, 세타2)




- 좌측 좌표가 가운데로 하강을 할 수록 기울기가 점점 변경된다. 선형 회귀는 항시 하나의 최적값을 갖는 밥그릇의 Convex(볼록) 모양을 갖는다. 




결론 Cost function j의 해답을 얻기 위해 하강 기울기 알고리즘을 사용한다. 






Gradient Descent for Linear Regression


선형 회귀의 경우 새로운 형태의 Gradient Descent방정식이 도출된다. 

 - m: training set 사이즈

 - 세타0: 초기시작값 - 상수

 - 세타1: 기울기값 - 변경값 

 - xi, yi: training data set



Gradient Descent 식에 J(세타)즉 cost founction을 넣어서 식을 간략히 하면 다음과 같이된다. 





참조


posted by 윤영식

앤드류응 교수님의 코세라 강좌를 정리한다. 





Supervised Learning


지도학습과 자율학습에 대한 개념이해 


지도학습

- 정답을 주고 학습

- regression과 classification이 있음

- regression: 여러 개 중 하나 

- classification: 0 또는 1

In supervised learning, we are given a data set and already know what our correct output should look like, having the idea that there is a relationship between the input and the output.


Supervised learning problems are categorized into "regression" and "classification" problems. In a regression problem, we are trying to predict results within a continuous output, meaning that we are trying to map input variables to some continuous function. In a classification problem, we are instead trying to predict results in a discrete output. In other words, we are trying to map input variables into discrete categories.


Example 1:


Given data about the size of houses on the real estate market, try to predict their price. Price as a function of size is a continuous output, so this is a regression problem.


We could turn this example into a classification problem by instead making our output about whether the house "sells for more or less than the asking price." Here we are classifying the houses based on price into two discrete categories.


Example 2:


(a) Regression - Given a picture of a person, we have to predict their age on the basis of the given picture


(b) Classification - Given a patient with a tumor, we have to predict whether the tumor is malignant or benign.


예) Housing Prices 예측 - regression 문제



linear regression


training set을 통해 학습 알고리즘을(learning algorith) 적용해서 가설식(hypothesis)을 만든다. 



cost function

- 가설의 정확도를 측정하기 위해 사용한다

- 공식: 가설의 결과값 - 실제값의 제곱의 합 

- 점진적 감소 (gradient descent)를 위해 1/2를 사용한다. 

- 정확도 측정 J 함수를 얻음.




 세타0을 값 zero로 놓고 간략히 표현한 수식

-  x 값을 줄 때 시작점 세타0과 기울기 세타1의 상수 값을 찾아야 한다. 

- 비용함수인 J(세타0, 세타1)에서 세타0 을 zero로 놓으면 시작점이 zero부터 시작해서 비용함수 J(세타1)을 구하는 것이다. 





- J 공식 결과가 0 이면 가장 작은 비용을 들인 경우이다. 이때 J(세타1)에서 세타1의 값이 1일 때 비용이 전부 0으로 나온다. 



- 만일 세타1값이 0.5라면 cost function 처리 값은 다음과 같이 0.58이 나온다. 



- 음수의 영역을 이동하면 cost function 은 가설값 - 실제값의 제곱의 합이므로 다음과 같이 등고선 그래프가 나온다. 





등고선 그래프로 세타0, 세타1 눈으로 확인하기 


등고선을 입체적으로 보면 다음과 같다. 

- 수평선은 세타1, 세타0 이다.

- 수직선은 cost function의 결과값 J(세타0, 세타1) 이다.

세타1=기울기, 세타0은 시작점



이것을 다시 평면으로 H(x) 와 비교하기위해 평면 등고선을 가지고 눈으로 세타0, 세타1의 값을 가지고 가설의 선을 검증할 수 있다. 

예1) 같은 등고선에 있는 3개의 x는 값은 높이 값, 즉 비용함수 결과값을 갖는다. 이것을 h(x)상에 세타0, 세타1을 이용해 그리면 다음과 같이 나온다. 

데이터셋과 h(x) 선의 거리가 전혀 맞아 들어가지 않아 가설 선이 맞지 않음을 알 수 있다. 


예2) 좀 더 중심으로 이동한 빨간색 x는 등고선에서 세타0=360, 세타1=0 값을 가즌다. 이를 세타1=기울기, 세타0은 시작점으로 보면 h(x)는 수평선이 나온다. 이동 실 데이터의 거리값이 작지 않음을 보여준다.



중심으로 이동할 때 다음과 같이 최적의 가설 세타0과 세타1을 얻을 수 있다. 

세타0 = 250

세타1 = 0.12







Unsupervised

자율학습

- 확인된 답을 제공하지 않음 

   예) 고객이 어디 고객군에 속하는지 알 수 없다. 천문학등

- clustringr과 non-clustering 

Unsupervised learning allows us to approach problems with little or no idea what our results should look like. We can derive structure from data where we don't necessarily know the effect of the variables.


We can derive this structure by clustering the data based on relationships among the variables in the data.


With unsupervised learning there is no feedback based on the prediction results.


Example:


Clustering: Take a collection of 1,000,000 different genes, and find a way to automatically group these genes into groups that are somehow similar or related by different variables, such as lifespan, location, roles, and so on.


Non-clustering: The "Cocktail Party Algorithm", allows you to find structure in a chaotic environment. (i.e. identifying individual voices and music from a mesh of sounds at a cocktail party).





참고


- 앤드류 응교수님의 머신러닝 강좌

- 회귀 분석 예제

- 선형 회귀 분석 데이터 이해



posted by 윤영식
prev 1 next