1 import cifar10, cifar10_input
  2 import tensorflow as tf
  3 import numpy as np
  4 import time
  5 import math
  6 
  7 max_steps = 3000
  8 batch_size = 128
  9 data_dir = '/tmp/cifar10_data/cifar-10-batches-bin'
 10 
 11 
 12 def variable_with_weight_loss(shape, stddev, w1):
 13     '''定义初始化weight函数,使用tf.truncated_normal截断的正态分布,但加上L2的loss,相当于做了一个L2的正则化处理'''
 14     var = tf.Variable(tf.truncated_normal(shape, stddev=stddev))
 15     '''w1:控制L2 loss的大小,tf.nn.l2_loss函数计算weight的L2 loss'''
 16     if wl is not None:
 17         weight_loss = tf.multiply(tf.nn.l2_loss(var), w1, name='weight_loss')
 18         '''tf.add_to_collection:把weight losses统一存到一个collection,名为losses'''
 19         tf.add_to_collection('losses', weight_loss)
 20 
 21     return var
 22 
 23 
 24 # 使用cifar10类下载数据集并解压展开到默认位置
 25 cifar10.maybe_download_and_extract()
 26 
 27 '''distored_inputs函数产生训练需要使用的数据,包括特征和其对应的label,
 28 返回已经封装好的tensor,每次执行都会生成一个batch_size的数量的样本'''
 29 images_train, labels_train = cifar10_input.distored_inputs(data_dir=data_dir,
 30                                                            batch_size=batch_size)
 31 
 32 images_test, labels_test = cifar10_input.inputs(eval_data=True,
 33                                                 data_dir=data_dir,
 34                                                 batch_size=batch_size)
 35 
 36 image_holder = tf.placeholder(tf.float32, [batch_size, 24, 24, 3])
 37 label_holder = tf.placeholder(tf.int32, [batch_size])
 38 
 39 '''第一个卷积层:使用variable_with_weight_loss函数创建卷积核的参数并进行初始化。
 40 第一个卷积层卷积核大小:5x5 3:颜色通道 64:卷积核数目
 41 weight1初始化函数的标准差为0.05,不进行正则wl(weight loss)设为0'''
 42 weight1 = variable_with_weight_loss(shape=[5, 5, 3, 64], stddev=5e-2, wl=0.0)
 43 # tf.nn.conv2d函数对输入image_holder进行卷积操作
 44 kernel1 = tf.nn.conv2d(image_holder, weight1, [1, 1, 1, 1], padding='SAME')
 45 
 46 bias1 = tf.Variable(tf.constant(0.0, shape=[64]))
 47 
 48 conv1 = tf.nn.relu(tf.nn.bias_add(kernel1, bias1))
 49 # 最大池化层尺寸为3x3,步长为2x2
 50 pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1])
 51 # LRN层模仿生物神经系统的'侧抑制'机制
 52 norm1 = tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
 53 
 54 '''第二个卷积层:'''
 55 weight2 = variable_with_weight_loss(shape=[5, 5, 64, 64], stddev=5e-2, wl=0.0)
 56 kernel2 = tf.nn.conv2d(norm1, weight2, [1, 1, 1, 1], padding='SAME')
 57 # bias2初始化为0.1
 58 bias2 = tf.Variable(tf.constant(0.1, shape=[64]))
 59 
 60 conv2 = tf.nn.relu(tf.nn.bias_add(kernel2, bias2))
 61 norm2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75)
 62 pool2 = tf.nn.max_pool(norm2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
 63 
 64 # 全连接层
 65 reshape = tf.reshape(pool2, [batch_size, -1])
 66 dim = reshape.get_shape()[1].value
 67 weight3 = variable_with_weight_loss(shape=[dim, 384], stddev=0.04, wl=0.004)
 68 bias3 = tf.Variable(tf.constant(0.1, shape=[384]))
 69 local3 = tf.nn.relu(tf.matmul(reshape, weight3) + bias3)
 70 
 71 # 全连接层,隐含层节点数下降了一半
 72 weight4 = variable_with_weight_loss(shape=[384, 182], stddev=0.04, wl=0.004)
 73 bias4 = tf.Variable(tf.constant(0.1, shape=[192]))
 74 local4 = tf.nn.relu(tf.matmul(local3, weight4) + bias4)
 75 
 76 '''正态分布标准差设为上一个隐含层节点数的倒数,且不计入L2的正则'''
 77 weight5 = variable_with_weight_loss(shape=[192, 10], stddev=1 / 192.0, wl=0.0)
 78 bias5 = tf.Variable(tf.constant(0.0, shape=[10]))
 79 logits = tf.add(tf.matmul(local4, weight5), bias5)
 80 
 81 
 82 def loss(logits, labels):
 83     '''计算CNN的loss
 84     tf.nn.sparse_softmax_cross_entropy_with_logits作用:
 85     把softmax计算和cross_entropy_loss计算合在一起'''
 86     labels = tf.cast(labels, tf.int64)
 87     cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
 88         logits=logits, labels=labels, name='cross_entropy_per_example')
 89     # tf.reduce_mean对cross entropy计算均值
 90     cross_entropy_mean = tf.reduce_mean(cross_entropy,
 91                                         name='cross_entropy')
 92     # tf.add_to_collection:把cross entropy的loss添加到整体losses的collection中
 93     tf.add_to_collection('losses', cross_entropy_mean)
 94     # tf.add_n将整体losses的collection中的全部loss求和得到最终的loss
 95     return tf.add_n(tf.get_collection('losses'), name='total_loss')
 96 
 97 
 98 # 将logits节点和label_holder传入loss计算得到最终loss
 99 loss = loss(logits, label_holder)
