Tensorflow MLP classification
Classification ๊ฐ๋ ์ ๋ฆฌ
1 Softmax Regression (๋ชจ๋ธ)
์ด๋ค input x๊ฐ ์ฃผ์ด์ก์ ๋, ๊ทธ๊ฒ์ด class i๋ผ๊ณ ํ์ ํ๋ ์ ๋(evidence)
evidenceiโ=jโโWi,jโxjโ+biโ
์์ evidence๋ฅผ softmax function์ ํตํด ํ๋ก๊ทธ๋จ์ด ๋ ์ด๋ธ(Label)์ y๋ผ๊ณ ์์ธกํ ํ๋ฅ ๋ก ๋ฐ๊พผ๋ค.
y=softmax(evidence)
์ ์์์ softmax ํจ์ ์ผ์ข ์ link ํจ์๋ก์จ ์ ํํจ์์ ๊ฒฐ๊ณผ(evidence)๋ฅผ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ํํ๋ก ๋ฐ๊พธ์ด์ค๋ค.
์์ ์์ ๊ฐ๋จํ๊ฒ ๋ํ๋ด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
2 Cross-entropy (ํ๊ฐ)
softmax regression ๋ชจ๋ธ์ด ์ ํ์ตํ๊ณ ์๋์ง๋ฅผ ํ๊ฐํ๋ evaluation function ์ค ํ๋.
Hyโฒโ(y)=โiโโyiโฒโlog(yi)
3 Gradient descent (๊ฒฝ์ฌํ๊ฐ๋ฒ)
cross-entropy๋ฅผ ์ต์ํ ํ๋ ๋ฐฉํฅ์ผ๋ก ์ต์ ํ ํ๊ธฐ ์ํ ๋ฐฉ๋ฒ ์ค ํ๋.
python
# MNIST ๋ฐ์ดํฐ ๋ค์ด๋ก๋
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Tenworflow ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
import tensorflow as tf
import numpy as np
# ๋ณ์ ์ค์
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
# cross-entropy ๋ชจ๋ธ ์ค์
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
# ๊ฒฝ์ฌํ๊ฐ๋ฒ์ผ๋ก ๋ชจ๋ธ์ ํ์ตํ๋ค.
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
# ํ์ต๋ ๋ชจ๋ธ์ด ์ผ๋ง๋ ์ ํํ์ง ์ถ๋ ฅํ๋ค.
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_:mnist.test.labels}))
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
0.9169
- 20.12.07 ์์ ์์
'๐ก EE's DEV > ๋จธ์ ๋ฌ๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋จธ์ ๋ฌ๋] Ubuntu16.04์์ CUDA, cuDNN, Anaconda, Tensorflow, Pytorch ์ค์นํ๊ธฐ (0) | 2017.12.20 |
---|