반응형
반응형

#모두를 위한 딥러닝 강좌 lec 03 : Linear Regression의 cost 최소화 알고리즘

https://www.youtube.com/watch?v=TxIVr-nk1so&index=6&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm



How to minimize Cost Function


* H(x) = Wx +b

* minimize cost(W,b) 

  • cost 공식에 데이터 값을 넣고 계산하면 아래로 볼록한 포물선(2차 함수 그래프)을 볼 수 있는데(y축은 cost, x축은 W),
    cost가 0인 지점을 찾는 것이 Linear Regression중에서 minimize한 cost funtion목적이다. 
  • 이 방법을 기계적인 알고리즘을 통해서 학습시키는 방법중 하나가 바로 gradient descent algorithm이다.
* Gradient descent algorithm란?
  • 경사를 따라 내려가는 알고리즘
  • ML이외에도 여러 분야에 이 알고리즘을 통하여 minimize한 값들을 찾아낼 수가 있다. 
* 알고리즘의 내용 
  • 산 꼭데기에 있을 때, 산에서 내려오는 방법은 여러가지가 될 것이다. 그러나 그 도착지는 어디가 될지 모른다..
  • 그러나 아래가 볼록한 포물선에서 내려가는 방법은 어디를 선택하든 정답으로 내려가는 정답은 하나로 나온다는 점이다.
  • 그리고 이 경사도를 통해서 최대점을 알수가 있는데, 이때 미분(그래프 내의 한 점의 기울기를 구하는 것) 을 사용하여서 구한다. 
* 미분
  • 미분은 여러 웹사이트에서 바로 계산해주는 것들이 많음..ㅎㅎㅎ
* Gradient descent algorithm의 공식

  • 이 공식을 기계적으로 공식해서 minimize한 cost function을 구할 수 있다.

참고 링크 


반응형
반응형

#모두를 위한 딥러닝 강좌 lec 02 : Linear Regression의 Hypothesis 와 cost

https://www.youtube.com/watch?v=Hax03rCn3UI&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm&index=4



Linear Regression

  • 기본적인 트레이닝 세트(데이타 셋트)가 X=1, Y=1 ...과 같이 있을 경우, 이 데이타들의 그래프를 그려보면 x=y꼴 그래프가 될 것이다.
  • 이때, x=y 그래프가 정답이라고 가정을 하고! 이 선과 가장 가까운 일직선 라인(Linear)과 가까운 좌표값을 정답이라고 처리하는 학습 모델 방법을 말한다.

Hypothesis : H(x) = Wx + b

  • Linear Regression 학습방법 기본이 되는 저 방정식 따라서 각각의 값들이 얼만큼 x=y꼴 그래프와 가장 가까이 있는지를 구하는 식을 알면, Linear Regression 학습법을 ML 적용시킬 수 있다.

Cost Function = Loss Function 

  • Linear Regression에서 정답이라고 가정한 선( y=x 그래프선 )과 가장 가까운 거리의 값을 찾는 방법을 일컫어 Cost Function (=Loss Function) 이라고 한다. 
  • 즉, 가설과 실제 데이터의 차이값을 볼 수 있는 방법을 말한다.
  • H(x) - y의 값(정답 값 H(x)에서 실제 값인 y를 뺀 식)을 구하자. 하지만, H(x) - y를 할 경우 -(음수) 값이 나올 수도 있으므로 제곱을 해서 구한다. ⇒ (H(x)-y)^2

  • 즉, minimaize cost (W, b)를 구하는 것이 Linear Regression의 학습이 된다. 

 #텐서플로우를 이용한 Lab 실습 02 

https://www.youtube.com/watch?v=4HrSxpi3IAM&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm&index=5



Linear Regression을 통한 텐서플로우 활용 예제) https://github.com/hunkim/DeepLearningZeroToAll

텐서플로우로 선형회귀 분석하는 3단계
  • 1단계 : 그래프를 빌드하기
  • 2단계 : sess.run을 통해서 그래프를 실행하기
  • 3단계 : 2단계에서 실행한 결과 값 확인하기

1단계 : 그래프 빌드하기 (Build graph using TF operations)

H(x) = Wx + b 값 정의

# X and Y data
x_train = [1, 2, 3] # 학습할 x값
y_train = [1, 2, 3] # 학습할 y값 x=y 값이라는 학습 데이터

# Try to find values for W and b to compute y_data = x_data * W + b
# We know that W should be 1 and b should be 0
# But let TensorFlow figure it out
W = tf.Variable(tf.random_normal([1]), name='weight') # W와 b값을 Variable이라는 개념으로 정의한다. 텐서플로우가 사용하는 Variable이라는 개념
b = tf.Variable(tf.random_normal([1]), name='bias') # 우리는 W와 b값을 모르므로 임의의 값을 가져와서 정의한다.

# Our hypothesis XW+b
hypothesis = x_train * W + b # 우리의 node 값

