手写数字识别Mnist

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

#print("packs loaded")

 首先获取mnist数据集。

print("Download and Extract MNIST dataset")
mnist=input_data.read_data_sets('data/',one_hot=True)#

#print("type of 'mnist' is %s" % (type(mnist)))
#print("number of train data is %d"%(mnist.train.num_examples))
#print("number of test data is %d"%(mnist.test.num_examples))

 搭建网络,两个隐藏层。初始化参数。

n_hidden_1=256
n_hidden_2=128
n_input=784
n_classes=10

x=tf.placeholder("float",[None,n_input])#placeholder占位符
y=tf.placeholder("float",[None,n_classes])

stddev=0.1
weights={
    'w1':tf.Variable(tf.random_normal([n_input,n_hidden_1],stddev=stddev)),
    'w2':tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2],stddev=stddev)),
    'out':tf.Variable(tf.random_normal([n_hidden_2,n_classes],stddev=stddev)),
}#初始化
biases={
    'b1':tf.Variable(tf.random_normal([n_hidden_1])),
    'b2':tf.Variable(tf.random_normal([n_hidden_2])),
    'out':tf.Variable(tf.random_normal([n_classes])),
}
print("NETWORK READY")

 定义简单的网络计算。

def multilayer_perceptron(_X,_weights,_biases):
    layer_1=tf.nn.sigmoid(tf.add(tf.matmul(_X,_weights['w1']),_biases['b1']))
    layer_2=tf.nn.sigmoid(tf.add(tf.matmul(layer_1,_weights['w2']),_biases['b2']))
    return (tf.matmul(layer_2,_weights['out'])+_biases['out'])

定义预测结果及损失函数,采用梯度下降法进行优化。 

pred=multilayer_perceptron(x,weights,biases)#预测结果

cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))#损失函数
optm=tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost)#梯度下降优化
corr=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))#精度
accr=tf.reduce_mean(tf.cast(corr,"float"))#平均精度

init=tf.global_variables_initializer()
print("FUNCTION READY")

设置参数,进行训练,打印结果。 

training_epochs=20
batch_size=100
display_step=4

sess=tf.Session()
sess.run(init)

for epoch in range(training_epochs):
    avg_cost=0.
    total_batch=int(mnist.train.num_examples/batch_size)
    
    for i in range(total_batch):
        batch_xs,batch_ys=mnist.train.next_batch(batch_size)
        feeds={x:batch_xs,y:batch_ys}
        sess.run(optm,feed_dict=feeds)
        avg_cost+=sess.run(cost,feed_dict=feeds)
    avg_cost=avg_cost/total_batch
    
    if(epoch+1)%display_step==0:
        print("Epoch:%03d/%03d cost:%9f"%(epoch,training_epochs,avg_cost))
        feeds={x:batch_xs,y:batch_ys}
        train_acc=sess.run(accr,feed_dict=feeds)
        print("TRAIN ACCURACY:%.3f"%(train_acc))
        feeds={x:mnist.test.images,y:mnist.test.labels}
        test_acc=sess.run(accr,feed_dict=feeds)
        print("TEST ACCURACY :%.3f"%(test_acc))
print("OPTIMIZATION FINISHED")
        
        

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值