글과 사진, 그리고 이야기

IE & SWCON/Machine Learning

Kernel Trick

뱃놀이가자 2023. 11. 27. 22:12
728x90

kernel methods에서는 선형적으로 분류가 불가능한 모델에 대해서 일종의 mapping function을 사용해 차원을 확장시키고 그 후 역과정을 거쳐 boundary를 만들게 된다. 선형대수학에서 배운 개념인 linear transformation을 생각하면 쉽다.

 

보통 mapping function은 pi 로 나타냄

 

theta에 대해서는 선형성을 가지므로 최소제곱법 알고리즘을 사용할 수 있고 x에 대해서는 비선형적이라서 모델 내 데이터를 꽤 복잡하게 만들도록 핸들링할 수 있다.

 

다음과 같은 trick을 사용하면 시간을 줄이게 된다.

 

kernel trick은 지도학습과 비지도학습 모두에서 사용할 수 있다. 

-지도학습에서 아주 강력하다고 볼 수 있음-

kernel trick은 복잡한 비선형 feature를 아주 약간의 추가적인 계산비용만으로 문제를 해결할 수 있도록 한다는 점에서 꽤 의미가 있는 방법이다.

 


 

Kernel Trick in SVMs

 

 

커널에 대한 다양한 해석관점

-1. 내적

-2. x와 z의 기하학적 각도

-3. x와 z의 코사인 유사도

 

svm.SVC의 주요 메서드

kernel : {'linear', 'poly', 'rbf', 'sigmoid', 'precomputed'} or callable,          default='rbf'
    Specifies the kernel type to be used in the algorithm.
    If none is given, 'rbf' will be used. If a callable is given it is
    used to pre-compute the kernel matrix from data matrices; that matrix
    should be an array of shape ``(n_samples, n_samples)``.

degree : int, default=3
    Degree of the polynomial kernel function ('poly').
    Must be non-negative. Ignored by all other kernels.

gamma : {'scale', 'auto'} or float, default='scale'
    Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.

    - if ``gamma='scale'`` (default) is passed then it uses
      1 / (n_features * X.var()) as value of gamma,
    - if 'auto', uses 1 / n_features
    - if float, must be non-negative.

    .. versionchanged:: 0.22
       The default value of ``gamma`` changed from 'auto' to 'scale'.

 

Linear Kernel 과 Polynomial Kernel

 

polynomial kernel은 계산속도에 있어서 d^p차원을 d차원으로 계산하는 효과를 가진다.

 

polynomial에서 하이퍼파라미터는

clf = svm.SVC(kernel='poly', degree=3, gamma=2)

이정도만 사용함.

 

degreer가 위의 식에서 p에 해당하는 값이다. 참고로 3으로 설정했을때 mapping은 다음과 같이 이루어진다. 

 

추가로 RBF, Radial Basis Functional Kernel (sometimes called: Gaussian)

이 경우 hyperparameter로 gamma가 사용되는데 가우시안 구의 크기를 제한하는 매개변수로 이해할 수 있다.

gamma=2, 0.2 (좌, 우)

 


 

Pros and Cons of Kernels

 

커널은 매우 큰 차원의 매핑함수를 사용한다

그러나 계산 속도는 겨우 얼마되지 않으며

추정 해를 구하는 것은 빠르게 구해지나 방대한 데이터셋에서는 살짝 힘들 수도 있다

중간 이하의 데이터 사이즈를 가지는 경우 kernel method는 신경망만큼 아주 좋은 성능과 train 학습에도 아주 좋다.

 

 

728x90