MNIST

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

%matplotlib inline  
print ("packs loaded")
packs loaded

Download and Extract MNIST dataset

print ("Download and Extract MNIST dataset")
mnist = input_data.read_data_sets('data/', one_hot=True)
print
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))
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

 tpye of 'mnist' is <class 'tensorflow.contrib.learn.python.learn.datasets.mnist.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
print (" type of 'trainimg' is %s"    % (type(trainimg)))
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 (" 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 'trainimg' is <type 'numpy.ndarray'>
 type of 'trainlabel' is <type 'numpy.ndarray'>
 type of 'testimg' is <type 'numpy.ndarray'>
 type of 'testlabel' is <type '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))
How does the training data look like?
5847th Training Data Label is 3
11646th Training Data Label is 5
5996th Training Data Label is 6
41841th Training Data Label is 2
15149th Training Data Label is 4

png

png

png

png

png

# 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 <type 'numpy.ndarray'>
type of 'batch_ys' is <type 'numpy.ndarray'>
shape of 'batch_xs' is (100, 784)
shape of 'batch_ys' is (100, 10)
# Get Random Batch with 'np.random.randint'
print ("5. Get Random Batch with 'np.random.randint'")
randidx   = np.random.randint(trainimg.shape[0], size=batch_size)
batch_xs2 = trainimg[randidx, :]
batch_ys2 = trainlabel[randidx, :]
print ("type of 'batch_xs2' is %s" % (type(batch_xs2)))
print ("type of 'batch_ys2' is %s" % (type(batch_ys2)))
print ("shape of 'batch_xs2' is %s" % (batch_xs2.shape,))
print ("shape of 'batch_ys2' is %s" % (batch_ys2.shape,))
5. Get Random Batch with 'np.random.randint'
type of 'batch_xs2' is <type 'numpy.ndarray'>
type of 'batch_ys2' is <type 'numpy.ndarray'>
shape of 'batch_xs2' is (100, 784)
shape of 'batch_ys2' is (100, 10)
randidx
array([40597, 27095, 26480,  9962, 40322, 38562, 53100, 29354, 24853,
       39323, 12301, 29530,  6876, 17472, 11859, 32907, 31891, 43449,
       42376, 22173,   115, 16827, 47957, 10636, 43259, 16207, 33329,
       12654,  5640,  6254, 36093, 39494, 45642, 28959,  2347,  1911,
       11653, 40175, 28654, 29179, 36227,  3112, 35634, 22400, 38441,
       11548, 29659, 39165, 42957, 19418,  3168, 53571, 29323,  8976,
       18668, 46934, 19848, 19982,  9148, 16059, 48727, 31939, 11938,
       36669, 24021, 31104, 39372, 44231, 10097, 43418,  5704, 17825,
       54984, 38007,  2098, 31896, 41666,  8106, 37039,  3065, 44691,
        6446, 34830, 26956, 36618, 15762, 17894, 23088, 37065, 41814,
       20915, 13454, 43314,  2817, 34319, 13249, 39225, 36624, 53752, 44867])

results matching ""

    No results matching ""