Tensorflow MNIST 数据集使用

# Tensorflow MNIST 数据集使用
import tensorflow as tf
tf.__version__
'2.1.0'
import numpy as np
import matplotlib.pyplot as plt

ModuleNotFoundError: No module named ‘tensorflow.examples.tutorials’

解决方案1–针对没有tutorials文件报错的解决

  1. 先检查tensorflow中是否含有tutorials
    • 检测路径:…\Lib\site-packages\tensorflow_core\examples;
    • 文件夹下只有saved_model这个文件夹,没有tutorials文件夹;
  2. 进入github的tensorflow主页下载缺失的文件
    下载地址:tensorflow tutorials
  3. 拷贝下载的tutorials整个文件夹
    • 解压下载的包,找到tutorials整个文件夹;
    • 拷贝至…\Lib\site-packages\tensorflow_core\examples下;
  4. 再次加载包
    from tensorflow.examples.tutorials.mnist import input_data

解决办法2

tensorflow2.0的数据集集成到keras高级接口之中,使用如下代码一般都能下载

mint=tf.keras.datasets.mnist
(x_,y_),(x_1,y_1)=mint.load_data()
from tensorflow.examples.tutorials.mnist import input_data
print ("packs loaded")
packs loaded

input_data.read_data_sets() 下载数据集失败

from tensorflow.examples.tutorials.mnist import input_data
print ("Download and Extract MNIST dataset")
mnist = input_data.read_data_sets('data/', one_hot=True)
  1. mnist数据集获取:可从Yann LeCun教授管网获取;
  2. 导入下载到本地的mnist数据集; "data/"为数据集存放的位置;
# 会下载数据集失败
print ("Download and Extract MNIST dataset")
mnist = input_data.read_data_sets('data/', one_hot=True)
Download and Extract MNIST dataset
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
WARNING:tensorflow:From d:\progra~2\python\virtua~1\py37_x64\lib\site-packages\tensorflow_core\examples\tutorials\mnist\input_data.py:328: _DataSet.__init__ (from tensorflow.examples.tutorials.mnist.input_data) 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.
# 查看数据数量
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))
 tpye of 'mnist' is <class 'tensorflow.examples.tutorials.mnist.input_data._Datasets'>
 number of trian data is 55000
 number of test data is 10000
# 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("type of 'trainlabel' is %s"  % type(trainlabel))
print("type of 'testimg' is %s"     % type(testimg))
print("type of 'testlabel' is %s"   % type(testlabel))

print()
# 查看数据形状
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 'trainlabel' is <class 'numpy.ndarray'>
type of 'testimg' is <class 'numpy.ndarray'>
type of 'testlabel' is <class 'numpy.ndarray'>

shape of 'trainimg' is (55000, 784)
shape of 'trainlabel' is (55000, 10)
shape of 'testimg' is (10000, 784)
shape of 'testlabel' is (10000, 10)
# 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))
    plt.show()
How does the training data look like?
54259th Training Data Label is 4

在这里插入图片描述

33047th Training Data Label is 4

在这里插入图片描述
52715th Training Data Label is 1

在这里插入图片描述
15223th Training Data Label is 7

在这里插入图片描述
18188th Training Data Label is 4

在这里插入图片描述

# 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 <class 'numpy.ndarray'>
type of 'batch_ys' is <class 'numpy.ndarray'>
shape of 'batch_xs' is (100, 784)
shape of 'batch_ys' is (100, 10)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值