def train_ae(ae, patch_shape=(20, 20), n_hidden=400, n_samples=50000, batch_size=500, epsilon=0.1, show_input=False, show_filters=False, sparsity=0.01, beta=0.0, weight_decay=0.0, corruption_rate=0.3, img_dir=None, norm_axis=0, n_epochs=5, n_reconstruct=10): """ train an autoencoder """ img_data = get_input(n_samples=n_samples, patch_shape=patch_shape, epsilon=epsilon, norm_axis=norm_axis) data = theano.shared(img_data, borrow=True) if show_input: plot_images(img_data, shape=patch_shape) n_vis = patch_shape[0]*patch_shape[1] trained = ae.train(data, n_visible=n_vis, n_hidden=n_hidden, batch_size=batch_size, sparsity=sparsity, weight_decay=weight_decay, beta=beta, corruption_rate=corruption_rate, n_epochs=n_epochs) if img_dir is not None: if ae.__name__ == "dae": opts = "_{}".format(corruption_rate) elif ae.__name__ == "sae": opts = "_{}_{}_{}".format(weight_decay, beta, sparsity) fp = os.path.join(img_dir, "ff_{}_{}_{}_{}_{}_{}_{}{}.png" .format(ae.__name__, patch_shape, n_hidden, n_samples, batch_size, epsilon, norm_axis, opts)) else: fp = None render_filters(trained.w1.get_value(borrow=True), patch_shape, show=show_filters, image_file=fp) reconstruct(trained, img_data, patch_shape, n_reconstruct) return trained
def train_ae(ae, patch_shape=(20, 20), n_hidden=400, n_samples=50000, batch_size=500, epsilon=0.1, show_input=False, show_filters=False, sparsity=0.01, beta=0.0, weight_decay=0.0, corruption_rate=0.3, img_dir=None, norm_axis=0, n_epochs=5, n_reconstruct=10): """ train an autoencoder """ img_data = get_input(n_samples=n_samples, patch_shape=patch_shape, epsilon=epsilon, norm_axis=norm_axis) data = theano.shared(img_data, borrow=True) if show_input: plot_images(img_data, shape=patch_shape) n_vis = patch_shape[0] * patch_shape[1] trained = ae.train(data, n_visible=n_vis, n_hidden=n_hidden, batch_size=batch_size, sparsity=sparsity, weight_decay=weight_decay, beta=beta, corruption_rate=corruption_rate, n_epochs=n_epochs) if img_dir is not None: if ae.__name__ == "dae": opts = "_{}".format(corruption_rate) elif ae.__name__ == "sae": opts = "_{}_{}_{}".format(weight_decay, beta, sparsity) fp = os.path.join( img_dir, "ff_{}_{}_{}_{}_{}_{}_{}{}.png".format(ae.__name__, patch_shape, n_hidden, n_samples, batch_size, epsilon, norm_axis, opts)) else: fp = None render_filters(trained.w1.get_value(borrow=True), patch_shape, show=show_filters, image_file=fp) reconstruct(trained, img_data, patch_shape, n_reconstruct) return trained
def main(): """ Train a denoising autoencoder for mnist digits using various corruption rates. Display and save an image of the learned filters. """ datasets = load_data() train_set_x = np.asarray(datasets[0][0], dtype=theano.config.floatX) test_set_x = np.asarray(datasets[2][0], dtype=theano.config.floatX) rates = [0.3] n_reconstruct = 20 for rate in rates: ae = train_dae(train_set_x, corruption_rate=rate) fn = "img/mnist_filters_{}.png".format(rate) render_filters(ae.w1.get_value(borrow=True), tile_shape, show=True, image_file=fn) reconstruct(ae, test_set_x, tile_shape, n_reconstruct)