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

Publication

Category

Recent Post

Multinomial Classification에 사용하는 Softmax Classification을 Tensorflow로 구현하는 강좌를 정리한다.




Softmax Function


0.7 + 0.2 + 0.1 확률(Probabilities)값을 더하면 1이 된다. 






Cost Function


Loss function 구현하고 minize cost로 GradientDescentOptimizer를 사용한다. 




Tensorflow 실습


One-Hot Encoding은 하나의 자리만 1을 갖는다. 예) y_data

import tensorflow as tf

tf.set_random_seed(777)  # for reproducibility


x_data = [[1, 2, 1, 1],

          [2, 1, 3, 2],

          [3, 1, 3, 4],

          [4, 1, 5, 5],

          [1, 7, 5, 5],

          [1, 2, 5, 6],

          [1, 6, 6, 6],

          [1, 7, 7, 7]]

y_data = [[0, 0, 1],

          [0, 0, 1],

          [0, 0, 1],

          [0, 1, 0],

          [0, 1, 0],

          [0, 1, 0],

          [1, 0, 0],

          [1, 0, 0]]


X = tf.placeholder("float", [None, 4])

Y = tf.placeholder("float", [None, 3])

nb_classes = 3  #클래스의 갯수


# Shape

# 4 = X값, nb_classes 출력값

W = tf.Variable(tf.random_normal([4, nb_classes]), name='weight')

b = tf.Variable(tf.random_normal([nb_classes]), name='bias')


# tf.nn.softmax computes softmax activations

# softmax = exp(logits) / reduce_sum(exp(logits), dim)

hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)


# Cross entropy cost/loss

cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)


# Launch graph

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())


    #Hypothesis에 대해 학습하여 최적의 W,b를 구함

    for step in range(2001):

        sess.run(optimizer, feed_dict={X: x_data, Y: y_data})

        if step % 200 == 0:

            print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}))


    print('--------------')


    # Testing & One-hot encoding: 예측을 하게 한다. argmax에서 최강자를 골라준다.


    a = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9]]})

    print(a, sess.run(tf.argmax(a, 1)))


    print('--------------')


    b = sess.run(hypothesis, feed_dict={X: [[1, 3, 4, 3]]})

    print(b, sess.run(tf.argmax(b, 1)))


    print('--------------')


    c = sess.run(hypothesis, feed_dict={X: [[1, 1, 0, 1]]})

    print(c, sess.run(tf.argmax(c, 1)))


    print('--------------')


    all = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9], [1, 3, 4, 3], [1, 1, 0, 1]]})

    print(all, sess.run(tf.argmax(all, 1)))



==> 결과 1 0 2

[[1.3890432e-03 9.9860197e-01 9.0612402e-06]

 [9.3119204e-01 6.2902056e-02 5.9058843e-03]

 [1.2732767e-08 3.3411323e-04 9.9966586e-01]] [1 0 2]






Fancy Softmax Classifier


cross_entropy, reshape, one-shot encoding을 사용하여 좀 더 Fancy하게 Softmax Classifier를 만든다. 

- score(logit)을 구함 => softmax function을 거치면 확률값이 나옴. 식: hypothesis  tf.nn.softmax(tf.matmul(X,W))


- hypothesis를 Cross entropy cost/Loss 를 만듦. 1)번 수식을 2)번 수식처럼 단순화함. Logits을 사용함.



동물원 예제

- 6가지 종류의 동물이 있다. 이것을 one hot을 이용해 가설을 학습시킨다. 

- tf.one_hot(...)을 호출하면 한차원 더 생기고 이것을 원위치 시키려면 tf.reshape(...)을 호출한다. 


import tensorflow as tf

import numpy as np

tf.set_random_seed(777)  # for reproducibility


# Predicting animal type based on various features

xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)

x_data = xy[:, 0:-1]

y_data = xy[:, [-1]]


print(x_data.shape, y_data.shape)


nb_classes = 7  # 0 ~ 6


X = tf.placeholder(tf.float32, [None, 16])

Y = tf.placeholder(tf.int32, [None, 1])  # 0 ~ 6

Y_one_hot = tf.one_hot(Y, nb_classes)  # one hot

print("one_hot", Y_one_hot)

Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])

print("reshape", Y_one_hot)


W = tf.Variable(tf.random_normal([16, nb_classes]), name='weight')

b = tf.Variable(tf.random_normal([nb_classes]), name='bias')


# tf.nn.softmax computes softmax activations

# softmax = exp(logits) / reduce_sum(exp(logits), dim)

logits = tf.matmul(X, W) + b

hypothesis = tf.nn.softmax(logits)


# Cross entropy cost/loss

cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_one_hot)

cost = tf.reduce_mean(cost_i)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)


# 정확도를 구한다. 

prediction = tf.argmax(hypothesis, 1)

correct_prediction = tf.equal(prediction, tf.argmax(Y_one_hot, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# Launch graph

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())


    # 학습을 수행한다

    for step in range(2000):

        sess.run(optimizer, feed_dict={X: x_data, Y: y_data})

        if step % 100 == 0:

            loss, acc = sess.run([cost, accuracy], feed_dict={X: x_data, Y: y_data})

            print("Step: {:5}\tLoss: {:.3f}\tAcc: {:.2%}".format(

                step, loss, acc))


    # Let's see if we can predict: 잘 예측되는지 확인한다. 

    pred = sess.run(prediction, feed_dict={X: x_data})

    # y_data: (N,1) = flatten => (N, ) matches pred.shape

    for p, y in zip(pred, y_data.flatten()):

        print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y)))




동물을 특징 짖는 여러 가지 X 값에 대한 결과 Y값을 가지고 있을 때, 즉 Multinomial Classification을 할 때 Fancy Softmax Classifier를 사용한다.






참조


김성훈교수님의 Softmax Classification을 Tensorflow로 구현하는 강좌

- 김성훈교수 강좌 정리한 글-1, 잘 정리한 글-2, 잘 정리한 글-3

- Github Softmax Classification 예제-1

posted by 윤영식