본문 바로가기
수학

[ML / 선형대수] Linear Transformation in Neural Network

by Sangwook.Aaron.Kim 2020. 2. 9.

이번 포스트에서는 선형대수에서 Linear Transformation ( 선형 변환 ) 이 신경망 ( Neural Network ) 에서 어떻게 사용되는지 정리를 해보도록 하겠습니다. 

 

What is Linear Transformation ?

선형대수에서 말하는 선형결합은 함수 입니다. 다시 말하면 하나의 input vector 를 받아 transformation 을 하여 특정 dimension 의 vector 로 변환할 수 있는 함수입니다. 예를 들어 2차원 vector 를 input 으로 받아서 3 차원의 vector 로 변환을 할 수 있다면 이것은 dim 2 -> dim 3 선형변환이 될 수 있겠습니다.

또 다른 관점으로는 2차원의 vector 를 3 차원으로 투영시킨다는 의미에서 mapping 이라는 단어도 transformation 과 같은 의미를 갖습니다.  

Linear Transformation in Neural Network

그러면 linear transformationNeural Network 에서 어떻게 사용되는지 살펴보겠습니다.

Fully Connected Layer in Neural Network

먼저 Neural Network 에서 Fully Connected layer 를 생각해봅시다. Fully Connected Layer 는 신경망을 통해서 나온 결괏값들이 ( output nodes )  우리가 분류 ( 혹은 다른 문제 ) 하고자 class 로 출력되는 layer 를 말합니다. 

 

Image 에서 나온 Node 들을 fully connected layer ( Affine Layer ) 로 mapping 하는 방식 [출처 : 인공지능을 위한 선형대수 ( 강사 : 주재걸, edwith ) ]

위에 나와있는 그림을 보면 input 과 output layer 2개만을 갖고 있는 Neural Network 를 확인할 수 있습니다. Input layer 는 고양이 사진의 pixel 값을 input 으로 받고 있고, output layer 는 신경망을 통해 출력되어 마지막 3개의 node 로 mapping 되어 나타내어 집니다. 또한 input layer 에는 하나의 bias term 도 추가가 되어 ( 검은색 node ) 신경망을 구성하고 있습니다.

 

원래 Linear Transformation 은 bias term 이 추가가 되지 않은 형태로 나타내어 집니다. 하지만 bias term 이 추가되는 경우에는 Affine Transformation 이라고 말하며 별도의 node 를 추가하여 나타내주어 합니다. ( Linear Transformation 인 경우에는 Fully Connected Layer 라고 하지만 bias term 이 추가된 Affine Transformation 인 경우에는 Affine Layer 라고 대신해서 부르기도 합니다. )

 

이 때, bias term 의 coefficient 를 1로 설정한 후 다른 node 들과 같이 matrix 를 구성하여 나타낼 수 있습니다. 오른쪽 하단의 3 x 5 matrix 을 보면 기존 input node 의 3 x 4 matrix 와 3 x 1 matrix 인 bias term 을 동시에 나타내었습니다. 

 

위의 그림을 보면 Neural Network 는 결국 linear transformation 으로 나타내어 차원의 변환을 matrix 로 나타낼 수 있다는 것을 확인할 수 있습니다. 

 

Non-Linearity + Linear Transformation

Neural Network 에는 Linear 한 요소만 있는 것은 아닙니다. Output layer 에 non-linear 한 요소 ( ReLU, Tanh ... ) 를 추가하여 결과를 표현할 수 있습니다. 이러한 non linearity 는 Neural Network 에서 데이터의 non linearity 를 잘 표현할 수 있습니다. 

 

먼저 Linear transformation 이 공간을 변형한다는 개념을 이해해야 합니다. 2 차원 공간에서 unit vector 가 linear transformation 을 통해 다른 vector 로 mapping 이 되면 이 때 새로 생긴 vector를 새로운 공간에서의 unit vector 라고 생각할 수 있습니다. 이렇게 되면 vector space 의 distortion 이 일어나게 되어 linear transformation 의 결과 set 혹은 subspace 를 표현하게 됩니다.

 

이렇게 linear transformation 을 진행하게 되면 vector space distortion 이 일어나게 되어 기존의 feature representation 과 다르게 표현되는데 여기에 non-linearity 를 추가하게 되면 이에 해당하는 결과 feature 가 표현됩니다. 

 

Vector Space distortion with Linear Transformation + Non Linearity [ 출처 ; Neural Networks, Manifolds, and Topology ( colah's blog ) ]

 

위의 그림은 기존에 서로 직교하는 X,Y 축으로 표현되는 좌표 공간을 Linear Transformation 과 Non-linearity 를 추가하여 space distortion 이 일어난 것을 보여줍니다. 가로, 세로로 표현되는 두 개의 축이 서로 직교하지 않는 것은 기존 좌표계에서 unit vector들이 새로운 공간에서 다르게 표현되는 것을 보여주며 0 에서 멀어질수록 각 칸의 간격이 좁아지는 것은 Sigmoid 함수의 특성을 반영한 것을 확인할 수 있습니다. 이렇게 Non linearity 를 추가하게 되면 Linear transformation 으로만 표현할 수 없었던 feature representation 이 가능하게 됩니다. 

 

Feature Representation with Neural Network with non linearity [ 출처 ; Neural Networks, Manifolds, and Topology ( colah's blog ) ]

 

 

댓글