欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于tensorflow实现mnist手写识别 (多层神经网络)

程序员文章站 2022-12-23 12:08:11
标题党其实也不多,一个输入层,三个隐藏层,一个输出层 老样子先上代码 导入mnist的路径很长,现在还记不住 设置输入层,X为样本数据,y是标签值 X 784是因为28*28,None是因为不知道需要用多少样本 Y 10是因为 0~9的预测输出,None理由同上 3层这样写有点啰嗦 下一版有个用函数 ......

标题党其实也不多,一个输入层,三个隐藏层,一个输出层

老样子先上代码

导入mnist的路径很长,现在还记不住

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import numpy as np
import matplotlib.pyplot as plt
from time import time
mnist =  input_data.read_data_sets("data/",one_hot = true)
#导入tensorflwo和mnist数据集等 常用库

  

设置输入层,x为样本数据,y是标签值

x 784是因为28*28,none是因为不知道需要用多少样本

y 10是因为 0~9的预测输出,none理由同上

x = tf.placeholder(tf.float32,[none,784],name='x')
y = tf.placeholder(tf.float32,[none,10],name='y')

3层这样写有点啰嗦 下一版有个用函数实现的比这个好。

tf.truncated_normal([784,h1_nn],stddev = 0.1)以截断正态分布的随机初始化,数学原理不解释(budong),反正大小控制在stddev里面 方便后面训练
h1_nn = 256 #第一层神经元节点数
h2_nn = 64 #第二层神经元节点数
h3_nn = 32 #第三层神经元节点数
#第一层
w1 = tf.variable(tf.truncated_normal([784,h1_nn],stddev = 0.1))
b1 = tf.variable(tf.zeros(h1_nn))
#第二层
w2 = tf.variable(tf.truncated_normal([h1_nn,h2_nn],stddev = 0.1))
b2 = tf.variable(tf.zeros(h2_nn))
#第三层
w3 = tf.variable(tf.truncated_normal([h2_nn,h3_nn],stddev = 0.1))
b3 = tf.variable(tf.zeros(h3_nn))
#输出层
w4 = tf.variable(tf.truncated_normal([h3_nn,10],stddev = 0.1)) 
b4 = tf.variable(tf.zeros(10))

输出 不多讲了 前三层使用了relu,最后输出因为是10分类所有使用了softmax

(今天写的时候记错了pred输出使用了loss函数的softmax计算导致程序报错,先记下来)

y1 = tf.nn.relu(tf.matmul(x,w1)+b1) #使用relu当作激活函数
y2 = tf.nn.relu(tf.matmul(y1,w2)+b2)#使用relu当作激活函数
y3 = tf.nn.relu(tf.matmul(y2,w3)+b3)#使用relu当作激活函数
forward = tf.matmul(y3,w4)+b4 
pred = tf.nn.softmax(forward)#输出层分类应用使用softmax当作激活函数

没错上面说的就是这个tf.nn.sofmax_cross_entropy_with_logits,不使用这个使用第一版的

#损失函数使用交叉熵
loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = forward,labels = y))

会因为log为0导致梯度爆炸 数学原理不太懂 以后补一下,会了再来填充

loss_function = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),
                                              reduction_indices=1))

超参数设置啥的没啥好说,值得一提total_batch 好像是类似一个洗牌的函数

#设置训练参数
train_epochs = 50
batch_size = 50
total_batch = int(mnist.train.num_examples/batch_size) #随机抽取样本
learning_rate = 0.01
display_step = 1

优化器,(反向传播?)不确定 反正用来调整最优的w和b

#优化器
opimizer = tf.train.adamoptimizer(learning_rate).minimize(loss_function)

利用argmax对比预测结果和标签值,方便后面统计准确率

