【T-Tensorflow框架学习】Tensorflow Mnist数据集简介

Tensorflow Mnist数据集简介:

'''
Creat by HuangDandan
dandanhuang@sjtu.edu.cn
2018-08-26
'''
#Tensorflow Mnist数据集简介
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
#从tensorfolw.examples.tutorials.mnist数据集中导入数据
from tensorflow.examples.tutorials.mnist import input_data

print("packs loaded")
print("Download and Extract MNIst dataset")
#运行时会自动下载下载数据集合到主文件夹下的data文件夹
mnist = input_data.read_data_sets('data/',one_hot=True)

print
#打印数据的类型
print("type of 'mnist' is %s"%(type(mnist)))
#打印训练集和测试集数据的个数
print("number of train data is %d"%(mnist.train.num_examples))
print("number of test data is %d"%(mnist.test.num_examples))

#MNIST数据集的数据可视化
print("MNIST database")
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 'testing'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 'testing'is %s" %(testimg.shape,))
print("shape of 'testlabel' is %s" %(testlabel.shape,))

#training data可视化
print("training data")
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,:]) #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()

#Batch Learning 调用mnist.train.next_batch(batch_size)
#通过nest_batch拿到x和y
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_xs' 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,))

输出:

packs loaded
Download and Extract MNIst dataset
WARNING:tensorflow:From D:/PycharmProjects/验证码识别小项目.py:173: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) 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.
Extracting data/train-images-idx3-ubyte.gz
WARNING:tensorflow:From F:\Anaconda\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From F:\Anaconda\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting data/train-labels-idx1-ubyte.gz
WARNING:tensorflow:From F:\Anaconda\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
WARNING:tensorflow:From F:\Anaconda\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From F:\Anaconda\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) 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.
type of 'mnist' is <class 'tensorflow.contrib.learn.python.learn.datasets.base.Datasets'>
number of train data is 55000
number of test data is 10000
MNIST database
type of 'trainimg' is <class 'numpy.ndarray'>
type of 'trainlabel' is <class 'numpy.ndarray'>
type of 'testing'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 'testing'is (10000, 784)
shape of 'testlabel' is (10000, 10)
training data
45460th Training Datalabel is5
F:\Anaconda\lib\site-packages\matplotlib\figure.py:2267: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
  warnings.warn("This figure includes Axes that are not compatible "
12171th Training Datalabel is1
47707th Training Datalabel is7
8450th Training Datalabel is0
27075th Training Datalabel is2
Batch Learning
type of 'batch_xs' is <class 'numpy.ndarray'>
type of 'batch_xs' is <class 'numpy.ndarray'>
shape of 'batch_xs' is (100, 784)
shape of 'batch_ys' is (100, 10)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MLP-MNIST数据集是一个用于手写数字识别的数据集,其中包含了一系列的手写数字图像和相应的标签。每个图像都是一个28x28像素的灰度图像,标签表示了图像所代表的数字。 为了对MLP-MNIST数据集进行处理和分析,以下是一些常见的步骤和方法: 1. 导入数据集:首先,我们需要从数据集中读取图像和标签数据。这可以通过使用适当的库和函数来完成,例如使用Python的NumPy库或者深度学习框架TensorFlow或PyTorch提供的接口。 2. 数据预处理:在使用MLP模型之前,通常需要对数据进行一些预处理操作。这可能包括将图像数据转换为适当的张量形式,对图像进行标准化处理以确保数据的一致性和可比性,还可以将标签进行独热编码以适应模型的输出要求。 3. 定义模型结构:MLP-MNIST数据集通常使用多层感知机(MLP)模型进行训练和预测。这种模型是一种前馈神经网络,由多个全连接层组成。可以定义不同层的神经元数量、激活函数和其他参数来适应具体的任务。 4. 初始化模型参数:在训练模型之前,需要初始化模型的参数。这些参数可以是权重和偏置,通过随机初始化或者使用特定的初始化方法来获取合适的初始值。 5. 定义激活函数:对于MLP模型的隐含层,通常需要使用激活函数对输出进行非线性变换。常见的激活函数包括ReLU、Sigmoid和Tanh等,它们可以增加模型的表达能力和非线性拟合能力。 6. 防止过拟合:在使用带有隐含层的MLP模型时,过拟合是一个常见的问题。为了解决这个问题,可以采用一些技术来防止过拟合,如正则化方法(如权重衰减)和丢弃法(Dropout)等。 综上所述,处理和分析MLP-MNIST数据集的一般步骤包括导入数据集、数据预处理、定义模型结构、初始化模型参数、定义激活函数以及防止过拟合。这些步骤可以帮助我们构建一个用于手写数字识别的MLP模型,并进行训练和预测。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值