一、mnist的属性和方法

为了方便我只检查了后20个属性和方法

 from tensorflow.examples.tutorials.mnist import input_data

 mnist = input_data.read_data_sets('G:\MNIST DATABASE\MNIST_data',one_hot=True)
print(dir(mnist)[-20:])

1:从tensorflow.examples.tutorials.mnist库中导入input_data文件

3:调用input_data文件的read_data_sets方法,需要2个参数,第1个参数的数据类型是字符串,是读取数据的文件夹名,第2个关键字参数ont_hot数据类型为布尔bool,设置为True,表示预测目标值是否经过One-Hot编码;

4:打印mnist后20个属性和方法

结果:

Extracting G:\MNIST DATABASE\MNIST_data\train-labels-idx1-ubyte.gz
WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting G:\MNIST DATABASE\MNIST_data\t10k-images-idx3-ubyte.gz
WARNING:tensorflow:From C:\Program Files\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
Extracting G:\MNIST DATABASE\MNIST_data\t10k-labels-idx1-ubyte.gz
['__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_asdict', '_fields', '_make', '_replace', '_source', 'count', 'index', 'test', 'train', 'validation']

二、查看mnist里的训练集、验证集、测试集包括多少图片

train集合有55000张图片,validation集合有5000张图片,这两个集合组成MNIST本身提供的训练数据集

 print('训练数据数量',mnist.train.num_examples)
print('验证数据数量',mnist.validation.num_examples)
print('测试数据数量',mnist.test.num_examples) #结果:
训练数据数量 55000
验证数据数量 5000
测试数据数量 10000

三、mnist.train.next_batch()函数

input_data.read_data_sets函数生成的类提供的mnist.train.next_batch()函数,它可以从所有的训练数据中读取一小部分作为一个训练batch

 batch_size = 100
#从train集合中选取100个训练数据,100个训练数据的标签
xs,ys = mnist.train.next_batch(batch_size)
print('xs shape',xs.shape)
print('ys shape',ys.shape) #结果:
xs shape (100, 784)
ys shape (100, 10)

四、mnist.train.images观察

mnist.train.images的数据类型是数组,每一个数据是一位数组,每个数据一维数组的长度是784,即每张图片的像素数
 print('train集合数据的类型:',type(mnist.train.images),'train集合数据矩阵形状:',mnist.train.images.shape)
print('train集合数据标签的类型:',type(mnist.train.labels),'train集合数据标签矩阵形状:',mnist.train.labels.shape) #结果:
train集合数据的类型: <class 'numpy.ndarray'> train集合数据矩阵形状: (55000, 784)
train集合数据标签的类型: <class 'numpy.ndarray'> train集合数据标签矩阵形状: (55000, 10) print('train集合第一个数据长度、内容:',len(mnist.train.images[0]),mnist.train.images[0])
print('train集合第一个数据标签长度、内容:',len(mnist.train.labels[0]),mnist.train.labels[0]) 结果:
train集合第一个数据长度、内容: 784 [ 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. 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.38039219 0.37647063
0.3019608 0.46274513 0.2392157 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.35294119 0.5411765
0.92156869 0.92156869 0.92156869 0.92156869 0.92156869 0.92156869
0.98431379 0.98431379 0.97254908 0.99607849 0.96078438 0.92156869
0.74509805 0.08235294 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.54901963 0.98431379 0.99607849 0.99607849 0.99607849 0.99607849
0.99607849 0.99607849 0.99607849 0.99607849 0.99607849 0.99607849
0.99607849 0.99607849 0.99607849 0.99607849 0.74117649 0.09019608
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.88627458 0.99607849 0.81568635
0.78039223 0.78039223 0.78039223 0.78039223 0.54509807 0.2392157
0.2392157 0.2392157 0.2392157 0.2392157 0.50196081 0.8705883
0.99607849 0.99607849 0.74117649 0.08235294 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.14901961 0.32156864 0.0509804 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0.13333334 0.83529419 0.99607849 0.99607849 0.45098042 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.32941177 0.99607849 0.99607849 0.91764712 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.32941177 0.99607849 0.99607849 0.91764712 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0.41568631 0.6156863 0.99607849 0.99607849 0.95294124 0.20000002
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.09803922 0.45882356 0.89411771
0.89411771 0.89411771 0.99215692 0.99607849 0.99607849 0.99607849
0.99607849 0.94117653 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.26666668 0.4666667 0.86274517
0.99607849 0.99607849 0.99607849 0.99607849 0.99607849 0.99607849
0.99607849 0.99607849 0.99607849 0.55686277 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.14509805 0.73333335 0.99215692
0.99607849 0.99607849 0.99607849 0.87450987 0.80784321 0.80784321
0.29411766 0.26666668 0.84313732 0.99607849 0.99607849 0.45882356
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.44313729
0.8588236 0.99607849 0.94901967 0.89019614 0.45098042 0.34901962
0.12156864 0. 0. 0. 0. 0.7843138
0.99607849 0.9450981 0.16078432 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0.66274512 0.99607849 0.6901961 0.24313727 0. 0.
0. 0. 0. 0. 0. 0.18823531
0.90588242 0.99607849 0.91764712 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.07058824 0.48627454 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.32941177 0.99607849 0.99607849 0.65098041 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.54509807 0.99607849 0.9333334 0.22352943 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.82352948 0.98039222 0.99607849 0.65882355 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.94901967 0.99607849 0.93725497 0.22352943 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.34901962 0.98431379 0.9450981 0.33725491 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.01960784 0.80784321 0.96470594 0.6156863 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.01568628 0.45882356 0.27058825 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. ]
train集合第一个数据标签长度、内容: 10 [ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]