Cost(W, b) 값 정의

# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - y_train))

* 여기서 reduce_mean이란 무엇인가?
값을 평균내주는 함수


그러면 이제 값을 최소화하는 알고리즘을 또 TF에서는 어떻게 지원하고 있는가?
( = 우리의 Hypothesis 선과 y값의 차이를 최소로 찾아줄 식 )

# Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) # 최적화의 minimize 함수를 찾아서 cost를 최소화하라고 명시하면

train = optimizer.minimize(cost) # 자기가 알아서 최적화된 최소값을 찾아내기 시작함



2단계 : sess.run을 통해서 그래프를 실행하기

# Launch the graph in a session.
sess = tf.Session() # 세션 생성
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer()) # Variable을 Initialize 한다.

# Fit the line
for step in range(2001):
sess.run(train) # train 그래프 상에 있는 node 값을 실행한다.
if step % 20 == 0:
print(step, sess.run(cost), sess.run(W), sess.run(b))



3단계 : 결과 값 해석하기

'''
0 2.82329 [ 2.12867713] [-0.85235667]
20 0.190351 [ 1.53392804] [-1.05059612]
40 0.151357 [ 1.45725465] [-1.02391243]
...
1920 1.77484e-05 [ 1.00489295] [-0.01112291]
1940 1.61197e-05 [ 1.00466311] [-0.01060018]
1960 1.46397e-05 [ 1.004444] [-0.01010205]
1980 1.32962e-05 [ 1.00423515] [-0.00962736]
2000 1.20761e-05 [ 1.00403607] [-0.00917497]
'''

W 값은 최적화된 값을 찾았고, b값은 0으로 수렴하는 것을 알 수 있다. 

노랑색 : Cost값

초록색 : W값 

하늘색 : b값



Placeholders란?

직접 값을 주지 않고, 필요할 때마다 값을 알아서 넘겨주는 것..!

이 placeholders를 화룡ㅇ해서 선형분석을 할 수 있다.


#x_train = [1,2,3]

#y_train = [1,2,3] 이렇게 주지 않고


X = tf.placeholder(tf.float32)

Y = tf.placeholder(tf.float32)

.... 이렇게! 선언을 하고 나서 


# Fit the line
for step in range(2001):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(cost), sess.run(W), sess.run(b))

이 부분 소스를

# Fit the line
for step in range(2001):
cost_val, W_val, b_val, _ = \

sess.run([cost, W, b, train], feed_dict = {X: [1,2,3], Y: [1, 2, 3]})
if step % 20 == 0:
print(step, cost_val, W_val, b_val)

노란색 글씨처럼 feed_dict를 통해서 값을 전달한다.


이것을 사용하는 이유는?

만들어진 모델에 대해서 값을 따로 지정해서 줄 수 있다.

Linear Regression 모델을 통해서 값을 넣어줄 수 있다는 장점이 있다.


학습을 시키게 될 때! 즉, train을 넣어줄 때 각각 feed_dict를 통해서 정의가 가능하다.





최종 소스 (기본 Linear Regression)


# Lab 2 Linear Regression
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility

# X and Y data
x_train = [1, 2, 3]
y_train = [1, 2, 3]

# Try to find values for W and b to compute y_data = x_data * W + b
# We know that W should be 1 and b should be 0
# But let TensorFlow figure it out
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

# Our hypothesis XW+b
hypothesis = x_train * W + b

# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - y_train))

# Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)

# Launch the graph in a session.
sess = tf.Session()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())

# Fit the line
for step in range(2001):
sess.run(train)
if step % 20 == 0:
print(step, sess.run(cost), sess.run(W), sess.run(b))

# Learns best fit W:[ 1.], b:[ 0.]

'''
0 2.82329 [ 2.12867713] [-0.85235667]
20 0.190351 [ 1.53392804] [-1.05059612]
40 0.151357 [ 1.45725465] [-1.02391243]
...
1920 1.77484e-05 [ 1.00489295] [-0.01112291]
1940 1.61197e-05 [ 1.00466311] [-0.01060018]
1960 1.46397e-05 [ 1.004444] [-0.01010205]
1980 1.32962e-05 [ 1.00423515] [-0.00962736]
2000 1.20761e-05 [ 1.00403607] [-0.00917497]
'''


최종 소스 ( placeholder를 사용한 Linear Regression )


# Lab 2 Linear Regression
import tensorflow as tf
tf.set_random_seed(777) # for reproducibility

# Try to find values for W and b to compute y_data = W * x_data + b
# We know that W should be 1 and b should be 0
# But let's use TensorFlow to figure it out
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

# Now we can use X and Y in place of x_data and y_data
# # placeholders for a tensor that will be always fed using feed_dict
# See http://stackoverflow.com/questions/36693740/
X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])

# Our hypothesis XW+b
hypothesis = X * W + b

# cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))

# Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)

# Launch the graph in a session.
sess = tf.Session()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())

