Skip to content

Commit 6c4dbd5

Browse files
committed
디렉토리 재구성
1 parent 0c38766 commit 6c4dbd5

82 files changed

Lines changed: 2449 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

03 - MNIST, CNN/01 - MNIST.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
# 머신러닝 학습의 Hello World 와 같은 MNIST(손글씨 숫자 인식) 문제를 신경망으로 풀어봅니다.
3+
4+
import tensorflow as tf
5+
6+
from tensorflow.examples.tutorials.mnist import input_data
7+
# 텐서플로우에 기본 내장된 mnist 모듈을 이용하여 데이터를 로드합니다.
8+
# 지정한 폴더에 MNIST 데이터가 없는 경우 자동으로 데이터를 다운로드합니다.
9+
# one_hot 옵션은 레이블을 동물 분류 예제에서 보았던 one_hot 방식의 데이터로 만들어줍니다.
10+
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
11+
12+
13+
#########
14+
# 신경망 모델 구성
15+
######
16+
# 입력 값의 차원은 [배치크기, 특성값] 으로 되어 있습니다.
17+
# 손글씨 이미지는 28x28 픽셀로 이루어져 있고, 이를 784개의 특성값으로 정합니다.
18+
X = tf.placeholder(tf.float32, [None, 784])
19+
# 결과는 0~9 의 10 가지 분류를 가집니다.
20+
Y = tf.placeholder(tf.float32, [None, 10])
21+
22+
# 신경망의 레이어는 다음처럼 구성합니다.
23+
# 784(입력 특성값)
24+
# -> 256 (히든레이어 뉴런 갯수) -> 256 (히든레이어 뉴런 갯수)
25+
# -> 10 (결과값 0~9 분류)
26+
W1 = tf.Variable(tf.random_normal([784, 256], stddev=0.01))
27+
W2 = tf.Variable(tf.random_normal([256, 256], stddev=0.01))
28+
W3 = tf.Variable(tf.random_normal([256, 10], stddev=0.01))
29+
30+
# 입력값에 가중치를 곱하고 ReLU 함수를 이용하여 레이어를 만듭니다.
31+
L1 = tf.nn.relu(tf.matmul(X, W1))
32+
# L1 레이어의 출력값에 가중치를 곱하고 ReLU 함수를 이용하여 레이어를 만듭니다.
33+
L2 = tf.nn.relu(tf.matmul(L1, W2))
34+
# 최종 모델의 출력값은 W3 변수를 곱해 10개의 분류를 가지게 됩니다.
35+
model = tf.matmul(L2, W3)
36+
37+
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model, Y))
38+
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
39+
40+
41+
#########
42+
# 신경망 모델 학습
43+
######
44+
init = tf.global_variables_initializer()
45+
sess = tf.Session()
46+
sess.run(init)
47+
48+
batch_size = 100
49+
total_batch = int(mnist.train.num_examples/batch_size)
50+
51+
for epoch in range(15):
52+
total_cost = 0
53+
54+
for i in range(total_batch):
55+
# 텐서플로우의 mnist 모델의 next_batch 함수를 이용해
56+
# 지정한 크기만큼 학습할 데이터를 가져옵니다.
57+
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
58+
sess.run(optimizer, feed_dict={X: batch_xs, Y: batch_ys})
59+
total_cost += sess.run(cost, feed_dict={X: batch_xs, Y: batch_ys})
60+
61+
print 'Epoch:', '%04d' % (epoch + 1),\
62+
'Avg. cost =', '{:.3f}'.format(total_cost / total_batch)
63+
64+
print '최적화 완료!'
65+
66+
67+
#########
68+
# 결과 확인
69+
######
70+
# model 로 예측한 값과 실제 레이블인 Y의 값을 비교합니다.
71+
# tf.argmax 함수를 이용해 예측한 값에서 가장 큰 값을 예측한 레이블이라고 평가합니다.
72+
# 예) [0.1 0 0 0.7 0 0.2 0 0 0 0] -> 4
73+
check_prediction = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
74+
accuracy = tf.reduce_mean(tf.cast(check_prediction, tf.float32))
75+
print '정확도:', sess.run(accuracy,
76+
feed_dict={X: mnist.test.images,
77+
Y: mnist.test.labels})

