超全注释
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
batch_size = 100
num_batch = mnist.train.num_examples // batch_size #550
#定义初始化权值
def weight_variable(shape):
initial = tf.truncated_normal(shape,stddev=0.1) #生成一个截断的正态分布
return tf.Variable(initial)
#定义初始化偏置值
def bias_variable(shape):
initial = tf.constant(0.1,shape=shape)
return tf.Variable(initial)
#定义卷积层
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
#定义最大池化层
def max_pool_2x2(x):
"""
ksize 是窗口大小~~~
strides 是步长
"""
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784]) #28*28
y = tf.placeholder(tf.float32,[None,10])
#改变x的格式,转换为4D向量 [batch,height,width,channels]
x_image = tf.reshape(x,[-1,28,28,1]) #batch=-1表示不确定个数,相当于前面定义的100
#初始化第一个卷积层的权值和偏置
W_conv1 = weight_variable([5,5,1,32]) #卷积核5*5,1个特征平面,32个卷积核
b_conv1 = weight_variable([32]) #每个卷积核1个偏置
#把x_image进行卷积(x_image×卷积+偏置,再用relu函数激活)
A_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
#再经过最大池化层
A_pool1 = max_pool_2x2(A_conv1)
#初始化第二个卷积层的权值和偏置
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = weight_variable([64])
A_conv2 = tf.nn.relu(conv2d(A_pool1,W_conv2)+b_conv2)
A_pool2 = max_pool_2x2(A_conv2)
# print('A_pool2的shape:',A_pool2.shape)
#初始化第一个全连接层的权值
W_fc1 = weight_variable([7*7*64,1024])
b_fc1 = weight_variable([1024])
#把池化层2的输出扁平化为1维
A_pool2_flag = tf.reshape(A_pool2,[-1,7*7*64])
# print('A_pool2_flag的shape:',A_pool2_flag.shape)
#求第一个全连接层的输出
A_fc1 = tf.nn.relu(tf.matmul(A_pool2_flag,W_fc1)+b_fc1)
#keep_prob用来表示神经元的输出概率
keep_prob = tf.placeholder(tf.float32)
A_fc1_dropout = tf.nn.dropout(A_fc1,keep_prob)
#初始化第二个全连接层
W_fc2 = weight_variable([1024,10])
b_fc2 = weight_variable([10])
#求第二个全连接层的输出(输出用softmax函数)
prediction = tf.nn.softmax(tf.matmul(A_fc1_dropout,W_fc2)+b_fc2)
#定义代价函数为交叉熵
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#优化器选用adam
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
#结果存放在一个bool列表中
correct_prection = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prection,tf.float32))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(21):
for batch in range(num_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})
acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
print('Iter'+str(epoch)+',Testing Accuracy:'+str(acc))
Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Iter0,Testing Accuracy:0.963
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-18-1a74dac98566> in <module>
91 sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.7})
92
---> 93 acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
94 print('Iter'+str(epoch)+',Testing Accuracy:'+str(acc))
95
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
948 try:
949 result = self._run(None, fetches, feed_dict, options_ptr,
--> 950 run_metadata_ptr)
951 if run_metadata:
952 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1171 if final_fetches or final_targets or (handle and feed_dict_tensor):
1172 results = self._do_run(handle, final_targets, final_fetches,
-> 1173 feed_dict_tensor, options, run_metadata)
1174 else:
1175 results = []
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1348 if handle is None:
1349 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1350 run_metadata)
1351 else:
1352 return self._do_call(_prun_fn, handle, feeds, fetches)
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1354 def _do_call(self, fn, *args):
1355 try:
-> 1356 return fn(*args)
1357 except errors.OpError as e:
1358 message = compat.as_text(e.message)
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1339 self._extend_graph()
1340 return self._call_tf_sessionrun(
-> 1341 options, feed_dict, fetch_list, target_list, run_metadata)
1342
1343 def _prun_fn(handle, feed_dict, fetch_list):
~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1427 return tf_session.TF_SessionRun_wrapper(
1428 self._session, options, feed_dict, fetch_list, target_list,
-> 1429 run_metadata)
1430
1431 def _call_tf_sessionprun(self, handle, feed_dict, fetch_list):
KeyboardInterrupt:
鉴于cpu版跑太慢,所以我就把代码放在服务器上用GPU跑了。运行结果如下: