DeepLearning_L02 - 8BitsCoding/RobotMentor GitHub Wiki

Menu


์ด๋ก 

๊ฐ€์„ค H(x)๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜ ํ–ˆ๋‹ค๋ฉด

์ด๋ฏธ์ง€

๋ชฉ์  : ๊ฐ€์„ค๊ณผ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ทธ๋ž˜ํ”„๋ฅผ ์ฐพ๋Š”๋‹ค.

์ด๋ฏธ์ง€

์ด๋ฏธ์ง€

์ด๋ฏธ์ง€

๋‹ค์Œ์˜ costํ•จ์ˆ˜๊ฐ€ ์ตœ์†Œ๊ฐ€ ๋œ๋‹ค๋ฉด? -> ๋ชฉ์ ์„ ์ด๋ฃฐ ์ˆ˜ ์žˆ๋‹ค.

์–ด๋–ป๊ฒŒ ์ตœ์†Œํ™” ํ•˜๋Š”์ง€๋Š” ๋‹ค์Œ๊ฐ•์˜์—์„œ ์„ค๋ช…(์ฐธ๊ณ ๋กœ ์•„๋ž˜ ๊ตฌํ˜„๋ถ€์— ์„ค๋ช…์ด ๋˜์–ด ์žˆ์ง€๋งŒ gradient descent algorithm์„ ์ด์šฉํ•œ๋‹ค.)


์‹ค์Šต

๊ธฐ๋ณธ์  import

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

tf.set_random_seed(777) # for reproducibility๋Š” ์•„์ง ๋ญ”์ง€ ๋ชจ๋ฆ„ ์ผ๋‹จ ๋ฌด์‹œ

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

๊ฐ„๋‹จํ•œ x, y๊ฐ’์„ ์ฃผ์–ด์ง€๊ณ 

์ฃผ์–ด์ง„ x, y๋ฐ์ดํ„ฐ์— ๋”ฐ๋ฅธ W, b(Weight, bias)๋ฅผ ์ฐพ์•„๋ณด์ž.

์ด๋ฏธ์ง€

# 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")

tf.Variable์—์„œ ๋งํ•˜๋Š” variable์€ tensorflow๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” variable์„ ์˜๋ฏธ tensorflow๊ฐ€ ๊ฐ’์„ ๋ฐ”๊ฟ”๊ฐ€๋ฉฐ ์‚ฌ์šฉํ•  ๊ฐ’์ž„. (tranninable varible์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค.)

W = tf.Variable(tf.random_normal([1]), name="weight") ํ•ด์„ :

W๋ผ๋Š” tensor๋ฅผ Variable(tensorflow๊ฐ€ ๋ณ€๊ฒฝ๊ฐ€๋Šฅํ•œ ๊ฐ’)์œผ๋กœ ๋‘๊ณ  random_normal๋ฅผ ๋„ฃ๋Š”๋ฐ shape์€ [1]์ด๋‹ค

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

๊ฐ€์„ค์€ ์œ„์™€๊ฐ™์ด ์ •์˜ ํ•  ์ˆ˜ ์žˆ๊ณ 

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

ํ•จ์ˆ˜๋Š” ์œ„์™€ ๊ฐ™์ด ์ •์˜ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฏธ์ง€

tf.reduce_mean๋Š” ํ‰๊ท ์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ์„ ์˜๋ฏธ

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

GradientDescentOptimizer๋ฅผ ํ†ตํ•˜์—ฌ cost(lost)๋ฅผ minimizeํ•œ๋‹ค.

minimize๋Š” ์–ด๋–ป๊ฒŒ ํ•˜๋Š”๋ฐ?? -> ์ง€๊ธˆ์€ tensorflow๊ฐ€ ํ•ด์ฃผ๋Š” ๋งค์ง์ด๋ผ ์ƒ๊ฐ

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

์„ธ์…˜์„ ์ƒ์„ฑ ํ›„ with tf.Session() as sess:

W, b๋ผ๋Š” tensorflow์˜ variable์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  sess.run(tf.global_variables_initializer())๋ฅผ ํ˜ธ์ถœํ•ด ์ฃผ์–ด์•ผํ•œ๋‹ค.

    # Fit the line
    for step in range(2001):
        _, cost_val, W_val, b_val = sess.run([train, cost, W, b])

        # 2001๋ฒˆ ๋„๋Š”๋ฐ 20๋ฒˆ์— ํ•œ ๋ฒˆ์”ฉ ์ถœ๋ ฅํ•ด์ค˜
        if step % 20 == 0:
            print(step, cost_val, W_val, b_val)

์ œ์ผ ๊ถ๊ธˆํ•œ ์ ์€ _, cost_val, W_val, b_val = sess.run([train, cost, W, b])๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€ ์ด๋‹ค

sess.run ์„ธ์„ ์„ ๋Œ๋ฆฌ๋Š”๋ฐ

([train, cost, W, b]) ๊ฐ ๋…ธ๋“œ๋ฅผ ๋Œ๋ ค๋‹ฌ๋ผ

train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

costํ•จ์ˆ˜๋ฅผ ์ตœ์†Œํ™” ํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ์€๋ฐ tensorflow๊ฐ€ ๋ณ€๊ฒฝ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜๋Š” W, b๋กœ ์„ ์–ธํ–ˆ๊ธฐ์— W, b๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉฐ ๋™์ž‘

cost = tf.reduce_mean(tf.square(hypothesis - y_train))

W = tf.Variable(tf.random_normal([1]), name="weight")

b = tf.Variable(tf.random_normal([1]), name="bias")

W, b๋Š” ๋ณ€์ˆ˜๋กœ ์„ ์–ธ


์ „์ฒด์ฝ”๋“œ

# 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))

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

# Launch the graph in a session.
with tf.Session() as sess:
    # 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([train, cost, W, b])

        if step % 20 == 0:
            print(step, cost_val, W_val, b_val)

# 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]
...
1960 1.46397e-05 [ 1.004444] [-0.01010205]
1980 1.32962e-05 [ 1.00423515] [-0.00962736]
2000 1.20761e-05 [ 1.00403607] [-0.00917497]
"""