Contenu connexe
Similaire à 1시간만에 머신러닝 개념 따라 잡기 (20)
1시간만에 머신러닝 개념 따라 잡기
- 1. © 2020, Amazon Web Services, Inc. or its Affiliates.
Sungmin, Kim
AWS Solutions Architect
1시간 만에 머신 러닝
개념 따라잡기
- 2. © 2020, Amazon Web Services, Inc. or its Affiliates.
Agenda
• Machine Learning 이란?
• Machine Learning으로 풀 수 있는 문제 유형
• Machine Learning 작업 순서
• ML 학습을 위한 입력 데이터 가공하기 - Feature Engineering
• ML과 최적화 문제 - Loss Function & Gradient Decent method (경사하강법)
• ML 학습 속도와 모델 성능 향상을 위한 Hyper Parameter Tuning
• 미래를 위한 준비 - 데이터 나누기 (Train/Validation/Test)
• ML 모델 선택 시 주의할 점 - Overfitting vs. Underfitting
• 여러 가지 ML 모델 결합 하기 - Ensemble method
• Deep Learning
- 3. © 2020, Amazon Web Services, Inc. or its Affiliates.
Marketing Offer On A New Product
- 4. © 2020, Amazon Web Services, Inc. or its Affiliates.
Option 1- Build A Rule Engine
Age Gender Purchase
Date
Items
30 M 3/1/2017 Toy
40 M 1/3/2017 Books
…. …… ….. …..
Input Output
Age Gender Purchase
Date
Items
30 M 3/1/2017 Toy
…. …… ….. …..
Rule 1: 15 <age< 30
Rule 2: Bought Toy=Y,
Last Purchase<30 days
Rule 3: Gender = ‘M’,
Bought Toy =‘Y’
Rule 4: ……..
Rule 5: ……..
Human
Programmer
- 5. © 2020, Amazon Web Services, Inc. or its Affiliates.
Model
Output
Historical Purchase Data
(Training Data)
Prediction
Age Gender Items
35 F
39 M Toy
Input - New Unseen Data
Age Gender Purchase
Date
Items
30 M 3/1/2017 Toy
40 M 1/3/2017 Books
…. …… ….. …..
Learning
Algorithm
Option 2 - Learn The Business Rules From Data
- 6. © 2020, Amazon Web Services, Inc. or its Affiliates.
We Call This Approach Machine Learning
Model
Output
Historical Purchase Data
(Training Data)
Prediction
Age Gender Items
35 F
39 M Toy
Input - New Unseen Data
Age Gender Purchase
Date
Items
30 M 3/1/2017 Toy
40 M 1/3/2017 Books
…. …… ….. …..
Rule 1: 15 <age< 30
Rule 2: Bought Toy=Y,
Last Purchase<30
days
Rule 3: Gender = ‘M’,
Bought Toy =‘Y’
Rule 4: ……..
Rule 5: ……..
Human
Programmer
Learning
Algorithm
- 7. © 2020, Amazon Web Services, Inc. or its Affiliates.
Classical
Programming
Machine
Learning
Data
Rules
Data
Answers
Answers
Rules
Machine Learning: Paradigm Shift
- 8. © 2020, Amazon Web Services, Inc. or its Affiliates.
What is Learning Algorithm?
Input X (size of house)
Output Y
(price)
f(x) = W*x + b
xo
xn
2
3
1
- 9. © 2020, Amazon Web Services, Inc. or its Affiliates.
Machine Learning Algorithm
f(x) = W*x + b
• W = ?
• b = ?
모델
학습
대상
Size (X)
2104
1600
2400
1416
3000
1985
1534
1427
1380
1494
Price (y)
400
330
369
232
540
300
315
199
212
243
✗
✗
✗
✗
✗
✗
✗
✗
Input X (size of house)
Output Y
(Price)
2
3
1
(1), (2), (3) 중 가장 적합한(fit) 것은?
Dataset
- 10. © 2020, Amazon Web Services, Inc. or its Affiliates.
Machine Learning Algorithm
f(x) = W*x + b
• W = ?
• b = ?
모델
학습
대상
Size (X)
2104
1600
2400
1416
3000
1985
1534
1427
1380
1494
Price (y)
400
330
369
232
540
300
315
199
212
243
(1), (2), (3) 중 가장 적합한(fit) 것은?
Dataset
• 입력 데이터는 모두 숫자이다.
• ML의 모델은 수학적 모형(수식) 이다.
• ML의 결과물은 수식의 매개변수 이다.
- 11. © 2020, Amazon Web Services, Inc. or its Affiliates.
Machine Learning Algorithm
Input
Data Model
Program
ML
Algorithm
숫자
0, 1, 2,..
0.01, 0.02,..
001, 010, 011, ..
model
{w1, w2, …, wn, b}
f(x) = w1*x1 + w2*x2 + … + wn*xn + b
= W*x + b
ML 알고리즘은 Numeric data로 f(x)라는 식(모델)의 매개변수(w1, w2, …, b)를 추정 하는 방법
Output
- 12. © 2020, Amazon Web Services, Inc. or its Affiliates.
Machine Learning으로 풀 수 있는 문제 유형
Supervised
Unsupervised
정답?
Prediction
Classification
Clustering
Reinforcement
• Linear Regression
• Logistic Regression
• SVM
• K-NN
• XGBoost
• K-Means
• DBScan
Density
Estimation
• PCA
• t-SNE
Yes
No
• Monte Carlo
• Q-Learning
• DQN
- 13. © 2020, Amazon Web Services, Inc. or its Affiliates.
Supervised Learning
It is a cat.
No, it’s a
Labrador.
• The algorithm is given a set of training examples
where the data and target are known. It can then
predict the target value for new datasets,
containing the same attributes
• Human intervention and validation required
Example: Photo classification and tagging
- 14. © 2020, Amazon Web Services, Inc. or its Affiliates.
Supervised Learning – How Machines Learn
Human intervention and validation required
e.g. Photo classification and tagging
Input
Label
Machine
Learning
Algorithm
Labrador
Prediction
Cat
Training Data
?
Label
Labrador
Adjust Model
- 15. © 2020, Amazon Web Services, Inc. or its Affiliates.
Unsupervised Learning
Input
Machine
Learning
Algorithm
Prediction
• Algorithm is given mass amounts of data, and it must find
patterns and relationships between the data. It can then draw
inferences from datasets
• Human intervention is not required
Example: Auto-classification of documents based on context
- 16. © 2020, Amazon Web Services, Inc. or its Affiliates.
Reinforcement Learning
Action
Environment
State Rewards
Agent
- 17. © 2020, Amazon Web Services, Inc. or its Affiliates.
Machine Learning Use Cases
Supervised Learning
Ø Classification
• Spam detection
• Customer churn
prediction
Ø Regression
• House price prediction
• Demand forecasting
Unsupervised Learning
Ø Clustering
• Customer segmentation
Reinforcement Learning
Ø Self Driving Car
Ø Robots
- 18. © 2020, Amazon Web Services, Inc. or its Affiliates.
기계 학습 유형
Supervised
Unsupervised
정답?
Prediction
Classification
Clustering
Reinforcement
• Linear Regression
• Logistic Regression
• SVM
• K-NN
• XGBoost
• K-Means
• DBScan
Density
Estimation
• PCA
• t-SNE
Yes
No
• Monte Carlo
• Q-Learning
• DQN
- 19. © 2020, Amazon Web Services, Inc. or its Affiliates.
Machine Learning Workflow
현실 문제를 Machine Learning을 이용해서 어떻게 풀 수 있을까?
ML
Problem
Framing
Raw Data
Real-World
Problem
Define ML
Problem
Data
Preparation
Build Training Deploy
- 20. © 2020, Amazon Web Services, Inc. or its Affiliates.
ML Problem Framing
현실
문제
ML
문제
집 값 예측
(1) 어떤 ML 문제 유형인지 파악
• 예측
• 분류
• 군집화
• ….
입력 (X)
• 방 개수
• 방 크기
• 위치
• 준공 연도
• 주변 시설
• …
집 값 (y)
(2) 입력/출력 설계
✓
Prediction
(Supervised Learning)
- 21. © 2020, Amazon Web Services, Inc. or its Affiliates.
ML 문제 분류
Predict or
forecast a
value?
Unsupervised
Learning
Supervised
Learning
Classification Regression Clustering
Density
estimation
Yes No
Discrete
target value
Continuous
target value
Fit data into
discrete groups
Numeric
estimate to fit
https://scikit-learn.org/stable/tutorial/machine_learning_map/index.html
- 22. © 2020, Amazon Web Services, Inc. or its Affiliates.
Machine Learning: Input 설계
Data Model
ML
Algorithm
숫자
0, 1, 2,..
0.01, 0.02,..
001, 010, 011, ..
model
{w1, w2, …, wn, b}
f(x) = w1*x1 + w2*x2 + … + wn*xn + b
= W*x + b
ML 알고리즘은 Numeric data로 f(x)라는 식(모델)의 매개변수(w1, w2, …, b)를 추정 하는 방법
- 23. © 2020, Amazon Web Services, Inc. or its Affiliates.
모든 입력 데이터는 숫자(Number)
손 글씨 이미지 숫자
- 24. © 2020, Amazon Web Services, Inc. or its Affiliates.
모든 입력 데이터는 숫자(Number)
문자열 또는 카테고리 데이터는 One-Hot Encoding 방식을 이용해서 숫자로 변환
상품 분류 가격
TV 1,000,000
냉장고 1,500,000
전자렌지 200,000
컴퓨터 800,000
선풍기 100,000
선풍기 100,000
믹서 50,000
믹서 50,000
상품 분류 가격
0 1,000,000
1 1,500,000
4 200,000
5 800,000
3 100,000
3 100,000
2 50,000
2 50,000
TV 냉장고 믹서 선풍기 전자렌지 컴퓨터 가격
1 0 0 0 0 0 1,000,000
0 1 0 0 0 0 1,500,000
0 0 0 0 1 0 200,000
0 0 0 0 0 1 800,000
0 0 0 1 0 0 100,000
0 0 0 1 0 0 100,000
0 0 1 0 0 0 50,000
0 0 1 0 0 0 50,000
원본 데이터 숫자 인코딩 One-Hot 인코딩
- 25. © 2020, Amazon Web Services, Inc. or its Affiliates.
Feature: 숫자로 변환된 ML 알고리즘의 입력 데이터
- 26. © 2020, Amazon Web Services, Inc. or its Affiliates.
Feature Engineering
• 입력 데이터를 숫자로 변환하기
ex) One-Hot Encoding
• 데이터의 가지 수 (차원)를 줄이거나
늘리는 작업
• 불필요한 데이터를 제거하기
• 누락된 데이터를 채워 넣기
• …
1차원
2차원
6 차원
Feature
- 27. © 2020, Amazon Web Services, Inc. or its Affiliates.
차원의 저주 (Curse of Dimensionality)
…
…
1차원 2차원 3 차원 4 차원
…
…
…
…
13 차원 784 차원 54877 차원
Iris MNIST Farm Ads
꽃받침 길이,
꽃받침 너비,
꽃잎 길이,
꽃잎 너비
pixel1,
pixel2,
…,
pixel784
단어1,
단어2,
…,
단어54877
- 28. © 2020, Amazon Web Services, Inc. or its Affiliates.
모델 평가
Data Model
ML
Algorithm
숫자
0, 1, 2,..
0.01, 0.02,..
001, 010, 011, ..
model
{w1, w2, …, wn, b}
f(x) = w1*x1 + w2*x2 + … + wn*xn + b
= W*x + b
ML 알고리즘은 Numeric data로 f(x)라는 식(모델)의 매개변수(w1, w2, …, b)를 추정 하는 방법
✗
✗
✗
✗
✗
✗
✗
✗
Input X (size of house)
Output Y
(Price)
2
3
1
Q) (1), (2), (3) 중 가장 적합한(fit) 것은?
- 29. © 2020, Amazon Web Services, Inc. or its Affiliates.
Input X (size of house)
Loss Function (Objective Function)
Output Y
(Price)
f(x) = W*x + b
Error (f(Xj) – Yj)
Loss(W, b) = ∑(f(xj) – yj)2
1
n j=1
n
xo
xn
Mean Square Error(MSE) Function
Guessing
Value
Actual
Value
- 30. © 2020, Amazon Web Services, Inc. or its Affiliates.
Loss Function (Objective Function)
선형 회귀 문제
선형 회귀를 위한 목적 함수
• 𝑓!(𝐱")는 예측함수의 출력, yi는 예측함수가
맞추어야 하는 목표 값
• 𝑓!(𝐱") - yi는 오차 = 평균제곱오차MSE(mean squared error)
- 31. © 2020, Amazon Web Services, Inc. or its Affiliates.
Loss Function (Objective Function)
선형 회귀 문제
선형 회귀를 위한 목적 함수
• 𝑓!(𝐱")는 예측함수의 출력, yi는 예측함수가
맞추어야 하는 목표 값
• 𝑓!(𝐱") - yi는 오차 = 평균제곱오차MSE(mean squared error)
처음에는 최적 매개변수 값을 알 수 없으므로 임의
값으로 Θ! = 𝑤!, 𝑏! 설정 à Θ" = 𝑤", 𝑏" 로
개선 à Θ# = 𝑤#, 𝑏# 로 개선 à Θ#는 최적해 &
Θ
• 이때 𝐽 Θ# > 𝐽 Θ$ > 𝐽 Θ%
- 32. © 2020, Amazon Web Services, Inc. or its Affiliates.
Plotting Loss Function (Single Variable)
W
Loss (W)
Minimum loss
- 33. © 2020, Amazon Web Services, Inc. or its Affiliates.
Optimization - Minimize Loss Function
W
Minimum loss
현 지점의
기울기
(Partial
Derivative)
Loss를 낮추는
방향(Descent)
WJ WJ+1
Gradient Descent
Wj+1 = Wj 𝛼 Loss(Wj)
∂
∂Wj
Random
initial value
Loss (W)
𝛼
한 발자국 크기
(Learning Rate)
- 34. © 2020, Amazon Web Services, Inc. or its Affiliates.
Gradient Decent: 최적화 문제 해결법
Learning Step
weight의 업데이트 = 에러 낮추는 방향 x 한 발자국 크기 x 현 지점의 기울기
(decent) (learning rate) (gradient)
Loss function의 현재 가중치에서 기울기(gradient)를 구해서 Loss를 줄이는 방향으로 가중치를 업데이트해
나간다
Learning Rate
- 35. © 2020, Amazon Web Services, Inc. or its Affiliates.
Hyper Parameter
Learning Rate(학습률)는 어떻게 설정해야할까?
Data Model
ML
Algorithm
숫자
0, 1, 2,..
0.01, 0.02,..
001, 010, 011, ..
model
{w1, w2, …, wn, b}
f(x) = w1*x1 + w2*x2 + … + wn*xn + b
= W*x + b
Gradient Descent
알고 싶은 값
(Parameter)
모델 학습을 위해서 미리
알아야 하는 값
(Hyperparameter)
- 36. © 2020, Amazon Web Services, Inc. or its Affiliates.
small learning rate right learning rate large learning rate
Hyper Parameter Tuning (HPO)
(학습이) 너무 느리다 답을 찾지 못한다
- 37. © 2020, Amazon Web Services, Inc. or its Affiliates.
Weight 초기값, Learning Rate 등을 적절하게 선택해야한다.
Gradient Descent pitfalls
Weight
초기값1
Weight
초기값2
- 38. © 2020, Amazon Web Services, Inc. or its Affiliates.
Batch
Gradient Decent
Stochastic
Gradient Decent
full-batch
mini-batch
mini-batch
mini-batch
mini-batch
mini-batch
mini-batch
mini-batch
mini-batch
기울기를 계산 할 때, 몇 개의 입력 데이터를 사용할 것인지?
전체 m개의
데이터 사용
n개 데이터(작은 묶음) 사용
(n=1: Stochastic,
1< n < m: Mini-Batch)
batch size = m batch size = 1
batch size = n
- 39. © 2020, Amazon Web Services, Inc. or its Affiliates.
최적화를 이용한 기계 학습의 문제 풀이 과정
f(x) = W*x + b
House Price
Prediction
Guessing
Value
Actual
Value
Real-World
Problem
최적화
Objective Function
ML Model
Gradient Descent
- 40. © 2020, Amazon Web Services, Inc. or its Affiliates.
학습(Training)의 최종 목표
한번도 본적이 없는 문제를 잘 푸는 것
학습 모의 평가 최종 평가
- 41. © 2020, Amazon Web Services, Inc. or its Affiliates.
데이터 나누기
전체 데이터
Training Test
Training Validation
한번도 본적 없는 문제를 잘 풀 수 있도록 훈련 시키기 위해서 데이터를 나눈다
“훈련은 실전처럼, 실전은 훈련처럼”
- 42. © 2020, Amazon Web Services, Inc. or its Affiliates.
2 가지 질문
Q1. 모델은 복잡할 수록 좋을까?
예)
Q2. 문제를 해결할 때, 1가지 모델만 사용해야 하는 것인가?
2개 이상의 모델을 사용할 수 없을까?
- 43. © 2020, Amazon Web Services, Inc. or its Affiliates.
단순한 모델 vs. 복잡한 모델이 어느 것이 좋을까?
- 44. © 2020, Amazon Web Services, Inc. or its Affiliates.
Model Selection: Overfitted vs. Underfitted
Underfitted Overfitted
Good Fit/Robust
- 45. © 2020, Amazon Web Services, Inc. or its Affiliates.
앙상블(Ensemble): Weak Learner의 결합
Cancer
No Cancer
Cancer
Cancer
New patient
New patient
Machine Learning Model1
Machine Learning Model2
Machine Learning Modeln
∑ Ensemble’s prediction
(e.g., majority vote)
- 46. © 2020, Amazon Web Services, Inc. or its Affiliates.
Deep Learning ⊂ Machine Learning
Artificial Intelligence
Machine Learning
Deep
Learning
- 47. © 2020, Amazon Web Services, Inc. or its Affiliates.
Artificial Neuron
자
극
전
달
- 48. © 2020, Amazon Web Services, Inc. or its Affiliates.
Perceptron – 생물학적 뉴런의 수학적 모형 (인공 뉴런)
x1
x2
out
가중합
활성화
함수
W1
W2
b
예: sigmoid
출력
입력
- 49. © 2020, Amazon Web Services, Inc. or its Affiliates.
Deep Neural Network
shallow network
(세로 쌓기)
deep network
(가로-세로 쌓기)
=
- 50. © 2020, Amazon Web Services, Inc. or its Affiliates.
Deep Neural Network
=
입력층 출력층
은닉층1 은닉층2 은닉층3
- 51. © 2020, Amazon Web Services, Inc. or its Affiliates.
Deep Learning – 무수히 많은 가중치(Weight) 계산 필요!
Data Model
ML
Algorithm
숫자
0, 1, 2,..
0.01, 0.02,..
001, 010, 011, ..
model
{w1, w2, …, wn, b}
f(x) = w1*x1 + w2*x2 + … + wn*xn + b
= W*x + b
ML 알고리즘은 Numeric data로 f(x)라는 식(모델)의 매개변수(w1, w2, …, b)를 추정 하는 방법
인공 뉴런
Deep Neural Network
- 52. © 2020, Amazon Web Services, Inc. or its Affiliates.
자동 미분(Autodiff) - Backpropagation
Machine Learning
f(x) = W*x + b
미분 값을
직접 계산
Real-World Problem
(e.g. House Price Prediction)
Deep Learning
f(x) = W*x + b 계산 그래프 순회
자동 미분
계산
- 53. © 2020, Amazon Web Services, Inc. or its Affiliates.
미분 값을
직접 계산
계산 그래프
backward 순회
계산 그래프
forward 순회
Machine Learning Deep Learning
- 54. © 2020, Amazon Web Services, Inc. or its Affiliates.
Deep Learning Framework = 자동 미분(Autodiff) Framework
- 55. © 2020, Amazon Web Services, Inc. or its Affiliates.
Why GPUs are more suited for Deep Learning?
CPU GPU
Deep Neural Network
무수히 많은 가중치 계산
- 56. © 2020, Amazon Web Services, Inc. or its Affiliates.
End-to-End 학습
Machine Learning
Deep Learning
- 57. © 2020, Amazon Web Services, Inc. or its Affiliates.
Machine Learning or Deep Learning?
Entities are not to be multiplied without necessity.
쓸데 없이 복잡하게 만들지 말라.
All things being equal, the simplest solution tends to be the best one.
모든 것이 같다면 가장 단순한 해가 가장 좋다.
- Occam’s Razor
- 58. © 2020, Amazon Web Services, Inc. or its Affiliates.
Summary
• Machine Learning
▪ 현실 문제 → 수학 모형(모델) → 모수 (Parameter, 가중치) 추정
• Feature Engineering: 입력 데이터 → 숫자 변환 (Feature), One-Hot Encoding, 차원 축소/증가
• 수학 모형의 모수(Parameter) 추정 방법
▪ Loss Function (Objective, Cost Function) = Error(Guessing value – Actual value) 함수
▪ Error를 최소화 시키는 모수 (Parameter, 가중치) 찾기
▪ Gradient Descent method (경사하강법)
• Train/Validation/Test Set으로 데이터 분리 – 일반화
• ML 모델 선택 – Overfitted vs Underfitted
• 앙상블(Ensemble) – Weak Learner의 결합
• Deep Learning
▪ 현실 문제 → Deep Neural Network (수학 모형, 모델) → 무수히 많은 모수 (Parameter, 가중치) 추정
▪ Deep Neural Network (DNN) - 인공 뉴런(Perceptron)을 가로-세로 방향으로 쌓아서 연결한 그래프(Network)
▪ 인공 뉴런 (Perceptron) – 생물학적 뉴런의 수학적 모형 (입력과 가중치의 곱셈합 + 편향 → 활성화 함수)
▪ 자동 미분 (Autodiff) - 계산 그래프 순회 (backpropagation) 방식으로 미분 자동 계산