概述
1.思路
使用两层隐藏层的神经网络来实现mnist分类,这里不使用卷积神经网络来处理。
两层隐藏层含有神经元数量分别为100和200。
2.代码
# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def get_data():
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
return mnist
def weight_variable(shape):
# 产生随机数
init = tf.truncated_normal(shape,stddev = 0.1)
return tf.Variable(init)
def bias_variable(shape):
init = tf.constant(0.1,shape = shape)
return tf.Variable(init)
def network(mnist):
# 第一层隐藏层,100个神经元
X = tf.placeholder(tf.float32,shape = [None,784])
weight1 = weight_variable([784,100])
bias1 = bias_variable([100])
h1 = tf.nn.sigmoid(tf.matmul(X,weight1)+bias1)
# 第二层隐藏层,100个神经元
weight2 = weight_variable([100,200])
bias2 = bias_variable([200])
h2 = tf.nn.sigmoid(tf.matmul(h1,weight2)+bias2)
# 输出层
weight3 = weight_variable([200,10])
bias3 = bias_variable([10])
h3 = tf.nn.sigmoid(tf.matmul(h2,weight3)+bias3)
# 配置dropout层
keep_prob = tf.placeholder("float")
h3_drop = tf.nn.dropout(h3, keep_prob)
# softmax输出层
y_predict = tf.nn.softmax(h3_drop)
# 接受label
y_label = tf.placeholder(dtype=tf.float32,shape = [None,10])
# 损失函数----交叉熵函数
loss = -tf.reduce_sum(y_label * tf.log(y_predict))
# 优化器反向传播
train_step = tf.train.AdamOptimizer(learning_rate=1e-3).minimize(loss)
# 准确率
prediction = tf.equal(tf.argmax(y_predict,1),tf.argmax(y_label,1))
accuracy = tf.reduce_mean(tf.cast(prediction,"float"))
# 初始化参数
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
# 开始训练
for i in range(1000):
train_set = mnist.train.next_batch(50)
if i%100 == 0:
train_acc = accuracy.eval(feed_dict = {X:train_set[0],y_label:train_set[1],keep_prob:1})
print("step %d,training accuracy is %g"%(i,train_acc))
else:
sess.run(train_step,feed_dict={X:train_set[0],y_label:train_set[1],keep_prob:0.5})
# 测试网络性能
point = 0
for i in range(10):
test_set = mnist.test.next_batch(50)
temp_point = sess.run(accuracy,feed_dict={X:test_set[0],y_label:test_set[1],keep_prob : 1})
print(temp_point)
point += temp_point
average_point = point/10
print("average_point:%g"%(average_point))
if __name__ =="__main__":
mnist = get_data()
network(mnist)
3.结果
step 0,training accuracy is 0.16
step 100,training accuracy is 0.44
step 200,training accuracy is 0.72
step 300,training accuracy is 0.86
step 400,training accuracy is 0.92
step 500,training accuracy is 0.92
step 600,training accuracy is 0.86
step 700,training accuracy is 0.86
step 800,training accuracy is 0.92
step 900,training accuracy is 0.96
0.88
0.94
0.92
0.9
0.92
0.9
0.88
0.86
0.98
0.88
average_point:0.906
Process finished with exit code 0
最后
以上就是务实音响为你收集整理的BP神经网络实现mnist分类的全部内容,希望文章能够帮你解决BP神经网络实现mnist分类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复