TesorFlow Mnist数据集相关

导入相关包:

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

print("大吉大利,今晚吃鸡")

当出现如下结果代表包已导入:

大吉大利,今晚吃鸡

下载Mnist数据集,第一次下载速度较慢,如果用的是公司内网有可能下载不了,如果第一次下载失败,多尝试几次:

print("下载中~别催了")
# 若在Windows环境下运行,下载的数据会保存在程序所在目录的data文件夹下,即./data
mnist = input_data.read_data_sets('data/', one_hot=True)
print
print("类型是 %s" % (type(mnist)))
print("训练数据有 %d" % (mnist.train.num_examples))
print("测试数据有 %d" % (mnist.test.num_examples))

在多类场景下,one_hot=true表示,只有一个元素的值是1,其他元素的值是0, 一个长度为n的数组,只有一个元素是1.0,其他元素是0.0。

one_hot=False则没有这样的限制。

例如在n为10的情况下,标签2对应的one_hot标签就是 [0.0,  0.0,  1.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0,  0.0]

使用onehot的直接原因是现在多分类cnn网络的输出通常是softmax层,输出是一个概率分布,从而要求输入的标签也以概率分布的形式出现,进而算交叉熵。

执行结果如下:

下载中~别催了
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数据集一共有65000条数据,其中训练数据55000条,测试数据1000条。

查看Mnist数据规格:

trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels
print
print("数据类型 is %s" % (type(trainimg)))
print("标签类型 %s" % (type(trainlabel)))
print("训练集的shape %s" % (trainimg.shape,))
print("训练集的标签shape %s" % (trainlabel.shape,))
print("测试集的shape is %s" % (testimg.shape,))
print("测试集的标签的shape %s" % (testlabel.shape,))

执行结果:

数据类型 is <class 'numpy.ndarray'>
标签类型 <class 'numpy.ndarray'>
训练集的shape (55000, 784)
训练集的标签shape (55000, 10)
测试集的shape is (10000, 784)
测试集的标签的shape (10000, 10)

上述打印结果表明,训练集的图片大小为28*28*1(黑白图)=784像素点,并且以numpy数组的形式保存。训练集标签长度为10,当训练集标签值为5,则第五个“0”的位置置为“1”,如下表:

Mnist数据集标签表示方法(one_hot=True)
标签值表示方式
01000000000
10100000000
20010000000
30001000000
40000100000
50000010000
60000001000
70000000100
80000000010
90000000001

Mnist数据集可视化:

# 看看庐山真面目
nsample = 5
# trainimg.shape[0]返回55000。
randidx = np.random.randint(trainimg.shape[0], size=nsample)
print(randidx)

for i in randidx:
    # trainimg[i, :]取第i维中所有数据(即第i张图片的一维数组),之后把一维数据转成28*28矩阵
    curr_img = np.reshape(trainimg[i, :], (28, 28))
    # 标签。np.argmax(a):取出a中元素最大值所对应的索引(索引值默认从0开始)
    curr_label = np.argmax(trainlabel[i, :])
    plt.matshow(curr_img, cmap=plt.get_cmap('gray'))
    print("" + str(i) + "th 训练数据 " + "标签是 " + str(curr_label))
    plt.show()

numpy.random.randint(low, high=None, size=None, dtype='l'),函数的作用是,返回一个随机整型数,范围从低(包括)到高(不包括),即[low, high)。如果没有写参数high的值,则返回[0,low)的值。

参数如下:

  • low: int

生成的数值最低要大于等于low。(hign = None时,生成的数值要在[0, low)区间内)

  • high: int (可选)

如果使用这个值,则生成的数值在[low, high)区间。

  • size: int or tuple of ints(可选)

输出随机数的尺寸,比如size = (m * n* k)则输出同规模即m * n* k个随机数。默认是None的,仅仅返回满足要求的单一随机数。

  • dtype: dtype(可选):

想要输出的格式。如int64、int等等

运行的结果如下:

读批量数据:

# Batch数据
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)

参考:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值