热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

tensorflowtutorials(八):手写数字数据集MNIST介绍

声明:版权所有,转载请联系作者并注明出处:http:blog.csdn.netu013719780?viewmodecontents在做机器学习相关实验的时候,首先我们就


声明:版权所有,转载请联系作者并注明出处:  http://blog.csdn.net/u013719780?viewmode=contents


在做机器学习相关实验的时候,首先我们就是需要一份通用的数据集,以便与其他的算法得到的实验结果进行比较。在图像分类领域MNIST数据集就是这样一个通用的数据集,前面几篇博文都用到了MNIST数据集,本文对其进行一些简单的介绍!


MNIST

In [1]:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

%matplotlib inline
print ("packs loaded")
packs loaded

Download and Extract MNIST dataset

In [2]:
print ("Download and Extract MNIST dataset")
mnist = input_data.read_data_sets('/tmp/data/', one_hot=True)
print
print (" tpye of 'mnist' is %s" % (type(mnist)))
print (" number of trian data is %d" % (mnist.train.num_examples))
print (" number of test data is %d" % (mnist.test.num_examples))
Download and Extract MNIST dataset
Extracting /tmp/data/train-images-idx3-ubyte.gz
Extracting /tmp/data/train-labels-idx1-ubyte.gz
Extracting /tmp/data/t10k-images-idx3-ubyte.gz
Extracting /tmp/data/t10k-labels-idx1-ubyte.gz

tpye of 'mnist' is
number of trian data is 55000
number of test data is 10000
In [3]:
# What does the data of MNIST look like? 
print ("What does the data of MNIST look like?")
trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels
print
print (" type of 'trainimg' is %s" % (type(trainimg)))
print (" type of 'trainlabel' is %s" % (type(trainlabel)))
print (" type of 'testimg' is %s" % (type(testimg)))
print (" type of 'testlabel' is %s" % (type(testlabel)))
print (" shape of 'trainimg' is %s" % (trainimg.shape,))
print (" shape of 'trainlabel' is %s" % (trainlabel.shape,))
print (" shape of 'testimg' is %s" % (testimg.shape,))
print (" shape of 'testlabel' is %s" % (testlabel.shape,))
What does the data of MNIST look like?

type of 'trainimg' is
type of 'trainlabel' is
type of 'testimg' is
type of 'testlabel' is
shape of 'trainimg' is (55000, 784)
shape of 'trainlabel' is (55000, 10)
shape of 'testimg' is (10000, 784)
shape of 'testlabel' is (10000, 10)
In [4]:
# How does the training data look like?
print ("How does the training data look like?")
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'))
plt.title("" + str(i) + "th Training Data "
+ "Label is " + str(curr_label))
print ("" + str(i) + "th Training Data "
+ "Label is " + str(curr_label))
How does the training data look like?
12118th Training Data Label is 5
46324th Training Data Label is 8
33th Training Data Label is 4
36491th Training Data Label is 3
6910th Training Data Label is 3
In [5]:
# Batch Learning? 
print ("Batch Learning? ")
batch_size = 100
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
print ("type of 'batch_xs' is %s" % (type(batch_xs)))
print ("type of 'batch_ys' is %s" % (type(batch_ys)))
print ("shape of 'batch_xs' is %s" % (batch_xs.shape,))
print ("shape of 'batch_ys' is %s" % (batch_ys.shape,))
Batch Learning? 
type of 'batch_xs' is
type of 'batch_ys' is
shape of 'batch_xs' is (100, 784)
shape of 'batch_ys' is (100, 10)
In [6]:
# Get Random Batch with 'np.random.randint'
print ("5. Get Random Batch with 'np.random.randint'")
randidx = np.random.randint(trainimg.shape[0], size=batch_size)
batch_xs2 = trainimg[randidx, :]
batch_ys2 = trainlabel[randidx, :]
print ("type of 'batch_xs2' is %s" % (type(batch_xs2)))
print ("type of 'batch_ys2' is %s" % (type(batch_ys2)))
print ("shape of 'batch_xs2' is %s" % (batch_xs2.shape,))
print ("shape of 'batch_ys2' is %s" % (batch_ys2.shape,))
5. Get Random Batch with 'np.random.randint'
type of 'batch_xs2' is
type of 'batch_ys2' is
shape of 'batch_xs2' is (100, 784)
shape of 'batch_ys2' is (100, 10)
In [7]:
randidx
Out[7]:
array([51472, 13751, 33562, 23281,  8489, 48481,  7799, 30307, 37366,
25312, 46149, 49712, 5083, 52853, 29819, 36444, 34829, 8769,
39518, 54911, 6720, 43675, 41703, 35594, 9300, 14474, 33318,
14808, 53456, 41978, 8047, 34524, 30978, 53455, 42119, 22660,
30329, 27169, 53798, 2125, 41759, 38951, 1438, 33511, 38784,
15822, 16785, 9229, 1216, 19569, 3116, 22172, 14766, 16153,
1707, 20899, 9087, 21263, 24853, 27784, 38324, 29287, 21828,
34511, 26340, 39194, 38272, 34238, 28050, 29294, 42672, 18696,
17796, 48147, 41841, 47077, 5925, 48237, 30605, 9169, 11260,
9155, 39346, 41049, 11342, 536, 5927, 11155, 40424, 33583,
38991, 16569, 34801, 870, 20546, 25061, 17601, 4521, 24359, 4613])