# Fit the line
for step in range(2001):
cost_val, W_val, b_val, _ = \
sess.run([cost, W, b, train],
feed_dict={X: [1, 2, 3], Y: [1, 2, 3]})
if step % 20 == 0:
print(step, cost_val, W_val, b_val)

# Learns best fit W:[ 1.], b:[ 0]
'''
...
1980 1.32962e-05 [ 1.00423515] [-0.00962736]
2000 1.20761e-05 [ 1.00403607] [-0.00917497]
'''

# Testing our model
print(sess.run(hypothesis, feed_dict={X: [5]}))
print(sess.run(hypothesis, feed_dict={X: [2.5]}))
print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]}))

'''
[ 5.0110054]
[ 2.50091505]
[ 1.49687922 3.50495124]
'''


# Fit the line with new training data
for step in range(2001):
cost_val, W_val, b_val, _ = \
sess.run([cost, W, b, train],
feed_dict={X: [1, 2, 3, 4, 5],
Y: [2.1, 3.1, 4.1, 5.1, 6.1]})
if step % 20 == 0:
print(step, cost_val, W_val, b_val)

# Testing our model
print(sess.run(hypothesis, feed_dict={X: [5]}))
print(sess.run(hypothesis, feed_dict={X: [2.5]}))
print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]}))



참고 링크 


반응형
반응형

#모두를 위한 딥러닝 강좌 lec 01 : 기본적인 Machine Learning 의 용어와 개념

https://www.youtube.com/watch?v=qPMeuL2LIqY&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm&index=2



머신러닝(ML)이란?
  • 명시적인 학습을 하는 프로그램으로 ex) 스팸 필터, 자율 주행과 같은 시스템을 만들 때 개발자가 일일히 룰을 만들긴 어려울 것이다. 이럴 때, 기계를 학습시켜서 해당 룰을 스스로 만들 수 있다면 좋을 것이다.

ML의 학습방법 : Supervised Learning(지도학습)과 Unsupervised Learning(비지도 학습/자율 학습)

  1. Supervised Learning 
    • 정해진 라벨이 붙은 트레이닝 세트를 가지고 학습 시키는 방법
    • ex) 고양이 사진들(정답)을 보여주며 이 사진들은 고양이라고 학습 시킴
  2. Unsupervised Learning
    • 정해지지 않은 새로운 데이타들을 가지고 학습시키는 방법 (데이타를 보고 스스로 학습 하는 방법)
    • ex) 구글 뉴스에서는 자동적으로 유사한 그룹을 grouping을 하는 경우, 미리 라벨링하기 어려우므로 자기가 알아서 분류하여 라벨링 함
Supervised Learning(지도학습)의 종류
  1. Regression (회귀)
    • ex) 성적 0에서 100점까지의 점수(범위)를 예측할 때 쓰는 방법
  2. binary classification (바이너리 분류)
    • 예측을 Pass와 Non-Pass 등으로 2가지로 나누어 학습시키는 방법
    • ex) 성적이 올랐는지 안올랐는지 2가지만 선택할 경우 Binary classification
  3. multi-label classification (멀티 분류)
    • 예측을 하되, A,B,C.. 등 많은 라벨을 선택할 경우 쓰는 방법
    • ex) A 분류, B 분류, C 분류 .... 
  4. 의사결정나무
  5. 판별분석
  6. 신경망
Supervised Learning을 하기 위해서는 반드시 트레이닝 세트가 필요하다. 
트레이닝 세트란, 해당 ML에게 학습을 시키기 위한 데이터들을 말한다. 

UnSupervised Learning(비지도 학습/자율학습)의 종류
  1. 연관성 규칙
  2. 데이터 축소기법
  3. 군집분석


#텐서플로우를 이용한 Lab 실습 01

https://www.youtube.com/watch?v=cbPjsOivFOs&index=3&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm



텐서플로우란? (https://www.tensorflow.org/)

python으로 개발할 수 있는 머신러닝 오픈 소스 라이브러리


데이타 플로우 그래프란?

  • 노드(Nodes) : 수학적인 계산을 하는 Operations
  • 엣지(Edges) : 데이타의 행렬(arrays=tensors)

이러한 텐서(데이타)들이 돌아다는 것을 일컫어 텐서 플로우 라고 말한다.

이것들을 통해서 딥러닝/ML에 사용할 수 있다. 


텐서플로우의 설치와 Hellow World 찍기

  • 텐서플로우는 모든 것들이 하나의 Node(Operation)가 되고, 이 Node(Operation)를 실행시키는 것이 바로 run이라는 함수이다. 

* Placeholder

  • 하나의 모델에 대해서도 마치 함수에 값을 줄 수 있다.
  • placeholder로 변수 타입을 만들고 실제로 실행시킬 때, a와 b로 해당 하는 값으로 치환하여서 실행시킬 수 있게 된다. 

참고 링크 


반응형

+ Recent posts