#定义准确率
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(pred,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

开始训练,后面会补一个保存和调用模型的代码不然以后模型大了 不保存都要程序跑一次才能用太费时间,这里print我把用format的删了 因为不太会用

#开始训练
sess = tf.session()
init = tf.global_variables_initializer()
starttime = time()
sess.run(init)
for epochs in range(train_epochs):
    for batch in range(total_batch):
        xs,ys = mnist.train.next_batch(batch_size)#读取批次数据
        sess.run(opimizer,feed_dict={x:xs,y:ys})#执行批次数据训练
    
    #total_batch个批次训练完成后,使用验证数据计算误差与准确率
    loss,acc =  sess.run([loss_function,accuracy],
                        feed_dict={
                            x:mnist.validation.images,
                            y:mnist.validation.labels})
    #输出训练情况
    if(epochs+1) % display_step == 0:
        epochs += 1 
        print("train epoch:",epochs,
               "loss=",loss,"accuracy=",acc)
duration = time()-starttime
print("trian finshed takes:","{:.2f}".format(duration))#显示预测耗时

最后50轮训练后准确率是0.97左右 已经收敛了

使用测试集评估模型

#评估模型
accu_test =  sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("model accuracy:",accu_test)

  准确率0.9714,还行

到这里就结束了,最后把完整代码放上来 方便以后看

基于tensorflow实现mnist手写识别 (多层神经网络)
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import numpy as np
import matplotlib.pyplot as plt
from time import time
mnist =  input_data.read_data_sets("data/",one_hot = true)
#导入tensorflwo和mnist数据集等 常用库
x = tf.placeholder(tf.float32,[none,784],name='x')
y = tf.placeholder(tf.float32,[none,10],name='y')
h1_nn = 256 #第一层神经元节点数
h2_nn = 64 #第二层神经元节点数
h3_nn = 32 #第三层神经元节点数
#第一层
w1 = tf.variable(tf.truncated_normal([784,h1_nn],stddev = 0.1))
b1 = tf.variable(tf.zeros(h1_nn))
#第二层
w2 = tf.variable(tf.truncated_normal([h1_nn,h2_nn],stddev = 0.1))
b2 = tf.variable(tf.zeros(h2_nn))
#第三层
w3 = tf.variable(tf.truncated_normal([h2_nn,h3_nn],stddev = 0.1))
b3 = tf.variable(tf.zeros(h3_nn))
#输出层
w4 = tf.variable(tf.truncated_normal([h3_nn,10],stddev = 0.1)) 
b4 = tf.variable(tf.zeros(10))
#计算结果
y1 = tf.nn.relu(tf.matmul(x,w1)+b1) #使用relu当作激活函数
y2 = tf.nn.relu(tf.matmul(y1,w2)+b2)#使用relu当作激活函数
y3 = tf.nn.relu(tf.matmul(y2,w3)+b3)#使用relu当作激活函数
forward = tf.matmul(y3,w4)+b4 
pred = tf.nn.softmax(forward)#输出层分类应用使用softmax当作激活函数
#损失函数使用交叉熵
loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = forward,labels = y))
#设置训练参数
train_epochs = 50
batch_size = 50
total_batch = int(mnist.train.num_examples/batch_size) #随机抽取样本
learning_rate = 0.01
display_step = 1
#优化器
opimizer = tf.train.adamoptimizer(learning_rate).minimize(loss_function)
#定义准确率
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(pred,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#开始训练
sess = tf.session()
init = tf.global_variables_initializer()
starttime = time()
sess.run(init)
for epochs in range(train_epochs):
    for batch in range(total_batch):
        xs,ys = mnist.train.next_batch(batch_size)#读取批次数据
        sess.run(opimizer,feed_dict={x:xs,y:ys})#执行批次数据训练
    
    #total_batch个批次训练完成后,使用验证数据计算误差与准确率
    loss,acc =  sess.run([loss_function,accuracy],
                        feed_dict={
                            x:mnist.validation.images,
                            y:mnist.validation.labels})
    #输出训练情况
    if(epochs+1) % display_step == 0:
        epochs += 1 
        print("train epoch:",epochs,
               "loss=",loss,"accuracy=",acc)
duration = time()-starttime
print("trian finshed takes:","{:.2f}".format(duration))#显示预测耗时
#评估模型
accu_test =  sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("model accuracy:",accu_test)
全部代码