1. Tensorflow 처리의 기본과정 (전문가가 보면 이상한 부분도 있을 수 있음)

   1) Tensorflow는 먼저 가설(hypothesis)으로부터 시작한다..아래는 회귀(Linear Regression) 가설의 기본 식이다.

       


 ==> X에 대한 1차방정식으로 W(Weight, 기울기)와 b(bias,절편) 을 무한으로 변경하면서 비용이 최소값이 

       나오도록 하면 된다.

       위 공식은 tensorflow에서 y_model=tf.multiply(W, X) 으로 표현된다.

     

      위에서 언급된 비용함수는 Cost(W,b) 로 표현되고, 수식은 아래와 같다.

     

   <-- 미분  

       위 수식은 tensorflow에서 cost = tf.reduce_mean(tf.square(Y - y_model)) 로 표현된다.   (Y : 실측치, y_model : 예측치)


    결론 : 위 식에서 cost(비용)이 최소화가 되는 y_model을 찾는 것이다. (x와 b를 찾는 과정이다.)


2. 선행설치

   - 그래프를 그리기 위해 matplot라이브러리를 설치한다. : pip install matplotlib

   - 데이타를 tensor형태(1차원->2차원) 형태로 자유자재로 만들 수 있는 numpy를 설치한다. : pip install numpy

   - python을 편하게 실행하기 위해 jupyter notebook을 설치한다. : pip install jupyter

    


3. 예제소스 (python코드입력을 위해 command 창에서 jupyter notebook 입력-> 서버가 구동되면서 브라우저 창이 뜬다)

   : 미국 Portland, Oregon 집 크기에 따른 가격을 유추할 수 있는 단순회귀 예제임. 

   - 참고 : python 3.5 기준의 소스

import tensorflow as tf
import numpy
import matplotlib.pyplot as plt

# Train을 위한 data set 이다. 이것은 실제 발생한 데이타라고 보면 된다.

size_data = numpy.asarray([ 2104,  1600,  2400,  1416,  3000,  1985,  1534,  1427,
  1380,  1494,  1940,  2000,  1890,  4478,  1268,  2300,
  1320,  1236,  2609,  3031,  1767,  1888,  1604,  1962,
  3890,  1100,  1458,  2526,  2200,  2637,  1839,  1000,
  2040,  3137,  1811,  1437,  1239,  2132,  4215,  2162,
  1664,  2238,  2567,  1200,   852,  1852,  1203 ])
price_data = numpy.asarray([ 399900,  329900,  369000,  232000,  539900,  299900,  314900,  198999,
  212000,  242500,  239999,  347000,  329999,  699900,  259900,  449900,
  299900,  199900,  499998,  599000,  252900,  255000,  242900,  259900,
  573900,  249900,  464500,  469000,  475000,  299900,  349900,  169900,
  314900,  579900,  285900,  249900,  229900,  345000,  549000,  287000,
  368500,  329900,  314000,  299000,  179900,  299900,  239500 ])

# Test data set 
#이것은 위 데이타에 없는 다른 실제 발생한 값을 몇 개 추출하여 위에서 선형회귀로 만들어진 식에 테스트데이타를 대입해서 
#얼마나 오차가 발생하는지를 검증하기 위한 데이타셋이다.

size_data_test = numpy.asarray([ 1600, 1494, 1236, 1100, 3137, 2238 ])
price_data_test = numpy.asarray([ 329900, 242500, 199900, 249900, 579900, 329900 ])

#정규화식 함수 ==> 배열의 각 수에 평균을 뺀 후 표준편차로 나누어줌
def normalize(array): 
    return (array - array.mean()) / array.std()

# Normalize a data set
#데이터의 범위를 일치시키거나 분포를 유사하게 만들어 주는 등의 작업을 위해 데이타를 정규화한다.
size_data_n = normalize(size_data)
price_data_n = normalize(price_data)

#테스트 데이타를 정규화한다.
size_data_test_n = normalize(size_data_test)
price_data_test_n = normalize(price_data_test)

# Display a plot
#정규화한 값들을 그래프에 그려본다.
plt.plot(size_data, price_data, 'ro', label='Samples data')
plt.legend()
plt.draw()

samples_number = price_data_n.size

# TF graph input
X = tf.placeholder("float")
Y = tf.placeholder("float")

# Create a model

# Set model weights
# 위에 설명했던 Weight 과 bias  ==> 변경시킬값들을 선언한다.
W = tf.Variable(numpy.random.randn(), name="weight")
b = tf.Variable(numpy.random.randn(), name="bias")

# Set parameters
# learning_rate를 cost가 최소가 되는 점에 선을 그을때(기울기가 최소인 점을 찾는 과정) step간격을 어느정도 할것이냐 설정하는것이다.
# 너무크면 큼직하게 간격을 두고 선을 긋기 때문에 자칫 최소가 되는 지점을 지나칠 수가 있다.
# 그러나 너무 작으면 그만큼 촘촘하게 많이 체크해야하므로 시간도 많이 걸리고 학습횟수내에서 cost가 최저가 되는 지점에 도달할 수 없을 수도 있다.
# 그래서 정확도가 제대로 안나올 경우 이 값을 약간씩 조정해 줄 필요가 있다.
learning_rate = 0.1 
training_iteration = 200

# Construct a linear model
# 위에서 설명한 모델을 tensorflow 수식으로 표현
model = tf.add(tf.multiply(X, W), b)

# Minimize squared errors,   tensorflow에는 여러 optimizer 함수가 있는데 이후의 글에서 추가설명하도록 하겠다.
cost_function = tf.reduce_sum(tf.pow(model - Y, 2))/(2 * samples_number) #L2 loss   Cost함수
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function) #Gradient descent,  

# Initialize variables 
# 항상 초기화를 해주고 시작한다.
init = tf.global_variables_initializer()

# Launch a graph
with tf.Session() as sess:
    sess.run(init)

    display_step = 20
    # Fit all training data
    for iteration in range(training_iteration):
        for (x, y) in zip(size_data_n, price_data_n):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        # Display logs per iteration step  위에 display_step을 20으로 했으니 20번째씩 학습했을때의 값을 찍어주는것이다.
        if iteration % display_step == 0:   
            print("Iteration:", '%04d' % (iteration + 1), "cost=", "{:.9f}".format(sess.run(cost_function, 
                  feed_dict={X:size_data_n, Y:price_data_n})),"W=", sess.run(W), "b=", sess.run(b))
                
    tuning_cost = sess.run(cost_function, feed_dict={X: normalize(size_data_n), Y: normalize(price_data_n)})            
    print ("Tuning completed:", "cost=", "{:.9f}".format(tuning_cost), "W=", sess.run(W), "b=", sess.run(b))
    
    # Validate a tuning model    
    ##위에서 학습 다 했으면 테스트데이타를 넣어 얼마나 정확한지 확인해본다.
    testing_cost = sess.run(cost_function, feed_dict={X: size_data_test_n, Y: price_data_test_n})    
    print("Testing data cost:" , testing_cost)
    
    # Display a plot
    plt.figure()
    plt.plot(size_data_n, price_data_n, 'ro', label='Normalized samples')
    plt.plot(size_data_test_n, price_data_test_n, 'go', label='Normalized testing samples')
    plt.plot(size_data_n, sess.run(W) * size_data_n + sess.run(b), label='Fitted line')
    plt.legend()
    
    plt.show() #실제 그래프를 보여준다.

-------------------------------------------------------------------------------------------------------------------------------------

실행결과

-------------------------------------------------------------------------------------------------------------------------------------



만족하셨나요? ~~~~~~~


+ Recent posts