zoukankan      html  css  js  c++  java
  • 【2-3】入门Demo : MNIST数据集分类

    MNIST是深度学习的经典入门demo,它是由6万张训练图片(mnist.train)和1万张测试图片(mnist.test)构成的,每张图片都是28*28大小。MNIST训练数据集mnist.train.images 是一个形状为 [60000,784] 的张量,第一个维度数字用来索引图片,第二个维度数字用来索引每张图片中的像素点。图片里的某个像素的强度值介于0-1 之间。(黑色越深表示数值越靠近1)

    MNIST数据集的标签是介于0-9的数字,我们要把标签转化为“one-hot vectors”。一个onehot向量除了某一位数字是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] 的数字矩阵。

    4张MNIST图片

     1 import tensorflow as tf
     2 from tensorflow.examples.tutorials.mnist import input_data
     3 
     4 #载入数据集
     5 mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
     6 
     7 #每个批次的大小
     8 batch_size = 100
     9 #计算一共有多少个批次
    10 n_batch = mnist.train.num_examples // batch_size
    11 
    12 #定义两个placeholder
    13 x = tf.placeholder(tf.float32,[None,784])
    14 y = tf.placeholder(tf.float32,[None,10])
    15 
    16 #创建一个简单的神经网络
    17 W = tf.Variable(tf.zeros([784,10]))
    18 b = tf.Variable(tf.zeros([10]))
    19 prediction = tf.nn.softmax(tf.matmul(x,W)+b)
    20 
    21 #二次代价函数
    22 #loss= tf.reduce_mean(tf.square(y-prediction))
    23 #交叉熵
    24 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
    25 
    26 #使用梯度下降法
    27 train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
    28 
    29 #初始化变量
    30 init = tf.global_variables_initializer()
    31 
    32 #结果存放在一个布尔型列表中
    33 correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大值所在的位置
    34 #求准确率
    35 accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    36 
    37 with tf.Session() as sess:
    38     sess.run(init)
    39     for epoch in range(21):
    40         for batch in range(n_batch):
    41             batch_xs,batch_ys = mnist.train.next_batch(batch_size)
    42             sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
    43         
    44         acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
    45         print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
     1 #二次代价函数
     2 Iter 0,Testing Accuracy 0.8309
     3 Iter 1,Testing Accuracy 0.8701
     4 Iter 2,Testing Accuracy 0.8818
     5 Iter 3,Testing Accuracy 0.89
     6 Iter 4,Testing Accuracy 0.8933
     7 Iter 5,Testing Accuracy 0.8977
     8 Iter 6,Testing Accuracy 0.8995
     9 Iter 7,Testing Accuracy 0.9018
    10 Iter 8,Testing Accuracy 0.9041
    11 Iter 9,Testing Accuracy 0.9054
    12 Iter 10,Testing Accuracy 0.9053
    13 Iter 11,Testing Accuracy 0.9075
    14 Iter 12,Testing Accuracy 0.9083
    15 Iter 13,Testing Accuracy 0.9091
    16 Iter 14,Testing Accuracy 0.9101
    17 Iter 15,Testing Accuracy 0.9112
    18 Iter 16,Testing Accuracy 0.9124
    19 Iter 17,Testing Accuracy 0.9123
    20 Iter 18,Testing Accuracy 0.914
    21 Iter 19,Testing Accuracy 0.9139
    22 Iter 20,Testing Accuracy 0.9144
    23 
    24 #交叉熵
    25 Iter 0,Testing Accuracy 0.8241
    26 Iter 1,Testing Accuracy 0.8894
    27 Iter 2,Testing Accuracy 0.901
    28 Iter 3,Testing Accuracy 0.9052
    29 Iter 4,Testing Accuracy 0.9089
    30 Iter 5,Testing Accuracy 0.9097
    31 Iter 6,Testing Accuracy 0.9111
    32 Iter 7,Testing Accuracy 0.9127
    33 Iter 8,Testing Accuracy 0.9146
    34 Iter 9,Testing Accuracy 0.9152
    35 Iter 10,Testing Accuracy 0.9176
    36 Iter 11,Testing Accuracy 0.9188
    37 Iter 12,Testing Accuracy 0.919
    38 Iter 13,Testing Accuracy 0.9189
    39 Iter 14,Testing Accuracy 0.9189
    40 Iter 15,Testing Accuracy 0.92
    41 Iter 16,Testing Accuracy 0.9207
    42 Iter 17,Testing Accuracy 0.9197
    43 Iter 18,Testing Accuracy 0.9212
    44 Iter 19,Testing Accuracy 0.9211
    45 Iter 20,Testing Accuracy 0.9216

    x(图片的特征值):这里使用了一个28*28=784列的数据来表示一个图片的构成,也就是说,每一个点都是这个图片的一个特征,这个其实比较好理解,因为每一个点都会对图片的样子和表达的含义有影响,只是影响的大小不同而已。将表示每一张图片的28*28的矩阵摊平成为一个1行784列的一维数组。

    y(真实结果):来自MNIST的训练集,每一个图片所对应的真实值,如果是6,则表示为:[0 0 0 0 0 0 1 0 0 0]。

    prediction(预测的结果):单个样本被预测出来是哪个数字的概率。分别对应于数字为0,1,2,3,4,5,6,7,8,9的概率,然后会取一个概率最大值所对应的数字来作为本次预测的结果。

    W(特征值对应的权重):这个值很重要,因为我们深度学习的过程,就是发现特征,经过一系列训练,从而得出每一个特征对结果影响的权重,我们训练,就是为了得到这个最佳权重值。


     1.one_hot=True:数组中的元素不是0就是1。

    比如一个图片上写的是0,存为一个数组:[1,0,0,0,0,0,0,0,0,0]。一个图片上面写的是1,保存的就是[0,1,0,0,0,0,0,0,0,0],以此类推。


    2.softmax:softmax输入是一个向量,输出是一个归一化后的向量。用于计算向量中各个值在整个向量中的权重(将数值转换为相对概率 )。

    已知:向量 a=[a1,a2,a3]

    1 import tensorflow as tf
    2 a = tf.constant([-2,3,-1,0],tf.float32)
    3 result = tf.nn.softmax(a)
    4 sess = tf.Session()
    5 print(sess.run(result))
    6 sess.close()
    7 >>>
    8 [ 0.00626879  0.93037045  0.01704033  0.04632042]

    3.二次代价函数与交叉熵代价函数讲解:

    https://blog.csdn.net/yuanjunliang/article/details/79394805

    在Tensorflow中:

    tf.nn.sigmoid_cross_entropy_with_logits()来表示跟sigmoid搭配使用的交叉熵。

    tf.nn.softmax_cross_entropy_with_logits()来表示跟softmax搭配使用的交叉熵。

    关于tf.nn.softmax_cross_entropy_with_logits(labels=None, logits=None)中的参数labels与logits:

    labels:为神经网络期望的输出;

    logits:为神经网络最后一层的输出;应该为没有经过softmax函数处理的数据;因为这个函数内部自动计算softmax,然后再计算交叉熵代价函数;

    于是将prediction更改:

    1 #创建一个简单的神经网络
    2 W = tf.Variable(tf.zeros([784,10]))
    3 b = tf.Variable(tf.zeros([10]))
    4 prediction = tf.matmul(x,W)+b
    5 
    6 #交叉熵
    7 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))

    运行结果:

     1 Iter 0,Testing Accuracy 0.9083
     2 Iter 1,Testing Accuracy 0.9164
     3 Iter 2,Testing Accuracy 0.9195
     4 Iter 3,Testing Accuracy 0.9202
     5 Iter 4,Testing Accuracy 0.9232
     6 Iter 5,Testing Accuracy 0.9212
     7 Iter 6,Testing Accuracy 0.9238
     8 Iter 7,Testing Accuracy 0.9215
     9 Iter 8,Testing Accuracy 0.9228
    10 Iter 9,Testing Accuracy 0.9212
    11 Iter 10,Testing Accuracy 0.923
    12 Iter 11,Testing Accuracy 0.9241
    13 Iter 12,Testing Accuracy 0.9233
    14 Iter 13,Testing Accuracy 0.924
    15 Iter 14,Testing Accuracy 0.9233
    16 Iter 15,Testing Accuracy 0.9235
    17 Iter 16,Testing Accuracy 0.9233
    18 Iter 17,Testing Accuracy 0.9233
    19 Iter 18,Testing Accuracy 0.9241
    20 Iter 19,Testing Accuracy 0.9256
    21 Iter 20,Testing Accuracy 0.9238

     4.correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))

    找到y中最大值所在的位置,即索引值;找出prediction中最大值所在的位置,即索引值,二者进行比较,相同(预测正确)则为True,不同则为False,是一个布尔型数组。


     5.tf.cast(correct_prediction, tf.float32):是把bool型数组转化为float型,True转化为1.0, False转化为0.0;

    reduce_mean时求float型数组的平均值,即正确的个数与所有个数之比,这个数越大越好,等于1说明100%分类正确。


     6.batch_xs,batch_ys = mnist.train.next_batch(batch_size)

    mnist.train.next_batch(batch_size)是每次从训练集中取出100张图片,这就是所谓的随机梯度下降算。

    batch_xs是一个batch_size*784的矩阵,是训练的数据;

    batch_ys是一个batch_size*10的矩阵,是训练数据的标签。


     7.mnist.test.images和mnist.test.labels是测试集,用来测试。accuracy是预测准确率。

    参考:

    https://www.cnblogs.com/lizheng114/p/7439556.html

    https://www.cnblogs.com/zhouyang209117/p/6517684.html

    2019-05-31 10:07:37

     

  • 相关阅读:
    【原创】构建高性能ASP.NET站点之一 剖析页面的处理过程(前端)
    .NET 并行(多核)编程系列之七 共享数据问题和解决概述
    架构设计解惑
    项目开发经验谈之:设计失败的挫败感
    项目开发经验谈之:忆第一次设计Framework
    盲目的项目开发
    扩展GridView之添加单选列
    日期转换格式
    动手完善个性化弹出提示框的过程及乐趣
    SQL开发中容易忽视的一些小地方(六)
  • 原文地址:https://www.cnblogs.com/direwolf22/p/10950183.html
Copyright © 2011-2022 走看看