북극곰의 개발일기

모두를 위한 딥러닝 lecture 4 - 여러개의 입력(feature)의 Linear Regression (실습)





posted by purplebeen on Mon Jan 28 2019 21:33:46 GMT+0900 (KST) in AI


x1(quiz 1) x2 (quiz 2) x3(miterm1) Y (final)
73 80 75 152
93 88 93 185
89 91 90 180
96 98 100 196
73 66 70 142

H(x) = x1w1 + x2w2 + x3w3

기본적인 방법

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., 180., 196., 142.]
#Placeholders for a tensor that will be always fed.
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.randoml_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 / loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
# Minimize, Need a very small Learning 
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 1e-5)
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())
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)

결과

1950 Cost :  13.574587 
Prediction:
 [145.334   188.44484 178.25494 198.5504  143.42151]
1960 Cost :  13.525175 
Prediction:
 [145.34593 188.43672 178.25871 198.55208 143.41174]
1970 Cost :  13.476076 
Prediction:
 [145.35786 188.42868 178.26251 198.55382 143.40204]
1980 Cost :  13.427165 
Prediction:
 [145.36975 188.42062 178.2663  198.55551 143.39233]
1990 Cost :  13.378581 
Prediction:
 [145.38158 188.41257 178.27002 198.55717 143.38268]
2000 Cost :  13.330137 
Prediction:
 [145.39346 188.4046  178.27382 198.55891 143.37306]

과정이 너무 복잡하다는 것을 알 수 있다. 이를 해결하기 위하면 어떻게 해야 할까?

행렬(Matrix)를 이용하여 해결 할 수 있다.

행렬을 이용해서 예측하기

import tensorflow as tf
x_data = [[73., 80., 75.], [93., 88., 93.], 
         [89., 91., 90.], [96., 98., 100], [73., 66., 70.]]
y_data = [[152.], [185.], [180.], [196.], [142.]]
# placeholders for a tensor that will be alwas fed.
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
hypothesis = tf.matmul(X, W) + b
#Simplified cost / loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
#Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
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())
for step in range(2001):
    cost_val, hy_val, _ = sess.run(
    [cost, hypothesis, train], feed_dict={X: x_data, Y:y_data})
    if step % 10 == 0:
        print(step, "Cost : ", cost_val, "\nPrediction:\n", hy_val)

결과

1940 Cost :  21.969662 
Prediction:
 [[147.4178 ]
 [187.8624 ]
 [179.94542]
 [191.43684]
 [149.73518]]
1950 Cost :  21.873262 
Prediction:
 [[147.43358]
 [187.85147]
 [179.9501 ]
 [191.44124]
 [149.72   ]]
1960 Cost :  21.777376 
Prediction:
 [[147.4493 ]
 [187.84059]
 [179.95477]
 [191.44563]
 [149.70485]]
1970 Cost :  21.682068 
Prediction:
 [[147.46503]
 [187.82977]
 [179.95949]
 [191.45003]
 [149.68979]]
1980 Cost :  21.587215 
Prediction:
 [[147.48065]
 [187.81894]
 [179.96414]
 [191.4544 ]
 [149.67473]]
1990 Cost :  21.492887 
Prediction:
 [[147.49625]
 [187.8081 ]
 [179.9688 ]
 [191.45877]
 [149.65973]]
2000 Cost :  21.399042 
Prediction:
 [[147.51183]
 [187.79735]
 [179.97346]
 [191.46312]
 [149.64476]]

아까보다 훨씬 더 과정이 단순해지고 코드가 짧아진 것을 알 수 있다.