美文网首页
TF - the mnist database of handw

TF - the mnist database of handw

作者: leo567 | 来源:发表于2018-10-28 10:30 被阅读0次
    • MNIST数据集的官网:Yann LeCun's websi

    • 下载下来的数据集被分成两部分:60000行的训练数据集(mnist.train)和10000行的测试数据
      集(mnist.test)


      MNIST
    • 每一张图片包含28x28个像素,我们把这一个数组展开成一个向量,长度是28x28=784。因此在MNIST训练数据集中mnist.train.images 是一个形状为 [60000, 784] 的张量,第一个维度数字用来索引图片,第二个维度数字用来索引每张图片中的像素点。图片里的某个像素的强度值介于0-1之间。

    • MNIST数据集的标签是介于0-9的数字,我们要把标签转化为“one-hot vectors”。一个one-hot向量除了某一位数字是1以外,其余维度数字都是0,比如标签0将表示为([1,0,0,0,0,0,0,0,0,0]),标签3将表示为([0,0,0,1,0,0,0,0,0,0]) 。* 因此, mnist.train.labels 是一个 [60000, 10] 的数字矩阵。
    • 神经网络构建
    • Softmax函数

      我们知道MNIST的结果是0-9,我们的模型可能推测出一张图片是数字9的概率是80%,是数字8的概率是10%,然后其他数字的概率更小,总体概率加起来等于1。这是一个使用softmax回归模型的经典案例. softmax模型可以用来给不同的对象分配概率。

    比如输出结果为[1,5,3]


    import tensorflow as tf
    import os
    from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
    
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    
    # 载入数据集(放在当前代码目录)
    mnist = read_data_sets("MNIST_data/", one_hot=True)
    
    # 每个批次的大小(每次训练图片的数量)
    batch_size = 100
    # 计算一共有多少个批次
    n_batch = mnist.train.num_examples // batch_size
    
    # 定义两个placeholder(输入图片和标签)
    x = tf.placeholder(tf.float32, [None, 784])
    y = tf.placeholder(tf.float32, [None, 10])
    
    # 创建神经网络
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    prediction = tf.nn.softmax(tf.matmul(x, W) + b)
    
    # 二次代价函数
    loss = tf.reduce_mean(tf.square(y - prediction))
    # 使用梯度下降法
    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
    
    # 初始化变量
    init = tf.global_variables_initializer()
    
    # 比较真实值和预测值概率最大标签是否相同,结果存放在一个布尔型列表中
    # argmax 返回一维张量中最大的值所在的位置
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.arg_max(prediction, 1))
    # 求准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    
    with tf.Session() as sess:
        sess.run(init)
        for epoch in range(21):
            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})
    
            acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels})
            print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
    
    
    

    结果:


    • 使用交叉熵代价函数:
    ...
    # 二次代价函数
    # loss = tf.reduce_mean(tf.square(y - prediction))
    
    # 换成交叉熵代价函数
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
    
    ...
    
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    
    with tf.Session(config=config) as sess:
       ...
    
    
    image.png

    使用交叉熵代价函数收敛的较快,最终的精度也较高

    相关文章

      网友评论

          本文标题:TF - the mnist database of handw

          本文链接:https://www.haomeiwen.com/subject/kmdptqtx.html