본문 바로가기
Dev/딥러닝

04-1. Tensor Flow 변수가 여러개일때 Linear regression

by bsion 2018. 8. 17.
04-1. Multi-variable Linear Regression

출처 : 모두를위한 머신러닝 (http://hunkim.github.io/ml/)


이론





변수가 여러개일때는 Matrix 를 이용


Matrix 사용 안한 코드

In [9]:
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.]

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 % 500 == 0:
        print("Step : ", step, "\nCost : ", cost_val, "\nPrediction : \n", hy_val, "\nOriginal : \n", y_data)
        print("----------------------------------------------")
Step :  0 
Cost :  2243.66 
Prediction : 
 [ 115.27577209  130.34362793  132.64096069  144.92398071   96.93706512] 
Original : 
 [152.0, 185.0, 180.0, 196.0, 142.0]
----------------------------------------------
Step :  500 
Cost :  15.9657 
Prediction : 
 [ 156.31207275  181.17593384  181.9432373   198.68035889  136.0291748 ] 
Original : 
 [152.0, 185.0, 180.0, 196.0, 142.0]
----------------------------------------------
Step :  1000 
Cost :  12.3782 
Prediction : 
 [ 155.61561584  181.6554718   181.73249817  198.50715637  136.67601013] 
Original : 
 [152.0, 185.0, 180.0, 196.0, 142.0]
----------------------------------------------
Step :  1500 
Cost :  9.63831 
Prediction : 
 [ 155.0085144   182.07373047  181.54910278  198.35398865  137.24223328] 
Original : 
 [152.0, 185.0, 180.0, 196.0, 142.0]
----------------------------------------------
Step :  2000 
Cost :  7.54508 
Prediction : 
 [ 154.47941589  182.43850708  181.3895874   198.21835327  137.73800659] 
Original : 
 [152.0, 185.0, 180.0, 196.0, 142.0]
----------------------------------------------

Matrix 를 사용한 코드

In [11]:
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.]]

X = tf.placeholder(tf.float32, shape=[None, 3])   # None : row 가 몇일지 모르는 상태 (numpy 의 경우 -1)
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

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={X: x_data, Y: y_data})
    
    if step % 500 == 0:
        print("Step : ", step, "\nCost : ", cost_val, "\nPrediction : \n", hy_val, "\nOriginal : \n", y_data)
        print("----------------------------------------------")
Step :  0 
Cost :  22.3722 
Prediction : 
 [[ 144.86044312]
 [ 181.85890198]
 [ 174.84020996]
 [ 194.50100708]
 [ 137.29353333]] 
Original : 
 [[152.0], [185.0], [180.0], [196.0], [142.0]]
----------------------------------------------
Step :  500 
Cost :  5.06568 
Prediction : 
 [[ 148.68766785]
 [ 186.15722656]
 [ 179.23652649]
 [ 199.21725464]
 [ 140.55636597]] 
Original : 
 [[152.0], [185.0], [180.0], [196.0], [142.0]]
----------------------------------------------
Step :  1000 
Cost :  4.85262 
Prediction : 
 [[ 148.83230591]
 [ 186.06314087]
 [ 179.28727722]
 [ 199.20394897]
 [ 140.47511292]] 
Original : 
 [[152.0], [185.0], [180.0], [196.0], [142.0]]
----------------------------------------------
Step :  1500 
Cost :  4.6673 
Prediction : 
 [[ 148.96122742]
 [ 185.97973633]
 [ 179.33311462]
 [ 199.18768311]
 [ 140.40737915]] 
Original : 
 [[152.0], [185.0], [180.0], [196.0], [142.0]]
----------------------------------------------
Step :  2000 
Cost :  4.50371 
Prediction : 
 [[ 149.07643127]
 [ 185.90570068]
 [ 179.37472534]
 [ 199.16896057]
 [ 140.35145569]] 
Original : 
 [[152.0], [185.0], [180.0], [196.0], [142.0]]
----------------------------------------------


댓글