%pylab inline
Populating the interactive namespace from numpy and matplotlib

在Yann LeCun教授的网站中(http://yann.lecun.com/exdb/mnist ) 对MNIST数据集做出了详细的介绍。

在TensorFlow中对MNIST数据集进行了封装。

MNIST数据集是NIST数据集的一个子集,它包含了60000张图片作为训练数据,10000张图片作为测试数据。在MNIST数据集中的每一张图片都代表了 0~9中的一个数字。图片大小均为\(28 * 28\),且数字都会出现在图片的正中间。

TensorFlow提供了一个类来处理MNIST数据:自动下载并转化MNIST数据的格式,将数据从原始的数据包中解析成训练和测试神经网络时使用的格式。

1. 读取数据集,第一次TensorFlow会自动下载数据集到下面的路径中。

\[\begin{pmatrix}
\text{train-images-idx3-ubyte.gz:} & \text{training set images (9912422 bytes)} \\
\text{train-labels-idx1-ubyte.gz:} & \text{training set labels (28881 bytes)} \\
\text{t10k-images-idx3-ubyte.gz:} & \text{test set images (1648877 bytes)} \\
\text{t10k-labels-idx1-ubyte.gz:} & \text{test set labels (4542 bytes)} \\
\end{pmatrix}
\]

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("../../datasets/MNIST_data/", one_hot=True)
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting ../../datasets/MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting ../../datasets/MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting ../../datasets/MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting ../../datasets/MNIST_data/t10k-labels-idx1-ubyte.gz

2. 数据集会自动被分成3个子集,train(训练)validation(验证)test(测试)。以下代码会显示数据集的大小。

print("Training data size: ", mnist.train.num_examples)
print("Validating data size: ", mnist.validation.num_examples)
print("Testing data size: ", mnist.test.num_examples)
Training data size:  55000
Validating data size: 5000
Testing data size: 10000

3. 查看training数据集中某个成员的像素矩阵生成的一维数组和其属于的数字标签。

print("Example training data: ", mnist.train.images[0])
print("Example training data label: ", mnist.train.labels[0])
Example training data:  [ 0.          0.          0.          0.          0.          0.          0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0.01176471 0.46274513 0.99215692 0.45882356 0.01176471
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.46274513 0.98823535 0.98823535 0.98823535
0.16862746 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.65882355 0.99215692 0.98823535 0.98823535
0.98823535 0.65882355 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0.10196079 0.73333335 0.97254908 0.99215692 0.98823535
0.89019614 0.25882354 0.75294125 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0.08627451 0.80392164 0.98823535 0.98823535 0.99215692
0.6156863 0.0627451 0. 0.04313726 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0.01176471 0.66666669 0.99215692 0.99215692 0.99215692
0.41568631 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.66666669 0.98823535 0.98823535 0.98823535
0.41568631 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.45490199 0.99215692 0.98823535 0.91372555
0.5529412 0.02352941 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.29803923 0.97254908 0.99215692 0.98823535
0.5529412 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.1254902 0.85490203 0.98823535 0.99215692
0.6156863 0.02352941 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.01960784 0.74901962 0.99215692 0.99215692
0.90588242 0.16470589 0. 0. 0. 0.
0.05882353 0.09411766 0.46274513 0.33725491 0.34117648 0.2392157 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.47058827 0.98823535
0.98823535 0.90588242 0.16470589 0. 0. 0.06666667
0.18431373 0.43137258 0.8588236 0.98823535 0.98823535 0.98823535
0.99215692 0.92549026 0.17254902 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.06666667 0.89411771 0.98823535 0.89019614 0.18823531 0.
0.17647059 0.39607847 0.81960791 0.98823535 0.99215692 0.98823535
0.98823535 0.98823535 0.98823535 0.99215692 0.98823535 0.67058825
0.0509804 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.09411766 0.98823535 0.98823535
0.4666667 0. 0.03529412 0.76078439 0.98823535 0.98823535
0.98823535 0.99215692 0.98823535 0.98823535 0.82352948 0.98823535
0.99215692 0.98823535 0.98823535 0.54509807 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.09411766 0.98823535 0.98823535 0.17647059 0. 0.91372555
0.98823535 0.98823535 0.98823535 0.98823535 0.29411766 0.16862746
0.53725493 0.8705883 0.98823535 0.99215692 0.98823535 0.98823535
0.627451 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.09411766 0.99215692 0.72156864
0. 0. 1. 0.99215692 0.92549026 0.90196085
0.36862746 0.54509807 0.83137262 0.99215692 0.99215692 0.99215692
1. 0.93725497 0.45098042 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.09411766 0.98823535 0.89019614 0.26274511 0.18431373 0.95294124
0.82745105 0.72941178 0.63137257 0.95686281 0.99215692 0.98823535
0.98823535 0.92156869 0.80784321 0.76862752 0.12941177 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.02745098 0.60392159 0.98823535 0.98823535
0.98823535 0.99215692 0.98823535 0.98823535 0.98823535 0.98823535
0.99215692 0.98823535 0.91372555 0.25098041 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.03529412
0.76862752 0.98823535 0.98823535 0.99215692 0.98823535 0.98823535
0.98823535 0.98823535 0.96078438 0.54509807 0.12941177 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0.06666667 0.45882356 0.94901967 0.74509805 0.53725493
0.41568631 0.45882356 0.1254902 0.08235294 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. ]
Example training data label: [ 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]

因为神经网络的输入是一个特征向量,所以将一张二维图像的像素矩阵放到一个一维数组中可以方便TensorFlow将图片的像素矩阵提供给神经网络的输入层。像素矩阵中的元素的取值 \([0,1]\) ,它代表了颜色的深浅。其中 \(0\) 表示白色背景(backgroud),\(1\) 表示黑色前景(forefround)。

mnist.train.images[0].shape
(784,)
sqrt(784)
28.0
a = mnist.train.images[0]
a.shape = [28,28]
imshow(a)
<matplotlib.image.AxesImage at 0x2115388dba8>

4. 使用mnist.train.next_batch来实现随机梯度下降。

mnist.train.next_batch可以从所有的训练数据中读取一小部分作为一个训练batch。

batch_size = 100
xs, ys = mnist.train.next_batch(batch_size) # 从train的集合中选取batch_size个训练数据。
print("X shape:", xs.shape)
print("Y shape:", ys.shape)
X shape: (100, 784)
Y shape: (100, 10)

1. MNIST读取数据的更多相关文章

  1. 吴裕雄 PYTHON 神经网络——TENSORFLOW MNIST读取数据

    from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("E ...

  2. Tensorflow中使用TFRecords高效读取数据--结合Attention-over-Attention Neural Network for Reading Comprehension

    原文链接:https://arxiv.org/pdf/1607.04423.pdf 本片论文主要讲了Attention Model在完形填空类的阅读理解上的应用. 转载:https://blog.cs ...

  3. TF从文件中读取数据

    从文件中读取数据 在TensorFlow中进行模型训练时,在官网给出的三种读取方式,中最好的文件读取方式就是将利用队列进行文件读取,而且步骤有两步: 把样本数据写入TFRecords二进制文件 从队列 ...

  4. TensorFlow高效读取数据的方法——TFRecord的学习

    关于TensorFlow读取数据,官网给出了三种方法: 供给数据(Feeding):在TensorFlow程序运行的每一步,让python代码来供给数据. 从文件读取数据:在TensorFlow图的起 ...

  5. [深度学习] pytorch利用Datasets和DataLoader读取数据

    本文简单描述如果自定义dataset,代码并未经过测试(只是说明思路),为半伪代码.所有逻辑需按自己需求另外实现: 一.分析DataLoader train_loader = DataLoader( ...

  6. struts2中从后台读取数据到<s:select>

    看到网上好多有struts2中从后台读取数据到<s:select>的,但都 不太详细,可能是我自己理解不了吧!所以我自己做了 一个,其中可能 有很多不好的地方,望广大网友指出 结果如图 p ...

  7. [MVC4]ASP.NET MVC4+EF5(Lambda/Linq)读取数据

    继续上一节初始ASP.NET MVC4,继续深入学习,感受了一下微软的MVC4+EF5(EntityFramework5)框架的强大,能够高效的开发出网站应用开发系统,下面就看一下如何用MVC4+EF ...

  8. ubuntu server 12.04U盘安装,提示无法挂载安装光盘或光盘读取数据出错

    今天用Ultraiso将Ubuntu server 12.04 刻入U盘中安装系统,中间提示错误:1.检测不到cdrom(即U盘没有挂载上):2.从光盘中读取数据出错.问题如下图所示: 上网搜了下解决 ...

  9. MySQL数据库中tinyint类型字段读取数据为true和false

    今天遇到这么一个问题,公司最近在做一个活动,然后数据库需要建表,其中有个字段是关于奖励发放的状态的字段,结果读取出来的值为true 一.解决读取数据为true/false的问题 场景: 字段:stat ...

随机推荐

  1. Java设计模式の责任链模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其 ...

  2. [LeetCode] 27. Remove Element ☆

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  3. 新Linux系统配置yum源

    新的Linux系统安装好以后,yum的源还是需要配置一下的,我使用的是redhat6.6版本,同时为了不注册而使用更多的yum源的资源,也需要做一下的修改. 1. 删除redhat原有的yum源 # ...

  4. 什么是Qt Widget?

    Qt帮助文档如此解释: The user interface contains visual elements that are called widgets in Qt. Examples of w ...

  5. html跑马灯效果

    实现跑马灯的方法很多,其中最简单的是采用一句Html代码来实现,我们在需要出现跑马灯效果的地方插入“<marquee>滚动的文字</marquee>”语句,它的效果如下所示: ...

  6. Java 9 中的 9 个新特性

    Java 8 发布三年多之后,java9已经发布了 . 你可能已经听说过 Java 9 的模块系统,但是这个新版本还有许多其它的更新. 这里有九个令人兴奋的新功能将与 Java 9 一起发布.   1 ...

  7. Kali设置代理

    原文:Kali-linux设置ProxyChains ProxyChains是Linux和其他Unices下的代理工具.它可以使任何程序通过代理上网,允许TCP和DNS通过代理隧道,支持HTTP.SO ...

  8. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

  9. 基于canvas实现的fontawesome动态图标

    由于还没有全部实现,实现了一些demo,demo地址在 https://github.com/jiangzhenfei/canvas-fontawesome 实现了动态loading 实现动态电池充电 ...

  10. 64_t2

    texlive-biblatex-chem-doc-svn42065-33.fc26.2.no..> 24-May-2017 15:44 1044190 texlive-biblatex-che ...