Friday, August 24, 2018

Rebuild MNIST Number Image from the Vector

IDE 
pyCharm CE

Source 
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

#Load data
X_train, y_train),(X_test, y_test) = tf.keras.datasets.mnist.load_data()

#60000, which is the value of the 0th dimention i.e. shape[0]
image_record_numbers = X_train.shape[0]

#record from the mnist train data to be plotted, total 60000
image_record_number = 0

#calculate number of pixels for each image, X_train.shape[1] returns 28 & X_train.shape[2] returns 28
number_of_pixels = X_train.shape[1] * X_train.shape[2];

#get a vector (784 pixels), representsation of the image with 28 x 28
X_train_record_vectors = X_train.reshape(image_record_numbers, number_of_pixels).astype('float32')

#Convert back a vector (784 pixels), into the Image of 28 x 28 pixels
X_train_record_matrix = X_train_record_vectors[image_record_number].reshape((28,28))

#Plot the image using reshaped vector
plt.imshow(X_train_record_matrix,cmap=plt.get_cmap('gray'))
plt.show()

Output 



No comments:

Post a Comment

Installation of Tensorflow within Python Env and Jupyter

1. Download Anaconda: https://www.anaconda.com/distribution/#macos Error while creating a Session with Tensorflow (command line) >...