tensorflow(3)——Mnist数据集

学习《Tensorflow入门教程》记录 

一、加载数据集

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

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))

运行结果:
类型是 <class 'tensorflow.contrib.learn.python.learn.datasets.base.Datasets'>
 训练数据有 55000
 测试数据有 10000
注意:如果Mnist加载失败,可以自行下载数据集,放在当前路径的data文件夹下。

二、数据集的规格

trainimg   = mnist.train.images
trainlabel = mnist.train.labels
testimg    = mnist.test.images
testlabel  = mnist.test.labels
# 28 * 28 * 1
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)

三、数据集的形式

nsample = 2
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 训练数据 "
           + "0标签是 " + str(curr_label))
    plt.show()

结果是:
48118th 训练数据 标签是 0
8268th 训练数据 标签是 0

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值