03 - MNIST, CNN/02 - Dropout.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
# 과적합 방지 기법 중 하나인 Dropout 을 사용해봅니다.
3+
4+
import tensorflow as tf
5+
6+
from tensorflow.examples.tutorials.mnist import input_data
7+
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
8+
9+
10+
#########
11+
# 신경망 모델 구성
12+
######
13+
X = tf.placeholder(tf.float32, [None, 784])
14+
Y = tf.placeholder(tf.float32, [None, 10])
15+
16+
W1 = tf.Variable(tf.random_normal([784, 256], stddev=0.01))
17+
W2 = tf.Variable(tf.random_normal([256, 256], stddev=0.01))
18+
W3 = tf.Variable(tf.random_normal([256, 10], stddev=0.01))
19+
20+
L1 = tf.nn.relu(tf.matmul(X, W1))
21+
# 텐서플로우에 내장된 함수를 이용하여 dropout 을 적용합니다.
22+
# 함수에 적용할 레이어와 확률만 넣어주면 됩니다. 겁나 매직!!
23+
L1 = tf.nn.dropout(L1, 0.8)
24+
L2 = tf.nn.relu(tf.matmul(L1, W2))
25+
L2 = tf.nn.dropout(L2, 0.8)
26+
model = tf.matmul(L2, W3)
27+
28+
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model, Y))
29+
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
30+
31+
32+
#########
33+
# 신경망 모델 학습
34+
######
35+
init = tf.global_variables_initializer()
36+
sess = tf.Session()
37+
sess.run(init)
38+
39+
batch_size = 100
40+
total_batch = int(mnist.train.num_examples/batch_size)
41+
42+
for epoch in range(15):
43+
total_cost = 0
44+
45+
for i in range(total_batch):
46+
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
47+
sess.run(optimizer, feed_dict={X: batch_xs, Y: batch_ys})
48+
total_cost += sess.run(cost, feed_dict={X: batch_xs, Y: batch_ys})
49+
50+
print 'Epoch:', '%04d' % (epoch + 1), \
51+
'Avg. cost =', '{:.3f}'.format(total_cost / total_batch)
52+
53+
print '최적화 완료!'
54+
55+
56+
#########
57+
# 결과 확인
58+
######
59+
check_prediction = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
60+
accuracy = tf.reduce_mean(tf.cast(check_prediction, tf.float32))
61+
print '정확도:', sess.run(accuracy,
62+
feed_dict={X: mnist.test.images,
63+
Y: mnist.test.labels})

