Skip to content

Latest commit

ย 

History

History
207 lines (125 loc) ยท 5.28 KB

Linear regression ์‹ค์Šต.md

File metadata and controls

207 lines (125 loc) ยท 5.28 KB

[๋”ฅ๋Ÿฌ๋‹] Tensorflow๋กœ ๊ฐ„๋‹จํ•œ Linear regression ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„


์‹œํ—˜ ์ ์ˆ˜๋ฅผ ์˜ˆ์ƒํ•ด์•ผ ํ•  ๋•Œ (0~100) > regression์„ ์‚ฌ์šฉ

regression์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž



์—ฌ๋Ÿฌ x์™€ y ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๋ฉฐ ๊ฐ€์žฅ ๊ทผ์ ‘ํ•˜๋Š” ์„ ํ˜•(Linear)์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

์ด ์„ ํ˜•์„ ํ†ตํ•ด์„œ ์•ž์œผ๋กœ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” x ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ€์žฅ ๊ทผ์ ‘ํ•œ y ๊ฐ’์„ ์ถœ๋ ฅํ•ด๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.


ํ˜„์žฌ ํŒŒ๋ž€ ์„ ์ด ๊ฐ€์„ค H(x)์— ํ•ด๋‹นํ•œ๋‹ค.

์‹ค์ œ ์ž…๋ ฅ ๊ฐ’๋“ค (1,1) (2,2) (3,3)๊ณผ ์„ ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋น„๊ตํ•ด์„œ ๊ทผ์ ‘ํ• ์ˆ˜๋ก ์ข‹์€ ๊ฐ€์„ค์„ ํ–ˆ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.



์ด๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ Hypothesis(๊ฐ€์„ค)์„ ์„ธ์›Œ cost(๋น„์šฉ)์„ ๊ตฌํ•ด W์™€ b์˜ ๊ฐ’์„ ๋„์ถœํ•ด์•ผ ํ•œ๋‹ค.


Linear regression ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ตœ์ข… ๋ชฉ์  : cost ๊ฐ’์„ ์ตœ์†Œํ™”ํ•˜๋Š” W์™€ b๋ฅผ ์ฐพ์ž


  • H(x) : ๊ฐ€์„ค

  • cost(W,b) : ๋น„์šฉ

  • W : weight

  • b : bias

  • m : ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜

  • H(x^(i)) : ์˜ˆ์ธก ๊ฐ’

  • y^(i) : ์‹ค์ œ ๊ฐ’


(์˜ˆ์ธก๊ฐ’ - ์‹ค์ œ๊ฐ’)์˜ ์ œ๊ณฑ์„ ํ•˜๋Š” ์ด์œ ๋Š”?

์–‘์ˆ˜๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ๊ณ , ์Œ์ˆ˜๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค. ๋˜ํ•œ ์ œ๊ณฑ์„ ํ•˜๋ฉด, ๊ฑฐ๋ฆฌ๊ฐ€ ๋” ๋จผ ๊ฒฐ๊ณผ์ผ ์ˆ˜๋ก ๊ฐ’์€ ๋”์šฑ ์ปค์ง€๊ฒŒ ๋˜์–ด ํŒจ๋„ํ‹ฐ๋ฅผ ๋” ์ค„ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.


์ด์ œ ์‹ค์ œ๋กœ, ํŒŒ์ด์ฌ์„ ์ด์šฉํ•ด์„œ Linear regression์„ ๊ตฌํ˜„ํ•ด๋ณด์ž



๋ฏธ๋ฆฌ x์™€ y ๊ฐ’์„ ์ฃผ์—ˆ์„ ๋•Œ

import tensorflow as tf
 
# 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 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))

x_train = [1, 2, 3]
y_train = [1, 2, 3]

2000๋ฒˆ ๋Œ๋ฆฐ ๊ฒฐ๊ณผ, [W = 1, b = 0]์œผ๋กœ ์ˆ˜๋ ดํ•ด๊ฐ€๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ, H(x) = (1)x + 0๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.



optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)

์ตœ์†Œํ™” ๊ณผ์ •์—์„œ ๋‚˜์˜ค๋Š” learning_rate๋Š” ๋ฌด์—‡์ธ๊ฐ€?

GradientDescent๋Š” Cost function์ด ์ตœ์†Œ๊ฐ’์ด ๋˜๋Š” ์ตœ์ ์˜ ํ•ด๋ฅผ ์ฐพ๋Š” ๊ณผ์ •์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์ด๋•Œ ๋‹ค์Œ point๋ฅผ ์–ด๋Š ์ •๋„๋กœ ์˜ฎ๊ธธ ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์„ learning_rate๋ผ๊ณ  ํ•œ๋‹ค.


learning rate๋ฅผ ๋„ˆ๋ฌด ํฌ๊ฒŒ ์žก์œผ๋ฉด?

  • ์ตœ์ ์˜ ๊ฐ’์œผ๋กœ ์ˆ˜๋ ดํ•˜์ง€ ์•Š๊ณ  ๋ฐœ์‚ฐํ•ด๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒ(Overshooting)

learning rate๋ฅผ ๋„ˆ๋ฌด ์ž‘๊ฒŒ ์žก์œผ๋ฉด?

  • ์ˆ˜๋ ดํ•˜๋Š” ์†๋„๊ฐ€ ๋„ˆ๋ฌด ๋Š๋ฆฌ๊ณ , local minimum์— ๋น ์งˆ ํ™•๋ฅ  ์ฆ๊ฐ€

๋ณดํ†ต learning_rate๋Š” 0.01์—์„œ 0.5๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„๋ณด์ธ๋‹ค.



placeholder๋ฅผ ์ด์šฉํ•ด์„œ ์‹คํ–‰๋˜๋Š” ๊ฐ’์„ ๋‚˜์ค‘์— ๋˜์ ธ์ค„ ๋•Œ

import tensorflow as tf
 
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
 
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, 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)

feed_dict = {X: [1, 2, 3, 4, 5],
            Y: [2.1, 3.1, 4.1, 5.1, 6.1]})

2000๋ฒˆ ๋Œ๋ฆฐ ๊ฒฐ๊ณผ, [W = 1, b = 1.1]๋กœ ์ˆ˜๋ ดํ•ด๊ฐ€๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, H(x) = (1)x + 1.1๋กœ ํ‘œํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.



์ด ๊ตฌํ˜„๋œ ๋ชจ๋ธ์„ ํ†ตํ•ด x๊ฐ’์„ ์ž…๋ ฅํ•ด์„œ ๋„์ถœ๋˜๋Š” y๊ฐ’์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ๋‹ค.