deep = DeepAutoencoder() data = train_images for i in range(n_layers): vis_func = None if i == 0 else neuron_fn # create autoencoder for the next layer auto = Autoencoder(shapes[i], shapes[i + 1], rf_shape=rf_shapes[i], vis_func=vis_func, hid_func=neuron_fn) deep.autos.append(auto) # train the autoencoder using SGD auto.auto_sgd(data, deep, test_images, n_epochs=n_epochs, rate=rates[i]) # hidden layer activations become training data for next layer data = auto.encode(data) plt.figure(99) plt.clf() recons = deep.reconstruct(test_images) show_recons(test_images, recons) print "recons error", rms(test_images - recons, axis=1).mean() deep.auto_sgd(train_images, test_images, rate=0.3, n_epochs=30) print "recons error", rms(test_images - recons, axis=1).mean() # --- train classifier with backprop deep.train_classifier(train, test)
assert len(rf_shapes) == n_layers assert len(rates) == n_layers n_epochs = 5 batch_size = 100 deep = DeepAutoencoder() data = train_images for i in range(n_layers): savename = "sigmoid-auto-%d.npz" % i if not os.path.exists(savename): auto = Autoencoder( shapes[i], shapes[i+1], rf_shape=rf_shapes[i], vis_func=funcs[i], hid_func=funcs[i+1]) deep.autos.append(auto) auto.auto_sgd(data, deep, test_images, noise=0.1, n_epochs=n_epochs, rate=rates[i]) auto.to_file(savename) else: auto = FileObject.from_file(savename) assert type(auto) is Autoencoder deep.autos.append(auto) data = auto.encode(data) plt.figure(99) plt.clf() recons = deep.reconstruct(test_images) show_recons(test_images, recons) print "recons error", rms(test_images - recons, axis=1).mean()