TensorFlow教程06:MNIST的CNN实现——源码和运行结果

来源:互联网 发布:java 线程模型 编辑:程序博客网 时间:2024/05/22 00:34

[小编推荐] 假定您已经安装好了TensorFlow,这里放了第二个MNIST实验的代码和参考结果,你可以直接运行验证。

源码

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #!/usr/bin/python  
  2. import tensorflow as tf  
  3. import sys  
  4. from tensorflow.examples.tutorials.mnist import input_data  
  5.   
  6. def weight_variable(shape):  
  7.   initial = tf.truncated_normal(shape, stddev=0.1)  
  8.   return tf.Variable(initial)  
  9.   
  10. def bias_variable(shape):  
  11.   initial = tf.constant(0.1, shape=shape)  
  12.   return tf.Variable(initial)  
  13.   
  14. def conv2d(x, W):  
  15.   return tf.nn.conv2d(x, W, strides=[1111], padding='SAME')  
  16.   
  17. def max_pool_2x2(x):  
  18.   return tf.nn.max_pool(x, ksize=[1221], strides=[1221], padding='SAME')  
  19.   
  20. mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)  
  21.   
  22. sess = tf.InteractiveSession()  
  23.   
  24. x = tf.placeholder("float", shape=[None784])  
  25. y_ = tf.placeholder("float", shape=[None10])  
  26.   
  27. W = tf.Variable(tf.zeros([784,10]))  
  28. b = tf.Variable(tf.zeros([10]))  
  29.   
  30. W_conv1 = weight_variable([55132])  
  31. b_conv1 = bias_variable([32])  
  32.   
  33. x_image = tf.reshape(x, [-128281])  
  34.   
  35. h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)  
  36. h_pool1 = max_pool_2x2(h_conv1)  
  37.   
  38. W_conv2 = weight_variable([553264])  
  39. b_conv2 = bias_variable([64])  
  40.   
  41. h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)  
  42. h_pool2 = max_pool_2x2(h_conv2)  
  43.   
  44. # Now image size is reduced to 7*7  
  45. W_fc1 = weight_variable([7 * 7 * 641024])  
  46. b_fc1 = bias_variable([1024])  
  47.   
  48. h_pool2_flat = tf.reshape(h_pool2, [-17*7*64])  
  49. h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)  
  50.   
  51. keep_prob = tf.placeholder("float")  
  52. h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)  
  53.   
  54. W_fc2 = weight_variable([102410])  
  55. b_fc2 = bias_variable([10])  
  56. y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)  
  57.   
  58.   
  59. cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))  
  60. train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)  
  61. correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))  
  62. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))  
  63. sess.run(tf.initialize_all_variables())  
  64.   
  65. for i in range(20000):  
  66.   batch = mnist.train.next_batch(50)  
  67.   if i%100 == 0:  
  68.     train_accuracy = accuracy.eval(feed_dict={  
  69.         x:batch[0], y_: batch[1], keep_prob: 1.0})  
  70.     print "step %d, training accuracy %.3f"%(i, train_accuracy)  
  71.   train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})  
  72.   
  73. print "Training finished"  
  74.   
  75. print "test accuracy %.3f" % accuracy.eval(feed_dict={  
  76.     x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})  

运行结果

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Extracting MNIST_data/train-images-idx3-ubyte.gz  
  2. Extracting MNIST_data/train-labels-idx1-ubyte.gz  
  3. Extracting MNIST_data/t10k-images-idx3-ubyte.gz  
  4. Extracting MNIST_data/t10k-labels-idx1-ubyte.gz  
  5. step 0, training accuracy 0.140  
  6. step 100, training accuracy 0.840  
  7. step 200, training accuracy 0.900  
  8. step 300, training accuracy 0.840  
  9. step 400, training accuracy 0.980  
  10. step 500, training accuracy 0.940  
  11. ...  
  12. step 19900, training accuracy 1.000  
  13. Training finished  
  14. test accuracy 0.993  

0 0
原创粉丝点击