- 数据集
数据集: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%。