본문 바로가기
IT 기록/머신러닝

[머신러닝]04-2.multi-variable linear regression TensorFlow 구현

by Lazy Quant 2018. 7. 23.
반응형

※본 포스팅은 직접 강의하는 내용이 아닌, 김성훈 교수님의 머신러닝 강의를 정리하기 위한 포스팅입니다.

김성훈 교수님의 강의는 모두를 위한 머신러닝/딥러닝(http://hunkim.github.io/ml)에서 들을 수 있습니다.


실습예제

X1(quiz 1) 

X2 (quiz 2) 

X3 (midterm1) 

Y(final) 

73 

80 

75 

152 

93 

88

93 

185 

89 

91 

90 

180 

96 

98 

100 

196 

73 

66

70 

142 

quiz1, quiz2, midterm1의 성적으로 Final score 예측하기


4-1.기존의 방식대로 연산

# Lab 4 Multi-variable linear regression
import tensorflow as tf
tf.set_random_seed(777)  # for reproducibility

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.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
print(hypothesis)

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

# Minimize. Need a very small learning rate for this data set
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)

 위와 같이 기존에 이용했던 방식으로도 연산은 가능하지만, 입력 데이터 값에 대한 소스코드가 너무 복잡해진다. 이 부분을 더 간단하게 표현하기 위해 Matrix를 사용한다.



4-2.Matrix를 사용한 연산

# Lab 4 Multi-variable linear regression import tensorflow as tf tf.set_random_seed(777) # for reproducibility 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 always 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)

Matrix를 사용하면, Variable의 개수와 상관없이 간편하게 표현할 수 있다.


참고) shape=[None, 3] : None은 갯수를 알수 없다는 것을 의미한다. 연산 때마다 데이타의 양이 다를 수 있기 때문에, None으로 해놓으면 들어오는 숫자에 맞춰서 저장을 한다.


참고) tf.matmul두 텐서를 행렬곱셈하여 결과 텐서를 리턴합니다.

반응형

댓글