def main(): dataset_file = 'mnist.npz' if not isfile(dataset_file): downloadMnist(dataset_file) data = dict(np.load(dataset_file)) data['x_tr'] = data['x_tr'] * 2.0 - 1.0 data['x_va'] = data['x_va'] * 2.0 - 1.0 data['x_te'] = data['x_te'] * 2.0 - 1.0 data['t_tr'] = data['t_tr'].astype(np.int32) data['t_va'] = data['t_va'].astype(np.int32) data['t_te'] = data['t_te'].astype(np.int32) rng = np.random.RandomState() srng = RandomStreamsGPU(rng.randint(1, 2147462579, size=(6,))) # Setup data loaders train_generator = DefaultDataLoader(data['x_tr'], data['t_tr'], 100, rng=rng) validation_generator = DefaultDataLoader(data['x_va'], data['t_va'], 100) test_generator = DefaultDataLoader(data['x_te'], data['t_te'], 100) # Load real-valued model parameters for initialization init_model_file = 'mnist_pi_model_ternary_tanh.npz' if isfile(init_model_file): initial_parameters = dict(np.load(init_model_file)) print 'Loading initial parameters from \'%s\'' % (init_model_file) print 'Parameters:', [e for e in initial_parameters] else: raise Exception('Cannot find initial model \'%s\'' % (init_model_file)) # Create model global parameters layer, parameters = getMnistPIModel(initial_parameters, rng, srng) # Do optimization print layer.getMessage() cbErrVaDecreased = lambda : cbValidationErrorDecreased() global p_vals optimizeNetwork(layer, loader_tr=train_generator, loader_va=validation_generator, loader_te=test_generator, optimization_algorithm='adam', step_size=1e-3, step_size_discrete=1e-2, step_size_scale_fn={'type' : 'plateau', 'monitor' : 'ce_va', 'cooldown' : 50, 'patience' : 10, 'factor' : 0.5}, n_epochs=500, do_bn_updates_after_epoch=True, callback_validation_error_decreased=[(cbErrVaDecreased, [])]) # Store model parameters. The model parameters of the best model according to the validation error are now in # p_vals. model_file = 'mnist_pi_model_ternary_sign_from_tanh.npz' print 'Optimization finished. Storing model parameters to ''%s''' % model_file np.savez_compressed(model_file, **p_vals)
def main(): dataset_file = 'mnist.npz' if not isfile(dataset_file): downloadMnist(dataset_file) data = dict(np.load(dataset_file)) data['x_tr'] = data['x_tr'] * 2.0 - 1.0 data['x_va'] = data['x_va'] * 2.0 - 1.0 data['x_te'] = data['x_te'] * 2.0 - 1.0 data['t_tr'] = data['t_tr'].astype(np.int32) data['t_va'] = data['t_va'].astype(np.int32) data['t_te'] = data['t_te'].astype(np.int32) rng = np.random.RandomState() srng = RandomStreamsGPU(rng.randint(1, 2147462579, size=(6, ))) # Setup data loaders train_generator = DefaultDataLoader(data['x_tr'], data['t_tr'], 100, rng=rng) validation_generator = DefaultDataLoader(data['x_va'], data['t_va'], 100) test_generator = DefaultDataLoader(data['x_te'], data['t_te'], 100) # Create model global parameters layer, parameters = getMnistPIModel(rng, srng) # Do optimization print layer.getMessage() cbErrVaDecreased = lambda: cbValidationErrorDecreased() global p_vals optimizeNetwork(layer, loader_tr=train_generator, loader_va=validation_generator, loader_te=test_generator, optimization_algorithm='adam', step_size=1e-3, step_size_scale_fn={ 'type': 'plateau', 'monitor': 'ce_va', 'cooldown': 150, 'patience': 25, 'factor': 0.5 }, n_epochs=1000, callback_validation_error_decreased=[(cbErrVaDecreased, []) ]) # Store model parameters. The model parameters of the best model according to the validation error are now in # p_vals. model_file = 'mnist_pi_model_real.npz' print 'Optimization finished. Storing model parameters to ' '%s' '' % model_file np.savez_compressed(model_file, **p_vals)
def main(): dataset_file = 'cifar100.npz' if not isfile(dataset_file): downloadCifar100(dataset_file) data = dict(np.load(dataset_file)) data['x_tr'] = ((data['x_tr'] / 255.0 * 2.0) - 1.0).astype( np.float32).reshape(-1, 3, 32, 32) data['x_va'] = ((data['x_va'] / 255.0 * 2.0) - 1.0).astype( np.float32).reshape(-1, 3, 32, 32) data['x_te'] = ((data['x_te'] / 255.0 * 2.0) - 1.0).astype( np.float32).reshape(-1, 3, 32, 32) data['t_tr'] = data['t_tr'].astype(np.int32) data['t_va'] = data['t_va'].astype(np.int32) data['t_te'] = data['t_te'].astype(np.int32) rng = np.random.RandomState() srng = RandomStreamsGPU(rng.randint(1, 2147462579, size=(6, ))) # Setup data loaders train_generator = Cifar10FlipShiftDataLoader(data['x_tr'], data['t_tr'], 100, flip_axis=1, max_shift=4, requires_train=True, rng=rng) validation_generator = DefaultDataLoader(data['x_va'], data['t_va'], 100) test_generator = DefaultDataLoader(data['x_te'], data['t_te'], 100) # Create model global parameters layer, parameters = getCifar100Model(rng, srng) # Do optimization print layer.getMessage() cbErrVaDecreased = lambda: cbValidationErrorDecreased() global p_vals optimizeNetwork(layer, loader_tr=train_generator, loader_va=validation_generator, loader_te=test_generator, optimization_algorithm='adam', step_size=3e-4, step_size_scale_fn={ 'type': 'plateau', 'monitor': 'ce_va', 'cooldown': 50, 'patience': 10, 'factor': 0.5 }, n_epochs=500, callback_validation_error_decreased=[(cbErrVaDecreased, []) ]) # Store model parameters. The model parameters of the best model according to the validation error are now in # p_vals. model_file = 'cifar100_model_real.npz' print 'Optimization finished. Storing model parameters to ' '%s' '' % model_file np.savez_compressed(model_file, **p_vals)