dir_name : str Name of the directory to save the images to """ for i, w in enumerate(weights): # Remove bias and normalize on [0, 1] w = w[1:] w -= w.min() w /= (w.max() - w.min()) w_name = dir_name.joinpath(f'feat_{i:02d}.png') matplotlib.image.imsave(str(w_name), w.reshape(28, 28, order='F'), cmap='Greys_r') # Seed for consistency np.random.seed(SEED) # Load best weights back up for each model auto_clean = Autoencoder(input_size=INPUTS) for weight_file in sorted(AUTO_CLEAN_MODEL_DIR.iterdir()): auto_clean.addLayer(file_name=weight_file) auto_noisy = Autoencoder(input_size=INPUTS) for weight_file in sorted(AUTO_NOISY_MODEL_DIR.iterdir()): auto_noisy.addLayer(file_name=weight_file) # Neurons to check neuron_count = auto_clean.layers[0].num_neurons neurons = np.random.choice(np.arange(neuron_count), 20, replace=False) clean_weights = auto_clean.layers[0].w[neurons] drawFeatures(clean_weights, AUTO_CLEAN_IMG_DIR) noisy_weights = auto_noisy.layers[0].w[neurons] drawFeatures(noisy_weights, AUTO_NOISY_IMG_DIR)
d.reshape(28, 28, order='F'), cmap='Greys_r') pred = autoencoder.predict(d, one_hot=False) p_name = dir_name.joinpath(f'pred_{i}.png') matplotlib.image.imsave(str(p_name), pred.reshape(28, 28, order='F'), cmap='Greys_r') # Seed for consistency np.random.seed(SEED) # Load best weights back up autoencoder = Autoencoder(input_size=INPUTS) weight_files = sorted(AUTO_MODEL_DIR.iterdir()) for weight_file in weight_files[:-1]: autoencoder.addLayer(file_name=weight_file, output=False) autoencoder.addLayer(file_name=weight_files[-1], output=True) # Test on all data and draw samples test_err = autoencoder.eval(test_data, test_data) print(f'Test loss: {test_err:0.3f}') sample_title = 'Autoencoder Sample Outputs' drawSamples(autoencoder, test_data, 8, AUTO_SAMPLE_DIR, sample_title) # Graph loss by class print('Testing train set') train_loss = getLossByClass(autoencoder, train_data, train_labels) print('Testing test set') test_loss = getLossByClass(autoencoder, test_data, test_labels) x = np.arange(len(train_loss)) plt.figure() rect_width = 0.35 plt.bar(x - rect_width / 2, train_loss, rect_width, label='Train')
import numpy as np import matplotlib.pyplot as plt import pathlib import csv if __name__ == '__main__': # Seed for consistency np.random.seed(SEED) # Delete old model for f in AUTO_NOISY_MODEL_DIR.iterdir(): f.unlink() # Training constants # Test network autoencoder = Autoencoder(input_size=INPUTS) for h in HIDDEN_LAYER_SIZES: autoencoder.addLayer(neurons=h, output=False, trainable=True) autoencoder.addLayer(neurons=INPUTS, output=True, trainable=True) # Train the network print('* * * Begin training autoencoder * * *') autoencoder.train( noisy_train_data, train_data, points_per_epoch=AUTO_NOISY_POINTS_PER_EPOCH, valid_points=AUTO_NOISY_VALID_POINTS, max_epochs=AUTO_NOISY_MAX_EPOCHS, eta=AUTO_NOISY_ETA, alpha=AUTO_NOISY_ALPHA, decay=AUTO_NOISY_DECAY, L=AUTO_NOISY_L, H=AUTO_NOISY_H, patience=AUTO_NOISY_PATIENCE,
dir_name : str Name of the directory to save the images to """ for i, w in enumerate(weights): # Remove bias and normalize on [0, 1] w = w[1:] w -= w.min() w /= (w.max() - w.min()) w_name = dir_name.joinpath(f'feat_{i:02d}.png') matplotlib.image.imsave(str(w_name), w.reshape(28, 28, order='F'), cmap='Greys_r') # Seed for consistency np.random.seed(SEED) # Load best weights back up for each model autoencoder = Autoencoder(input_size=INPUTS) for weight_file in sorted(AUTO_MODEL_DIR.iterdir()): autoencoder.addLayer(file_name=weight_file) classifier = Classifier(input_size=INPUTS) for weight_file in sorted(CLASS_MODEL_DIR.iterdir()): classifier.addLayer(file_name=weight_file) # Neurons to check neuron_count = classifier.layers[0].num_neurons neurons = np.random.choice(np.arange(neuron_count), 20, replace=False) class_weights = classifier.layers[0].w[neurons] drawFeatures(class_weights, CLASS_FEAT_DIR) auto_weights = autoencoder.layers[0].w[neurons] drawFeatures(auto_weights, AUTO_FEAT_DIR)