2. 2
▣ Lecture 2: supervised learning
▲ Machine learning : supervised learning, unsupervised learning, etc.
▲ Supervised learning: linear regression, logistic regression (classification), etc.
1-page Review
Supervised learning
Given a set of labeled example,
𝐷𝐷 = (𝑥𝑥𝑡𝑡, 𝑦𝑦𝑡𝑡) 𝑡𝑡=1
𝑁𝑁
, learn a mapping 𝑓𝑓: 𝑋𝑋 → 𝑌𝑌
which minimizes L(�𝑌𝑌 = 𝑓𝑓 𝑋𝑋 , 𝑌𝑌)
Unsupervised learning
Given a set of unlabeled example,
𝐷𝐷 = (𝑥𝑥𝑡𝑡) 𝑡𝑡=1
𝑁𝑁
, learn a meaningful
representation of the data
Linear regression
ℎ𝜽𝜽 𝒙𝒙 = ∑𝑖𝑖=0
𝑛𝑛
𝜃𝜃𝑖𝑖 𝑥𝑥𝑖𝑖 = 𝜽𝜽𝑇𝑇
𝒙𝒙
𝐽𝐽 𝜽𝜽 =
1
2
Σ𝑖𝑖=1
𝑚𝑚
ℎ𝜽𝜽 𝒙𝒙 𝒊𝒊
− 𝑦𝑦 𝑖𝑖 2
𝜃𝜃𝑗𝑗 ≔ 𝜃𝜃𝑗𝑗 − 𝛼𝛼 ℎ𝜽𝜽 𝒙𝒙 𝑖𝑖
− 𝑦𝑦 𝑖𝑖
⋅ 𝑥𝑥𝑗𝑗
(𝑖𝑖)
ℎ𝜽𝜽 𝒙𝒙 =
1
1+𝑒𝑒−𝜽𝜽 𝑇𝑇 𝒙𝒙
ℎ𝜽𝜽 𝒙𝒙 ≥ 0.5 → 𝑦𝑦 = 1
ℎ𝜽𝜽 𝒙𝒙 < 0.5 → 𝑦𝑦 = 0
𝑙𝑙 𝜽𝜽 = ∑𝑖𝑖=1
𝑚𝑚
𝑦𝑦(𝑖𝑖)
log ℎ𝜽𝜽(𝒙𝒙 𝑖𝑖
) + (1 − 𝑦𝑦 𝑖𝑖
) log(1 − ℎ𝜽𝜽(𝒙𝒙 𝑖𝑖
))
𝜃𝜃𝑗𝑗 ≔ 𝜃𝜃𝑗𝑗 + 𝛼𝛼 𝑦𝑦 𝑖𝑖
− ℎ𝜽𝜽 𝒙𝒙 𝑖𝑖
⋅ 𝑥𝑥𝑗𝑗
𝑖𝑖
Logistic regression
3. ▣ Overfitting?
Training data에 지나치게(over) fit 되어 일반적인 추세를 표현하지 못하는 문제
일반적으로 학습데이터에 지나치게 편향된 복잡한 모델(불필요한 curve)로 인하여 발생
▣ Overfitting을 줄이기 위한 방법
▲ Reduce the number of parameters
(데이터를 표현하는 feature vector를 효과적으로 구성함으로써 data의 dimension 축소)
▲ Regularization (cost function)
3
The problem of overfitting
적절한 hypothesis function은
단순할 함수일 것이라는 가정
4. ▣ Regularization
Parameter의 개수를 유지하되 크기를 감소시킴으로써 overfitting을 피하는 방법
Housing price prediction 문제에서의 hypothesis function:
ℎ𝜽𝜽 𝒙𝒙 = 𝜃𝜃0 + 𝜃𝜃1 𝑥𝑥1 + 𝜃𝜃2 𝑥𝑥2
모델에 따라 under-fitting (high bias) 또는 overfitting (high variance)의 문제가 발생할 수 있다!
4
The problem of overfitting
5. ▣ Regularization
Linear regression에서의 cost function:
𝐽𝐽 𝜽𝜽 =
1
2𝑚𝑚
�
𝑖𝑖=1
𝑚𝑚
ℎ𝜽𝜽 𝒙𝒙 𝒊𝒊
− 𝑦𝑦 𝑖𝑖 2
위 식에서 parameter 𝜽𝜽 의 크기를 줄이는 regularization parameter 𝜆𝜆의 도입
Gradient descent에서 update rule:
5
The problem of overfitting
𝐽𝐽 𝜽𝜽 =
1
2𝑚𝑚
�
𝑖𝑖=1
𝑚𝑚
ℎ𝜽𝜽 𝒙𝒙 𝒊𝒊
− 𝑦𝑦 𝑖𝑖 2
+ 𝜆𝜆 ⋅ �
𝑗𝑗=1
𝑛𝑛
𝜃𝜃𝑗𝑗
2
𝜃𝜃0 ≔ 𝜃𝜃0 − 𝛼𝛼 ℎ𝜽𝜽 𝒙𝒙 𝑖𝑖
− 𝑦𝑦 𝑖𝑖
⋅ 𝑥𝑥0
(𝑖𝑖)
𝜃𝜃𝑗𝑗 ≔ 𝜃𝜃𝑗𝑗 1 − 𝛼𝛼
𝜆𝜆
𝑚𝑚
− 𝛼𝛼
1
𝑚𝑚
�
𝑖𝑖=1
𝑚𝑚
ℎ𝜽𝜽 𝒙𝒙 𝑖𝑖
− 𝑦𝑦 𝑖𝑖
⋅ 𝑥𝑥𝑗𝑗
𝑖𝑖
𝜃𝜃0은 regularize 하지 않음
Update 할 때 parameter의 크기를 감소시키는 효과!
6. ▣ Normal equation의 변화
Linear regression에서의 normal equation :
, where 𝑋𝑋 =
−𝒙𝒙 𝟏𝟏 𝑇𝑇
−
⋮
−𝒙𝒙 𝒎𝒎 𝑇𝑇
−
, 𝜽𝜽 =
𝜃𝜃0
⋮
𝜃𝜃𝑛𝑛
, and 𝒚𝒚 =
𝑦𝑦(1)
⋮
𝑦𝑦(𝑚𝑚)
Regularization parameter를 도입하면
참고: 𝑋𝑋 𝑇𝑇
𝑋𝑋 + 𝜆𝜆 ⋅ 𝐼𝐼의 역행렬은 항상 존재 (𝜆𝜆 > 0)
6
Regularization
Positive semi-definiter (if 𝑚𝑚 ≤ 𝑛𝑛, 𝑋𝑋 𝑇𝑇
𝑋𝑋 is not invertiable)
Positive definite
7. ▣ Validation set의 구성!
▲ 여러 복잡도의 모델을 구성하여 validation 성능을 관찰
ℎ𝜽𝜽 𝒙𝒙 = 𝜃𝜃0 + 𝜃𝜃1 𝑥𝑥1 + ⋯ + 𝜃𝜃𝑑𝑑 𝑥𝑥𝑑𝑑
▲ 적절한 크기의 regularization parameter (𝜆𝜆)를 정해주는 것이 중요!
(𝜆𝜆값이 너무 크면 under-fitting 되는 문제가 발생)
7
Bias vs. Variance
그림 출처: https://www.coursera.org/learn/machine-learning/resources/LIZza
8. 8
▣ Clustering
비슷한 특성의 데이터들을 묶는 알고리즘
▲ K-means algorithm
▲ Spectral clustering
▣ Anomaly detection
▲ Density estimation
Unsupervised learning
EECE695J (2017)
Sang Jun Lee (POSTECH)
K-means
Cluster의 center로부터의
거리를 기준으로 clustering
Spectral clustering
𝐺𝐺(𝑉𝑉, 𝐸𝐸): vertices and edges
얼마나 적은 비용으로 그래
프를 partitioning 할 것인가
9. 9
주어진 𝑁𝑁개의 데이터( 𝒙𝒙𝒕𝒕 ∈ ℝ𝐷𝐷
𝑡𝑡=1
𝑁𝑁
)를 대표하는 𝑘𝑘개의 center 𝝁𝝁𝒋𝒋 (𝑗𝑗 = 1, ⋯ , 𝐾𝐾)를 찾는 문제
→ 데이터들을 𝑘𝑘개의 그룹으로 partitioning
Initialize 𝝁𝝁𝒋𝒋 (𝑗𝑗 = 1, ⋯ , 𝐾𝐾)
Repeat until convergence!
K-means algorithm
EECE695J (2017)
Sang Jun Lee (POSTECH)
Assignment step
Assign all data points to the cluster for which 𝒙𝒙𝒕𝒕 − 𝝁𝝁𝒋𝒋
2
is smallest
Update step
Compute new means for every cluster
𝝁𝝁𝒋𝒋
𝒏𝒏𝒏𝒏𝒏𝒏
=
1
𝑁𝑁𝑗𝑗
Σ𝑡𝑡∈𝐶𝐶𝑗𝑗
𝒙𝒙𝒕𝒕
찾아야하는게 optimal cluster와 optimal mean인데..
mean을 고정하여 optimal cluster를 먼저 찾고
찾은 optimal cluster 안에서
새로운 optimal mean 계산
Not optimal solution!
16. 16
K-means algorithm in TensorFlow
EECE695J (2017)
Sang Jun Lee (POSTECH)
ipython의 웹상에서 그림을 그리기 위한 명령어
두 개의 normal distribution으로
sample data 구성
vectors_set : 2000x2
18. 18
K-means algorithm in TensorFlow
EECE695J (2017)
Sang Jun Lee (POSTECH)
2000개의 샘플 중 k개를 임의로 선
택하여 initial mean 값으로 활용
expanded_vectors: (tensor) 1x2000x2
expanded_centroids: (tensor) 4x1x2
19. 19
K-means algorithm in TensorFlow
EECE695J (2017)
Sang Jun Lee (POSTECH)
Assignment 과정도 값이 들어가는 것이 아니라
일종의 operation에 해당하는 graph
20. 20
K-means algorithm in TensorFlow
EECE695J (2017)
Sang Jun Lee (POSTECH)
실제 학습과정 (100회 반복)
K=4 K=2
21. 21
▣ K-means algorithm의 중요한 두 가지 이슈
▲ Mean의 initialization을 어떻게 할 것인가?
Random initialization:
• N개의 샘플 중 k개를 무작위로 뽑아 초기값으로 사용
• 초기값에 따라 clustering의 결과가 안좋을 수 있음
• Random initialization 과정을 여러 번 수행
▲ Cluster의 개수 k의 결정
일반적으로 cluster의 개수를 데이터의 분포에 따라 사람이 결정하는데..
K-means algorithm
EECE695J (2017)
Sang Jun Lee (POSTECH)
그림 출처: https://wikidocs.net/4693
22. 22
▣ K-means algorithm의 중요한 두 가지 이슈
▲ Cluster의 개수 k의 결정
Elbow method:
• k값에 따른 cost function에 대한 함수를 그렸을 때, 특정 k 이후 cost가 변화가 적은 elbow point를 k값으로 결정
• k값에 따른 cost function의 변화가 smooth 할 경우 elbow point를 찾기 힘들 수 있다.
K-means algorithm
EECE695J (2017)
Sang Jun Lee (POSTECH)
그림 출처: https://wikidocs.net/4693
23. 23
▣ Clustering criteria:
▲ The affinities of data within the same cluster should be high
▲ The affinities of data between different clusters should be low
Spectral clustering
EECE695J (2017)
그림 참조: POSTECH CSED441 lecture13
데이터 샘플 사이의 관련성
24. 24
Optimization objective:
Affinity의 총 합이 최대가 되는 cluster를 찾는 것이 목적 (𝒙𝒙 : 데이터의 cluster 정보를 나타내는 vector)
Convert the discrete optimization problem to continuous domain (+ 𝑥𝑥의 length로 normalize)
Spectral clustering
EECE695J (2017)
Discrete optimization problem
Maximum eigenvalue problem
28. 28
위의 최적화 문제는 결국 아래와 같은 eigenvalue problem으로 바뀌는데..
(Unnormalized) graph Laplacian:
• For every vector 𝒙𝒙 ∈ ℝ𝑛𝑛
, 𝒙𝒙𝑇𝑇
𝐿𝐿𝒙𝒙 =
1
2
∑𝑖𝑖=1
𝑛𝑛
∑𝑗𝑗=1
𝑛𝑛
𝑤𝑤𝑖𝑖𝑖𝑖 𝑥𝑥𝑖𝑖 − 𝑥𝑥𝑗𝑗
2
≥ 0 (positive semi-definite)
• 𝐿𝐿은 단 1개의 𝜆𝜆 = 0을 만족하는 eigenvalue를 가지며, 그 때의 eigenvector는 𝒙𝒙 = 𝟏𝟏 이다.
• 모든 데이터가 하나의 cluster로 묶이는 것은 의미가 없으므로..
Minimum-cut algorithm의 solution은 second minimum eigenvalue에 해당하는 eigenvector
Spectral clustering
EECE695J (2017)
𝐿𝐿 = 𝐷𝐷 − 𝑊𝑊
29. 29
Minimum-cut algorithm:
• Find the second minimum eigenvector of 𝐿𝐿 = 𝐷𝐷 − 𝑊𝑊
• Partition the second minimum eigenvector
참고: Two moon data에서의 second minimum eigenvector
참고:
Minimum-cut algorithm은 optimal solution을 찾는 것이 아니라 optimal solution의 approximation을 찾는다
Spectral clustering
EECE695J (2017)
Second smallest eigenvector
30. 30
K-means
• 알고리즘이 간단!
• 초기값에 따른 알고리즘 결과의 변동성
Spectral clustering
• 계산량이 많음
• 데이터의 cluster가 highly non-convex일 때 효과적!
• 동일 데이터에 대하여 비슷한 결과 도출
K-means vs. spectral clustering
EECE695J (2017)
Reference: POSTECH Machine Learning lecture8
K-means
Spectral
clustering
31. 31
일반적인 데이터가 파란색과 같은 분포를 가질 때,
(빨간색 점들과 같이) 이상한 데이터들을 검출해내는 방법?
Anomaly detection
EECE695J (2017)
32. 32
일반적인 데이터가 파란색과 같은 분포를 가질 때,
(빨간색 점들과 같이) 이상한 데이터들을 검출해내는 방법?
Density estimation using multivariate Gaussian distribution
Anomaly detection
EECE695J (2017)
33. 33
Density estimation using multivariate Gaussian distribution
Parameter fitting:
Given training set {𝑥𝑥 𝑖𝑖
∈ ℝ𝑛𝑛
: 𝑖𝑖 = 1, ⋯ , 𝑚𝑚}
𝜇𝜇 =
1
𝑚𝑚
�
𝑖𝑖=1
𝑚𝑚
𝑥𝑥(𝑖𝑖)
Σ =
1
𝑚𝑚
�
𝑖𝑖=1
𝑚𝑚
𝑥𝑥 𝑖𝑖
− 𝜇𝜇 𝑥𝑥 𝑖𝑖
− 𝜇𝜇
𝑇𝑇
Anomaly if 𝑝𝑝 𝑥𝑥; 𝜇𝜇, Σ < 𝜖𝜖 for given 𝜖𝜖
Anomaly detection
EECE695J (2017)