从上面的运行结果可以看出,在变量mnist.train中总共有55000个样本,每个样本有784个特征。
原图片形状为28*28,28*28=784,每个图片样本展平后则有784维特征。
选取1个样本,用3种作图方式查看其图片内容,代码如下:

 #将数组张换成图片形式
image = mnist.train.images[1].reshape(-1,28)
fig = plt.figure("图片展示")
ax0 =fig.add_subplot(131)
ax0.imshow(image)
ax0.axis('off') #不显示坐标尺寸 plt.subplot(132)
plt.imshow(image,cmap='gray')
plt.axis('off')#不显示坐标尺寸 plt.subplot(133)
plt.imshow(image,cmap='gray_r')
plt.axis('off')
plt.show()

结果:

从上面的运行结果可以看出,调用plt.show方法时,参数cmap指定值为graygray_r符合正常的观看效果。

五、查看手写数字图

从训练集mnist.train中选取一部分样本查看图片内容,即调用mnist.train的next_batch方法随机获得一部分样本,代码如下

 from tensorflow.examples.tutorials.mnist import input_data
import math
import matplotlib.pyplot as plt
import numpy as np
mnist = input_data.read_data_sets('G:\MNIST DATABASE\MNIST_data',one_hot=True)
#画单张mnist数据集的数据
def drawdigit(position,image,title):
plt.subplot(*position)
plt.imshow(image,cmap='gray_r')
plt.axis('off')
plt.title(title) #取一个batch的数据,然后在一张画布上画batch_size个子图
def batchDraw(batch_size):
images,labels = mnist.train.next_batch(batch_size)
row_num = math.ceil(batch_size ** 0.5)
column_num = row_num
plt.figure(figsize=(row_num,column_num))
for i in range(row_num):
for j in range(column_num):
index = i * column_num + j
if index < batch_size:
position = (row_num,column_num,index+1)
image = images[index].reshape(-1,28)
title = 'actual:%d'%(np.argmax(labels[index]))
drawdigit(position,image,title) if __name__ == '__main__':
batchDraw(196)
plt.show()

结果:

