TensorFlow学习2——使用简单的softmax回归识别手写数字识别mnist数据集

一、内容理解

1.mnist数据集介绍

1)7 万张黑底白字手写数字图片,其中 55000 张为训练集,5000 张为验证集,10000 张为测试集。

2)每张图片大小为 28*28 像素,数据集的标签是长度为 10 的一维数组,数组中每个元素索引号表示对应数字出现的概率。

一般将图片记为xs,将标签记为ys

3)加载方式:

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets(’./data/’,one_hot=True)

在 read_data_sets()函数中有两个参数,分别表示数据集存放路径、数据集的存取形式。

当第二个参数为 Ture 时,表示以独热码形式存取数据集。read_data_sets()函数运行时,会检查指定路径内是否已经有数据

集,若指定路径中没有数据集,则自动下载,并将 mnist数据集分为训练集 train、验证集 validation 和测试集 test 存放。

4)数据集预处理:将图片展开为28*28=784的一维向量,丢失了二维结构。因此

mnist.train.images是[55000,784]矩阵;mnist.train.labels是[55000,10]矩阵,

2.简单模型搭建——softmax regression

1)基本概念:softmax回归,就是将预测结果对应分配为概率

2)权重,代表样本是否具有某样特征,偏置,代表与输入无关的判别证据;待训练的参数使用Variable类型,一般赋0

3)placeholder,占位函数,为数据占位,在回话中一起投入,节省资源

4)评价指标/损失函数,选择交叉熵,使用-tf.reduce_sum(y_*tf.log(y))实现,

5)模型训练方法,实质为利用算法减少损失函数的过程,选择梯度下降算法,使用tf.train.GradientDescentOptimizer(x).minimize(loss)实现,

参数x为学习率,表示每次学习变量的改变程度;参数loss为损失函数,即需要最小化的内容

3.执行模型

1)初始化

2)规定每次投入数据量,使用mnist.train.next_batch(n)规下次投入多少个数据样本,每次随机抓取数据

3)执行训练,使用sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys})执行回话

一参为

4.评价模型

1)比较预测与标签,使用tf.equal(tf.argmax(y,1),tf.argmax(y_,1))实现

2)结果汇总为比例数据,使用tf.reduce_mean(tf.cast(correct_prediction,'float')实现

3)执行预测,使用sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels})实现

5.后(hua)期(she)修(tian)改(zu):

将学习率、训练轮数、数据投入量、输入输出尺寸定义为常量

 

6.使用交互方式进行会话,加载它自身作为默认构建的session,主要区别:

1)开启会话时,使用sess = tf.InteractiveSession()实现

2)在执行训练操作时,使用train_step.run(feed_dict={x:batch_xs,y_:batch_ys})直接实现

   在执行损失函数及预测率输出时,使用cross_entroy.eval(feed_dict={x:mnist.test.images,y_:mnist.test.labels})、

    accuracy.eval(feed_dict={x:mnist.test.images,y_:mnist.test.labels})实现

3)在结束时,使用sess.close()

 

*回话执行函数,在fetches中运行操作和计算张量

sess.run(

    fetches,

    feed_dict=None,

    options=None,

    run_metadata=None

)

fetches:单个图形元素,图形元素列表或其值为图元素或图元素列表的字典。即已经搭建好的需要进行计算的过程

feed_dict:将图表元素映射到值的字典。即把数值指定给占位符

options:一个[ RunOptions]协议缓冲区

run_metadata:一个[ RunMetadata]协议缓冲区

*张量变换,改变张量的数据类型

tf.cast(x, dtype, name=None)

x:输入

dtype:转换目标类型

name:名称

*独热码:直观来说就是有多少个状态就有多少比特,而且只有一个比特为1,其他全为0的一种码制。

 

二、代码

# 简单回归模型实现

import tensorflow.examples.tutorials.mnist.input_data as input_data
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

