와이드용 애드센스


[Tensorflow] Convolutional Neural Network with MNIST dataset






















네트워크 구성

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#%%
import tensorflow as tf
import matplotlib.pyplot as plt
import random
from tensorflow.examples.tutorials.mnist import input_data
 
 
= tf.placeholder(tf.float32, [None,784])
X_img = tf.reshape(X, [-1,28,28,1])
= tf.placeholder(tf.float32, [None,10])
 
# F1
P1 = tf.Variable(tf.random_normal([5,5,1,5], stddev=0.1))
B1 = tf.Variable(tf.random_normal([5], stddev=0.1))
L1 = tf.nn.conv2d(X_img, P1, strides=[1,1,1,1], padding='VALID'+ B1
L1 = tf.nn.relu(L1)
 
# F2
P2 = tf.Variable(tf.random_normal([5,5,5,10], stddev = 0.1))
B2 = tf.Variable(tf.random_normal([10], stddev=0.1))
L2 = tf.nn.conv2d(L1, P2, strides=[1,1,1,1], padding='VALID'+ B2
 
# Pooling
L2 = tf.nn.relu(L2)
L2 = tf.nn.max_pool(L2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
L2 = tf.reshape(L2, [-1,11000])
 
# F3
W1 = tf.Variable(tf.truncated_normal(shape=[1000,100]))
B3 = tf.Variable(tf.random_normal([1], stddev=0.1))
L3 = tf.matmul(L2, W1) + B3
L3 = tf.nn.relu(L3)
 
W2 = tf.Variable(tf.truncated_normal(shape=[100,10]))
L4 = tf.matmul(L3, W2)
 
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=L4, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
 
# init
sess = tf.Session()
sess.run(tf.global_variables_initializer())
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
training_epochs = 100
batch_size = 100
 
# train
cost_log = []
for epoch in range(training_epochs):
    avg_cost = 0
    total_batch = int(mnist.train.num_examples / batch_size)
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        feed_dict = {X: batch_xs, Y: batch_ys}
        c, _, = sess.run([cost,optimizer], feed_dict=feed_dict)
        avg_cost += c / total_batch        
    #print("Epoch:","%04d"%(epoch + 1),"cost =","{:.9f}".format(avg_cost))
    cost_log.append(float("{:.9f}".format(avg_cost)))
 
plt.plot(cost_log)
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()
cs

소스 코드


실행 결과

1 2 3 4 5 6 7 8 9 10 다음



애드센스