mnist数据集探究的更多相关文章

  1. 从零到一:caffe-windows(CPU)配置与利用mnist数据集训练第一个caffemodel

    一.前言 本文会详细地阐述caffe-windows的配置教程.由于博主自己也只是个在校学生,目前也写不了太深入的东西,所以准备从最基础的开始一步步来.个人的计划是分成配置和运行官方教程,利用自己的数 ...

  2. mnist的格式说明,以及在python3.x和python 2.x读取mnist数据集的不同

    有一个关于mnist的一个事例可以参考,我觉得写的很好:http://www.cnblogs.com/x1957/archive/2012/06/02/2531503.html #!/usr/bin/ ...

  3. Caffe初试(二)windows下的cafee训练和测试mnist数据集

    一.mnist数据集 mnist是一个手写数字数据库,由Google实验室的Corinna Cortes和纽约大学柯朗研究院的Yann LeCun等人建立,它有60000个训练样本集和10000个测试 ...

  4. 【Mxnet】----1、使用mxnet训练mnist数据集

    使用自己准备的mnist数据集,将0-9的bmp图像分别放到0-9文件夹下,然后用mxnet训练. 1.制作rec数据集 (1).制作list

  5. 使用libsvm对MNIST数据集进行实验

    使用libsvm对MNIST数据集进行实验 在学SVM中的实验环节,老师介绍了libsvm的使用.当时看完之后感觉简单的说不出话来. 1. libsvm介绍 虽然原理要求很高的数学知识等,但是libs ...

  6. mnist数据集转换bmp图片

    Mat格式mnist数据集下载地址:http://www.cs.nyu.edu/~roweis/data.html Matlab转换代码: load('mnist_all.mat'); type = ...

  7. caffe在windows编译project及执行mnist数据集測试

    caffe在windows上的配置和编译能够參考例如以下的博客: http://blog.csdn.net/joshua_1988/article/details/45036993 http://bl ...

  8. 使用caffe训练mnist数据集 - caffe教程实战(一)

    个人认为学习一个陌生的框架,最好从例子开始,所以我们也从一个例子开始. 学习本教程之前,你需要首先对卷积神经网络算法原理有些了解,而且安装好了caffe 卷积神经网络原理参考:http://cs231 ...

  9. 实践详细篇-Windows下使用VS2015编译的Caffe训练mnist数据集

    上一篇记录的是学习caffe前的环境准备以及如何创建好自己需要的caffe版本.这一篇记录的是如何使用编译好的caffe做训练mnist数据集,步骤编号延用上一篇 <实践详细篇-Windows下 ...

随机推荐

  1. springboot单元测试@test的使用

    @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class Springtest { ...

  2. MyCat(转)

    https://www.cnblogs.com/bingosblog/p/7171501.html    http://www.cnblogs.com/joylee/p/7513038.html ht ...

  3. Visual Studio 2019 (VS2019)正式版安装 VisualSVN Server 插件

    VS2019 正式版最近刚刚推出来,目前 Ankhsvn 还不支持,它最高只支持 VS2017,全网搜索了一下,也没有找到.在 Stackoverflow 上看了一下,找到这篇问答: 自己按照这种方法 ...

  4. 发布.net core项目 System.AggregateException: 发生一个或多个错误

    背景:之前创建.net core webapi项目的时候SDK是2.2的版本,后改成2.1,发布的时候报错. 发布的时候报错,展示的信息是: 其实这里也大致能看到部分信息,但由于信息量太小,没办法知道 ...

  5. Spring源码系列 — 注解原理

    前言 前文中主要介绍了Spring中处理BeanDefinition的扩展点,其中着重介绍BeanDefinitionParser方式的扩展.本篇文章承接该内容,详解Spring中如何利用BeanDe ...

  6. java架构之路-(mysql底层原理)Mysql索引和查询引擎

    今天我们来说一下我们的mysql,个人认为现在的mysql能做到很好的优化处理,不比收费的oracle差,而且mysql确实好用. 当我们查询慢的时候,我会做一系列的优化处理,例如分库分表,加索引.那 ...

  7. CSS教程详解

    CSS学习笔记 一.CSS基础 1.CSS简介 层叠:一层一层的: 样式表:很多的属性和样式 CSS语法: <style> 选择器 { 属性名:属性值; 属性名:属性值; ……  } &l ...

  8. 用T4生成EF框架下的DAL、IDAL、BLL、IBLL

    (一)什么是T4模板? T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit. T4文本模板,即一种自定义规则的代码生成器.根据业务模型可生成任何 ...

  9. Electron使用时拦截HTTP请求的解决方案

    背景 最近在做一个Web和Electron共用一份代码的工程,由于使用到了第三方的库(我们是在线地图),该库的认证方式是请求时加key,并且它在后台会校验referer. 于是问题就来了,Electr ...

  10. RVZicsr指令集

    Riscv中每个硬件线程(hart)有4096个独立地址空间的状态寄存器.我们可以通过Zicsr指令读写csr寄存器.总共有6条csr读写指令,这些指令之前都在RV32I/RV64I基础指令集里面,在 ...