티스토리 뷰

Untitled

출처

http://jorditorres.org/first-contact-with-tensorflow/#cap4 (First Contact with tensorflow)

단일신경망 Single Layer Neural Network (ver. python)

The MNIST data-set

숫자 손글씨(hand-written digits)에 관한 엄청 유명한 데이터 셋.

training set으로 6만개 이상, test set으로 1만개이다.

흑백사진으로 이루어져있고, anti-aliasing 돼있다. 전 처리가 다 돼있기 때문에, 패턴인식을 시작하는 사람들에게 이상적이라고 한다.

supervised learning의 한 예이다.

In [1]:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("~/MNIST_data/", one_hot=True)
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

모형에 대한 자세한 설명은 생략하도록 하겠습니다.

Neural Network, activation function (softmax), loss function (cross-entropy), optimizer (gradient descent, batchsize) etc..

Single layer neural network

변수 지정

In [2]:
x = tf.placeholder("float", [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

모형 설정

Activation function : softmax

In [3]:
matm = tf.matmul(x,W)
y = tf.nn.softmax(tf.matmul(x,W) + b)
y_ = tf.placeholder("float", [None, 10])

Loss function : cross-entropy

In [4]:
cross_entropy = tf.reduce_sum(y_ * tf.log(y))

Optimizer : Gradient Descent (learning_rate = 0.01)

In [5]:
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

최적화 반복

In [6]:
sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(101):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict = {x:batch_xs, y_:batch_ys})
    if i in [j * 5 for j in range(21)]:
        correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print(i, sess.run(accuracy, feed_dict = {x: mnist.test.images, y_: mnist.test.labels}))
0 0.0145
5 0.098
10 0.098
15 0.098
20 0.098
25 0.098
30 0.098
35 0.098
40 0.098
45 0.098
50 0.098
55 0.098
60 0.098
65 0.098
70 0.098
75 0.098
80 0.098
85 0.098
90 0.098
95 0.098
100 0.098

batch size = 100개로 하고, 계속 Gradient Descent를 반복하면서,

5의 배수일 때마다 test set의 정확도를 계산해서 출력한다.

정확도가 기껏해서 0.098로, 성능 안 좋다는 것을 알 수 있다.

같이보기

http://deepstat.tistory.com/8 (단일신경망 Single Layer Neural Network (ver.python)

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
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 31
글 보관함