Tensorflow初步之MNIST手写数字分类

神经网络(输入:784个神经元)(中间层:(1):1000,(2):300个神经元)(输出:10个神经元)

一、

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST",one_hot=True)#获得MNIST数据集,并进行0-1编码

二、小批次带入神经网络

#每个批次数
batch_size = 100
#批次量
n_batch = mnist.train.num_examples // batch_size

#定义两个占位符
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32)

三、神经网络中间层

#神经网络中间层
Weights_L1 = tf.Variable(tf.truncated_normal([784,1000],stddev=0.1))
biases_L1 = tf.Variable(tf.zeros([1,1000])+0.1)
Wx_plus_b_L1 = tf.matmul(x,Weights_L1) + biases_L1
L1 = tf.nn.tanh(Wx_plus_b_L1)
L1_drop = tf.nn.dropout(L1,keep_prob==keep_prob)

#神经网络中间层2
Weights_L2 = tf.Variable(tf.truncated_normal([1000,300],stddev=0.1))
biases_L2 = tf.Variable(tf.zeros([1,300])+0.1)
Wx_plus_b_L2 = tf.matmul(L1_drop,Weights_L2) + biases_L2
L2 = tf.nn.tanh(Wx_plus_b_L2)
L2_drop = tf.nn.dropout(L2,keep_prob==keep_prob)

四、神经网络输出层

 

#神经网络输出层
W = tf.Variable(tf.truncated_normal([300,10],stddev=0.1))
b = tf.Variable(tf.zeros([1,10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L2_drop,W)+b)

五、二次代价函数

#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=prediction))
#softmax激活函数对应的交叉熵代价函数
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

六、对测试集进行预测及比较

init = tf.global_variables_initializer()
correct_prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(prediction,1)) #arg_max求出最大值的索引,tf.equal相等为True
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) #tf.cast,将布尔值转换为浮点

七、运行神经网络

with tf.Session() as sess:
    sess.run(init)
    for i in range(20):
        for _ in range(n_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.6})
            
        test_ass = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1})
        train_ass = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1})
        print('Iter : ' + str(i)+' ,Testing Accuracy : ' + str(test_ass) + ' ,Training Accuracy : ' + str(train_ass))
        #droup随机筛选神经元,使得训练集和测试集的预测速度更加相近,趋于稳定的状态,减小过拟合程度

#结果
Iter : 0 ,Testing Accuracy : 0.8456 ,Training Accuracy : 0.8446909
Iter : 1 ,Testing Accuracy : 0.9425 ,Training Accuracy : 0.94445455
Iter : 2 ,Testing Accuracy : 0.9512 ,Training Accuracy : 0.9563636
Iter : 3 ,Testing Accuracy : 0.9546 ,Training Accuracy : 0.96294546
Iter : 4 ,Testing Accuracy : 0.9574 ,Training Accuracy : 0.96745455
Iter : 5 ,Testing Accuracy : 0.9598 ,Training Accuracy : 0.9715091
Iter : 6 ,Testing Accuracy : 0.9635 ,Training Accuracy : 0.97516364
Iter : 7 ,Testing Accuracy : 0.9644 ,Training Accuracy : 0.97769094
Iter : 8 ,Testing Accuracy : 0.9647 ,Training Accuracy : 0.9784182
Iter : 9 ,Testing Accuracy : 0.9664 ,Training Accuracy : 0.98072726
Iter : 10 ,Testing Accuracy : 0.9688 ,Training Accuracy : 0.9825091
Iter : 11 ,Testing Accuracy : 0.9689 ,Training Accuracy : 0.9838727
Iter : 12 ,Testing Accuracy : 0.9702 ,Training Accuracy : 0.9851818
Iter : 13 ,Testing Accuracy : 0.971 ,Training Accuracy : 0.9860727
Iter : 14 ,Testing Accuracy : 0.971 ,Training Accuracy : 0.9866727
Iter : 15 ,Testing Accuracy : 0.9699 ,Training Accuracy : 0.98718184
Iter : 16 ,Testing Accuracy : 0.9717 ,Training Accuracy : 0.9879091
Iter : 17 ,Testing Accuracy : 0.9724 ,Training Accuracy : 0.9883636
Iter : 18 ,Testing Accuracy : 0.9723 ,Training Accuracy : 0.9887091
Iter : 19 ,Testing Accuracy : 0.9723 ,Training Accuracy : 0.9890909

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值