[딥러닝] Linear Regression의 cost 최소화의 TensorFlow 구현
가설과 실제가 얼마나 차이나는지 확인하는 것
Gradient descent 알고리즘
Gradient descent - 경사를 내려가다
minimization problem 사용
어떤 점에서나 시작이 가능
W를 조금씩 바꾸면서 나아감 → cost(W,b) 절감 시도
반복
어떤 점에서 시작하든, 최소점에 도달할 수 있는 알고리즘
'미분'을 사용한다.
여러번 실행시켜 W값이 변화되는 것이 cost를 minimize하는 값을 구하는 과정이다.
그래프에서 기울기가 +면 W식에서 점차 값이 감소하게 된다.
matplotlib 설치 (윈도우)
cmd 창에 다음과 같이 순서대로 입력하면 matplotlib이 설치된다. 이를 통해 Linear Regression의 모듈을 run해서 그래프 형태의 모습을 볼 수 있다.
python -m pip install -U pip setuptools
python -m pip install matplotlib
matplotlib을 이용한, Linear Regression 알고리즘의 그래프 형태 보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import tensorflow as tf import matplotlib.pyplot as plt X = [1, 2, 3] Y = [1, 2, 3] W = tf.placeholder(tf.float32) # Our hypothesis for linear model X * W hypothesis = X * W # cost/Loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) # Variables for plotting cost function W_val = [] cost_val = [] for i in range(-30, 50): feed_W = i * 0.1 curr_cost, curr_W = sess.run([cost, W], feed_dict={W: feed_W}) W_val.append(curr_W) cost_val.append(curr_cost) # Show the cost function plt.plot(W_val, cost_val) plt.show() | cs |
derivative를 사용한 Gradient Descent 알고리즘 최소화 방법
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import tensorflow as tf x_data = [1, 2, 3] y_data = [1, 2, 3] W = tf.Variable(tf.random_normal([1]), name='weight') X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) # Our hypothesis for linear model X * W hypothesis = X * W # cost/Loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize: Gradient Descent using derivative: W -= Learning_rate * derivative learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X) descent = W - learning_rate * gradient update = W.assign(descent) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) for step in range(21): sess.run(update, feed_dict={X: x_data, Y: y_data}) print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W)) | cs |
W는 점점 1로 수렴하는 모습을 확인할 수 있다.
즉, matplotlib으로 확인한 그래프에서 볼 수 있듯이 x가 어디 위치든 결국 W가 1로 가는 곳으로 움직인다.
'딥러닝(Deep-Learning)' 카테고리의 다른 글
[딥러닝] 머신러닝 & Regression 정리 (0) | 2018.05.28 |
---|---|
[딥러닝] Linear Regression 코드 실행 및 코드 분석 (0) | 2018.05.14 |
[딥러닝] 텐서플로우(Tensorflow)로 간단한 선형 회귀(Linear regression)을 구현 (0) | 2018.05.08 |
[딥러닝] 텐서플로우 설치 및 기본 오퍼레이션 (0) | 2018.05.08 |
[딥러닝] 머신러닝 기본 용어 및 개념 (0) | 2018.05.08 |
[딥러닝] Linear Regression의 cost 최소화의 TensorFlow 구현
가설과 실제가 얼마나 차이나는지 확인하는 것
Gradient descent 알고리즘
Gradient descent - 경사를 내려가다
minimization problem 사용
어떤 점에서나 시작이 가능
W를 조금씩 바꾸면서 나아감 → cost(W,b) 절감 시도
반복
어떤 점에서 시작하든, 최소점에 도달할 수 있는 알고리즘
'미분'을 사용한다.
여러번 실행시켜 W값이 변화되는 것이 cost를 minimize하는 값을 구하는 과정이다.
그래프에서 기울기가 +면 W식에서 점차 값이 감소하게 된다.
matplotlib 설치 (윈도우)
cmd 창에 다음과 같이 순서대로 입력하면 matplotlib이 설치된다. 이를 통해 Linear Regression의 모듈을 run해서 그래프 형태의 모습을 볼 수 있다.
python -m pip install -U pip setuptools
python -m pip install matplotlib
matplotlib을 이용한, Linear Regression 알고리즘의 그래프 형태 보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import tensorflow as tf import matplotlib.pyplot as plt X = [1, 2, 3] Y = [1, 2, 3] W = tf.placeholder(tf.float32) # Our hypothesis for linear model X * W hypothesis = X * W # cost/Loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) # Variables for plotting cost function W_val = [] cost_val = [] for i in range(-30, 50): feed_W = i * 0.1 curr_cost, curr_W = sess.run([cost, W], feed_dict={W: feed_W}) W_val.append(curr_W) cost_val.append(curr_cost) # Show the cost function plt.plot(W_val, cost_val) plt.show() | cs |
derivative를 사용한 Gradient Descent 알고리즘 최소화 방법
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import tensorflow as tf x_data = [1, 2, 3] y_data = [1, 2, 3] W = tf.Variable(tf.random_normal([1]), name='weight') X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) # Our hypothesis for linear model X * W hypothesis = X * W # cost/Loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize: Gradient Descent using derivative: W -= Learning_rate * derivative learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X) descent = W - learning_rate * gradient update = W.assign(descent) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) for step in range(21): sess.run(update, feed_dict={X: x_data, Y: y_data}) print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W)) | cs |
W는 점점 1로 수렴하는 모습을 확인할 수 있다.
즉, matplotlib으로 확인한 그래프에서 볼 수 있듯이 x가 어디 위치든 결국 W가 1로 가는 곳으로 움직인다.
'딥러닝(Deep-Learning)' 카테고리의 다른 글
[딥러닝] 머신러닝 & Regression 정리 (0) | 2018.05.28 |
---|---|
[딥러닝] Linear Regression 코드 실행 및 코드 분석 (0) | 2018.05.14 |
[딥러닝] 텐서플로우(Tensorflow)로 간단한 선형 회귀(Linear regression)을 구현 (0) | 2018.05.08 |
[딥러닝] 텐서플로우 설치 및 기본 오퍼레이션 (0) | 2018.05.08 |
[딥러닝] 머신러닝 기본 용어 및 개념 (0) | 2018.05.08 |