def get_brain_model_JointDBNConfig(shape, data_manager): config = DBN.DBNConfig(data_manager=data_manager) # Layer 1 bottom_tr = TrainParam(learning_rate=0.001, momentum_type=NESTEROV, momentum=0.5, weight_decay=0.00001, sparsity_constraint=True, sparsity_target=0.1, sparsity_decay=0.9, sparsity_cost=0.1, dropout=True, dropout_rate=0.5, epochs=2) h_n = 300 bottom_logger = ProgressLogger(img_shape=(shape * 2, shape)) bottom_rbm = RBMConfig(v_n=(shape ** 2) * 2, h_n=h_n, progress_logger=bottom_logger, train_params=bottom_tr) # Layer 2 rest_tr = TrainParam(learning_rate=0.0001, momentum_type=NESTEROV, momentum=0.5, weight_decay=0.0001, dropout=True, dropout_rate=0.8, epochs=10, batch_size=20) rest_logger = ProgressLogger() rest_rbm = RBMConfig(v_n=250, h_n=250, progress_logger=rest_logger, train_params=rest_tr) # Layer 3 top_tr = TrainParam(learning_rate=0.0001, momentum_type=NESTEROV, momentum=0.0, weight_decay=0.0000, dropout=False, dropout_rate=0.8, epochs=10, batch_size=5) top_h_n = 100 top_rbm = RBMConfig(v_n=h_n, h_n=top_h_n, progress_logger=rest_logger, train_params=rest_tr) config.rbm_configs = [bottom_rbm, top_rbm] # , rest_rbm] config.topology = [(shape ** 2) * 2, h_n, top_h_n] # , 250] return config
def __init__(self, config, data_manager=None): # Set parameters / Assertion config.left_dbn.data_manager = data_manager config.right_dbn.data_manager = data_manager top_rbm_config = config.top_rbm top_rbm_config.h_n = config.n_association v_n = config.left_dbn.topology[-1] v2_n = config.right_dbn.topology[-1] if config.opt_top: top_rbm_config.v_n = v_n + v2_n else: top_rbm_config.v_n = config.left_dbn.topology[-1] top_rbm_config.v2_n = config.right_dbn.topology[-1] config.top_rbm = top_rbm_config self.config = config self.opt_top = config.opt_top self.data_manager = data_manager self.dbn_left = DBN(config.left_dbn) self.dbn_right = DBN(config.right_dbn) if not config.reuse_dbn else self.dbn_left print '... initialising association layer' self.association_layer = RBM(config=config.top_rbm)
def get_dbn_config(shape, data_manager, n_hidden=500, lr=0.01, epochs=10, l=2): # Initialise RBM parameters base_tr = TrainParam(learning_rate=0.0001, momentum_type=NESTEROV, momentum=0.5, weight_decay=0.0001, sparsity_constraint=True, sparsity_decay=0.9, sparsity_cost=0.01, sparsity_target=0.1, dropout=True, dropout_rate=0.5, batch_size=10, epochs=epochs) rest_tr = TrainParam( learning_rate=lr, momentum_type=NESTEROV, momentum=0.5, weight_decay=0.0001, sparsity_constraint=True, sparsity_target=0.1, sparsity_decay=0.9, sparsity_cost=0.01, dropout=True, dropout_rate=0.5, batch_size=10, epochs=epochs, ) top_tr = TrainParam( learning_rate=lr, momentum_type=NESTEROV, momentum=0.5, weight_decay=0.0001, sparsity_constraint=True, sparsity_target=0.1, sparsity_decay=0.9, sparsity_cost=0.01, dropout=True, dropout_rate=0.5, batch_size=10, epochs=epochs, ) topology = [(shape**2), n_hidden, n_hidden, n_hidden] # batch_size = 10 first_progress_logger = ProgressLogger(img_shape=(shape, shape), monitor_weights=False) rest_progress_logger = ProgressLogger(monitor_weights=False) first_rbm_config = RBMConfig(train_params=base_tr, progress_logger=first_progress_logger) first_rbm_config.v_unit = rbm_units.GaussianVisibleUnit rest_rbm_config = RBMConfig(train_params=rest_tr, progress_logger=rest_progress_logger) top_rbm_config = RBMConfig(train_params=top_tr, progress_logger=rest_progress_logger) rbm_configs = [first_rbm_config, rest_rbm_config, top_rbm_config] topology = topology[:(l + 1)] rbm_configs = rbm_configs[:l] config = DBN.DBNConfig(topology=topology, training_parameters=base_tr, rbm_configs=rbm_configs, data_manager=data_manager) return config
def assess_dbn(clf, noisy_data, noisy_label, noisy_levels, tr_x, manager, postfix=''): f_score = open('dbn3_report{}.txt'.format(postfix), 'a') f_metric = open('dbn3_metric{}.txt'.format(postfix), 'a') epochs = 4 # Initialise architecture pred_table = {} for l in xrange(len(noisy_levels) * 2): pred_table[l] = [] for i in xrange(25): # Train architecture config = get_dbn_config(25, data_manager=manager, n_hidden=500, epochs=epochs, l=2) new_epochs = epochs + i * epochs config.rbm_configs[1].train_params.epochs = new_epochs model = DBN.DBN(config) # model.pretrain(tr_x, # cache=['epoch{}'.format(new_epochs - epochs), False, False], # train_further=[True, True, True], names=['epoch{}'.format(new_epochs)]*3) model.pretrain(tr_x, cache=[ 'epoch{}'.format(new_epochs), 'epoch{}'.format(new_epochs), False ], train_further=[False, False, True], names=['epoch{}'.format(new_epochs)] * 3) j = 0 for xs, ys in zip(noisy_data, noisy_label): recon_xs = model.reconstruct(xs, img_name='test_dbn') pred, metric = clf.get_score(recon_xs, ys, True) print pred print metric f_metric.write('{}25_25, Epoch:{}\n'.format(noisy_levels[j], i)) f_metric.write(metric) pred_table[j].append(pred) j += 1 model.fine_tune(tr_x, epochs=1) for xs, ys in zip(noisy_data, noisy_label): recon_xs = model.reconstruct(xs, img_name='test_dbn') pred, metric = clf.get_score(recon_xs, ys, True) print pred print metric f_metric.write('[FT] {}25_25, Epoch:{}\n'.format( noisy_levels[j % len(noisy_levels)], i)) f_metric.write(metric) pred_table[j].append(pred) j += 1 for k in pred_table: f_score.write('{}:{}\n'.format(noisy_levels[k % len(noisy_levels)], pred_table[k])) f_score.close() f_metric.close()
def get_brain_model_DBN(shape, data_manager): # Initialise RBM parameters base_tr = TrainParam(learning_rate=0.0001, momentum_type=NESTEROV, momentum=0.9, weight_decay=0.0001, sparsity_constraint=False, sparsity_decay=0.9, sparsity_cost=10, sparsity_target=0.01, dropout=True, dropout_rate=0.5, batch_size=10, epochs=5) rest_tr = TrainParam(learning_rate=0.1, momentum_type=CLASSICAL, momentum=0.5, weight_decay=0.0001, batch_size=10, epochs=5, dropout=True, dropout_rate=0.5) top_tr = TrainParam(learning_rate=0.1, momentum_type=NESTEROV, momentum=0.5, weight_decay=0.0001, sparsity_constraint=False, sparsity_target=0.01, sparsity_decay=0.9, sparsity_cost=1, batch_size=10, epochs=5, dropout=True, dropout_rate=0.5) # Layer 1 # Layer 2 # Layer 3 topology = [2 * (shape**2), 500, 500, 1000] # batch_size = 10 first_progress_logger = ProgressLogger(img_shape=(shape * 2, shape)) rest_progress_logger = ProgressLogger() first_rbm_config = RBMConfig(train_params=base_tr, progress_logger=first_progress_logger) first_rbm_config.v_unit = rbm_units.GaussianVisibleUnit rest_rbm_config = RBMConfig(train_params=rest_tr, progress_logger=rest_progress_logger) top_rbm_config = RBMConfig(train_params=top_tr, progress_logger=rest_progress_logger) rbm_configs = [first_rbm_config, rest_rbm_config, top_rbm_config] config = DBN.DBNConfig(topology=topology, training_parameters=base_tr, rbm_configs=rbm_configs, data_manager=data_manager) return config
def experiment_dbn(project_name, mapping, shape): # Project set up data_manager = StorageManager(project_name, log=True) f = open(project_name + '.txt', mode='a') f.write(project_name) f.write('%d' % shape) f.write('\n') dataset_name = 'sharp_equi{}_{}'.format(shape, shape) preprocesssing = {'scale': True} # Get dataset happy_set = kanade_loader.load_kanade(set_name=dataset_name, emotions=mapping.keys(), pre=preprocesssing, n=100) h_tr, h_vl, h_te = happy_set h_tr_x, h_tr_y = h_tr h_vl_x, h_vl_y = h_vl h_te_x, h_te_y = h_te # Sample Parent emotion p_tr_x, p_tr_y = kanade_loader.sample_image2(h_tr_y, mapping=mapping, pre=preprocesssing, set_name=dataset_name) concat1 = theano.function([], T.concatenate([h_tr_x, p_tr_x], axis=1))() tr_x = theano.shared(concat1, name='tr_x') # initial_y = np.zeros(h_te_x.get_value(True).shape) initial_y = np.random.normal(0, 1, h_te_x.get_value(True).shape) initial_y = theano.shared(initial_y, name='initial_y') te_x = theano.shared( theano.function([], T.concatenate([h_te_x, initial_y], axis=1))().astype(t_float_x)) configs = [] jj = 0 for lr1 in [0.0001]: #, 0.001, 0.01]: for h_n1 in [500]: for h_n2 in [100, 250, 500]: for h_n3 in [100, 250, 500]: config = get_brain_model_DBN(shape, data_manager=StorageManager( '{}/{}'.format( project_name, jj), log=False)) config.rbm_configs[0].h_n = h_n1 config.rbm_configs[1].v_n = h_n1 config.rbm_configs[1].h_n = h_n2 config.rbm_configs[2].v_n = h_n2 config.rbm_configs[2].h_n = h_n3 config.topology = [25 * 25 * 2, h_n1, h_n2, h_n3] config.rbm_configs[1].train_params.learning_rate = lr1 config.rbm_configs[2].train_params.learning_rate = lr1 configs.append(config) jj += 1 for epoch in xrange(10): for i, config in enumerate(configs): brain_c = DBN.DBN(config) brain_c.pretrain(tr_x, cache=[True, True, True], train_further=[True, True, True]) recon_pair = brain_c.reconstruct(tr_x, k=1, plot_n=100, img_name='{}_{}_recon_{}'.format( i, epoch, shape)) recon_p_tr_x = recon_pair[:, (shape**2):] recon_pair = brain_c.reconstruct( te_x, k=1, plot_n=100, img_name='{}_{}_single_recon_{}'.format(i, epoch, shape)) recon = recon_pair[:, (shape**2):] labels, proportion = evaluate(p_tr_y, recon, recon_p_tr_x) # write_evaluation(f, labels, proportion) errors = {} for emo in xrange(len(kanade_loader.emotion_dict)): errors[emo] = [proportion[emo]] for j in xrange(3): brain_c.fine_tune(tr_x, epochs=1) recon_pair = brain_c.reconstruct( tr_x, k=1, plot_n=100, img_name='{}_{}_recon_ft_{}'.format(i, epoch, shape)) recon_p_tr_x = recon_pair[:, (shape**2):] recon_pair = brain_c.reconstruct( te_x, k=1, plot_n=100, img_name='{}_{}_single_recon_ft_{}'.format( i, epoch, shape)) recon = recon_pair[:, (shape**2):] labels, proportion = evaluate(p_tr_y, recon, recon_p_tr_x) for k, l in enumerate(labels): errors[k].append(proportion[k]) print errors f.write('{}, {}\n'.format(i, brain_c)) for emo in errors: f.write('{}:'.format(kanade_loader.emotion_rev_dict[emo + 1])) for v in errors[emo]: f.write('%.2f,' % v) f.write('\n') f.write('\n') f.write('\n') f.close() data_manager.finish()
def associate_data2dataJDBN(cache=False, train_further=False): print "Testing Associative RBM which tries to learn even-oddness of numbers" f = open('errors.txt', 'w') # project set-up proj_name = 'JDBN_digits' data_manager = store.StorageManager(proj_name, log=False) shape = 28 train_n = 10000 test_n = 1000 # Load mnist hand digits, class label is already set to binary dataset = m_loader.load_digits(n=[train_n, 0, test_n], digits=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], pre={'binary_label': True}) tr_x, tr_y = dataset[0] te_x, te_y = dataset[2] tr_x01 = m_loader.sample_image(tr_y) te_x01 = m_loader.sample_image(te_y) ones = m_loader.load_digits(n=[test_n, 0, 0], digits=[1])[0][0] zeroes = m_loader.load_digits(n=[test_n, 0, 0], digits=[0])[0][0] tr_X = theano.shared(np.concatenate([tr_x.get_value(), tr_x01.get_value()], axis=1)) initial_y = np.random.binomial(n=1, p=0.0, size=te_x.get_value(True).shape).astype(t_float_x) initial_y_uni = np.random.uniform(low=0, high=1, size=te_x.get_value(True).shape).astype(t_float_x) initial_y_bi001 = np.random.binomial(n=1, p=0.01, size=te_x.get_value(True).shape).astype(t_float_x) initial_y_bi01 = np.random.binomial(n=1, p=0.1, size=te_x.get_value(True).shape).astype(t_float_x) te_X = theano.shared(np.concatenate([te_x.get_value(), initial_y], axis=1)) te_X2 = theano.shared(np.concatenate([te_x.get_value(), initial_y_uni], axis=1)) te_X3 = theano.shared(np.concatenate([te_x.get_value(), initial_y_bi01], axis=1)) te_X4 = theano.shared(np.concatenate([te_x.get_value(), initial_y_bi001], axis=1)) clf = SimpleClassifier('logistic', te_x01.get_value(), te_y.eval()) configs = [] for h_n in [100, 250, 500]: config1 = get_brain_model_JointDBNConfig(shape, data_manager) config1.topology = [784 * 2, h_n, h_n] config1.rbm_configs[0].h_n = h_n config1.rbm_configs[1].v_n = h_n config1.rbm_configs[1].h_n = h_n configs.append(config1) for h_n in [100, 250, 500]: config1 = get_brain_model_JointDBNConfig(shape, data_manager) config1.topology = [784 * 2, h_n, h_n * 2] config1.rbm_configs[0].h_n = h_n config1.rbm_configs[1].v_n = h_n config1.rbm_configs[1].h_n = h_n * 2 configs.append(config1) for h_n in [100, 250, 500]: config1 = get_brain_model_JointDBNConfig(shape, data_manager) config1.topology = [784 * 2, h_n * 2, h_n] config1.rbm_configs[0].h_n = h_n * 2 config1.rbm_configs[1].v_n = h_n * 2 config1.rbm_configs[1].h_n = h_n configs.append(config1) for a in xrange(10): for config in configs: f.write('{}:{}:'.format(a, config.topology)) brain_c = DBN.DBN(config=config) brain_c.pretrain(tr_X, cache=[True, True], train_further=[True, True]) recon_x = brain_c.reconstruct(te_X, k=1, plot_n=100, img_name='{}_{}_recon'.format(a, config.topology)) recon = recon_x[:, 784:] error = clf.get_score(recon, te_y.eval()) print error f.write('{}, '.format(error)) for i in xrange(0, 5): brain_c.fine_tune(tr_X) recon_x = brain_c.reconstruct(te_X, k=1, plot_n=100, img_name=('{}_{}_recon_fine_tune{}'.format(a, config.topology, i))) recon = recon_x[:, 784:] error = clf.get_score(recon, te_y.eval()) print error f.write('{}, '.format(error)) recon_x = brain_c.reconstruct(te_X4, k=1, plot_n=100, img_name=('{}_{}_recon_fine_tune_2_{}'.format(a, config.topology, i))) recon = recon_x[:, 784:] error = clf.get_score(recon, te_y.eval()) print error f.write('{}, '.format(error)) f.write('\n') f.close()
class AssociativeDBN(object): def __init__(self, config, data_manager=None): # Set parameters / Assertion config.left_dbn.data_manager = data_manager config.right_dbn.data_manager = data_manager top_rbm_config = config.top_rbm top_rbm_config.h_n = config.n_association v_n = config.left_dbn.topology[-1] v2_n = config.right_dbn.topology[-1] if config.opt_top: top_rbm_config.v_n = v_n + v2_n else: top_rbm_config.v_n = config.left_dbn.topology[-1] top_rbm_config.v2_n = config.right_dbn.topology[-1] config.top_rbm = top_rbm_config self.config = config self.opt_top = config.opt_top self.data_manager = data_manager self.dbn_left = DBN(config.left_dbn) self.dbn_right = DBN(config.right_dbn) if not config.reuse_dbn else self.dbn_left print '... initialising association layer' self.association_layer = RBM(config=config.top_rbm) def __str__(self): return 'l{}_r{}_t{}'.format(self.dbn_left, self.dbn_right, self.association_layer.h_n) def train(self, x1, x2, cache=False, train_further=False): cache_left = cache_right = cache_top = cache if type(cache) is bool else False train_further_left = train_further_right = train_further_top = train_further if type( train_further) is bool else False if type(cache) is list: cache_left = cache[0] cache_right = cache[1] cache_top = cache[2] if type(train_further) is list: train_further_left = train_further[0] train_further_right = train_further[1] train_further_top = train_further[2] # Train left & right DBN's self.dbn_left.pretrain(x1, cache=cache_left, train_further=train_further_left) if self.config.reuse_dbn: self.dbn_right = self.dbn_left else: self.dbn_right.pretrain(x2, cache=cache_right, train_further=train_further_right) # Pass the parameter to top layer x1_np = self.dbn_left.bottom_up_pass(x1.get_value(True)) x2_np = self.dbn_right.bottom_up_pass(x2.get_value(True)) x1_features = theano.shared(x1_np) x2_features = theano.shared(x2_np) # Train top association layer top = self.association_layer tr = top.train_parameters # Check Cache out_dir = 'association_layer/{}_{}/'.format(len(self.dbn_left.rbm_layers), len(self.dbn_right.rbm_layers)) load = self.data_manager.retrieve('{}_{}'.format(self.opt_top, top), out_dir=out_dir) if load and cache_top: self.association_layer = load print '... top layer RBM loaded' if not load and tr.sparsity_constraint: top.set_initial_hidden_bias() if self.opt_top: # Concatenate images x = theano.shared(np.concatenate((x1_np, x2_np), axis=1)) top.set_hidden_mean_activity(x) else: top.set_hidden_mean_activity(x1_features, x2_features) if not load or train_further_top: if self.opt_top: # Concatenate images x = theano.shared(np.concatenate((x1_np, x2_np), axis=1)) top.train(x) else: top.train(x1_features, x2_features) self.data_manager.persist(top, '{}_{}'.format(self.opt_top, top), out_dir=out_dir) def recall(self, x, associate_steps=10, recall_steps=5, img_name='dbn', y=None, y_type='sample_active_h'): ''' left dbn bottom-up -> associate -> right dbn top-down :param x: data :param associate_steps: top level gibbs sampling steps :param recall_steps: right dbn sampling :return: ''' self.data_manager.move_to('reconstruct') if self.data_manager.log: print '... moved to {}'.format(os.getcwd()) left = self.dbn_left top = self.association_layer right = self.dbn_right if utils.isSharedType(x): x = x.get_value(borrow=True) # Pass to association layer top_out = left.bottom_up_pass(x) assoc_in = theano.shared(top_out, 'top_in', allow_downcast=True) # Sample from the association layer # associate_x = top.reconstruct_association(assoc_in, k=associate_steps) if self.opt_top: # Initialise y according to the neuron distribution if type(top.v_unit) is GaussianVisibleUnit: # TODO print 'GAUSSIAN INPUT IS NOT SUPPORTED' top_shape = top_out.shape[0] right_top_rbm = right.rbm_layers[-1] shape = (top_shape, right_top_rbm.h_n) y_base = np.zeros(shape).astype(t_float_x) p = right_top_rbm.active_probability_h.get_value(borrow=True) if y_type == 'sample_active_h' or type(y) is None: print 'initialise reconstruction by active_h' # import matplotlib.pyplot as plt # plt.plot(p) # plt.show() y_base = right_top_rbm.np_rand.binomial(size=shape, n=1, p=p).astype(t_float_x) if y_type == 'active_h': y_base = np.tile(p, (shape[0], 1)).astype(t_float_x) if y_type == 'v_noisy_active_h': y_base = right_top_rbm.np_rand.normal(loc=0, scale=0.2, size=shape) + np.tile(p, (shape[0], 1)) y_base = y_base.astype(t_float_x) if y_type == 'noisy_active_h': y_base = right_top_rbm.np_rand.normal(loc=0, scale=0.1, size=shape) + np.tile(p, (shape[0], 1)) y_base = y_base.astype(t_float_x) if 'binomial' in y_type: p = float(y_type.strip('binomial')) y_base = right_top_rbm.np_rand.binomial(size=shape, n=1, p=p).astype(t_float_x) y = theano.shared(y_base, name='assoc_y') associate_x = top.mean_field_inference_opt(assoc_in, y=y, sample=True, k=associate_steps) else: associate_x = top.mean_field_inference(assoc_in, sample=True, k=associate_steps) # associate_x = top.reconstruct_association(assoc_in, k=associate_steps) if recall_steps > 0: top_in = theano.shared(associate_x, 'associate_x', allow_downcast=True) # Allow right dbn to day dream by extracting top layer rbm right_top_rbm = right.rbm_layers[-1] ass, ass_p, ass_s = right_top_rbm.sample_v_given_h(top_in) associate_x_in = theano.function([], ass_s)() associate_x_reconstruct = right_top_rbm.reconstruct(associate_x_in, k=recall_steps, img_name='recall') # pass down to visible units, take the penultimate layer because we sampled at the top layer if len(right.rbm_layers) > 1: res = right.top_down_pass(associate_x_reconstruct, start=len(right.rbm_layers) - 1) else: res = associate_x_reconstruct # res = result.get_value(borrow=True) else: res = right.top_down_pass(associate_x.astype(t_float_x)) n = res.shape[0] img_shape = right.rbm_layers[0].track_progress.img_shape save_images(x, img_name + '_orig.png', shape=(n / 10, 10), img_shape=img_shape) save_images(res, img_name + '_recon.png', shape=(n / 10, 10), img_shape=img_shape) self.data_manager.move_to_project_root() return res def fine_tune_cd(self, wake_state): # CONTRASTIVE DIVERGENCE AT TOP LAYER rbm = self.association_layer pen_state = wake_state [updates, chain_end, _, _, _, _, _, _, _] = rbm.negative_statistics(pen_state) cost = T.mean(rbm.free_energy(pen_state)) - T.mean(rbm.free_energy(chain_end)) grads = T.grad(cost, rbm.params, consider_constant=[chain_end]) lr = rbm.train_parameters.learning_rate for (p, g) in zip(rbm.params, grads): # TODO all the special updates like momentum updates[p] = p - lr * g return chain_end, updates def get_fine_tune_updates(self, xs, ys, batch_size=10): # WAKE-PHASE [hid_prob, pen_prob, ...], [hid_state, pen-state,...] wake_probs_l, wake_states_l = self.dbn_left.wake_phase(xs) wake_probs_r, wake_states_r = self.dbn_right.wake_phase(ys) # TOP LAYER CD wake_state = T.concatenate([wake_states_l[-1], wake_states_r[-1]], axis=1) chain_end, updates = self.fine_tune_cd(wake_state) left_end = self.dbn_left.topology[-1] chain_end_l, chain_end_r = chain_end[:, :left_end], chain_end[:, left_end:] # SLEEP PHASE: [hid_prob, vis_prob], [pen_state, hid_state, vis_state] ... sleep_probs_l, sleep_states_l = self.dbn_left.sleep_phase(chain_end_l) sleep_probs_r, sleep_states_r = self.dbn_right.sleep_phase(chain_end_r) # Prediction psleep_states_l, pwake_states_l, sleep_states_l, wake_states_l = self.dbn_left.get_predictions(xs, sleep_probs_l, sleep_states_l, wake_states_l) psleep_states_r, pwake_states_r, sleep_states_r, wake_states_r = self.dbn_right.get_predictions(ys, sleep_probs_r, sleep_states_r, wake_states_r) # UPDATES TO GENERATIVE PARAMETERS updates = self.dbn_left.update_generative_weights(batch_size, pwake_states_l, wake_states_l, updates) updates = self.dbn_right.update_generative_weights(batch_size, pwake_states_r, wake_states_r, updates) # UPDATES TO INFERENCE PARAMETERS updates = self.dbn_left.update_inference_weights(batch_size, psleep_states_l, sleep_states_l, updates) updates = self.dbn_right.update_inference_weights(batch_size, psleep_states_r, sleep_states_r, updates) return updates def fine_tune(self, data_r, data_l, epochs=10, batch_size=10): if not self.dbn_right.untied: self.dbn_left.untie_weights(include_top=True) self.dbn_right.untie_weights(include_top=True) mini_batches = data_r.get_value(borrow=True).shape[0] / batch_size i = T.iscalar() x = T.matrix('x') y = T.matrix('y') updates = self.get_fine_tune_updates(x, y, batch_size) fine_tune = theano.function([i], [], updates=updates, givens={ x: data_r[i * batch_size: (i + 1) * batch_size], y: data_l[i * batch_size: (i + 1) * batch_size] }) for epoch in xrange(epochs): print '... epoch %d' % epoch start_time = time.clock() for mini_batche_i in xrange(mini_batches): fine_tune(mini_batche_i) end_time = time.clock() print ('... fine tuning took %f minutes' % ((end_time - start_time) / 60))
def KanadeJointDBN(cache=False): print "Testing JointDBN which tries to learn id map association" # project set-up data_manager = store.StorageManager('Kanade/JointDBN', log=True) shape = 25 dataset_name = 'sharp_equi{}_{}'.format(shape, shape) preprocessing = {'scale': True} # Load kanade database mapping = None # mapping = {'anger': 'sadness', # 'contempt': 'happy', # 'disgust': 'sadness', # 'fear': 'sadness', # 'happy': 'happy', # 'sadness': 'sadness', # 'surprise': 'happy'} dataset = loader.load_kanade( # n=3000, pre=preprocessing, set_name=dataset_name) mapped_dataset = loader.load_kanade( # n=3000, # emotions=['sadness', 'happy'], pre=preprocessing, set_name=dataset_name) # Target Image train, valid, test = dataset train_x, train_y = train test_x, test_y = test # Sample associated image train_x_ass, train_y_ass = loader.sample_image(train_y, mapping=mapping, pre=preprocessing, set_name=dataset_name) test_x_ass, test_y_ass = loader.sample_image(test_y, mapping=mapping, pre=preprocessing, set_name=dataset_name) # Initialise RBM parameters base_tr = rbm_config.TrainParam(learning_rate=0.0001, momentum_type=rbm_config.NESTEROV, momentum=0.9, weight_decay=0.0001, sparsity_constraint=False, sparsity_target=0.00001, sparsity_decay=0.9, sparsity_cost=10000, epochs=100, batch_size=10) rest_tr = rbm_config.TrainParam(learning_rate=0.0001, momentum_type=rbm_config.CLASSICAL, momentum=0.5, weight_decay=0.01, epochs=100, batch_size=10) # Layer 1 # Layer 2 # Layer 3 topology = [2 * (shape**2), 100, 100] # batch_size = 10 first_progress_logger = rbm_logger.ProgressLogger(img_shape=(shape * 2, shape)) rest_progress_logger = rbm_logger.ProgressLogger() first_rbm_config = rbm_config.RBMConfig( train_params=base_tr, progress_logger=first_progress_logger) first_rbm_config.v_unit = rbm_units.GaussianVisibleUnit rest_rbm_config = rbm_config.RBMConfig( train_params=rest_tr, progress_logger=rest_progress_logger) rbm_configs = [first_rbm_config, rest_rbm_config, rest_rbm_config] config = DBN.DBNConfig(topology=topology, training_parameters=base_tr, rbm_configs=rbm_configs, data_manager=data_manager) # construct the Deep Belief Network dbn = DBN.DBN(config) # Train DBN on concatenated images train_tX = theano.function([], T.concatenate([train_x, train_x_ass], axis=1))() train_X = theano.shared(train_tX) test_tX = theano.function([], T.concatenate([test_x, test_x_ass], axis=1))() test_X = theano.shared(test_tX) test_tX2 = theano.function([], T.concatenate( [test_x, T.zeros_like(test_x)], axis=1))() test_X2 = theano.shared(test_tX2) origs = [] recons = [] recons2 = [] # Train DBN dbn.pretrain(train_X, cache=[True, True, False], train_further=[True, True, True]) recon = dbn.reconstruct(train_X, k=1, plot_n=20, img_name='stackedRBM_train_recon_{}_{}'.format( topology, 0)) train_x_ass_recon = recon[:, shape**2:] recon = dbn.reconstruct(test_X, k=1, plot_n=20, img_name='stackedRBM_test_recon_{}_{}'.format( topology, 0)) test_x_ass_recon = recon[:, shape**2:] recon = dbn.reconstruct(test_X2, k=2, plot_n=20, img_name='stackedRBM_test_zero_recon_{}_{}'.format( topology, 0)) test_x_ass_recon2 = recon[:, shape**2:] clf_recon = SimpleClassifier('logistic', train_x, train_y) score_orig = clf_recon.get_score(test_x_ass_recon, test_y_ass.eval()) clf_recon.retrain(train_x_ass_recon, train_y_ass.eval()) score_recon = clf_recon.get_score(test_x_ass_recon, test_y_ass.eval()) score_recon2 = clf_recon.get_score(test_x_ass_recon2, test_y_ass.eval()) print 'classification rate: {}, {}, {}'.format(score_orig, score_recon, score_recon2) origs.append(score_orig) recons.append(score_recon) recons2.append(score_recon2)