티스토리 뷰

[업데이트 2017.06.29 13:58]

 

TensorFlow를 통해 Linear Regression을 구현해보고자 합니다. Machine Learning은 크게 Supervised Learning, Unsupervised Learning으로 나뉘며, 그 외에 Reinforcement Learning, Recommender System등이 있습니다.

 

Supervised Learning과 Unsupervised Learning의 차이는 주어진 데이터의 정답(Labeling)이 존재하느냐 하지 않느냐에 따라 구분됩니다.

 

ex) e-mail spam 여부 검출, 부동산 가격 예측 등 => Supervised Learning

     유전자 패턴 군집화, 비슷한 주제의 뉴스 기사 검색 등 => Unsupervised Learning

 

Supervised Learning은 다시 regression문제와 classification문제로 나뉘게 됩니다.

regression은 정답이 continuous valued output(Real Number)의 형태를 띄는 경우이며, classification은 discrete valued output(Integer Number)의 형태를 가지는 문제를 의미합니다.

 

ex) regression label = [1.203, 1.0, 0.001]    // 부동산 매매가 상승률 예측 등

     classification label = [0, 1]                 // e-mail spam 구분(yes/no), 자동차 구분(포르쉐, 아우디, BMW,..) 등

 

아래 코드는 Coursera homework ex1의 multivariate linear regression data를 가지고 Tensorflow에서 테스트한 내용입니다. 학습 횟수에 따른 Cost 함수의 값이 줄어드는지 확인하기 위해 matplotlib를 사용하여 그래프로 확인하였습니다.(주석 처리된 부분 참고) 해당 data의 경우 x1, x2의 데이터의 scale이 다르기 때문에 mean normalizarion을 수행한 후 학습 하였습니다. 각 epoch마다 전체 데이터를 가지고 계산하는 Batch Gradient Descent를 사용하였습니다.

 

 

추가로 matplotlib를 쓰지 않고, Tensor Board를 이용하여 Cost J 그래프를 확인한 결과입니다.

다음과 같이 tensor board 로그를 저장할 path를 지정 후 Tensor Board를 실행합니다.

Tensor Board가 실행 된 host IP에 접속합니다. (기본 port 6006)

 

C:/tf>tensorboard --logdir=./board/sample1

 

 

아래는 기존 코드에 Tensor Board 코드를 추가한 코드입니다. 매번 실행시 마다 새로운 그래프 데이터를 확인하기 위해 매번 로그 디렉토리 삭제 후 Tensor Board가 실행되도록 하였습니다.

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


def readcsvfile(filename, param_record_defaults, batch_size, num_epochs, shuffle):
    min_after_dequeue = 10000
    capacity = min_after_dequeue + 3 * batch_size

    filename_queue = tf.train.string_input_producer([filename], num_epochs=num_epochs, shuffle=shuffle)

    reader = tf.TextLineReader()
    key, value = reader.read(filename_queue)

    features = tf.stack(tf.decode_csv(value, record_defaults=param_record_defaults))

    if True == shuffle:
        features_batch = tf.train.shuffle_batch([features],
                                                batch_size=batch_size, capacity=capacity,
                                                min_after_dequeue=min_after_dequeue)
    else:
        features_batch = tf.train.batch([features],
                                        batch_size=batch_size, capacity=capacity)

    return features_batch


def addBiasTerm(X):
    m = np.size(X, 0)
    n = np.size(X, 1)
    X_bias = np.ones([m, n + 1])

    for i in range(m):
        for j in range(n):
            X_bias[i, j + 1] = X[i, j]

    return X_bias


def featureNormalization(X):
    m = np.size(X, 0)
    n = np.size(X, 1)

    mu = np.zeros([n])  # x1, x2
    sigma = np.zeros([n])
    X_norm = X

    tol = 1e-10

    for j in range(n):
        mu[j] = np.mean(X_norm[:, j])
        sigma[j] = np.std(X_norm[:, j])

        # Avoid zero variance issue
        if sigma[j] <= tol:
            sigma[j] = 1

        for i in range(m):
            X_norm[i, j] = (X_norm[i, j] - mu[j]) / sigma[j]

    return X_norm, mu, sigma


