(Tensorflow学习) MNIST手写体识别

首先下载MNIST数据集  http://yann.lecun.com/exdb/mnist/ 4个东西

训练部分,搭了一个简单的全连接网络,一层隐藏层,输入节点784 隐藏节点500 输出节点10 激活函数relu 损失函数是交叉熵

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

INPUT_NODE=784
OUTPUT_NODE=10
LAYER1_NODE=500
BATCH_SIZE=100
TRAINING_STEPS=30000

def inferenece(input_tensor,avg_class,weights1,biases1,weights2,biases2):
    layer1=tf.nn.relu(tf.matmul(input_tensor,weights1)+biases1)
    return tf.matmul(layer1,weights2)+biases2

def train(mnist):
    x=tf.placeholder(tf.float32,[None,INPUT_NODE],name='x-input')
    y_=tf.placeholder(tf.float32,[None,OUTPUT_NODE],name='y-input')
    weight1=tf.Variable(tf.truncated_normal([INPUT_NODE,LAYER1_NODE],stddev=0.1))
    biases1=tf.Variable(tf.constant(0.1,shape=[LAYER1_NODE]))
    weight2=tf.Variable(tf.truncated_normal([LAYER1_NODE,OUTPUT_NODE],stddev=0.1))
    biases2=tf.Variable(tf.constant(0.1,shape=[OUTPUT_NODE]))
    y=inferenece(x,None,weight1,biases1,weight2,biases2)
    cross_entropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y_,1))
    cross_entropy_mean=tf.reduce_mean(cross_entropy)
    train_step=tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy_mean)
    correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
    accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    saver=tf.train.Saver()
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        validate_feed={x:mnist.validation.images,y_:mnist.validation.labels}
        test_feed={x:mnist.test.images,y_:mnist.test.labels}
        for i in range(TRAINING_STEPS):
            if i%1000==0:
                validate_acc=sess.run(accuracy,feed_dict=validate_feed)
                print("After %d training step(s),validation accuracy is %g" %(i,validate_acc))
            xs,ys=mnist.train.next_batch(BATCH_SIZE)
            sess.run(train_step,feed_dict={x:xs,y_:ys})
        test_acc=sess.run(accuracy,feed_dict=test_feed)
        print("After %d training step(s),test accuracy is %g" %(TRAINING_STEPS,test_acc))
        saver.save(sess,"D:/mnist/model.ckpt")

mnist=input_data.read_data_sets("D:/mnist",one_hot=True)
train(mnist)


训练30000次,差不多准确率在98%以上,ckpt文件保存在D盘mnist下

然后测试下做的模型行不行,自己用手写一个数字

 

测试程序

import tensorflow as tf
from PIL import  Image
import numpy as np

INPUT_NODE=784
OUTPUT_NODE=10
LAYER1_NODE=500

def inferenece(input_tensor,avg_class,weights1,biases1,weights2,biases2):
    layer1=tf.nn.relu(tf.matmul(input_tensor,weights1)+biases1)
    return tf.matmul(layer1,weights2)+biases2

def evaluate(mnist):
    x=tf.placeholder(tf.float32,[None,INPUT_NODE],name='x-input')
    weight1=tf.Variable(tf.truncated_normal([INPUT_NODE,LAYER1_NODE],stddev=0.1))
    biases1=tf.Variable(tf.constant(0.1,shape=[LAYER1_NODE]))
    weight2=tf.Variable(tf.truncated_normal([LAYER1_NODE,OUTPUT_NODE],stddev=0.1))
    biases2=tf.Variable(tf.constant(0.1,shape=[OUTPUT_NODE]))
    y=inferenece(x,None,weight1,biases1,weight2,biases2)

    saver=tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess,"D:/mnist/model.ckpt")
        test_feed = {x: [mnist]}
        result=sess.run(tf.argmax(y,1),feed_dict=test_feed)
        print(result)

im=Image.open("D:/7.bmp")
im=im.resize((28,28),Image.ANTIALIAS)
nnn=np.array(im).flatten()
n3=1-nnn/256

evaluate(n3)

OK!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值