tensorflow学习(2)MNIST数据集

Pycharm今天get一个小点: 如果图像显示不出来,在file->setting->Tools->python scientific  把出来的钩钩去掉。

这个程序的作用是下载数据集到data 文件夹,显示数据及大小,显示示例图片。


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

#从mnist 下载数据集存到data文件夹。在你的项目文件夹下。


mnist = input_data.read_data_sets('data/', one_hot=True)
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))


#数据分为 训练集和测试集,分别有图片数据和标签
trainimg   = mnist.train.images
trainlabel = mnist.train.labels
testimg    = mnist.test.images
testlabel  = mnist.test.labels

#显示随机的5个图片实例,大小为28*28

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

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

 shape of 'trainimg' is (55000, 784)   55000个训练图片,大小是784=28*28

 shape of 'trainlabel' is (55000, 10)   55000个标签, 10种标签

 shape of 'testimg' is (10000, 784)

 shape of 'testlabel' is (10000, 10)

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)

猜你喜欢

转载自blog.csdn.net/weixin_40820983/article/details/84932614