hidden layer를 추가하여 multiple neural network를 구성한다.


개선한점


1. hidden layer + adam optimizer = 97.8%

2. hidden layer + adam optimizer + xavier initialization = 98%



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
64
65
66
67
import tensorflow as tf
 
# parameters
 
learning_rate = 0.001
training_epochs = 15
batch_size = 100
display_step = 1
 
# test data 다운로드
from tensorflow.examples.tutorials.mnist import input_data
 
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
 
# tf Graph Input
= tf.placeholder(tf.float32, [None, 784])
= tf.placeholder(tf.float32, [None, 10])
 
# store layers weight & bias
W1 = tf.get_variable("W1", shape = [784,256], initializer=tf.contrib.layers.xavier_initializer())
W2 = tf.get_variable("W2", shape = [256,256], initializer=tf.contrib.layers.xavier_initializer())
W3 = tf.get_variable("W3", shape = [256,10], initializer=tf.contrib.layers.xavier_initializer())
 
B1 = tf.Variable(tf.random_normal([256]))
B2 = tf.Variable(tf.random_normal([256]))
B3 = tf.Variable(tf.random_normal([10]))
 
dropout_rate = tf.placeholder(tf.float32)
 
# construct model
_L1 = tf.nn.relu(tf.add(tf.matmul(x, W1), B1))
L1 = tf.nn.dropout(_L1, dropout_rate)
_L2 = tf.nn.relu(tf.add(tf.matmul(L1, W2), B2))
L2 = tf.nn.dropout(_L2, dropout_rate)
hypothesis = tf.matmul(L2, W3) + B3
 
cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=hypothesis, labels=y)
cost = tf.reduce_mean(cost_i)
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
 
# launch the graph
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
 
    # training cycle
    for epoch in range(training_epochs):
        avg_cost = 0
        total_batch = int(mnist.train.num_examples / batch_size)
 
        # loop over all batches
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, dropout_rate: 0.7})
            # compute average loss
            c = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, dropout_rate: 0.7})
            avg_cost += c / total_batch
        # display logs per epoch step
        print ('Epoch :', epoch + 1'\tcost :', avg_cost)
    
    print ("Optimization Finished!")
 
    # test model
    correct_prediction = tf.equal(tf.argmax(hypothesis,1), tf.argmax(y,1))
    # calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print("Accuracy:", accuracy.eval(session=sess, feed_dict={x: mnist.test.images, y:mnist.test.labels, dropout_rate: 1}))
 
cs


'tensorflow > tutorial' 카테고리의 다른 글

mnist 초급  (0) 2017.11.15

+ Recent posts