def main(): # set/get visible_size, hidden_size global visible_size global hidden_size # get input data & save input data data = load_MNIST.load_MNIST_images(r'D:\MNIST\train-images.idx3-ubyte') data = sample_MNIST_images(data) pickle.dump(data, open("input_data.pydump", 'wb')) # train sparse autoencoder & save weights/bias the_output_theta = train(input_data=data, visible_size=visible_size, hidden_size=hidden_size) pickle.dump(the_output_theta, open("test.pydump", 'wb'))
def analyze_theta(): # set/get visible_size, hidden_size global visible_size global hidden_size # We first convert theta to the (W1, W2, b1, b2) matrix/vector format, so that this # follows the notation convention of the lecture notes. data_theta = pickle.load(open("test.pydump", 'rb')) W1 = data_theta[0:hidden_size * visible_size].reshape( hidden_size, visible_size) b1 = data_theta[2 * hidden_size * visible_size:2 * hidden_size * visible_size + hidden_size] W2 = data_theta[hidden_size * visible_size:2 * hidden_size * visible_size].reshape(visible_size, hidden_size) b2 = data_theta[2 * hidden_size * visible_size + hidden_size:] # data data = load_MNIST_images(r'D:\MNIST\train-images.idx3-ubyte') data = sample_MNIST_images(data) # Number of training examples m = data.shape[1] # z2 = W1.dot(data) + np.tile(b1, (m, 1)).transpose() # a2 = sigmoid(z2) a2 = sparse_autoencoder(data_theta, hidden_size=hidden_size, visible_size=visible_size, data=data) z3 = W2.dot(a2) + np.tile(b2, (m, 1)).transpose() h = sigmoid(z3) from used.display_network import display_network display_network(W1.transpose(), filename="weights.jpg", opt_normalize=False) display_network(W2, filename="weights2.jpg", opt_normalize=False)
img = image_data[:, :, image_id] patch = img[image_x:image_x + patch_size, image_y:image_y + patch_size].reshape(patch_size * patch_size) patches[:, i] = patch return patches def sample_MNIST_images(data: np.ndarray, patch_size=8, num_patches=10000): num_images = data.shape[1] image_size = math.floor(math.sqrt(data.shape[0])) patches = np.empty(shape=[patch_size * patch_size, num_patches], dtype=np.float64) for i in range(num_patches): img_id = random.choice(range(0, num_images)) img_x = random.choice(range(0, image_size - patch_size)) img_y = random.choice(range(0, image_size - patch_size)) img = data[:, img_id].reshape([image_size, image_size]) patch = img[img_x: img_x + patch_size, img_y: img_y + patch_size].reshape([patch_size ** 2]) patches[:, i] = patch return patches if __name__ == '__main__': from used.load_MNIST import load_MNIST_images from PIL import Image data = load_MNIST_images(r'D:\MNIST\train-images.idx3-ubyte') data2 = sample_MNIST_images(data) img = (data2[:, 0] * 255).astype(np.uint8).reshape([8, 8]) Image.fromarray(img, "L").show() print(data2.shape) print(data2.dtype)
# change the parameters below. input_size = 28 * 28 num_classes = 10 hidden_size_L1 = 200 # Layer 1 Hidden Size hidden_size_L2 = 200 # Layer 2 Hidden Size sparsity_param = 0.1 # desired average activation of the hidden units. lambda_ = 3e-3 # weight decay parameter beta = 3 # weight of sparsity penalty term ##====================================================================== ## STEP 1: Load data from the MNIST database # # This loads our training data from the MNIST database files. train_images = load_MNIST.load_MNIST_images( 'data/mnist/train-images-idx3-ubyte') train_labels = load_MNIST.load_MNIST_labels( 'data/mnist/train-labels-idx1-ubyte') ##====================================================================== ## STEP 2: Train the first sparse autoencoder # This trains the first sparse autoencoder on the unlabelled STL training # images. # If you've correctly implemented sparseAutoencoderCost.m, you don't need # to change anything here. # Randomly initialize the parameters sae1_theta = sparse_autoencoder.initialize(hidden_size_L1, input_size) J = lambda x: sparse_autoencoder.sparse_autoencoder_cost( x, input_size, hidden_size_L1, lambda_, sparsity_param, beta, train_images)
input_size = 28 * 28 num_labels = 5 hidden_size = 196 sparsity_param = 0.1 # desired average activation of the hidden units. lambda_ = 3e-3 # weight decay parameter beta = 3 # weight of sparsity penalty term ## ====================================================================== # STEP 1: Load data from the MNIST database # # This loads our training and test data from the MNIST database files. # We have sorted the data for you in this so that you will not have to # change it. images = load_MNIST.load_MNIST_images('data/mnist/train-images-idx3-ubyte') labels = load_MNIST.load_MNIST_labels('data/mnist/train-labels-idx1-ubyte') unlabeled_index = np.argwhere(labels >= 5).flatten() labeled_index = np.argwhere(labels < 5).flatten() num_train = round(labeled_index.shape[0] / 2) train_index = labeled_index[0:num_train] test_index = labeled_index[num_train:] unlabeled_data = images[:, unlabeled_index] train_data = images[:, train_index] train_labels = labels[train_index] test_data = images[:, test_index]