[关闭]
@Pigmon 2017-04-14T23:45:45.000000Z 字数 4434 阅读 905

convolutional_network.py 注释

教案


  1. '''
  2. A Convolutional Network implementation example using TensorFlow library.
  3. This example is using the MNIST database of handwritten digits
  4. (http://yann.lecun.com/exdb/mnist/)
  5. Author: Aymeric Damien
  6. Project: https://github.com/aymericdamien/TensorFlow-Examples/
  7. '''
  8. from __future__ import print_function
  9. import tensorflow as tf
  10. # Import MNIST data
  11. from tensorflow.examples.tutorials.mnist import input_data
  12. mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
  13. # Parameters
  14. # 参数
  15. learning_rate = 0.001 # 学习率
  16. training_iters = 200000 # 训练次数
  17. batch_size = 128 # 批处理大小,即每次输入多少个数据进入网络
  18. display_step = 10 # 每隔多少步输出一次 log
  19. # Network Parameters
  20. # 卷积神经网络的参数
  21. n_input = 784 # MNIST data input (img shape: 28*28)
  22. # 输入特征维度,因为MNIST数据集是28x28的图像,所以维度是28x28=784
  23. n_classes = 10 # MNIST total classes (0-9 digits)
  24. # 分类数,因为是区分0-9的数字,所以类别有10个
  25. dropout = 0.75 # Dropout, probability to keep units
  26. # 优化网络的参数,代表每个神经元每次计算有 25% 的几率被忽略
  27. # tf Graph input
  28. # 输入数据 x: 特征, y: 人工标记类别
  29. x = tf.placeholder(tf.float32, [None, n_input])
  30. y = tf.placeholder(tf.float32, [None, n_classes])
  31. keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)
  32. # Create some wrappers for simplicity
  33. # 建立一个简化的建立卷积层的函数
  34. # x: 输入特征值
  35. # W:权重
  36. # b: bias
  37. # strides: 卷积核移动步长
  38. def conv2d(x, W, b, strides=1):
  39. # Conv2D wrapper, with bias and relu activation
  40. x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
  41. x = tf.nn.bias_add(x, b)
  42. return tf.nn.relu(x)
  43. # 定义一个简化的建立下采样层的函数
  44. # x: 被下采样的数据,4-D tensor [块大小,长,宽,通道数(灰度就是1,rgb就是3)]
  45. # k:下采样核的尺寸,2 代表 2x2
  46. def maxpool2d(x, k=2):
  47. # MaxPool2D wrapper
  48. return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
  49. padding='SAME')
  50. # Create model
  51. # 建立卷积神经网络模型
  52. def conv_net(x, weights, biases, dropout):
  53. # Reshape input picture
  54. # reshape 输入的图像,-1 代表让 tf 自动计算,这里是图像的数量
  55. x = tf.reshape(x, shape=[-1, 28, 28, 1])
  56. # ----------卷积网络----------
  57. # Convolution Layer
  58. # 加一个卷积层
  59. conv1 = conv2d(x, weights['wc1'], biases['bc1'])
  60. # Max Pooling (down-sampling)
  61. # 加一个下采样层
  62. conv1 = maxpool2d(conv1, k=2)
  63. # Convolution Layer
  64. # 再加一个卷积层
  65. conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
  66. # Max Pooling (down-sampling)
  67. # 再加一个下采样层
  68. conv2 = maxpool2d(conv2, k=2)
  69. # ----------全连接层----------
  70. # Fully connected layer
  71. # 建立全连接层
  72. # Reshape conv2 output to fit fully connected layer input
  73. # 把上一层输出的数据 reshape 到适应神经网络的输入
  74. fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])
  75. # wx + b
  76. fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
  77. # 建立全连接层,激活函数为 relu
  78. fc1 = tf.nn.relu(fc1)
  79. # Apply Dropout
  80. # 丢弃参数
  81. fc1 = tf.nn.dropout(fc1, dropout)
  82. # ----------输出层----------
  83. # Output, class prediction
  84. # 输出层,用于分类。wx + b
  85. out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
  86. return out
  87. # Store layers weight & bias
  88. # 存储所有权重的 dict
  89. weights = {
  90. # 5x5 conv, 1 input, 32 outputs
  91. # 第一个卷积层的权重,卷积核的尺寸是 5×5,1个输入,输出定为32
  92. 'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
  93. # 5x5 conv, 32 inputs, 64 outputs
  94. # 上一层的32个输出变成这一层的输入,这层输出定为 64
  95. 'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
  96. # fully connected, 7*7*64 inputs, 1024 outputs
  97. # 全连接层权重,因为28×28的图像经过了2次下采样,每次小一半,
  98. # 所以现在是 7×7,全连接层是普通的神经网络,所以数据要拉平
  99. # 成一维数组,即数量为 7×7×64,64为上一层的输出数量
  100. # 而这一层的输出定为 1024
  101. 'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
  102. # 1024 inputs, 10 outputs (class prediction)
  103. # 输出层,上一层的 1024 个输入
  104. # 输出类别为10个数字的概率,所以为 10
  105. 'out': tf.Variable(tf.random_normal([1024, n_classes]))
  106. }
  107. # 存储所有 bias 的 dict
  108. # 数量跟每层定好的输出数量一致
  109. biases = {
  110. 'bc1': tf.Variable(tf.random_normal([32])),
  111. 'bc2': tf.Variable(tf.random_normal([64])),
  112. 'bd1': tf.Variable(tf.random_normal([1024])),
  113. 'out': tf.Variable(tf.random_normal([n_classes]))
  114. }
  115. # Construct model
  116. # 调用上面的自定义函数来构建卷积神经网络
  117. pred = conv_net(x, weights, biases, keep_prob)
  118. # Define loss and optimizer
  119. # 定义损失函数和训练函数
  120. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
  121. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
  122. # Evaluate model
  123. # 定义正确率评估方式
  124. correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
  125. accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
  126. # Initializing the variables
  127. # 初始化变量,所有有变量的程序都要有这句
  128. init = tf.global_variables_initializer()
  129. # Launch the graph
  130. # 启动图 (会话)
  131. with tf.Session() as sess:
  132. sess.run(init)
  133. step = 1
  134. # Keep training until reach max iterations
  135. # 循环到达到最大训练次数
  136. while step * batch_size < training_iters:
  137. # 从训练集获取 batch_size 个数据
  138. batch_x, batch_y = mnist.train.next_batch(batch_size)
  139. # Run optimization op (backprop)
  140. # 训练
  141. sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
  142. keep_prob: dropout})
  143. # 每 10 步输出一次 log
  144. if step % display_step == 0:
  145. # Calculate batch loss and accuracy
  146. # 计算损失函数值和正确率
  147. loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
  148. y: batch_y,
  149. keep_prob: 1.})
  150. print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
  151. "{:.6f}".format(loss) + ", Training Accuracy= " + \
  152. "{:.5f}".format(acc))
  153. step += 1
  154. print("Optimization Finished!")
  155. # Calculate accuracy for 256 mnist test images
  156. # 计算测试数据的正确率
  157. print("Testing Accuracy:", \
  158. sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
  159. y: mnist.test.labels[:256],
  160. keep_prob: 1.}))
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注