简单学习tensorflow mnist数字识别

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf 
mnist=input_data.read_data_sets("D:/pgl/desktop/Experiment/mnist/data/minst/",one_hot=True)  #数据集路径
print(mnist.train.num_examples)
'''计算图定义'''
v1=tf.Variable(tf.truncated_normal([784,500],stddev=0.1))
v2=tf.Variable(tf.truncated_normal([500,10],stddev=0.1))
b1=tf.Variable(tf.constant(0.1,shape=[500]))
b2=tf.Variable(tf.constant(0.1,shape=[10]))

input_tensor=tf.placeholder(dtype=tf.float32,shape=(None,784),name="input")
y_=tf.placeholder(dtype=tf.float32,shape=(None,10),name="output")
def inference(input_tensor,v1,b1,v2,b2):
    o1=tf.nn.relu(tf.matmul(input_tensor,v1)+b1)
    #o1=tf.matmul(input_tensor,v1)+b1
    o2=tf.matmul(o1,v2)+b2
    return o2
y=inference(input_tensor,v1,b1,v2,b2)

'''损失函数、优化方法、验证方法定义'''
cross_entropy=tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.arg_max(y_,1)))
regu=tf.contrib.layers.l2_regularizer(0.0001)
regu_loss=regu(v1)+regu(v2)
loss=regu_loss+cross_entropy

global_step=tf.Variable(0,trainable=False)
learn_rate=tf.train.exponential_decay(0.8,global_step,mnist.train.num_examples/100,0.99)
train_step=tf.train.GradientDescentOptimizer(learn_rate).minimize(loss,global_step=global_step)


acc=tf.equal(tf.arg_max(y_,1),tf.arg_max(y,1))
accuracy=tf.reduce_mean(tf.cast(acc,tf.float32))


'''神经网络优化'''
with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())
    for i in range(30000):
        if i%1000==0:
            vacc=sess.run(accuracy,feed_dict={input_tensor:mnist.validation.images,y_:mnist.validation.labels})
            print(vacc)
        xs,ys=mnist.train.next_batch(100)
        sess.run(train_step,feed_dict={input_tensor:xs,y_:ys})
    tacc=sess.run(accuracy,feed_dict={input_tensor:mnist.test.images,y_:mnist.test.labels})
    print(tacc)
    
    
"""
测试结果:
都使用:tacc=0.9842
不使用激活函数tacc=0.0958 why???
隐藏层节点改为100 tacc=0.9785
隐藏层节点改为1000 tacc=0.9832
不用正则化tacc=0.9823
学习率改为0.5 tacc=0.9824


"""

你可能感兴趣的:(简单学习tensorflow mnist数字识别)