# Training Images: http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz # Training Labels: http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz # Loading Sample Images # Loading 60K images from MNIST database # NOTE: YOU LIKELY NEED TO CHANGE THE FOLLOWING PATH TO WHERE YOU STORED THE MNIST DATA images = load_MNIST.load_MNIST_images('../data/mnist/train-images-idx3-ubyte') # Each column represents one 28x28 pixel image (784 total pixels) that has # been "unrolled" into a 784-element column vector. patches_train = images[:, 0: 100] # grabs the first 100 images (i.e., the first 100 columns) patches_test = images[:, 1200: 1300] # grabs 100 image patches that will be used for 'testing' visualize.plot_images(patches_train[:, 0:100]) # ====================================================================== # STEP 1: Visualize patches_train # You will now use the visualize.py function plot_images() to display different # sets if the MNIST dataset. You can provide a filename argument to plot_images # which will specify the name of the file the image is saved as; # default is to save the file in the directory under the name 'weights.png'. # Your task: # Plot the first 10, 50 and 100 images of patches_train # Also plot the first 100 image patches that will be used for 'testing' (patches 1200 to 1300) if RUN_STEP_1: ### YOUR CODE HERE ### pass
#visualize.plot_images(patches_train[:, 0:100]) # ====================================================================== # STEP 1: Visualize patches_train # You will now use the visualize.py function plot_images() to display different # sets if the MNIST dataset. You can provide a filename argument to plot_images # which will specify the name of the file the image is saved as; # default is to save the file in the directory under the name 'weights.png'. # Your task: # Plot the first 10, 50 and 100 images of patches_train # Also plot the first 100 image patches that will be used for 'testing' (patches 1200 to 1300) if RUN_STEP_1: for domain in [10, 50, 100]: visualize.plot_images(patches_train[:, 0:domain], filepath='warmup-{}'.format(domain)) # ====================================================================== # STEP 2: Implement utils.initialize # Obtain random parameters theta ; see Exercise 2 # You need to implement initialize in utils.py if RUN_STEP_2: theta = utils.initialize(hidden_size, visible_size) # ====================================================================== # STEP 3: Implement mlp_cost_and_grad # # In this step you will implement the calculation of the loss (cost) # and theta gradient (grad) in utils.autoencoder_cost_and_grad. # # You can implement all of the components in the cost function at once,
# ====================================================================== # STEP 1: Visualize patches_train # You will now use the visualize.py function plot_images() to display different # sets if the MNIST dataset. You can provide a filename argument to plot_images # which will specify the name of the file the image is saved as; # default is to save the file in the directory under the name 'weights.png'. # Your task: # Plot the first 10, 50 and 100 images of patches_train # Also plot the first 100 image patches that will be used for 'testing' (patches 1200 to 1300) if RUN_STEP_1: ### YOUR CODE HERE ### visualize.plot_images( patches_train[:, 0:10], filepath= "/home/pooja/Documents/INFO_521/HW-5/ista421ML-f2017-hw5-release/Figures/load_10_images" ) visualize.plot_images( patches_train[:, 0:50], filepath= "/home/pooja/Documents/INFO_521/HW-5/ista421ML-f2017-hw5-release/Figures/load_50_images" ) visualize.plot_images( patches_train[:, 0:100], filepath= "/home/pooja/Documents/INFO_521/HW-5/ista421ML-f2017-hw5-release/Figures/load_100_images" ) visualize.plot_images( patches_test[:, 0:100], filepath=
def plot_and_save_results(theta, visible_size, hidden_size, root_filepath=None, train_patches=None, test_patches=None, show_p=False, **params): """ This is a helper function to streamline saving the results of an autoencoder. The visible_size and hidden_size provide the information needed to retrieve the autoencoder parameters (w1, w2, b1, b2) from theta. This function does the following: (1) Saves the parameters theta, visible_size and hidden_size as a text file called '<root_filepath>_model.txt' (2) Extracts the layer 1 (input-to-hidden) weights and plots them as an image and saves the image to file '<root_filepath>_weights.png' (3) [optional] train_patches are intended to be a set of patches that were used during training of the autoencoder. Typically these will be the first 100 patches of the MNIST data set. If provided, the patches will be given as input to the autoencoder in order to generate output 'decoded' activations that are then plotted as patches in an image. The image is saved to '<root_filepath>_train_decode.png' (4) [optional] test_patches are intended to be a different set of patches that were *not* used during training. This permits inspecting how the autoencoder does decoding images it was not trained on. The output activation image is generated the same way as in step (3). The image is saved to '<root_filepath>_test_decode.png' The root_filepath is used as the base filepath name for all files generated by this function. For example, if you wish to save all of the results using the root_filepath='results1', and you have specified the train_patches and test_patches, then the following files will be generated: results1_model.txt results1_weights.png results1_train_decode.png results1_test_decode.png If no root_filepath is provided, then the output will default to: model.txt weights.png train_decode.png test_decode.png Note that if those files already existed, they will be overwritten. :param theta: model parameters :param visible_size: number of nodes in autoencoder visible layer :param hidden_size: number of nodes in autoencoder hidden layer :param root_filepath: base filepath name for files generated by this function :param train_patches: matrix of patches (typically the first 100 patches of MNIST) :param test_patches: matrix of patches (intended to be patches NOT used in training) :param show_p: flag specifying whether to show the plots before exiting :param params: optional parameters that will be saved with the model as a dictionary :return: """ filepath = 'model' if root_filepath: filepath = root_filepath + '_' + filepath save_model(theta, visible_size, hidden_size, filepath, **params) # extract the input to hidden layer weights and visualize all the weights # corresponding to each hidden node w1 = theta[0:hidden_size * visible_size].reshape(hidden_size, visible_size).T filepath = 'weights.png' if root_filepath: filepath = root_filepath + '_' + filepath visualize.plot_images(w1, show_p=False, filepath=filepath) if train_patches is not None: # Given: train_patches and autoencoder parameters, # compute the output activations for each input, and plot the resulting decoded # output patches in a grid. # You can then manually compare them (visually) to the original input train_patches filepath = 'train_decode.png' if root_filepath: filepath = root_filepath + '_' + filepath train_decode = autoencoder_feedforward(theta, visible_size, hidden_size, train_patches) visualize.plot_images(train_decode, show_p=False, filepath=filepath) if test_patches is not None: # Same as for train_patches, but assuming test_patches are patches that were not # used for training the autoencoder. # Again, you can then manually compare the decoded patches to the test_patches # given as input. test_decode = autoencoder_feedforward(theta, visible_size, hidden_size, test_patches) filepath = 'test_decode.png' if root_filepath: filepath = root_filepath + '_' + filepath visualize.plot_images(test_decode, show_p=False, filepath=filepath) if show_p: plt.show()
#visualize.plot_images(patches_train[:, 0:100]) # ====================================================================== # STEP 1: Visualize patches_train # You will now use the visualize.py function plot_images() to display different # sets if the MNIST dataset. You can provide a filename argument to plot_images # which will specify the name of the file the image is saved as; # default is to save the file in the directory under the name 'weights.png'. # Your task: # Plot the first 10, 50 and 100 images of patches_train # Also plot the first 100 image patches that will be used for 'testing' (patches 1200 to 1300) if RUN_STEP_1: fig1 = plt.figure() fig1.suptitle('First 10 training images from patches_train') visualize.plot_images(patches_train[:, 0:10]) fig2 = plt.figure() fig2.suptitle('First 50 training images from patches_train') visualize.plot_images(patches_train[:, 0:50]) fig3 = plt.figure() # fig3.suptitle('First 100 training images from patches_train') visualize.plot_images(patches_train[:, 0:100]) fig4 = plt.figure() fig4.suptitle('Testing images from patches_test') visualize.plot_images(patches_test, filepath="../fig8.png") pass theta = None # ====================================================================== # STEP 2: Implement utils.initialize # Obtain random parameters theta ; see Exercise 2
1300] # grabs 100 image patches that will be used for 'testing' print('called3') # ====================================================================== # STEP 1: Visualize patches_train # You will now use the visualize.py function plot_images() to display different # sets if the MNIST dataset. You can provide a filename argument to plot_images # which will specify the name of the file the image is saved as; # default is to save the file in the directory under the name 'weights.png'. # Your task: # Plot the first 10, 50 and 100 images of patches_train # Also plot the first 100 image patches that will be used for 'testing' (patches 1200 to 1300) if RUN_STEP_1: #plot_images(A, show_p=False, filepath=None, title=None): visualize.plot_images(patches_train[:, 0:10], title='first 10') visualize.plot_images(patches_train[:, 0:50], title='first 50') visualize.plot_images(patches_train[:, 0:100], title='first 100') visualize.plot_images(patches_test[:, 0:100], title='first 100 test') print('called4') pass # ====================================================================== # STEP 2: Implement utils.initialize # Obtain random parameters theta ; see Exercise 2 # You need to implement initialize in utils.py if RUN_STEP_2: theta = utils.initialize(hidden_size, visible_size) print('called5') # ======================================================================
1300] # grabs 100 image patches that will be used for 'testing' # ====================================================================== # STEP 1: Visualize patches_train # You will now use the visualize.py function plot_images() to display different # sets if the MNIST dataset. You can provide a filename argument to plot_images # which will specify the name of the file the image is saved as; # default is to save the file in the directory under the name 'weights.png'. # Your task: # Plot the first 10, 50 and 100 images of patches_train # Also plot the first 100 image patches that will be used for 'testing' (patches 1200 to 1300) if RUN_STEP_1: for num in [10, 50, 100]: visualize.plot_images(patches_train[:, 0:num], filepath='patches_train{:03d}.png'.format(num)) visualize.plot_images(patches_test[:, 0:100], filepath='patches_test100.png') # ====================================================================== # STEP 2: Implement utils.initialize # Obtain random parameters theta ; see Exercise 2 # You need to implement initialize in utils.py if RUN_STEP_2: theta = utils.initialize(hidden_size, visible_size) # ====================================================================== # STEP 3: Implement mlp_cost_and_grad #