深度学习框架tensorflow实战(Mnist数据集简介)

导入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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wujiekd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值