from __future__ import print_function
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
learning_rate = 0.001
training_iters = 200000
batch_size = 128
display_step = 10
n_input = 784
n_classes = 10
dropout = 0.75
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32)
def conv2d(x, W, b, strides=1):
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
def maxpool2d(x, k=2):
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME')
def conv_net(x, weights, biases, dropout):
# 输入-1代表图片数量不定 , 28*28图片,1通道灰色图
x = tf.reshape(x, shape=[-1, 28, 28, 1])
############# 第一层卷积 #####################
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
conv1 = maxpool2d(conv1, k=2)
############# 第二层卷积 #####################
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
conv2 = maxpool2d(conv2, k=2)
############# 第三层全连接 ###################
fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
fc1 = tf.nn.dropout(fc1, dropout)
############# 第四层全连接 ###################
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
return out
weights = {
# 5x5 conv, 1 input, 32 outputs, 第一层卷积结果14*14*32(32张图)
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
# 5x5 conv, 32 inputs, 64 outputs, 第二层卷积结果7*7*64(64张图)
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
# fully connected, 7*7*64 inputs, 1024 outputs
'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
# 1024 inputs, 10 outputs (class prediction)
'out': tf.Variable(tf.random_normal([1024, n_classes]))
}
biases = {
# xx通道输出,对应xx偏置量
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bd1': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
predict = conv_net(x, weights, biases, keep_prob)
############# 回归分类 #############
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predict, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
############# 预测 #####################
correct_pred = tf.equal(tf.argmax(predict, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
step = 1
############# 训练 #####################
while step * batch_size < training_iters:
batch_x, batch_y = mnist.train.next_batch(batch_size)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y, keep_prob: dropout})
if step % display_step == 0:
loss, acc = sess.run([cost, accuracy],
feed_dict={x: batch_x, y: batch_y, keep_prob: 1.})
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc))
step += 1
print("Optimization Finished!")
test_accuracy = sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
y: mnist.test.labels[:256], keep_prob: 1.})
print("Testing Accuracy:", test_accuracy)
结果:
Iter 1280, Minibatch Loss= 26739.099609, Training Accuracy= 0.25781
Iter 2560, Minibatch Loss= 9156.189453, Training Accuracy= 0.57031
Iter 3840, Minibatch Loss= 6651.129883, Training Accuracy= 0.67188
Iter 5120, Minibatch Loss= 6166.565430, Training Accuracy= 0.78906
Iter 6400, Minibatch Loss= 5917.207031, Training Accuracy= 0.76562
Iter 7680, Minibatch Loss= 4123.412598, Training Accuracy= 0.80469
Iter 8960, Minibatch Loss= 2672.761719, Training Accuracy= 0.87500
Iter 10240, Minibatch Loss= 4759.664062, Training Accuracy= 0.82812
Iter 11520, Minibatch Loss= 1856.829102, Training Accuracy= 0.89844
Iter 12800, Minibatch Loss= 3375.414062, Training Accuracy= 0.87500
Iter 14080, Minibatch Loss= 1702.190063, Training Accuracy= 0.88281
Iter 15360, Minibatch Loss= 2097.105225, Training Accuracy= 0.89844
Iter 16640, Minibatch Loss= 2330.599121, Training Accuracy= 0.92188
Iter 17920, Minibatch Loss= 2278.968750, Training Accuracy= 0.89062
Iter 19200, Minibatch Loss= 2017.522705, Training Accuracy= 0.86719
Iter 20480, Minibatch Loss= 2566.398682, Training Accuracy= 0.87500
Iter 21760, Minibatch Loss= 238.359406, Training Accuracy= 0.96875
Iter 23040, Minibatch Loss= 1531.023071, Training Accuracy= 0.93750
Iter 24320, Minibatch Loss= 2890.682861, Training Accuracy= 0.85938
Iter 25600, Minibatch Loss= 835.863403, Training Accuracy= 0.95312
Iter 26880, Minibatch Loss= 1700.663086, Training Accuracy= 0.92969
Iter 28160, Minibatch Loss= 1126.358643, Training Accuracy= 0.89844
Iter 29440, Minibatch Loss= 2107.322021, Training Accuracy= 0.87500
Iter 30720, Minibatch Loss= 530.002075, Training Accuracy= 0.93750
Iter 32000, Minibatch Loss= 850.515503, Training Accuracy= 0.96094
Iter 33280, Minibatch Loss= 1732.267334, Training Accuracy= 0.89844
Iter 34560, Minibatch Loss= 1890.741699, Training Accuracy= 0.92188
Iter 35840, Minibatch Loss= 1371.276733, Training Accuracy= 0.93750
Iter 37120, Minibatch Loss= 370.038940, Training Accuracy= 0.97656
Iter 38400, Minibatch Loss= 981.587280, Training Accuracy= 0.92188
Iter 39680, Minibatch Loss= 1120.410034, Training Accuracy= 0.92188
Iter 40960, Minibatch Loss= 1850.069580, Training Accuracy= 0.90625
Iter 42240, Minibatch Loss= 929.379517, Training Accuracy= 0.96094
Iter 43520, Minibatch Loss= 1343.690430, Training Accuracy= 0.94531
Iter 44800, Minibatch Loss= 965.836792, Training Accuracy= 0.95312
Iter 46080, Minibatch Loss= 1384.953125, Training Accuracy= 0.93750
Iter 47360, Minibatch Loss= 544.845093, Training Accuracy= 0.97656
Iter 48640, Minibatch Loss= 1975.952637, Training Accuracy= 0.91406
Iter 49920, Minibatch Loss= 784.620056, Training Accuracy= 0.93750
Iter 51200, Minibatch Loss= 921.763916, Training Accuracy= 0.94531
Iter 52480, Minibatch Loss= 584.898987, Training Accuracy= 0.96875
Iter 53760, Minibatch Loss= 1298.738037, Training Accuracy= 0.93750
Iter 55040, Minibatch Loss= 474.747986, Training Accuracy= 0.95312
Iter 56320, Minibatch Loss= 1118.239258, Training Accuracy= 0.91406
Iter 57600, Minibatch Loss= 500.907440, Training Accuracy= 0.96875
Iter 58880, Minibatch Loss= 605.852844, Training Accuracy= 0.96094
Iter 60160, Minibatch Loss= 320.352173, Training Accuracy= 0.95312
Iter 61440, Minibatch Loss= 90.246094, Training Accuracy= 0.98438
Iter 62720, Minibatch Loss= 952.188599, Training Accuracy= 0.94531
Iter 64000, Minibatch Loss= 709.913330, Training Accuracy= 0.92969
Iter 65280, Minibatch Loss= 1092.906006, Training Accuracy= 0.97656
Iter 66560, Minibatch Loss= 1067.758057, Training Accuracy= 0.93750
Iter 67840, Minibatch Loss= 482.389526, Training Accuracy= 0.96875
Iter 69120, Minibatch Loss= 1065.343384, Training Accuracy= 0.94531
Iter 70400, Minibatch Loss= 980.166809, Training Accuracy= 0.96875
Iter 71680, Minibatch Loss= 1640.528564, Training Accuracy= 0.93750
Iter 72960, Minibatch Loss= 612.411499, Training Accuracy= 0.96875
Iter 74240, Minibatch Loss= 432.089081, Training Accuracy= 0.96094
Iter 75520, Minibatch Loss= 326.332825, Training Accuracy= 0.95312
Iter 76800, Minibatch Loss= 903.289001, Training Accuracy= 0.92969
Iter 78080, Minibatch Loss= 440.528931, Training Accuracy= 0.96875
Iter 79360, Minibatch Loss= 431.268433, Training Accuracy= 0.96094
Iter 80640, Minibatch Loss= 215.546570, Training Accuracy= 0.97656
Iter 81920, Minibatch Loss= 476.220642, Training Accuracy= 0.96875
Iter 83200, Minibatch Loss= 66.687637, Training Accuracy= 0.99219
Iter 84480, Minibatch Loss= 109.571373, Training Accuracy= 0.97656
Iter 85760, Minibatch Loss= 780.713013, Training Accuracy= 0.92969
Iter 87040, Minibatch Loss= 470.991821, Training Accuracy= 0.93750
Iter 88320, Minibatch Loss= 665.605103, Training Accuracy= 0.94531
Iter 89600, Minibatch Loss= 486.712555, Training Accuracy= 0.95312
Iter 90880, Minibatch Loss= 1212.130615, Training Accuracy= 0.93750
Iter 92160, Minibatch Loss= 319.079803, Training Accuracy= 0.97656
Iter 93440, Minibatch Loss= 453.148254, Training Accuracy= 0.95312
Iter 94720, Minibatch Loss= 569.428711, Training Accuracy= 0.92188
Iter 96000, Minibatch Loss= 94.126587, Training Accuracy= 0.97656
Iter 97280, Minibatch Loss= 275.076508, Training Accuracy= 0.96094
Iter 98560, Minibatch Loss= 758.537231, Training Accuracy= 0.95312
Iter 99840, Minibatch Loss= 217.494034, Training Accuracy= 0.97656
Iter 101120, Minibatch Loss= 666.817688, Training Accuracy= 0.94531
Iter 102400, Minibatch Loss= 387.140015, Training Accuracy= 0.97656
Iter 103680, Minibatch Loss= 309.601013, Training Accuracy= 0.97656
Iter 104960, Minibatch Loss= 491.488007, Training Accuracy= 0.96875
Iter 106240, Minibatch Loss= 559.815063, Training Accuracy= 0.96094
Iter 107520, Minibatch Loss= 469.924438, Training Accuracy= 0.95312
Iter 108800, Minibatch Loss= 463.599823, Training Accuracy= 0.96875
Iter 110080, Minibatch Loss= 652.325684, Training Accuracy= 0.95312
Iter 111360, Minibatch Loss= 380.302612, Training Accuracy= 0.94531
Iter 112640, Minibatch Loss= 265.474548, Training Accuracy= 0.95312
Iter 113920, Minibatch Loss= 145.682693, Training Accuracy= 0.97656
Iter 115200, Minibatch Loss= 468.385132, Training Accuracy= 0.96094
Iter 116480, Minibatch Loss= 337.575378, Training Accuracy= 0.96875
Iter 117760, Minibatch Loss= 137.029846, Training Accuracy= 0.97656
Iter 119040, Minibatch Loss= 385.774475, Training Accuracy= 0.96875
Iter 120320, Minibatch Loss= 855.515930, Training Accuracy= 0.94531
Iter 121600, Minibatch Loss= 826.428955, Training Accuracy= 0.93750
Iter 122880, Minibatch Loss= 840.954712, Training Accuracy= 0.95312
Iter 124160, Minibatch Loss= 135.299774, Training Accuracy= 0.97656
Iter 125440, Minibatch Loss= 596.197998, Training Accuracy= 0.96094
Iter 126720, Minibatch Loss= 77.240379, Training Accuracy= 0.97656
Iter 128000, Minibatch Loss= 375.658081, Training Accuracy= 0.97656
Iter 129280, Minibatch Loss= 262.859131, Training Accuracy= 0.97656
Iter 130560, Minibatch Loss= 44.147209, Training Accuracy= 0.97656
Iter 131840, Minibatch Loss= 293.386353, Training Accuracy= 0.96875
Iter 133120, Minibatch Loss= 126.623993, Training Accuracy= 0.99219
Iter 134400, Minibatch Loss= 761.212891, Training Accuracy= 0.97656
Iter 135680, Minibatch Loss= 35.652237, Training Accuracy= 0.99219
Iter 136960, Minibatch Loss= 136.761017, Training Accuracy= 0.98438
Iter 138240, Minibatch Loss= 427.506470, Training Accuracy= 0.97656
Iter 139520, Minibatch Loss= 603.883118, Training Accuracy= 0.96094
Iter 140800, Minibatch Loss= 532.734253, Training Accuracy= 0.92969
Iter 142080, Minibatch Loss= 229.608871, Training Accuracy= 0.95312
Iter 143360, Minibatch Loss= 100.882248, Training Accuracy= 0.97656
Iter 144640, Minibatch Loss= 256.012390, Training Accuracy= 0.96875
Iter 145920, Minibatch Loss= 155.531174, Training Accuracy= 0.98438
Iter 147200, Minibatch Loss= 0.000000, Training Accuracy= 1.00000
Iter 148480, Minibatch Loss= 505.638275, Training Accuracy= 0.97656
Iter 149760, Minibatch Loss= 44.933327, Training Accuracy= 0.99219
Iter 151040, Minibatch Loss= 142.327377, Training Accuracy= 0.97656
Iter 152320, Minibatch Loss= 461.496185, Training Accuracy= 0.96094
Iter 153600, Minibatch Loss= 118.370140, Training Accuracy= 0.97656
Iter 154880, Minibatch Loss= 403.881134, Training Accuracy= 0.93750
Iter 156160, Minibatch Loss= 27.085678, Training Accuracy= 0.98438
Iter 157440, Minibatch Loss= 233.737488, Training Accuracy= 0.98438
Iter 158720, Minibatch Loss= 252.954391, Training Accuracy= 0.95312
Iter 160000, Minibatch Loss= 78.215767, Training Accuracy= 0.97656
Iter 161280, Minibatch Loss= 313.302979, Training Accuracy= 0.96875
Iter 162560, Minibatch Loss= 86.086960, Training Accuracy= 0.98438
Iter 163840, Minibatch Loss= 159.479080, Training Accuracy= 0.98438
Iter 165120, Minibatch Loss= 237.137192, Training Accuracy= 0.96875
Iter 166400, Minibatch Loss= 71.959213, Training Accuracy= 0.99219
Iter 167680, Minibatch Loss= 79.412460, Training Accuracy= 0.96094
Iter 168960, Minibatch Loss= 163.784363, Training Accuracy= 0.97656
Iter 170240, Minibatch Loss= 210.554535, Training Accuracy= 0.96875
Iter 171520, Minibatch Loss= 62.252472, Training Accuracy= 0.99219
Iter 172800, Minibatch Loss= 196.883423, Training Accuracy= 0.97656
Iter 174080, Minibatch Loss= 67.746567, Training Accuracy= 0.98438
Iter 175360, Minibatch Loss= 250.539902, Training Accuracy= 0.96875
Iter 176640, Minibatch Loss= 125.992264, Training Accuracy= 0.99219
Iter 177920, Minibatch Loss= 349.852203, Training Accuracy= 0.96875
Iter 179200, Minibatch Loss= 149.910400, Training Accuracy= 0.96875
Iter 180480, Minibatch Loss= 151.258850, Training Accuracy= 0.97656
Iter 181760, Minibatch Loss= 39.953766, Training Accuracy= 0.98438
Iter 183040, Minibatch Loss= 115.344940, Training Accuracy= 0.96094
Iter 184320, Minibatch Loss= 32.580063, Training Accuracy= 0.99219
Iter 185600, Minibatch Loss= 307.355103, Training Accuracy= 0.96875
Iter 186880, Minibatch Loss= 211.242493, Training Accuracy= 0.96875
Iter 188160, Minibatch Loss= 245.367706, Training Accuracy= 0.97656
Iter 189440, Minibatch Loss= 121.856567, Training Accuracy= 0.96875
Iter 190720, Minibatch Loss= 297.908020, Training Accuracy= 0.95312
Iter 192000, Minibatch Loss= 28.702240, Training Accuracy= 0.99219
Iter 193280, Minibatch Loss= 124.350609, Training Accuracy= 0.96094
Iter 194560, Minibatch Loss= 223.368149, Training Accuracy= 0.96094
Iter 195840, Minibatch Loss= 419.990204, Training Accuracy= 0.96875
Iter 197120, Minibatch Loss= 19.725143, Training Accuracy= 0.98438
Iter 198400, Minibatch Loss= 359.439301, Training Accuracy= 0.95312
Iter 199680, Minibatch Loss= 176.249619, Training Accuracy= 0.97656
Optimization Finished!
Testing Accuracy: 0.988281