加强版手写图片识别程序

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

#载入数据集
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)

#每批次放进50个
batch_size = 50
#计算一共有多少批次
n_batch = mnist.train.num_examples // batch_size

#定义两个placeholder 属性的个数是固定的  样本数是变化的
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10]) 
keep_prob = tf.placeholder(tf.float32)
L1_Node = 500
L2_Node = 300
L3_Node = 200
lr = tf.Variable(0.01,dtype = tf.float32)
#两个None对应位置是一样的

#创建第一个隐层神经网络
W_1 = tf.Variable(tf.truncated_normal([784,L1_Node],stddev=0.1))
b_1 = tf.Variable(tf.zeros([L1_Node])+0.1)
Wx_plus_b_1 = tf.matmul(x,W_1)+b_1
#虽然与ng讲的是相反的 但是这里维度是相对应乘起来的,所以没啥问题
#行数是样本数  一行里面有784个feature  所以列是784 列    最后维度要对应上
prediction_L1 = tf.nn.tanh(Wx_plus_b_1)
L1_drop = tf.nn.dropout(prediction_L1,keep_prob)

#创建第二隐层
W_2 = tf.Variable(tf.truncated_normal([L1_Node,L2_Node],stddev=0.1))
b_2 = tf.Variable(tf.zeros([L2_Node])+0.1)
Wx_plus_b_2 = tf.matmul(prediction_L1,W_2)+b_2
prediction_L2 = tf.nn.tanh(Wx_plus_b_2)
L2_drop = tf.nn.dropout(prediction_L2,keep_prob)

#创建第三隐层
W_3 = tf.Variable(tf.truncated_normal([L2_Node,L3_Node],stddev=0.1))
b_3 = tf.Variable(tf.zeros([L3_Node])+0.1)
Wx_plus_b_3 = tf.matmul(prediction_L2,W_3)+b_3
prediction_L3 = tf.nn.tanh(Wx_plus_b_3)
L3_drop = tf.nn.dropout(prediction_L3,keep_prob)

#创建输出层
W_4 = tf.Variable(tf.truncated_normal([L3_Node,10])*0.01)
b_4 = tf.Variable(tf.zeros([10])+0.1)
Wx_plus_b_4 = tf.matmul(prediction_L3,W_4)+b_4
prediction = tf.nn.softmax(Wx_plus_b_4)

#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))

#使用交叉熵损失
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y,logits= prediction))
#使用Adam优化器
train_step = tf.train.AdamOptimizer(lr).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#tf.equal(x,y) 看x,y是否一样  一样就true
# argmax的0就是纵向的元素中最大的那个的索引
# 1 就是每行横向的元素中最大的那个元素的索引
# 0轴就是竖向,1就是横向 但是要分清元素还是层级
#arg会返回一个array
#最终返回布尔类型列表

#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#tf.cast 是将布尔类型转化为浮点型

#创建会话
with tf.Session() as sess:
    sess.run(init)
    for epoch in range(51):
        sess.run(tf.assign(lr,0.001*(0.95**epoch)))
        for batch 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 : 1.0})
        
        learning_rate = sess.run(lr)
            
        test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob : 1.0})
        train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob : 1.0})
        #利用训练集上的数据来劲训练,用测试级来进行
        print('Iter'+ str(epoch)+',testing accuracy'+str(test_acc)+',train acc'+str(train_acc)+',learning_rate'+str(learning_rate))

模型经过调参,修改层数,节点,优化器后 准确率得到提升,最终达到0.98以上

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值