[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 03. Linear Regression ์ cost ์ต์ํ์ TensorFlow ๊ตฌํ
๋ชฉ์ฐจ
- Cost function ๊ทธ๋ํ
- Gradient descent algorithm ์ ์ฉ
- Optimizer ์ ์ฉ
Cost function ๊ทธ๋ํ

์ด์ ํฌ์คํธ์์ ์ฐ๋ฆฌ๋ Cost function์ ๋ฏธ๋ถ์ ๊ฐ๋จํ๊ฒ ํ๊ธฐ ์ํด์ ์์ ๊ฐ์ ์ถ์ฝ ์์ ์ฌ์ฉํ์๋ค.
[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 03. Linear Regression์ cost ์ต์ํ ์๊ณ ๋ฆฌ์ฆ์ ์๋ฆฌ ์ค๋ช
๋ชฉ์ฐจ Minimize Cost function Gradient descent algorithm Convex function Minimize Cost function ์ง๋๋ฒ ํฌ์คํธ์์ Hypothesis์ Cost function์ ์์๋ณด์๊ณ , ์ฐ๋ฆฌ๋ Cost function์ ์ต์ํ์ํค๋ W์ b๋ฅผ..
cjwoov.tistory.com
ํ์ด์ฌ์ ํตํด์ ์ง์ Cost function ์์ ํํํ๊ณ ๊ทธ๋ํ๋ก ๊ทธ๋ ค ๋ณผ ๊ฒ์ธ๋ฐ, ๊ทธ๋ํ๋ก ๊ทธ๋ฆฌ๊ธฐ ์ํด์ mtplotlib์ด๋ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น๊ฐ ํ์ํ๋ค.
matplotlib ์ค์น
python -m pip install -U pip python -m pip install -U matplotlib
ํ์ด์ฌ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
import tensorflow as tf import matplotlib.pyplot as plt X = [1, 2, 3] Y = [1, 2, 3] W = tf.placeholder(tf.float32) hypothesis = X * W cost = tf.reduce_mean(tf.square(hypothesis - Y)) sess = tf.Session() sess.run(tf.global_variables_initializer()) 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) plt.plot(W_val, cost_val) plt.show()
๊ฐ๋จํ๊ฒ ์ค๋ช ํ์๋ฉด [๊ทธ๋ฆผ 1]์ ๊ทธ๋๋ก Hypothesis๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ W์ ๋ฒ์๋ฅผ -3๋ถํฐ 5๊น์ง 0.1์ฉ ๋๋์ด์
๊ทธ๋ํ๋ก ๊ทธ๋ ค ์ค ์ฝ๋๋ค.

๊ทธ๋ฆผ์ ๋ณด๋ฉด ์๊ฒ ์ง๋ง cost๋ฅผ ์ต์ํํ๋ W์ ๊ฐ์ 1์ด๋ค.
Gradient descent algorithm ์ ์ฉ

Gradient descent algorithm์ ์์ [๊ทธ๋ฆผ 3]๊ณผ ๊ฐ๊ณ ์๊ณ ๋ฆฌ์ฆ์ ์ ์ฉํ๋ค๋ ๊ฒ์ W๊ฐ์ ์กฐ์ ํด ๋๊ฐ๋ฉฐ cost์ ์ต์๊ฐ์ ์ฐพ์๋๊ฐ๋ค๋ฅผ ์๋ฏธํ๋ค.
learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X) descent = W - learning_rate * gradient update = W.assign(descent)
[๊ทธ๋ฆผ 3]์ ์์์ ์ํ ๊ฐ์ learning rate๋ฅผ ์๋ฏธํ๊ณ ํ์ด์ฌ ์ฝ๋๋ก ๋ํ๋ด๋ฉด ์์ ๊ฐ๋ค.
ํ ์ํ๋ก์ฐ์์๋ W์ ๊ฐ์ ํ ๋นํ ๋ equal ์ฐ์ฐ์(=)๋ ์ฌ์ฉ์ด ์๋๋ฉฐ assign์ด๋ผ๋ ํจ์๋ฅผ ํตํด W์ ํ ๋น ๊ฐ๋ฅํ๋ค.
Gradient descent algorithm์ ์ ์ฉํ ์ ์ฒด ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
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) hypothesis = X * W cost = tf.reduce_mean(tf.square(hypothesis - Y)) learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X) descent = W - learning_rate * gradient update = W.assign(descent) sess = tf.Session() 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))
learning_rate(=์ํ ๊ฐ)์ 0.1๋ก ์ฃผ์๊ณ , ๊ทธ๋ค์๋ถํฐ [๊ทธ๋ฆผ 3]์ ์์ ๊ทธ๋๋ก ์ฝ๋๋ก ํํํ์๋ค.
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ๋ค.
0 0.6850602 [0.61685693] 1 0.1948616 [0.79565704] 2 0.05542727 [0.8910171] 3 0.01576599 [0.94187576] 4 0.0044845473 [0.9690004] 5 0.0012756082 [0.98346686] 6 0.00036283964 [0.9911823] 7 0.000103206636 [0.99529725] 8 2.9357458e-05 [0.99749184] 9 8.350654e-06 [0.9986623] 10 2.3756713e-06 [0.99928653] 11 6.756982e-07 [0.9996195] 12 1.9224537e-07 [0.99979705] 13 5.4676246e-08 [0.99989176] 14 1.5574232e-08 [0.99994224] 15 4.4351474e-09 [0.9999692] 16 1.2629471e-09 [0.99998355] 17 3.5721945e-10 [0.99999124] 18 9.976494e-11 [0.99999535] 19 2.984753e-11 [0.9999975] 20 7.716494e-12 [0.9999987]
Gradient descent algorithm์ ์ ์ฉํด ๋๊ฐ์๋ก cost๋ 0(์ต์๊ฐ)์ ๊ฐ๊น์์ง๊ณ W๋ 1์ ์๋ ดํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
Optimizer ์ ์ฉ
์์์ ์ฐ๋ฆฌ๋ Gradient descent algorithm์ ์ ์ฉํ๊ธฐ์ํด ์๋์ ๊ฐ์ด ๋ฏธ๋ถ์์ ์ฝ๋๋ก ํํํ์๋ค.
learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X) descent = W - learning_rate * gradient update = W.assign(descent)
ํ์ง๋ง ์ฐ๋ฆฌ๊ฐ ์ผ์ผํ ๋งค์ผ ๋ฏธ๋ถ ์์ ๋ณต์กํ๊ฒ ๊ณ์ฐํ๊ณ ํํํ๋ ์ผ์ ๋งค์ฐ ๋ฒ๊ฑฐ๋ก์ด ์ผ์ด๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ํ ์ํ๋ก์ฐ์๋ GradientDescentOptimizer๋ผ๋ ํจ์๊ฐ ์๋๋ฐ ์ด ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ์๋์ ๊ฐ์ด ๊ฐ๋จํ๊ฒ ํํ์ด ๊ฐ๋ฅํ๋ค.
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1) train = optimizer.minimize(cost)
GradientDescentOptimizer๋ฅผ ๋ง๋ค์ด ์ฃผ๊ณ cost ํจ์๋ฅผ optimizer์ minimizeํจ์์ ๋ฃ์ด์ฃผ๊ธฐ๋ง ํ๋ฉด ๋๋ค!
์ด๋ฅผ ์ ์ฉํ ์ ์ฒด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
import tensorflow as tf X = [1, 2, 3] Y = [1, 2, 3] W = tf.Variable(5.0) hypothesis = X * W cost = tf.reduce_mean(tf.square(hypothesis - Y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1) train = optimizer.minimize(cost) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(10): print(step, sess.run(W)) sess.run(train)
๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ๋ค.
0 5.0 1 1.2666664 2 1.0177778 3 1.0011852 4 1.000079 5 1.0000052 6 1.0000004 7 1.0 8 1.0 9 1.0
ํ์ต์ ๊ฑฐ๋ญํ ์๋ก ์ฐ๋ฆฌ๊ฐ ์ฐพ๊ณ ์ ํ๋ W๊ฐ์ ๊ฐ๊น์์ง๋ ๊ฒ์ ๋ณผ ์ ์๋ค!!! :)
์ฐธ๊ณ ์๋ฃ
https://youtu.be/Y0EF9VqRuEA
Sung Kim- ML lab 03.Linear Regression์ cost ์ต์ํ์ TensorFlow ๊ตฌํ
'Development > Machine Learning' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋ค๋ฅธ ๊ธ
-
[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 03. Linear Regression์ cost ์ต์ํ ์๊ณ ๋ฆฌ์ฆ์ ์๋ฆฌ ์ค๋ช
[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 03. Linear Regression์ cost ์ต์ํ ์๊ณ ๋ฆฌ์ฆ์ ์๋ฆฌ ์ค๋ช
2019.08.07๋ชฉ์ฐจ Minimize Cost function Gradient descent algorithm Convex function Minimize Cost function ์ง๋๋ฒ ํฌ์คํธ์์ Hypothesis์ Cost function์ ์์๋ณด์๊ณ , ์ฐ๋ฆฌ๋ Cost function์ ์ต์ํ์ํค๋ W์ b๋ฅผ ๊ตฌํ๋ ๊ฒ์ ๊ณผ์ ๋ก ์ผ์๋ค. ์ค๋ช ์ ์ํด์, Hypothesis๋ฅผ ์์ ๊ฐ์ด ๊ฐ๋จํ๊ฒ ๋ง๋ค์๋ค. (b = 0) ๋ํ ๋ฐ์ดํฐ๊ฐ ์๋ ํ์ ๊ฐ์ด ์ฃผ์ด์ง๋ค๊ณ ๊ฐ์ ํด๋ณด์. X Y 1 1 2 2 3 3 ์ฐ๋ฆฌ๋ cost๋ฅผ ์ต์ํ ์ํค๋ ์ง์ ์ ์ฐพ์์ผ ํ๋ค.(=์ต์ํ์ํค๋ W๋ฅผ ์ฐพ์์ผ ํจ) W = 1, cost(W) = 0 W = 0, cost(W) = 4.67 W = 2, cost(W) = 4.67 W์ ๋ฐ๋ฅธ cost(โฆ -
[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 02. TensorFlow๋ก Linear regression ๊ตฌํ
[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 02. TensorFlow๋ก Linear regression ๊ตฌํ
2019.07.04๋ชฉ์ฐจ Build graph using TensorFlow operations Place holders Build graph using TensorFlow operations Linear regression์ ๊ตฌํํ์ฌ ๊ทธ๋ํ๋ฅผ ๋ง๋ค๊ธฐ ์ํด ์์ ๊ทธ๋ฆผ์ ๋ณด๋ฉด์ ์ง๋ ๋ด์ฉ์ ๋ณต์ตํ๋๋ก ํ์. Hypothesis์ Cost function์ ์์ [๊ทธ๋ฆผ1]๊ณผ ๊ฐ๊ณ , ํ ์ํ๋ก์ฐ์ ๋ฉ์ปค๋์ฆ์ [๊ทธ๋ฆผ 2]์ ๊ฐ๋ค. # X and Y data x_train = [1, 2, 3] y_train = [1, 2, 3] W = tf.Variable(tf.random_normal([1]), name='weight') b = tf.Variable(tf.random_normal([1]), name='bias') # Our hyphโฆ -
[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 02. Linear Regression์ Hypothesis์ Cost
[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 02. Linear Regression์ Hypothesis์ Cost
2019.07.04๋ชฉ์ฐจ Linear Regression Cost function Linear Regression ์ฐ๋ฆฌ๊ฐ ์์ธก์ ํ๋ ค๋ ์ต์ข ๋ชฉํ๊ฐ ์ ์(0์ ์์ 100์ ์ฌ์ด)๋ผ๊ณ ํ ๋ ์ด๋ฐ ํํ์ ๋จธ์ ๋ฌ๋์ Supervised Learning ์ค์์๋ Regression์ด๋ผ ํ ์ ์๋ค. x (hours) y (score) 10 90 9 80 3 50 2 30 ์์ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ํตํด ํ์ต์ ์ํค๋ ๊ฒ์ training์ด๋ผ ํ๊ณ , training์ด ๋๋ ํ์ Regression์ ์ฌ์ฉํ๋ค๋ ๊ฒ์ ์ด๋ค ํ์์ด '๋๋ 7์๊ฐ ์ ๋ ๊ณต๋ถํ๋๋ฐ ๋ช ์ ์ด๋ ๋ฐ์ ์ ์์๊นโฆ?' ์ง๋ฌธ์ ํ๋ค๊ณ ํ๋ฉด ์ํ์ ์ผ๋ก๋ x๋ผ๋ ๊ฐ์ 7์ ๋์ง๊ณ Regression์ด ๊ฒฐ๊ณผ(y)๋ฅผ์์ธก์ ํด์ฃผ๋ ๊ฒ์ ๋งํ๋ค. ์์ ์ผ์ชฝ ํ์ ๊ฐ์ ๊ฐ๋จํ โฆ -
[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 01. TensorFlow์ ๊ธฐ๋ณธ์ ์ธ operations
[๋จธ์ ๋ฌ๋ ์ ๋ฌธ] 01. TensorFlow์ ๊ธฐ๋ณธ์ ์ธ operations
2019.07.01๋ชฉ์ฐจ ํ ์ํ๋ก์ฐ(TensorFlow)? ๋ฐ์ดํฐ ํ๋ก์ฐ ๊ทธ๋ํ(Data Flow Graph) ํ ์ํ๋ก์ฐ ์ค์น(Install TensorFlow) ๊ธฐ๋ณธ์ ์ธ ๋ช ๋ น์ด ์ฐ์ต Ranks, Shapes, Types ํ ์ํ๋ก์ฐ(TensorFlow)? ๊ตฌ๊ธ์์ ๋ง๋ ๋จธ์ ๋ฌ๋์ ์ํ ์คํ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค. ๋ฐ์ดํฐ ํ๋ก์ฐ ๊ทธ๋ํ(data flow graph)๋๊ฒ์ ์ฌ์ฉํด์ numerical ํ ๊ณ์ฐ์ ํ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ , ๋ง์ ์ฌ๋๋ค์ด ์ ํธํ๋ Python์ด๋ผ๋ ์ธ์ด๋ฅผ ๊ฐ์ง๊ณ ํ ์ํ๋ก์ฐ ํ๋ก๊ทธ๋๋ฐ์ ํ ์ ์๋ค. ์์ ๊ทธ๋ฆผ๋ง ๋ณด๋๋ผ๋ 2018๋ ๊ธฐ์ค์ด๊ธด ํ์ง๋ง ํ ์ํ๋ก์ฐ๊ฐ ์๋์ ์ผ๋ก 1์๋ฅผ ์ฐจ์งํ๊ณ ์์์ ์ ์ ์๋ค. ๋ฐ์ดํฐ ํ๋ก์ฐ ๊ทธ๋ํ(Data Flow Graph) Node(๋ ธ๋)์ Edge(์ฃ์ง)๋ก ๊ตฌ์ฑ๋์ด์๋ ๊ฒโฆ
๋๊ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.