zoukankan      html  css  js  c++  java
  • MNIST手写识别(一)

    通过TensorFlow实现Softmax Regression,识别MNIST数据集,最终正确率92%左右。
    ## 通过TensorFlow实现Softmax Regression,实现手写识别
    
    # 加载mnist数据
    from tensorflow.examples.tutorials.mnist import input_data
    mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
    
    # 查看mnist数据集的情况(训练集55000个样本,测试集10000个,验证集5000个)
    print(mnist.train.images.shape,mnist.train.labels.shape)
    print(mnist.test.images.shape,mnist.test.labels.shape)
    print(mnist.validation.images.shape,mnist.validation.labels.shape)
    
    # 载入tensorflow库,并创建一个新的InteractiveSession
    import tensorflow as tf
    sess = tf.InteractiveSession()
    x = tf.placeholder(tf.float32,[None,784]) #None代表不限条数的输入
    
    # 给softmax Regression模型中的weights和biases创建Variable对象
    W = tf.Variable(tf.zeros([784,10]))
    b = tf.Variable(tf.zeros([10]))
    
    # 实现softmax Regression算法,即公式:y = softmax(Wx + b)
    y = tf.nn.softmax(tf.matmul(x,W)+b)
    
    # 定义损失函数cross_entropy
    y_ = tf.placeholder(tf.float32,[None,10])
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices=[1]))
    
    # 定义优化算法,采用随机梯度下降SGD
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) #学习率0.5,优化目标设定为cross_entropy
    
    
    # 使用TensorFlow的全局参数初始化器,并执行它的run方法
    tf.global_variables_initializer().run()
    # 开始训练
    for i in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        train_step.run({x: batch_xs, y_: batch_ys})
    
    
    # 对准确率进行验证
    correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
    #将correct_prediction输出的bool值转化为float32
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))
    
  • 相关阅读:
    简明python_Day2_字典、集合、模块、类、编程习惯
    测试2T2
    测试2T1
    bzoj2761
    一元三次方程求根公式及韦达定理
    状压DP入门——铺砖块
    高精度模板
    测试1T3
    测试1T2
    测试1T1
  • 原文地址:https://www.cnblogs.com/yucen/p/9343572.html
Copyright © 2011-2022 走看看