mnist是什么?
它是在机器学习和计算机视觉领域相当于hello world的一个最基础的数据集,内容是手写的数字(从0到9)。我们想通过这个数据集来让计算机进行图像识别和手写识别。
from matplotlib import pyplot as plt
from PIL import Image
%matplotlib inline
下载和读取数据集
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
mnist里有已经划分好的训练集,测试集和验证集。
validation set用于调超参。超参确定后,用test set来测试模型的表现。如果提前用了test set,可能会导致过拟合。
print(mnist.train.images.shape)
print(mnist.train.labels.shape)
(55000, 784)
(55000, 10)
print(mnist.test.images.shape)
print(mnist.test.labels.shape)
(10000, 784)
(10000, 10)
print(mnist.validation.images.shape)
print(mnist.validation.labels.shape)
(5000, 784)
(5000, 10)
784表示每一张图有28x28=784个特征(像素点)。
10表示标签是one hot encoding的形式,也就是说每一张图都有可能是0,1,2,3,4,5,6,7,8,9这十个数字当中的一个,所以就设定十个位置,如果这个数字是9,9这个位置上就标成1,其他9个位置均标成0。
任选一张图片:
mnist.train.images[5]
mnist.train.images[5].shape
(784,)
这个784x1的向量,是一个28x28的矩阵经过一维化(flatten)得到的。一张28x28像素的图片原本是28x28的矩阵。
以下是逆操作:
a = np.reshape(mnist.train.images[5