推荐阅读
  • 帝国CMS多图上传插件详解及使用指南
    本文介绍了一款用于帝国CMS的多图上传插件,该插件通过Flash技术实现批量图片上传功能,显著提升了多图上传效率。文章详细说明了插件的安装、配置和使用方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 尽管深度学习带来了广泛的应用前景,其训练通常需要强大的计算资源。然而,并非所有开发者都能负担得起高性能服务器或专用硬件。本文探讨了如何在有限的硬件条件下(如ARM CPU)高效运行深度神经网络,特别是通过选择合适的工具和框架来加速模型推理。 ... [详细]
  • Coursera ML 机器学习
    2019独角兽企业重金招聘Python工程师标准线性回归算法计算过程CostFunction梯度下降算法多变量回归![选择特征](https:static.oschina.n ... [详细]
  • 深入浅出TensorFlow数据读写机制
    本文详细介绍TensorFlow中的数据读写操作,包括TFRecord文件的创建与读取,以及数据集(dataset)的相关概念和使用方法。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文介绍了在使用Visual Studio 2015进行项目开发时,遇到类向导弹出“异常来自 HRESULT:0x8CE0000B”错误的解决方案。通过具体步骤和实践经验,帮助开发者快速排查并解决问题。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 作为一名新手,您可能会在初次尝试使用Eclipse进行Struts开发时遇到一些挑战。本文将为您提供详细的指导和解决方案,帮助您克服常见的配置和操作难题。 ... [详细]
  • 机器学习中的相似度度量与模型优化
    本文探讨了机器学习中常见的相似度度量方法,包括余弦相似度、欧氏距离和马氏距离,并详细介绍了如何通过选择合适的模型复杂度和正则化来提高模型的泛化能力。此外,文章还涵盖了模型评估的各种方法和指标,以及不同分类器的工作原理和应用场景。 ... [详细]
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  •   上一篇博客中我们说到线性回归和逻辑回归之间隐隐约约好像有什么关系,到底是什么关系呢?我们就来探讨一下吧。(这一篇数学推导占了大多数,可能看起来会略有枯燥,但这本身就是一个把之前算法 ... [详细]
  • 深入理解K近邻分类算法:机器学习100天系列(26)
    本文详细介绍了K近邻分类算法的理论基础,探讨其工作原理、应用场景以及潜在的局限性。作为机器学习100天系列的一部分,旨在为读者提供全面且深入的理解。 ... [详细]
  • 机器学习核心概念与技术
    本文系统梳理了机器学习的关键知识点,涵盖模型评估、正则化、线性模型、支持向量机、决策树及集成学习等内容,并深入探讨了各算法的原理和应用场景。 ... [详细]
  • Python中HOG图像特征提取与应用
    本文介绍如何在Python中使用HOG(Histogram of Oriented Gradients)算法进行图像特征提取,探讨其在目标检测中的应用,并详细解释实现步骤。 ... [详细]
author-avatar
O依楼观雪O
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有