record_defaults = [tf.constant([1], dtype=tf.float32),
                   tf.constant([1], dtype=tf.float32),
                   tf.constant([1], dtype=tf.float32)]

# Hyper parameters
batch_size = 47
max_iter = 1000000
num_of_epochs = 400
learning_rate = 0.3
hyper_lambda = 0

features = readcsvfile("C:\\Users\\Build\\Desktop\\tf\\ex1data2.csv", record_defaults, batch_size, num_of_epochs, False)

'''
 Multivariate Linear Regression

 Variables, m training set
 theta = 1 x 3
 X = m x 3
 y = m x 1

 hypothesis = theta.transpose * X[i, :]  i th data of m training set

'''
theta = tf.Variable([tf.random_uniform([1], -0.5, 0.5), tf.random_uniform([1], -0.5, 0.5), tf.random_uniform([1], -0.5, 0.5)])  # 3 x 1
X = tf.placeholder(tf.float32, [batch_size, 3])  # m x 3
y = tf.placeholder(tf.float32, [batch_size, ])  # m x 1

hypothesis = tf.matmul(X, theta)
cost_function = tf.reduce_mean(tf.square(hypothesis - y)) + hyper_lambda*tf.reduce_mean(tf.square(theta))

optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(cost_function)

init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())

# for matplot
# iter_history = np.zeros([max_iter])
# cost_history =  np.zeros([max_iter])

# for Tensor Board
tb_cost_function = tf.summary.scalar("cost J", cost_function)
tb_merged = tf.summary.merge_all()

with tf.Session() as sess:
    sess.run(init_op)

    log_dir = "./board/sample1"

    os.system("taskkill /f /im cmd.exe")

    if tf.gfile.Exists(log_dir):
        tf.gfile.DeleteRecursively(log_dir)

    writer = tf.summary.FileWriter(log_dir, sess.graph)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    # for matplot
    # plt.figure(1)
    # plt.title("Multivariate Linear Regression")
    # plt.xlabel("Number of iterations")
    # plt.ylabel("Cost J")
    # plt.yscale("log")

    try:
        step = 0
        while not coord.should_stop():
            training_set = sess.run(features)

            X_data = training_set[:, 0:2]
            y_data = training_set[:, 2]

            # Mean Normalization
            X_data, mu, sigma = featureNormalization(X_data)

            # Add Bias term to data X
            X_data = addBiasTerm(X_data)

            sess.run(train, feed_dict={X: X_data, y: y_data})
            cur_cost = sess.run(cost_function, feed_dict={X: X_data, y: y_data})

            summary = sess.run(tb_merged, feed_dict={X: X_data, y: y_data})
            writer.add_summary(summary, step + 1)

            # for matplot
            # iter_history[step] = step+1
            # cost_history[step] = cur_cost
            print("iteration =", step + 1, "cost_function = ", cur_cost)

            step += 1

    except tf.errors.OutOfRangeError:
        print("Done -- epoch limit reached.")
        print("learning_rate=", learning_rate)
        print("batch size=", batch_size)
        print("num_of_epochs=", num_of_epochs)
        print("iterated=", step)

        print("\ntheta=", sess.run(theta))

        feed_x = tf.constant([[1, (2104 - mu[0]) / sigma[0], (3 - mu[1]) / sigma[1]]], dtype=tf.float32)
        answer = sess.run(tf.matmul(feed_x, theta))

        print(sess.run(feed_x), " => ", answer)

        # for matplot
        # plt.plot(iter_history[0:step], cost_history[0:step])
        # plt.show()


    finally:
        coord.request_stop()

    coord.join(threads)
    os.system("tensorboard --logdir=" + log_dir) 

 

 

 

* 참고: http://www.coursera.org/ > Coursera Machine Learning 강의

* 참고: https://www.tensorflow.org/programmers_guide/reading_data

* GitHub: https://github.com/asyncbridge/machine-learning/blob/master/tensorflow/multivariate_linear_regression.py


 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함