001_21 个项目玩转深度学习_MNIST 学习入门

- 数据集
数据集:MNIST数据集
训练图像:60000张
(其中55000张训练图片和5000张验证图片)
测试图像:10000张

- 数据集下载

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)

运行结果:
在这里插入图片描述

- 数据集信息
MNIST的图片信息显示如下:
在这里插入图片描述
Tensorflow的MNIST数据集中的图片被描述为1个784维的向量,但是其实际上是28*28的二维向量。可通过代码查看其shape信息:

print(mnist.train.images.shape)
print(mnist.train.labels.shape)

print(mnist.validation.images.shape)
print(mnist.validation.labels.shape)

print(mnist.test.images.shape)
print(mnist.test.labels.shape)

运行结果:
在这里插入图片描述
查看第0张图片对应的向量:

print(mnist.train.images[0,:])

运行结果:
在这里插入图片描述

- 读取前20张图片,并保存

import scipy.misc
import os
from PIL import Image

#将原始图片保存在MNIST_data/raw下
save_dir = 'MNIST_data/raw/'
if os.path.exists(save_dir) is False:
    os.makedirs(save_dir)

#保存前20张图片
for i in range(20):
    image_array = mnist.train.images[i,:]
    #Tensorflow中的MNIST图片是一个784的向量,将其还原为28*28的图像
    image_array = image_array.reshape(28,28)
    filename = save_dir+'mnist_train_%d.jpg'%i
    Image.fromarray((image_array*255).astype('uint8'), mode='L').convert('RGB').save(filename)
    #新版本的scipy库没有toimage方法,因此下面的方法不可用
    #scipy.misc.toimage(image_array,cmin=0.0,cmax=1.0).save(filename)

运行结果:
在这里插入图片描述

- 图像标签的独热表示
所谓独热表示,就是“一位高效编码” 。我们用N维的向量来表示N个类别,每个类别占据独立的一位,任何时候独热表示中只再一位是 ,其他都为0。
在这里插入图片描述
获取20张图片的独热表示

import numpy as np
#图片标签的显示
print(mnist.train.labels[0,:])

#打印前20张图片对应的数字
for i in range(20):
    one_hot_label = mnist.train.labels[i,:]
    #通过np.argmax,获取原始的label
    #因为是由9个1和1个0组成
    label = np.argmax(one_hot_label)
    print('mnist_train_%d.jpg label:%d' % (i,label))

在这里插入图片描述

- Softmax回归
逻辑回归是二分类模型,Softmax回归是多分类模型,Softmax函数的主要功能是将各个类别的打分转化为合理的概率值。概率值之和为1。在这里插入图片描述

#利用tensorflow识别MNIST
import tensorflow as tf
#x表示待识别的图片
#None是任意的意思,也就是说可以传递任意张训练图片给这个占位符,每张图片用一个784维向量表示
x = tf.placeholder(tf.float32,[None,784])
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
#y表示模型的输出
y = tf.nn.softmax(tf.matmul(x,w)+b)
#y_是实际的图像标签
y_ = tf.placeholder(tf.float32,[None,10])

#根据y和y_构造交叉熵损失
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y)))
#得到损失值后,用梯度下降法对模型的参数进行优化
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

#创建会话,只有在会话中才能优化
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

#进行1000步的梯度下降
for _ in range(1000):
    #每次提取100个数据进行训练,共训练1000次
    batch_xs,batch_ys = mnist.train.next_batch(100)
    #将变量喂入占位符中
    sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys})
#检测模型结果
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
#tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换
#此处是将布尔型转换为float32
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels}))

运行结果为0.91,即模型的准确率为91%。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值