블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

Multi Variable Linear Regression 강좌를 정리한다. 





Linear Regression 기본 개념

- Hypothesis: H(x) = Wx + b   => Input값을 통해 가설을 세운다. W, b에 따라 선의 모양일 달라진다.

- Cost function: cost(W,b) = H(x) - y 실제값의 차에 대한 제곱에 평균   =>    결과값을 바탕으로 가설을 검증하고 가장 적은 값을 갖는 W, b를 구한다. (참조)

- Gradient descent algorithm: 미분의 기울기에 따른 아래로 수렴되는 알고리즘  => Cost function을 최소화 하기 위한 최적화 알고리즘을 사용한다. (참조)

   -> 그릇처럼 밑으로 수렴되는 Convex Fuction을 사용하여 오류를 제거한다.


기존은 하나의 Input variable만 사용했다면 여러개의 multi variable에 대한 예측



변수(Variable)이 여러개(Multi)일 때 Matrix를 이용한다. Matrix를 사용할 때는 X가 앞에 오고 W가 뒤에 온다. + b 가 없을 때 수식은 아래와 같다. 



(x1, x2, x3)를 하나의 Instance라고 부른다. Instance가 많을 경우 다음과 같은 수식을 사용한다. W는 동일하다. 

- X: [Instance, #x]

- W: [#w, #]

연산시 #x와 #w의 숫자는 같아야 한다. 계산 결과는 [instance, #] 이 된다. 




위의 5값은 instance의 갯수는 가변이므로 n으로 표현한다. 보통 Tensorflow에서 None으로 표현하면 n개를 말한다. 그리고 n Output의 경우일때는 다음과 같다.



이론과 실제 구현상 공식. 수학적 의미는 거의 같다.







Tensorflow 실습하기 


x1, x2, x3 instance가 있고, 결과 Y 하나가 나온다.


 Maxtrix를 이용해서 구현해 본다. (예제)


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)


==> 결과

... 


2000 cost:  7.4047155

prediction:

 [[154.63629]

 [182.36778]

 [181.60802]

 [197.86899]

 [137.86888]]






파일에서 읽어 실행하기 


Python 의 numpy를 이용하여 읽어오고 slicing한다. (예제)


import numpy as np


xy = np.loadtxt('data-01-test-score.csv', delimiter=',', dtype=np.float32)

x_data = xy[:, 0:-1] # slicing

y_data = xy[:, [-1]]


읽을 내용이 많을 경우 한번에 가져오지 않고 Queuing하여 처리하는 Queue Runners를 제공한다. (예제)

Step-1: 여러개의 파일을 읽어온다. 

Step-2: 파일에서 key, value을 가져온다. 

Step-3: 값을 decoding한다.



import tensorflow as tf

tf.set_random_seed(777)  # for reproducibility


filename_queue = tf.train.string_input_producer(['data-01-test-score.csv'], shuffle=False, name='filename_queue')


reader = tf.TextLineReader()

key, value = reader.read(filename_queue)


# Default values, in case of empty columns. Also specifies the type of the

# decoded result.

record_defaults = [[0.], [0.], [0.], [0.]]

xy = tf.decode_csv(value, record_defaults=record_defaults)


# collect batches of csv in

train_x_batch, train_y_batch = tf.train.batch([xy[0:-1], xy[-1:]], batch_size=10) #10 개씩 처리 


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


# Start populating the filename queue.

coord = tf.train.Coordinator()

threads = tf.train.start_queue_runners(sess=sess, coord=coord)


for step in range(2001):

    x_batch, y_batch = sess.run([train_x_batch, train_y_batch])

    cost_val, hy_val, _ = sess.run(

        [cost, hypothesis, train], feed_dict={X: x_batch, Y: y_batch})

    if step % 10 == 0:

        print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)


coord.request_stop()

coord.join(threads)


# Ask my score

print("Your score will be ",

      sess.run(hypothesis, feed_dict={X: [[100, 70, 101]]}))


print("Other scores will be ",

      sess.run(hypothesis, feed_dict={X: [[60, 70, 110], [90, 100, 80]]}))





참고


- 김성훈교수님의 Multi-Variable linear regression 강좌

- Github 예제 4-1

- Github 예제 4-2 using Matrix

- 선형 & 다중 회귀분석


posted by 윤영식