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

本文通过使用卷积神经网络(CNN)在MNIST手写数字数据集上进行分类任务,展示了如何从零开始搭建并训练一个深度学习模型。文中详细介绍了模型各组成部分的设计与实现,包括权重和偏置初始化、卷积层、池化层及全连接层等,并给出了具体的Python代码和训练结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[小编推荐] 假定您已经安装好了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  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值