def objective(args): n_epochs = 10 rho = 0.01 lamb = 5 noise_std = 0.2 hidshape = (50, 50) rflen, rate, clip = args rfshape = (rflen, rflen) clip = (-clip, clip) layer = SparseAutoencoder( visshape=imshape, hidshape=hidshape, rfshape=rfshape, f=noisylif, g=linear) train_params = {'rho': rho, 'lamb': lamb, 'noise_std': noise_std} trainer = SparseTrainer(layer, **train_params) stats = sgd(trainer, patches, n_epochs=n_epochs, rate=rate, clip=clip, show=False) cost = stats['cost'][-1] filename = 'results/layer_opt_vh_cost=%0.3e.npz' % cost layer.to_file(filename) return cost
std0 = patches.std() mean, std = p.mean(axis=(1,2)), p.std(axis=(1,2)) return ((p - mean[:,None,None]) / np.maximum(std, 0.01*std0)[:,None,None]) patches = normalize(patches) patches = patches.clip(-3, 3) ################################################################################ ### train one layer filename = 'results/vh_layer.npz' if 'filename' not in locals() or not os.path.exists(filename): linear = Linear(slope=1.0) noisylif = NoisyLIFApprox( tRef=0.02, tauRC=0.06, alpha=10.0, xint=-0.5, amp=1./41, sigma=0.05) layer = SparseAutoencoder(visshape=imshape, hidshape=(50,50), rfshape=(11,11), f=noisylif, g=linear) # train_params = {'rho': 0.01, 'lamb': 25, 'noise_std': 0.2} # train_params = {'rho': 0.01, 'lamb': 5, 'noise_std': 0.2} train_params = {'rho': 0.05, 'lamb': 5, 'noise_std': 0.2} trainer = SparseTrainer(layer, **train_params) plt.figure(101) raw_input("Please place the figure...") sgd(trainer, patches, n_epochs=30, rate=0.05, vlims=(-2,2)) if 'filename' in locals(): layer.to_file(filename) else:
################################################################################ ### train one layer # loadfile = None loadfile = 'mnist_layer.pkl' if loadfile is None or not os.path.exists(loadfile): linear = Linear(slope=1.0) noisylif = NoisyLIFApprox( tRef=0.02, tauRC=0.06, alpha=10.0, xint=-0.5, amp=1./41, sigma=0.05) # layer = SparseAutoencoder(visshape=imshape, hidshape=(50,50), # rfshape=(9,9), f=noisylif, g=linear) layer = SparseAutoencoder(visshape=imshape, hidshape=(40,40), rfshape=(9,9), f=noisylif, g=linear) if loadfile is not None: layer.tofile(loadfile) else: layer = deepnet.CacheObject.fromfile(loadfile) ################################################################################ train_params = {'rho': 0.01, 'lamb': 25, 'noise_std': 0.2} trainer = SparseTrainer(layer, **train_params) sgd(trainer, images, nepochs=30, rate=0.05) if 0: ### untied training sgd(trainer, images, nepochs=1, rate=0.05)