배워봅시다.머신러닝
withTensorFlow
이름 : 장훈
나이 : 30세(1988-06-16)
이메일 : lunawyrd@gmail.com
블로그 : http://devhoon.tistory.com
Github : https://github.com/jang-hoon
[주요경력]
2016.08 ~ 현재 (주)EXEM, WAS 및 Web Server 모니터링 프로그램(APM) 개발
2015.09 ~ 2016.03 (주)두꺼비세상, 두꺼비세상 Android 앱 개발 및 Spring
Boot를 이용한 REST, Batch, Gateway 서버 개발
2015.04 ~ 2015.08 (주)옐로쇼핑미디어, 쿠차 Android 앱 개발
2009.12 ~ 2013.02 (주)아이콘랩, Android 및 BlackBerry 앱 개발,
산업기능요원 대체복무
손글씨를인식하는
앱을만들어보자
1. 앱에서 손 글씨를 입력 받고,
2. 입력 받은 손 글씨를 서버로 전송
3. 서버에서는 입력 받은 손 글씨를 분석
4. 결과를 앱으로 돌려주자
손글씨는어떻게분석하지?
찾아보니…
기본 원리 : Perceptron(퍼셉트론)
학습 방법 : Regression(회귀분석)
분류 방법 : Classification(분류법)
activation(y = Wx + b) = 1 or 0
퍼셉트론 알고리즘(Perceptron, 1957)
프랭크 로젠블랫
Frank Rosenblatt
(July 11, 1928 – July 11, 1971)
회귀분석(Regression)
“어떤 변수( Y )가 다른 변수( X )에 의해 설명된다고 보고 그 함수 관계(Y = WX + b)를
조사하는 통계적 해석 수법”
– Google
ex) 평균 온도( X )가 올라가면 아이스크림 판매량( Y )이 어떻게 변하는가?
y=Wx+b
x y
1 1
2 2
3 3
4 4
5 5
W와 b는 어떻게 정할까
y_
?
?
?
?
?
실제 데이터 예측 데이터
가설(hypothesis)
y=1x+0
우리가 생각하는 값
cost =
1
𝑚
(𝑦 − 𝑦_)2
y y_
1 ?
2 ?
3 ?
4 ?
5 ?
적합성 검증(평균제곱오차, MSE)
비용함수(Cost Function)
실제 예측
코스트를 어떻게 최소화할까?
경사하강법(Gradient Descent Algorithm)
Hypothesis설정
import tensorflow as tf
# 학습 데이터
x_data = [1, 2, 3]
y_data = [1, 2, 3]
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
# 가설 Y_ = WX + b
hypothesis = W * X + b
비용함수와오차보정
# 비용함수 cost = 평균( (Y_ - Y)^2 )
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# cost를 줄이는 방향으로 W, b를 수정하기 위한 경사하강법
a = tf.Variable(0.1) # Learning rate, alpha
optimizer = tf.train.GradientDescentOptimizer(a)
train = optimizer.minimize(cost)
TensorFlow실행을통해학습
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
# 학습
for step in range(2001):
# hypothesis = W * X + b
# cost = tf.reduce_mean(tf.square(hypothesis - Y))
# train = tf.train.GradientDescentOptimizer(0.1).minimize(cost)
sess.run(train, feed_dict={X: x_data, Y: y_data})
if step % 20 == 0:
print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W),
학습된모델을테스트
# Hypothesis 테스트, hypothesis = W * X + b
print(sess.run(hypothesis, feed_dict={X: 5}))
print(sess.run(hypothesis, feed_dict={X: 2.5}))
분류법(Classification)
“Classification은 Category를 나누는 것과 관련된 일반적인 과정으로, 아이디어와 사
물을 인식하고, 차별화하고, 이해하는 과정“
- Wikipedia
ex) 신문기사를 날씨, 경제, 연예, 스포츠 등으로 구분
메일을 일반 메일, 스팸 메일로 구분
신용카드 사용 패턴이 평소와 같은지 특이한지 구분
LogisticRegression(BinaryClassfication)
X(공부시간) Y(합격여부)
1 0
2 0
3 1
4 1
5 1
Y = Wx+ b
1(pass)
0(fail)
공부시간
0.5
1 2 3 4 5
MultinomialClassfication
X1(공부시간) X2(출석) Y(학점)
3 1 C
2 3 B
3 5 A
2 3 B
5 1 C
출석(X1)
공부시간(X2)
A
C
B Y = Wx+ b
Y = Wx+ b
Y = Wx+ b
MultinomialClassfication
MultinomialClassfication
MultinomialClassfication
SoftmaxNomalization
N(x)=
𝑥
𝑥 𝑛
적합성검증(평균제곱오차,MSE)
CrossEntropy
손글씨를인식하는
앱을만들어보자
1. 앱에 그려진 숫자를 분석 가능한 형태로 전처리
2. 전처리된 데이터를 서버로 전송
3. 서버는 전처리된 데이터를 미리 학습된 모델(y = Wx + b)에 적용
4. 학습된 모델에 의해 분석된 결과로 적합한 숫자를 찾음
5. 찾은 숫자를 앱으로 돌려줌
손글씨인식앱시연
이미지리사이징(Android)
Bitmap image = MainActivity.getBitmapFromView(mDrawingView);
int width = image.getWidth();
int height = image.getHeight();
int scaleWidth = 28;
int scale = width / scaleWidth;
int scaleHeight = height/scale;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newImage = Bitmap.createScaledBitmap(image, scaleWidth, scaleHeight, false)
부족한부분패딩(Android)
List<Float> pixelList = new ArrayList<>();
StringBuilder builder = new StringBuilder();
for(int w=0; w<newImage.getWidth(); w++) {
pixelList.add(0.0f);
}
for(int w=0; w<newImage.getWidth(); w++) {
pixelList.add(0.0f);
}
Threshold처리(Android)
for(int h=0; h<newImage.getHeight(); h++) {
builder.setLength(0);
for(int w=0; w<newImage.getWidth(); w++) {
float color = newImage.getPixel(w, h);
color = color == -1 ? 0.0f : 1.0f;
builder.append(color + " ");
pixelList.add(color);
}
}
for(int w=0; w<newImage.getWidth(); w++) {
pixelList.add(0.0f);
}
Sampleload및초기화
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
#학습 데이터 로드
mnist = input_data.read_data_sets("MNIST__data/", one_hot=True)
x = tf.placeholder("float", [None, 784]) # [?] x [784]
y = tf.placeholder("float", [None, 10]) # [?] x [10]
W = tf.Variable(tf.zeros([784, 10])) # [784] x [10]
b = tf.Variable(tf.zeros([10])) # [10]
Softmax와비용함수,오차보정
# y_ = softmax(Wx + b)
y_ = tf.nn.softmax(tf.matmul(x, W) + b)
# cost = -∑(y * log(y_)
cross_entropy = -tf.reduce_sum(y * tf.log(y_))
# cost를 줄이는 방향으로 W, b를 수정하기 위한 경사하강법
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
TensorFlow실행을통해학습
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
# y_ = tf.nn.softmax(tf.matmul(x, W) + b)
# cross_entropy = -tf.reduce_sum(y * tf.log(y_))
# train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})
correct_prediction = tf.equal(tf.argmax(y_, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels
학습된모델을이용해분석
def analysis(request):
message = "Test"
if request.method == "POST":
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
test_array = np.array(body)
test_array = test_array.reshape((1, 784))
# y_ = tf.nn.softmax(tf.matmul(x, W) + b)
test_result = sess.run(tf.argmax(y_, 1), feed_dict={x: test_array});
print(test_result[0])
message = test_result[0]
return HttpResponse(message)
Q&A
감사합니다.

배워봅시다 머신러닝 with TensorFlow