[실습] Fancy Softmax Classfication

ncy Softmax Classifier

  • cross-entropy, one-hot, reshape
# softmax_cross_entropy_with_logits 함수 소개
logits = tf.matmul(X, W) + b
hypothesis = tf.nn.softmax(logits)

# 1. 기존소스
#Cross entropy cost/loss
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))

# 2. 함수 활용한 소스
cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_one_hot)
cost = tf.reduce_mean(cost_i)

Animal classification (동물 특징에 따른 분류)

xp = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]

nb_classes = 7 # 0 ~ 6

X = tf.placeholder(tf.float32, [None, 16]) # 행 수가 16개
Y = tf.placeholder(tf.int32, [None, 1]) # 0 ~ 6, shape=(?, 1)

Y_one_hot = tf.one_hot(Y, nb_classes) # one hot shape=(?, 1, 7)
Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes]) # shape=(?, 7)

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

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

# Cross entropy cost/loss
cost_i = tf.nn.softmax_corss_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) # 0 ~ 6 사이의 값으로 만드는 것
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}\nLoss: {:.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: [[1], [0]] = flatten => [1, 0] matches pred.shape
    # zip .. 여러 요소들을 for 문 돌릴 수 있도록 한번에 처리
    for p, y in zip(pred, y_data.flatten()):
        print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y)))

step 이 커질수록 정확도가 올라간다. 예측값이 맞았는지 여부와 예측값, 실제값을 출력한다.

results for ""

    No results matching ""