#常量定义
Learning_rate = 0.01
Steps = 5000
Batch_size = 100
Input_size = 784
Output_size= 10

#数据读取
mnist = input_data.read_data_sets("data/",one_hot=True)

#模型搭建
#数据、标签占位
x = tf.placeholder('float',shape=[None, Input_size])
y_= tf.placeholder('float',shape=[None,Output_size])
#参数定义
w = tf.Variable(tf.zeros([Input_size,Output_size]))
b = tf.Variable(tf.zeros([Output_size]))
#预测模型构建
y = tf.nn.softmax(tf.matmul(x,w) + b)
#损失函数——交叉熵定义
cross_entrogy = -tf.reduce_sum(y_*tf.log(y))
#模型训练方法——梯度下降法定义
train_step = tf.train.GradientDescentOptimizer(Learning_rate).minimize(cross_entrogy)

#执行模型
with tf.Session() as sess:
    #初始化
    init_op = tf.initialize_all_variables()
    sess.run(init_op)
    for i in range(Steps):
        #投入量规定
        batch_xs,batch_ys = mnist.train.next_batch(Batch_size)
        #执行训练
        sess.run(train_step, feed_dict={x:batch_xs,y_:batch_ys})

        #评价模型,每500轮输出一次损失函数和准确率
        if i%500 == 0:
            #对比得到正确率,并处理汇总
            prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
            accuracy = tf.reduce_mean(tf.cast(prediction,'float'))
            #运行并输出
            loss,accuracy_result = sess.run([cross_entrogy,accuracy],feed_dict={x:mnist.test.images,y_:mnist.test.labels})
            print('After %d steps train, loss is %g, and the accuracy is %g' % (i,loss,accuracy_result))
    print('Fianlly, after %d steps train, loss is %g, and the accuracy is %g' % (i+1, loss, accuracy_result))
# 简单回归模型的交互式会话实现

import tensorflow.examples.tutorials.mnist.input_data as input_data
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

#常量定义
Learning_rate = 0.01
Steps = 5000
Batch_size = 100
Input_size = 784
Output_size= 10

#数据读取
mnist = input_data.read_data_sets("data/",one_hot=True)

#模型搭建
#数据、标签占位
x = tf.placeholder('float',shape=[None, Input_size])
y_= tf.placeholder('float',shape=[None,Output_size])
#参数定义
w = tf.Variable(tf.zeros([Input_size,Output_size]))
b = tf.Variable(tf.zeros([Output_size]))
#预测模型构建
y = tf.nn.softmax(tf.matmul(x,w) + b)
#损失函数——交叉熵定义
cross_entrogy = -tf.reduce_sum(y_*tf.log(y))
#模型训练方法——梯度下降法定义
train_step = tf.train.GradientDescentOptimizer(Learning_rate).minimize(cross_entrogy)

#执行模型
sess = tf.InteractiveSession()
#初始化
init_op = tf.initialize_all_variables()
sess.run(init_op)
for i in range(Steps):
    #投入量规定
    batch_xs,batch_ys = mnist.train.next_batch(Batch_size)
    #执行训练
    train_step.run(feed_dict={x:batch_xs,y_:batch_ys})

    #评价模型,每500轮输出一次损失函数和准确率
    if i%500 == 0:
        #对比得到正确率,并处理汇总
        prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
        accuracy = tf.reduce_mean(tf.cast(prediction,'float'))
        #运行并输出
        loss = cross_entrogy.eval(feed_dict={x:mnist.test.images,y_:mnist.test.labels})
        accuracy_result = accuracy.eval(feed_dict={x:mnist.test.images,y_:mnist.test.labels})
        print('After %d steps train, loss is %g, and the accuracy is %g' % (i,loss,accuracy_result))
sess.close()
print('Fianlly, after %d steps train, loss is %g, and the accuracy is %g' % (i+1, loss, accuracy_result))

猜你喜欢

转载自blog.csdn.net/nominior/article/details/80629050