AI/모두를 위한 ML (SungKim)

[ML lab 02] TensorFlow로 간단한 linear regression을 구현

ferozsun 2021. 2. 15. 11:18

X가 1일 때 Y가 1, X가 2일 때 Y가 2, X가 3일 때 Y가 3인 값들을 보고 학습한 후

cost function이 최소가 되는 hypothesis의 W(weight)와 b(bias)예측해 보라는 예제이다.

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

## 그래프 구현

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

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

## 그래프 실행

# 실행을 위해 그래프의 세션을 만듦
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))

 

W와 b는 Variable 노드로 정의한다.

Variable은 tensorflow가 사용하는 변수라는 의미이다.

tensorflow를 실행시키면 자체적으로 변경시키는 값이다. (= trainable variable)

Variable을 정의할 때는 shape을 정의하고, 값을 주면 되는데 W, b는 보통 랜덤값을 준다.

 

random_normal() # random 값을 반환한다.

 

shape이 [1]이면 element가 하나인 1차원 vector를 의미한다.

 

reduce_mean() # 평균 계산

ex.) t = [1., 2., 3., 4.]

     tf.reduce_mean(t) ==> 2.5

 

minimize(cost) # variable W, b를 조정하여 cost를 minimize함

 

W, b variable을 실행하기 전에는 반드시 global_variables_initializer()를 실행해야 한다.

 

노드 train을 2000번 정도의 step을 주어 실행시킨다.

20번 마다 한 번씩 값이 출력되도록 한다. (cost, W, b)

 

대략 H(x) = x 으로 train 된다. (W=1, b=0)

 

[출력]


placeholder 노드를 쓰면 x, y값을 미리 선언하지 않고 feed_dict를 통해 넘겨줄 수 있다.

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

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]) # None은 무엇이든 될 수 있다는 의미
Y = tf.placeholder(tf.float32, shape = [None])

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

# 실행을 위해 그래프의 세션을 만듦
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], # 리스트에 넣어 한번에 run 가능
        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)

직접 X, Y train data를 사용하지 않고 X, Y를 placeholder 노드로 만들어서 필요할 때 값을 주는 방법이다.

train할 때 feed_dict를 통해 값을 넘겨준다.

 

대략 H(x) = x + 1 으로 train 됨 (W=1, b=1)

 

[출력]


train이 잘 됐는지 확인

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

[출력]