导入Mnist数据集
关键:
mnist = input_data.read_data_sets(‘data/’,one_hot=True)
这里data/是下载到本地的路径,是可以自行选择的,但是只要下载一次下次导入就会快很多,所以尽量不要随意改动该路径。
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
print('哈哈哈哈哈哈哈哈~~~~')
4
print('下载中,别催我了~~')
mnist = input_data.read_data_sets('data/',one_hot=True) #关键~~~~~~~~~~~~~~~~~~~~~
print("类型是 %s"%(type(mnist)))
print("训练数据有 %d"%(mnist.train.num_examples))
print("测试数据有 %d"%(mnist.test.num_examples))
控制台显示:
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
类型是 <class 'tensorflow.contrib.learn.python.learn.datasets.base.Datasets'>
训练数据有 55000
测试数据有 10000
查看Mnist数据集的类型:
trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels
# 28 * 28 * 1 = 784 --
print (" 数据类型 %s" % (type(trainimg)))
print (" 标签类型 %s" % (type(trainlabel)))
print (" 训练集的shape %s" % (trainimg.shape,))
print (" 训练集的标签的shape %s" % (trainlabel.shape,))
print (" 测试集的shape %s" % (testimg.shape,))
print (" 测试集的标签的shape %s" % (testlabel.shape,))
784指的是图像:28 * 28 * 1
10指的是分类:有十个数字,它属于哪一类哪个数字就为1,其余数字就为0,其实这5w5张图片里一共就分为10类
控制台显示:
训练数据有 55000
测试数据有 10000
数据类型 is <class 'numpy.ndarray'>
标签类型 <class 'numpy.ndarray'>
训练集的shape (55000, 784)
训练集的标签的shape (55000, 10)
测试集的shape (10000, 784)
测试集的标签的shape (10000, 10)
随机抽出5张图片,把它的标签显示出来,并且把它的图片展示出来
nsample = 5
randidx = np.random.randint(trainimg.shape[0], size=nsample)
for i in randidx:
curr_img = np.reshape(trainimg[i, :], (28, 28)) # 28 by 28 matrix
curr_label = np.argmax(trainlabel[i, :] ) # Label
plt.matshow(curr_img, cmap=plt.get_cmap('gray'))
print ("" + str(i) + "th 训练数据 "
+ "标签是 " + str(curr_label))
plt.show()
控制台显示:
50316th 训练数据 标签是 2
4837th 训练数据 标签是 1
12177th 训练数据 标签是 7
14756th 训练数据 标签是 8
44626th 训练数据 标签是 6
Batch数据
可以指定batch_size的大小
通过这句话就给可以从mnist里抽出100张图片 返回两个值 一个是数据一个是标签
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('data/',one_hot=True)
print ("Batch Learning? ")
batch_size = 100
batch_xs, batch_ys = mnist.train.next_batch(batch_size) #关键~~~~~~~~~~~~~~~~~~~~~
print ("Batch数据 %s" % (type(batch_xs)))
print ("Batch标签 %s" % (type(batch_ys)))
print ("Batch数据的shape %s" % (batch_xs.shape,))
print ("Batch标签的shape %s" % (batch_ys.shape,))
Batch Learning?
Batch数据 <class 'numpy.ndarray'>
Batch标签 <class 'numpy.ndarray'>
Batch数据的shape (100, 784)
Batch标签的shape (100, 10)