import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#读取数据
mnist=input_data.read_data_sets('MNIST——data',one_hot=True)
#定义权重
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_pooling_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],padding='SAME')
#定义求准确度的方法
def compute_accuracy(v_xs,v_ys):
global prediction
y_pre=sess.run(prediction,feed_dict={xs:v_xs,keep_prob:1})
correct_prediction=tf.equal(tf.arg_max(y_pre,1),tf.arg_max(v_ys,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
result=sess.run(accuracy,feed_dic