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}))
    
  • 相关阅读:
    POJ 1469 COURSES 二分图最大匹配
    POJ 1325 Machine Schedule 二分图最大匹配
    USACO Humble Numbers DP?
    SGU 194 Reactor Cooling 带容量上下限制的网络流
    POJ 3084 Panic Room 求最小割
    ZOJ 2587 Unique Attack 判断最小割是否唯一
    Poj 1815 Friendship 枚举+求最小割
    POJ 3308 Paratroopers 最小点权覆盖 求最小割
    1227. Rally Championship
    Etaoin Shrdlu
  • 原文地址:https://www.cnblogs.com/yucen/p/9343572.html
Copyright © 2011-2022 走看看