03 - MNIST, CNN/03 - CNN.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# -*- coding: utf-8 -*-
2+
# 이미지 처리 분야에서 가장 유명한 신경망 모델인 CNN 을 이용하여 더 높은 인식률을 만들어봅니다.
3+
4+
import tensorflow as tf
5+
6+
from tensorflow.examples.tutorials.mnist import input_data
7+
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
8+
9+
10+
#########
11+
# 신경망 모델 구성
12+
######
13+
# 기존 모델에서는 입력 값을 28x28 하나의 차원으로 구성하였으나,
14+
# CNN 모델을 사용하기 위해 2차원 평면과 특성치의 형태를 갖는 구조로 만듭니다.
15+
X = tf.placeholder(tf.float32, [None, 28, 28, 1])
16+
Y = tf.placeholder(tf.float32, [None, 10])
17+
18+
# 각각의 변수와 레이어는 다음과 같은 형태로 구성됩니다.
19+
# W1 [3 3 1 32] -> [3 3]: 커널 크기, 1: 입력값 X 의 특성수, 32: 필터 갯수
20+
# L1 Conv shape=(?, 28, 28, 32)
21+
# Pool ->(?, 14, 14, 32)
22+
W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))
23+
# tf.nn.conv2d 를 이용해 한칸씩 움직이는 컨볼루션 레이어를 쉽게 만들 수 있습니다.
24+
# padding='SAME' 은 커널 슬라이딩시 최외곽에서 한칸 밖으로 더 움직이는 옵션
25+
L1 = tf.nn.relu(tf.nn.conv2d(X, W1, strides=[1, 1, 1, 1], padding='SAME'))
26+
# Pooling 역시 tf.nn.max_pool 을 이용하여 쉽게 구성할 수 있습니다.
27+
L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
28+
L1 = tf.nn.dropout(L1, 0.8)
29+
30+
# L2 Conv shape=(?, 14, 14, 64)
31+
# Pool ->(?, 7, 7, 64)
32+
# Reshape ->(?, 256)
33+
# W2 의 [3, 3, 32, 64] 에서 32 는 L1 에서 출력된 W1 의 마지막 차원, 필터의 크기 입니다.
34+
W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))
35+
L2 = tf.nn.relu(tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME'))
36+
L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
37+
# Full Connect 를 위해 차원을 줄입니다.
38+
# 직전의 Pool 사이즈인 (?, 7, 7, 64) 를 참고합니다.
39+
L2 = tf.reshape(L2, [-1, 7 * 7 * 64])
40+
L2 = tf.nn.dropout(L2, 0.8)
41+
42+
# FC 레이어: 입력값 7x7x64 -> 출력값 256
43+
W3 = tf.Variable(tf.random_normal([7 * 7 * 64, 256], stddev=0.01))
44+
L3 = tf.nn.relu(tf.matmul(L2, W3))
45+
L3 = tf.nn.dropout(L3, 0.5)
46+
47+
# 최종 출력값 L3 에서의 출력 256개를 입력값으로 받아서 0~9 레이블인 10개의 출력값을 만듭니다.
48+
W4 = tf.Variable(tf.random_normal([256, 10], stddev=0.01))
49+
model = tf.matmul(L3, W4)
50+
51+
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model, Y))
52+
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
53+
# 최적화 함수를 RMSPropOptimizer 로 바꿔서 결과를 확인해봅시다.
54+
# optimizer = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
55+
56+
57+
#########
58+
# 신경망 모델 학습
59+
######
60+
init = tf.global_variables_initializer()
61+
sess = tf.Session()
62+
sess.run(init)
63+
64+
batch_size = 100
65+
total_batch = int(mnist.train.num_examples/batch_size)
66+
67+
for epoch in range(15):
68+
total_cost = 0
69+
70+
for i in range(total_batch):
71+
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
72+
# 이미지 데이터를 CNN 모델을 위한 자료형태인 [28 28 1] 의 형태로 재구성합니다.
73+
batch_xs = batch_xs.reshape(-1, 28, 28, 1)
74+
_, cost_val = sess.run([optimizer, cost], feed_dict={X: batch_xs, Y: batch_ys})
75+
total_cost += cost_val
76+
77+
print 'Epoch:', '%04d' % (epoch + 1), \
78+
'Avg. cost =', '{:.3f}'.format(total_cost / total_batch)
79+
80+
print '최적화 완료!'
81+
82+
83+
#########
84+
# 결과 확인
85+
######
86+
check_prediction = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
87+
accuracy = tf.reduce_mean(tf.cast(check_prediction, tf.float32))
88+
print '정확도:', sess.run(accuracy,
89+
feed_dict={X: mnist.test.images.reshape(-1, 28, 28, 1),
90+
Y: mnist.test.labels})
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# -*- coding: utf-8 -*-
2+
# 신경망 구성을 손쉽게 해 주는 유틸리티 모음인 tensorflow.contrib.layers 를 사용해봅니다.
3+
# 03 - CNN.py 를 재구성한 것이니, 소스를 한 번 비교해보세요.
4+
# 이처럼 TensorFlow 에는 간단하게 사용할 수 있는 다양한 함수와 유틸리티들이 매우 많이 마련되어 있습니다.
5+
# 다만, 처음에는 기본적인 개념에 익숙히지는 것이 좋으므로 이후에도 가급적 기본 함수들을 이용하도록 하겠습니다.
6+
7+
import tensorflow as tf
8+
9+
from tensorflow.examples.tutorials.mnist import input_data
10+
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
11+
12+
13+
#########
14+
# 옵션 설정
15+
######
16+
n_width = 28 # MNIST 이미지의 가로 크기
17+
n_height = 28 # MNIST 이미지의 세로 크기
18+
n_output = 10
19+
20+
#########
21+
# 신경망 모델 구성
22+
######
23+
X = tf.placeholder(tf.float32, [None, n_width, n_height, 1])
24+
Y = tf.placeholder(tf.float32, [None, n_output])
25+
26+
# 기본적으로 inputs, outputs size, kernel_size 만 넣어주면
27+
# 활성화 함수 적용은 물론, 컨볼루션 신경망을 만들기 위한 나머지 수치들은 알아서 계산해줍니다.
28+
# 특히 Weights 를 계산하는데 xavier_initializer 를 쓰고 있는 등,
29+
# 크게 신경쓰지 않아도 일반적으로 효율적인 신경망을 만들어줍니다.
30+
L1 = tf.contrib.layers.conv2d(X, 32, [3, 3])
31+
L2 = tf.contrib.layers.max_pool2d(L1, [2, 2])
32+
# normalizer_fn 인자를 사용하면 과적합등을 막아주는 normalizer 기법을 간단히 적용할 수 있습니다.
33+
L3 = tf.contrib.layers.conv2d(L2, 64, [3, 3],
34+
normalizer_fn=tf.nn.dropout,
35+
normalizer_params={'keep_prob': 0.8})
36+
L4 = tf.contrib.layers.max_pool2d(L3, [2, 2])
37+
38+
L5 = tf.contrib.layers.flatten(L4)
39+
L5 = tf.contrib.layers.fully_connected(L5, 256,
40+
normalizer_fn=tf.contrib.layers.batch_norm)
41+
model = tf.contrib.layers.fully_connected(L5, n_output)
42+
43+
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model, Y))
44+
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
45+
46+
47+
#########
48+
# 신경망 모델 학습
49+
######
50+
init = tf.global_variables_initializer()
51+
sess = tf.Session()
52+
sess.run(init)
53+
54+
batch_size = 100
55+
total_batch = int(mnist.train.num_examples/batch_size)
56+
57+
for epoch in range(15):
58+
total_cost = 0
59+
60+
for i in range(total_batch):
61+
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
62+
# 이미지 데이터를 CNN 모델을 위한 자료형태인 [28 28 1] 의 형태로 재구성합니다.
63+
batch_xs = batch_xs.reshape(-1, 28, 28, 1)
64+
_, cost_val = sess.run([optimizer, cost], feed_dict={X: batch_xs, Y: batch_ys})
65+
total_cost += cost_val
66+
67+
print 'Epoch:', '%04d' % (epoch + 1), \
68+
'Avg. cost =', '{:.3f}'.format(total_cost / total_batch)
69+
70+
print '최적화 완료!'
71+
72+
73+
#########
74+
# 결과 확인
75+
######
76+
check_prediction = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
77+
accuracy = tf.reduce_mean(tf.cast(check_prediction, tf.float32))
78+
print '정확도:', sess.run(accuracy,
79+
feed_dict={X: mnist.test.images.reshape(-1, 28, 28, 1),
80+
Y: mnist.test.labels})

0 commit comments

Comments
 (0)