본문 바로가기

Deep Learning

여러 feature의 linear regression

개수가 많은 데이터를 학습시킬 때 다음과 같이 작성할 수 있다.

 

import tensorflow as tf

x1_data = [73., 93., 89., 96., 73.]
x2_data = [80., 88., 91., 98., 66.]
x3_data = [75., 93., 90., 100., 70.]
Y_data = [152., 185., 100., 196., 142.]

x1 = tf.placeholder(tf.float32)
x2 = tf.placeholder(tf.float32)
x3 = tf.placeholder(tf.float32)

Y = tf.placeholder(tf.float32)

w1 = tf.Variable(tf.random_normal([1]), name='weight1')
w2 = tf.Variable(tf.random_normal([1]), name='weight2')
w3 = tf.Variable(tf.random_normal([1]), name='weight3')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = x1 * w1 + x2 * w2 + x3 * w3 + b

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

optimizer = tf.train.GradientDescentOptimizer(learning_rate = 1e-5)
train = optimizer.minimize(cost)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for step in range(2001):
    cost_val, hy_val, _ = sess.run([cost, hypothesis, train],
                                  feed_dict={x1: x1_data, x2: x2_data, x3: x3_data, Y: Y_data})
    if step % 10 == 0 :
        print(step, "Cost:", cost_val, "\nPrediction:\n", hy_val)

 

이렇게 복잡한 코드를 행렬의 장점을 이용하여 아래와 같이 바꿀 수 있다. 

 

x_data = [[73., 80., 75.], [93., 88., 93.],
         [89., 91., 90.], [96., 98., 100.], [73., 66., 70.]]
y_data = [[152.], [185.], [180.], [196.], [142.]]

# X:[n,3], Y:[n,1]
X = tf.placeholder(tf.float32, shape=[None,3])
Y = tf.placeholder(tf.float32, shape=[None,1])

W = tf.Variable(tf.random_normal([3,1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

hypothesis = tf.matmul(X,W)+b

 

두 코드 모두 같은 결과를 출력한다.

 

0 Cost: 9358.304 
Prediction:
 [56.220093 68.97193  67.38192  72.349014 53.672737]
10 Cost: 1027.9869 
Prediction:
 [136.92758 166.03384 162.98839 176.46982 127.71447]
20 Cost: 1027.5779 
Prediction:
 [137.14125 166.34804 163.26773 176.78177 127.96201]
30 Cost: 1027.2476 
Prediction:
 [137.11148 166.36948 163.25873 176.77968 127.98623]
40 Cost: 1026.9187 
Prediction:
 [137.08101 166.38994 163.24884 176.77661 128.00967]
50 Cost: 1026.5908 
...
...
...
 [132.45676 169.48291 161.7272  176.44595 131.4295 ]
1970 Cost: 985.27606 
Prediction:
 [132.43803 169.49536 161.72092 176.44533 131.44258]
1980 Cost: 985.1403 
Prediction:
 [132.41937 169.50777 161.71466 176.44473 131.45563]
1990 Cost: 985.00555 
Prediction:
 [132.40074 169.52014 161.70842 176.44414 131.46861]
2000 Cost: 984.87146 
Prediction:
 [132.38214 169.5325  161.7022  176.44353 131.48157]

 

학습이 진행될 수록 cost가 낮아지는 것을 확인할 수 있다.

 

※ inflearn 모두를 위한 딥러닝 강좌를 듣고 정리한 내용입니다.