100 
101 train_op = tf.trian.AdamOptimizer(1e-3).minimize(loss)
102 # 求输出结果中top k的准确率,默认使用top 1(输出分类最高的那一类的准确率)
103 top_k_op = tf.nn.in_top_k(logits, label_holder, 1)
104 
105 sess = tf.InteractiveSession()
106 tf.global_variables_initializer().run()
107 tf.trian.start_queue_runners()
108 
109 for step in range(max_steps):
110     '''training:'''
111     start_time = time.time()
112     # 获得一个batch的训练数据
113     image_batch, label_batch = sess.run([images_train, labels_train])
114     # 将batch的数据传入train_op和loss的计算
115     _, loss_value = sess.run([train_op, loss],
116                              feed_dict={image_holder: image_batch, label_holder: label_batch})
117 
118     duration = time.time() - start_time
119     if step % 10 == 0:
120         # 每秒能训练的数量
121         examples_per_sec = batch_size / duration
122         # 一个batch数据所花费的时间
123         sec_per_batch = float(duration)
124 
125         format_str = ('step %d, loss=%.2f (%.1f examples/sec; %.3f sec/batch)')
126         print(format_str % (step, loss_value, examples_per_sec, sec_per_batch))
127 # 样本数
128 num_examples = 10000
129 num_iter = int(math.ceil(num_examples / batch_size))
130 true_count = 0
131 total_sample_count = num_iter * batch_size
132 step = 0
133 while step < num_iter:
134     # 获取images-test labels_test的batch
135     image_batch, label_batch = sess.run([images_test, labels_test])
136     # 计算这个batch的top 1上预测正确的样本数
137     preditcions = sess.run([top_k_op], feed_dict={image_holder: image_batch,
138                                                   label_holder: label_batch
139                                                   })
140     # 全部测试样本中预测正确的数量
141     true_count += np.sum(preditcions)
142     step += 1
143 # 准确率
144 precision = true_count / total_sample_count
145 print('precision @ 1 = %.3f' % precision)
1 step 2970, loss = 0.95 (877.4 examples/sec; 0.146 sec/batch)
2 step 2980, loss = 1.12 (862.6 examples/sec; 0.148 sec/batch)
3 step 2990, loss = 1.06 (967.1 examples/sec; 0.132 sec/batch)
4 precision @ 1 = 0.705