tensorflow实现加载mnist数据集

yipeiwu_com6年前Python基础

mnist作为最基础的图片数据集,在以后的cnn,rnn任务中都会用到

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

#数据集存放地址,采用0-1编码
mnist = input_data.read_data_sets('F:/mnist/data/',one_hot = True)
print(mnist.train.num_examples)
print(mnist.test.num_examples)

trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels

#打印相关信息
print(type(trainimg))
print(trainimg.shape,)
print(trainlabel.shape,)
print(testimg.shape,)
print(testlabel.shape,)

nsample = 5
randidx = np.random.randint(trainimg.shape[0],size = nsample)

#输出几张数字的图
for i in randidx:
  curr_img = np.reshape(trainimg[i,:],(28,28))
  curr_label = np.argmax(trainlabel[i,:])
  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))
  plt.show()

程序运行结果如下:

Extracting F:/mnist/data/train-images-idx3-ubyte.gz
Extracting F:/mnist/data/train-labels-idx1-ubyte.gz
Extracting F:/mnist/data/t10k-images-idx3-ubyte.gz
Extracting F:/mnist/data/t10k-labels-idx1-ubyte.gz
55000
10000
<class 'numpy.ndarray'>
(55000, 784)
(55000, 10)
(10000, 784)
(10000, 10)
52636th 

输出的图片如下:

Training Datalabel is9

下面还有四张其他的类似图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

浅谈python3中input输入的使用

浅谈python3中input输入的使用

今天谈一下关于python中input的一些基本用法(写给新手入门之用,故只谈比较实用的部分)。 首先,我们可以看一下官方文档给我们的解释(在python的shell中输入命令即可):...

python如何给字典的键对应的值为字典项的字典赋值

问题 1:需要得到一个类似{“demo”:{“key”:”value”}}这样格式的字典dic。 dic = dict() dic_temp = dict() dic_temp =...

python reverse反转部分数组的实例

python3中,list有个reverse函数,用来反转列表元素,但是如果想要反转部分元素呢? a = [1,2,3,4,5] a[0:3].reverse() # not wor...

通过python实现随机交换礼物程序详解

看到了一个面试题,想了两种解法,不知道符不符合要求,记录如下: 题目:有N个人,每人备一个圣诞礼物,现需要写一个程序,随机交互礼物,要求:自己不能换到自己的礼物,用python实现。 方...

python logging模块书写日志以及日志分割详解

python logging模块书写日志以及日志分割详解

本文范例是书写两个日志:错误日志(ERROR级别)和运行日志(DEBUG级别),其中运行日志每日凌晨进行分割 import logging,datetime,logging.hand...