def get_map(): """ Grabs the map data. """ updates.check_time() try: return get_json() except FileNotFoundError: updates.update() return get_json()
def remove_pointer(asker, object): result = updates.update(updates.remove_modifier(is_pointer()), object) result = updates.update(updates.remove_modifier(has_pointer()), object) result = updates.update( updates.apply_to( all_children(), lists.update_map(remove_pointer()) ), result ) return asker.reply(answer=result)
def add_pointer_to_bottom(asker, object): visible_children = asker.ask(fields.get_field(visible_children()), object).firm_answer if convert.check_hard(asker, lists.is_empty(), visible_children): return asker.reply(answer=updates.update(is_pointer_now(), object)) else: result = updates.update(has_pointer_now(), object) result = updates.update( updates.apply_to( fields.compose(visible_children(), lists.last_element()), add_pointer_to_bottom() ), result ) return asker.reply(answer=result)
def add_children_on_expanded(asker, old, new, bindings): children = [] for p in lists.iterator(asker, lists.from_dict(bindings)): k = asker.ask(fields.get_field(first(), p)).firm_answer v = asker.ask(fields.get_field(second(), p)).firm_answer prefix = strings.string_concat(k, T.from_str(": ")) new_node = node_from_term(asker, v) new_node = updates.update( updates.apply_to(headline(), strings.prepend_str(prefix)), new_node ) children.append(new_node) return asker.reply(answer=updates.update( fields.set_field(all_children(), T.from_list(children)), new ))
def set_image(asker, new_value, k, key, value, other): if booleans.ask_firmly(asker, builtins.equal(k, key)): return asker.reply(answer=cons(key=key, value=new_value, other=other)) else: return asker.reply(answer=cons(key=key, value=value, other=updates.update(updates.set_field(image(k), new_value), other) ))
def add_line(asker, view, new_line): return asker.reply(answer=updates.update( updates.apply_to_field( lines_field(), lists.append(new_line) ), view ))
def ifItHadBeenTheCase(cogstate, formula): """ INPUT: a cognitive state and a formula OUTPUT: a cognitive state WHAT IT DOES: given a formula \phi, it retracts ~\phi and then updates with \phi """ # It's so pretty! return update(retract(cogstate,proposition(cogstate,lnot(formula))), formula)
def easy_set(asker, object, new_value): #FIXME the cyclic import again, would be nice to do better import updates return asker.reply(answer=updates.update( updates.apply_to_field( modifier(), updates.set_field(implication_about(field), new_value) ), object ))
def bind_variable(asker, view, name, value): return asker.reply(answer=updates.update( updates.apply_to_field( bindings_field(), updates.set_field( dictionaries.image(name), value ) ), view ))
def checker(self, message, phrases): """Check and run if needed convert BD notes from old version to new.""" tables_dict, _ = updates.update(tables.NOTES, DEFAULT_DATA) conv = DBConverter(self.db_name) db_ver = conv.checker(self.db, tables_dict) if db_ver != tables.VERSION: self.db.disconnect() message.information(phrases.titles.info, phrases.conv.info_notes % (db_ver, tables.VERSION,)) if conv.run(tables.NOTES, tables_dict): message.information(phrases.titles.info, phrases.conv.success % (tables.VERSION,)) else: message.information(phrases.titles.error, phrases.conv.error) sys.exit() self.db.connect(self.db_name + '.db')
def main(): try: config = json.loads(open(CONFIG_FILE_PATH).read()) if config['model'] is None: config = update(config) except: print('Невозможно загрузить конфигурационный файл') return try: app = QtWidgets.QApplication(sys.argv) window = MainWindow(config=config) window.show() sys.exit(app.exec_()) except: pass
def update(params): """ Updates the repository and run necessary environment modifications. Please run this command instead of only 'git pull'. No parameters. """ # Requirements if len(params) != 0: logger.err("This command receives no parameters\n") help(["update"]) return 1 # Command execution try: # Make sure we are on master os.chdir("/Mjollnir") m = re.search(r"\* (.*)", check_output(["git", "branch"])) if not m: logger.err("Could not figure out current git branch") return 1 if m.group(1) != "master": logger.err("You are not on branch master") return 1 logger.info("Running git pull...") output = check_output(["git", "pull"]) if output == "Already up-to-date.\n": print "Already up-to-date. Nothing to do." return 0 # We must import only after having pulled, so we have the latest version import updates return updates.update(sys.modules[__name__]) except CalledProcessError as e: logger.err(str(e)) return 1 except KeyboardInterrupt as e: logger.err(repr(e)) return 1
def update_local(params): """ Stage all changes/new files and run necessary environment modifications. Run this command to build/rebuild a game and the bots of the game No parameters. """ # Requirements if len(params) != 0: logger.err("This command receives no parameters\n") help(["update"]) return 1 # Command execution try: # Make sure we are on a branch os.chdir("/Mjollnir") m = re.search(r"\* (.*)", check_output(["git", "branch"])) if not m: logger.err("Could not figure out current git branch") return 1 logger.info("All new files must be tracked") logger.info("Running git add . ...") call(["git", "add", "."]) import updates return updates.update(sys.modules[__name__], True) except CalledProcessError as e: logger.err(str(e)) return 1 except KeyboardInterrupt as e: logger.err(repr(e)) return 1
def update(self): self.config = update(self.config)
#hyper_losses = [] test_losses = [] norms = [] distances_from_final = [] #grad_norms = [] velocity_norms = [] theta_names = [param.name for param in model.params_theta] #update_lambda, fix_weight = temp_lambda, tmp_weights #update_llrs = temp_llrs dlossWithPenalty_dtheta = theano.grad(model.lossWithPenalty, model.params_theta) update_ele, update_valid, output_valid_list, share_var_dloss_dweight = update( model.params_theta, model.params_lambda, model.params_weight, velocities, model.loss, model.penalty, dlossWithPenalty_dtheta, lr_ele, mom, args.eleAlg) #update_ele, update_valid, output_valid_list, share_var_dloss_dweight = update2(model.params_theta, model.params_lambda, model.params_weight, # velocities, model.loss, model.penalty, model.lossWithPenalty, # log_learning_rates, lr_hyper, mom) #output_valid_list is used as dv_t in DrMAD func_elementary = theano.function( inputs=[x, y, lr_ele], #inputs=[x, y], outputs=[model.lossWithPenalty, model.loss, model.prediction], updates= update_ele, #Michael: update_ele will apply the SGD step when func_elementary is called on_unused_input='ignore', allow_input_downcast=True)
def run_exp(args, update_lambda, fix_weight): if args.predata is False: X_elementary, Y_elementary, X_hyper, Y_hyper, X_valid, Y_valid, X_test, Y_test = read_preprocess( params=args) np.savez(args.processedDataName, X_elementary=X_elementary, Y_elementary=Y_elementary, X_hyper=X_hyper, Y_hyper=Y_hyper, X_v=X_valid, Y_v=Y_valid, X_test=X_test, Y_test=Y_test) else: tmpload = np.load(args.processedDataName) X_elementary, Y_elementary, X_hyper, Y_hyper, X_valid, Y_valid, X_test, Y_test = \ tmpload['X_elementary'], tmpload['Y_elementary'], tmpload['X_hyper'], tmpload['Y_hyper'],\ tmpload['X_v'], tmpload['Y_v'], tmpload['X_test'], tmpload['Y_test'] """ Build Theano functions """ if args.model == 'convnet': x = T.ftensor4('x') elif args.model == 'mlp': x = T.matrix('x') else: raise AttributeError y = T.matrix('y') lr_ele = T.fscalar('lr_ele') lr_ele_true = np.array(args.lrEle, theano.config.floatX) mom = 0.95 lr_hyper = T.fscalar('lr_hyper') grad_valid_weight = T.tensor4('grad_valid_weight') if args.model == 'mlp': model = MLP(x=x, y=y, args=args) elif args.model == 'convnet': model = ConvNet(x=x, y=y, args=args) if args.dataset == 'mnist': nc = 1 nPlane = 28 else: nc = 3 nPlane = 32 X_elementary = X_elementary.reshape(-1, nc, nPlane, nPlane) X_hyper = X_hyper.reshape(-1, nc, nPlane, nPlane) X_valid = X_valid.reshape(-1, nc, nPlane, nPlane) X_test = X_test.reshape(-1, nc, nPlane, nPlane) else: raise AttributeError update_ele, update_valid, output_valid_list, share_var_dloss_dweight = update( model.params_theta, model.params_lambda, model.params_weight, model.loss, model.penalty, model.lossWithPenalty, lr_ele, lr_hyper, mom) if update_lambda: for up, origin in zip(update_lambda, model.params_lambda): origin.set_value(np.array(up)) boo = origin.get_value() # print 'update', type(up), type(boo), boo[1] # TIME.sleep(20) if fix_weight: for fix, origin in zip(fix_weight, model.params_weight): origin.set_value(np.array(fix)) else: fix_weight = [] for origin in model.params_weight: fix_weight.append(origin.get_value()) # Phase 1 func_elementary = theano.function( inputs=[x, y, lr_ele], outputs=[model.lossWithPenalty, model.prediction], updates=update_ele, on_unused_input='ignore', allow_input_downcast=True) func_eval = theano.function(inputs=[x, y], outputs=[model.loss, model.prediction], on_unused_input='ignore', allow_input_downcast=True) # Phase 2 # actually, in the backward phase func_hyper_valid = theano.function(inputs=[x, y], outputs=[model.loss, model.prediction] + output_valid_list, updates=update_valid, on_unused_input='ignore', allow_input_downcast=True) """ Phase 1: meta-forward """ X_mix = np.concatenate((X_valid, X_test), axis=0) Y_mix = np.concatenate((Y_valid, Y_test), axis=0) print X_valid.shape, X_mix.shape X_valid, Y_valid = X_mix[:len(X_mix) / 2], Y_mix[:len(X_mix) / 2] X_test, Y_test = X_mix[len(X_mix) / 2:], Y_mix[len(X_mix) / 2:] n_ele, n_valid, n_test = X_elementary.shape[0], X_valid.shape[ 0], X_test.shape[0] # TODO: remove this override n_ele = 20000 X_elementary, Y_elementary = X_elementary[:n_ele], Y_elementary[:n_ele] print "# of ele, valid, test: ", n_ele, n_valid, n_test n_batch_ele = n_ele / args.batchSizeEle test_perm, ele_perm = range(0, n_test), range(0, n_ele) last_iter = args.maxEpoch * n_batch_ele - 1 temp_err_ele = [] temp_cost_ele = [] eval_loss = 0. t_start = time() iter_index_cache = [] # save the model parameters into theta_initial theta_initial = [] for i, w in enumerate(model.params_theta): theta_initial.append(w.get_value()) for i in range(0, args.maxEpoch * n_batch_ele): curr_epoch = i / n_batch_ele curr_batch = i % n_batch_ele """ Learning rate and momentum schedules. """ t = 1. * i / (args.maxEpoch * n_batch_ele) """ Update """ sample_idx_ele = ele_perm[(curr_batch * args.batchSizeEle):((curr_batch + 1) * args.batchSizeEle)] iter_index_cache.append(sample_idx_ele) batch_x, batch_y = X_elementary[sample_idx_ele], Y_elementary[ sample_idx_ele] if i == 399: print "399!!!!!!!!!!!", batch_y tmp_y = np.zeros((args.batchSizeEle, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y res = func_elementary(batch_x, batch_y, lr_ele_true) (cost_ele, pred_ele, debugs) = (res[0], res[1], res[2:]) # print("Epoch %d, batch %d, time = %ds, train_loss = %.4f" % # (curr_epoch, curr_batch, time() - t_start, cost_ele)) # temp_err_ele += [1. * sum(batch_y != pred_ele) / args.batchSizeEle] temp_cost_ele += [cost_ele] eval_error = 0. # if np.isnan(cost_ele): # print 'NANS', cost_ele """ Evaluate """ if args.verbose or (curr_batch == n_batch_ele - 1): if args.model == 'mlp': n_eval = n_test else: n_eval = 1000 temp_idx = test_perm[:n_eval] batch_x, batch_y = X_test[temp_idx], Y_test[temp_idx] tmp_y = np.zeros((n_eval, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y eval_loss, y_test = func_eval(batch_x, batch_y) wrong = 0 for e1, e2 in zip(y_test, Y_test[temp_idx]): if e1 != e2: wrong += 1 # eval_error = 1. * sum(int(Y_test[temp_idx] != batch_y)) / n_eval eval_error = 100. * wrong / n_eval print "test sample", n_eval print( "Valid on Test Set: Epoch %d, batch %d, time = %ds, eval_loss = %.4f, eval_error = %.4f" % (curr_epoch, curr_batch + 1, time() - t_start, eval_loss, eval_error)) # save the model parameters after T1 into theta_final theta_final = [] for i, w in enumerate(model.params_theta): theta_final.append(w.get_value()) """ Phase 2: Validation on Hyper set """ n_hyper = X_hyper.shape[0] n_batch_hyper = n_hyper / args.batchSizeHyper hyper_perm = range(0, n_hyper) # np.random.shuffle(hyper_perm) err_valid = 0. cost_valid = 0. t_start = time() grad_l_theta = [] for i in range(0, n_batch_hyper): sample_idx = hyper_perm[(i * args.batchSizeHyper):((i + 1) * args.batchSizeHyper)] batch_x, batch_y = X_elementary[sample_idx], Y_elementary[sample_idx] # TODO: refactor, too slow tmp_y = np.zeros((args.batchSizeEle, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y res = func_hyper_valid(batch_x, batch_y) valid_cost, pred_hyper, grad_temp = res[0], res[1], res[2:] err_tmp = 0. # err_tmp = 1. * sum(batch_y != pred_hyper) / args.batchSizeHyper err_valid += err_tmp # print "err_temp", err_tmp cost_valid += valid_cost # accumulate gradient and then take the average if i == 0: for grad in grad_temp: grad_l_theta.append(np.asarray(grad)) else: for k, grad in enumerate(grad_temp): grad_l_theta[k] += grad err_valid /= n_batch_hyper cost_valid /= n_batch_hyper # get average grad of all iterations on validation set for i, grad in enumerate(grad_l_theta): print grad.shape grad_l_theta[i] = grad / (np.array(n_hyper * 1., dtype=theano.config.floatX)) print( "Valid on Hyper Set: time = %ds, valid_err = %.2f, valid_loss = %.4f" % (time() - t_start, err_valid * 100, cost_valid)) """ Phase 3: meta-backward """ # updates for phase 3 update_hyper, output_hyper_list, phase_3_input = updates_hyper( model.params_lambda, model.params_weight, model.lossWithPenalty, grad_l_theta, output_valid_list) # Phase 3 # dloss_dpenalty = T.grad(model.loss, model.params_lambda) func_hyper = theano.function(inputs=[x, y], outputs=output_hyper_list + output_valid_list, updates=update_hyper, on_unused_input='ignore', allow_input_downcast=True) # init for pseudo params pseudo_params = [] for i, v in enumerate(model.params_theta): pseudo_params.append(v.get_value()) def replace_pseudo_params(ratio): for i, param in enumerate(model.params_theta): pseudo_params[i] = ( 1 - ratio) * theta_initial[i] + ratio * theta_final[i] param.set_value(pseudo_params[i]) n_backward = len(iter_index_cache) / 10 print "n_backward", n_backward rho = np.linspace(0.001, 0.999, n_backward) # initialization up_lambda, up_v = [], [] for param in model.params_lambda: temp_param = np.zeros_like(param.get_value() * 0., dtype=theano.config.floatX) up_lambda += [temp_param] for param in model.params_weight: temp_v = np.zeros_like(param.get_value() * 0., dtype=theano.config.floatX) up_v += [temp_v] # time.sleep(20) up_theta = grad_l_theta iter_index_cache = iter_index_cache[:n_backward] for iteration in range(n_backward)[::-1]: replace_pseudo_params(rho[iteration]) # line 4 curr_epoch = iteration / n_batch_ele curr_batch = iteration % n_batch_ele if iteration % 40 == 0: print "Phase 3, ep{} iter{}, total{}".format( curr_epoch, curr_batch, iteration) sample_idx_ele = iter_index_cache[iteration] # sample_idx_ele = ele_perm[(curr_batch * args.batchSizeEle):((curr_batch + 1) * args.batchSizeEle)] batch_x, batch_y = X_elementary[sample_idx_ele], Y_elementary[ sample_idx_ele] if curr_batch == 399: print "399!!!!!!!!!!!", batch_y tmp_y = np.zeros((args.batchSizeEle, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y if args.model == 'mlp': for p3, p1, input_p in zip(up_v, up_theta, phase_3_input): # print p3.shape, p1.shape p3 += lr_ele_true * p1 input_p.set_value(p3) tmp = input_p.get_value() # print 'set up_v to obtain hypergrad', tmp[1][1] # TIME.sleep(2) else: for p3, p1, input_p in zip(up_v, up_theta, phase_3_input): p3 += lr_ele_true * p1 input_p.set_value(p3) # hessian vector product HVP_value = func_hyper(batch_x, batch_y) HVP_weight_value = HVP_value[:4] HVP_lambda_value = HVP_value[4:8] debug_orz = HVP_value[8:] # return cnt = 0 for p1, p2, p3, hvp1, hvp2 in zip(up_theta, up_lambda, up_v, HVP_weight_value, HVP_lambda_value): # this code is to monitor the up_lambda if cnt == 3: tmp2 = np.array(hvp2) tmp1 = np.array(hvp1) if iteration % 40 == 0: print "up_lambda", p2[3][0] else: cnt += 1 p1 -= (1. - mom) * np.array(hvp1) p2 -= (1. - mom) * np.array(hvp2) p3 *= mom # print up_lambda[2][0][0] return model.params_lambda, up_lambda, fix_weight, eval_loss, eval_error
def set_last_concat(asker, l, new_value): #FIXME deal with the case where b is empty return asker.reply(answer=l.simple_update(b=updates.update( updates.set_field(last(), new_value), l['b'] )))
def is_pointer_now(asker, object): result = updates.update(updates.remove_modifier(has_pointer()), object) result = properties.simple_add_modifier(result, is_pointer()) return asker.reply(answer=result)
def setup(self): """Create tables in database.""" tables_dict, default_data = updates.update(tables.NOTES, DEFAULT_DATA) self.db.setup(tables_dict, tables.get_columns_names, default_data)
def run_exp(args, fix_weight, theta_final, epochs): global X_elementary, Y_elementary, X_hyper, Y_hyper, X_test, Y_test global training_errors, unregularized_training_errors, valid_errors, hyper_errors, test_errors, training_losses, valid_losses, hyper_losses, test_losses, norms, theta_names #reinitialize the elementary parameters (exactly as before; break symmetry the same way again) if fix_weight: #for fix, origin in zip(fix_weight, model.params_weight): # origin.set_value(np.array(fix)) for fix, origin, velocity in zip(fix_weight, model.params_theta, velocities): #origin.set_value(np.array(fix)) velocity.set_value(velocity.get_value()*0.) #reset velocities to 0 else: #store the elementary parameters for the first time, as arrays #TODO: this could be put outside of run_exp() fix_weight = [] for origin in model.params_theta: fix_weight.append(origin.get_value()) #copies (borrow=False by default) # Phase 1 # elementary SGD variable update list (constant momenta and learning rates) dlossWithPenalty_dtheta = theano.grad(model.lossWithPenalty, model.params_theta) update_ele, update_valid, output_valid_list, share_var_dloss_dweight = update(model.params_theta, model.params_lambda, model.params_weight, velocities, model.loss, model.penalty, dlossWithPenalty_dtheta, lr_ele, mom, args.eleAlg) #update_ele, update_valid, output_valid_list, share_var_dloss_dweight = update2(model.params_theta, model.params_lambda, model.params_weight, # velocities, model.loss, model.penalty, model.lossWithPenalty, # log_learning_rates, lr_hyper, mom) #output_valid_list is used as dv_t in DrMAD func_elementary = theano.function( inputs=[x, y, lr_ele], #inputs=[x, y], outputs=[model.lossWithPenalty, model.loss, model.prediction], updates=update_ele, #Michael: update_ele will apply the SGD step when func_elementary is called on_unused_input='ignore', allow_input_downcast=True) func_eval = theano.function( inputs=[x, y], outputs=[model.loss_det, model.prediction_det], #use deterministic=True for these on_unused_input='ignore', allow_input_downcast=True) func_eval_train = theano.function( inputs=[x, y], outputs=[model.lossWithPenalty_det, model.loss_det, model.prediction_det], #use deterministic=True for these on_unused_input='ignore', allow_input_downcast=True) """ Phase 1: meta-forward """ #n_ele, n_valid, n_test = X_elementary.shape[0], X_valid.shape[0], X_test.shape[0] n_ele, n_hyper, n_test = X_elementary.shape[0], X_hyper.shape[0], X_test.shape[0] print("# of epochs: " + str(epochs)) print("# of ele, valid, test: ", n_ele, n_hyper, n_test) #number of batches, or number of iterations per epoch n_batch_ele = n_ele // args.batchSizeEle #integer division, so some data may be lost #n_batch_ele = 100 #TODO: print("# of ele batches ="+ str(n_batch_ele)) test_perm, ele_perm, hyper_perm = range(0, n_test), range(0, n_ele), range(0, n_hyper) n_eval = 1000 #sample size from training/elementary, hyper/valid and test sets to evaluate on #use func_eval on initial parameters for elementary, valid/hyper and test sets, starting new lists in each temp_idx = ele_perm[:n_eval] batch_x, batch_y = X_elementary[temp_idx], Y_elementary[temp_idx] tmp_y = np.zeros((n_eval, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y train_loss, unreg_train_loss, train_pred = func_eval_train(batch_x, batch_y) wrong = 0 for e1, e2 in zip(train_pred, Y_elementary[temp_idx]): if e1 != e2: wrong += 1 train_error = 100. * wrong / n_eval print("Eval on Train Set: unreg_loss = %.4f, loss = %.4f, error = %.4f" % (unreg_train_loss, train_loss, train_error)) unreg_train_losses.append([float(unreg_train_loss)]) train_losses.append([float(train_loss)]) train_errors.append([train_error]) temp_idx = hyper_perm[:n_eval] batch_x, batch_y = X_hyper[temp_idx], Y_hyper[temp_idx] tmp_y = np.zeros((n_eval, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y valid_loss, valid_pred = func_eval(batch_x, batch_y) wrong = 0 for e1, e2 in zip(valid_pred, Y_hyper[temp_idx]): if e1 != e2: wrong += 1 valid_error = 100. * wrong / n_eval print("Eval on Valid Set: loss = %.4f, error = %.4f" % (valid_loss, valid_error)) valid_losses.append([float(valid_loss)]) valid_errors.append([valid_error]) temp_idx = test_perm[:n_eval] batch_x, batch_y = X_test[temp_idx], Y_test[temp_idx] tmp_y = np.zeros((n_eval, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y test_loss, test_pred = func_eval(batch_x, batch_y) wrong = 0 for e1, e2 in zip(test_pred, Y_test[temp_idx]): if e1 != e2: wrong += 1 # eval_error = 1. * sum(int(Y_test[temp_idx] != batch_y)) / n_eval test_error = 100. * wrong / n_eval print("Eval on Test Set: loss = %.4f, error = %.4f" % (test_loss, test_error)) test_losses.append([float(test_loss)]) test_errors.append([test_error]) for param in model.params_theta: norms.append([np.linalg.norm(param.get_value(borrow=True))]) if theta_final: for param, fin in zip(model.params_theta, theta_final): distances_from_final.append([np.linalg.norm(param.get_value(borrow=True))]) else: for v in velocities: velocity_norms.append([np.linalg.norm(v.get_value(borrow=True))]) lr_ele_true = args.lrEle t_start = time() T = epochs * n_batch_ele #T = n_batch_ele//3 #TODO: change back #T = 40 for i in range(0, T): #SGD steps curr_epoch = i // n_batch_ele curr_batch = i % n_batch_ele """ Learning rate and momentum schedules. """ #From DenseNet if curr_epoch >= 30: lr_ele_true = args.lrEle/10. elif curr_epoch >= 60: lr_ele_true = args.lrEle/100. """ Update """ sample_idx_ele = ele_perm[(curr_batch * args.batchSizeEle):((curr_batch + 1) * args.batchSizeEle)] #Michael: batch indices batch_x, batch_y = X_elementary[sample_idx_ele], Y_elementary[sample_idx_ele] #batch data tmp_y = np.zeros((args.batchSizeEle, 10)) #10 for 10 classes; put a 1 in row=idx and column=class=element of idx for idx, element in enumerate(batch_y): #idx = index, element = element at that index tmp_y[idx][element] = 1 batch_y = tmp_y #update elementary parameters train_loss, unreg_train_loss, train_pred = func_elementary(batch_x, batch_y, lr_ele_true) for norm, param in zip(norms, model.params_theta): norm.append(np.linalg.norm(param.get_value(borrow=True))) if theta_final: for j, param, fin in zip(range(len(model.params_theta)), model.params_theta, theta_final): distances_from_final[j].append(np.linalg.norm(param.get_value(borrow=True))) else: for j, v in enumerate(velocities): velocity_norms[j].append(np.linalg.norm(v.get_value(borrow=True))) """ Evaluate """ if i%20==0: wrong = 0 for e1, e2 in zip(train_pred, Y_elementary[sample_idx_ele]): # if e1 != e2: wrong += 1 train_error = 100. * wrong / len(train_pred) print("Train Set: Epoch %d, batch %d, time = %ds, loss = %.4f, unreg loss = %.4f, error = %.4f" % (curr_epoch, curr_batch, time() - t_start, train_loss, unreg_train_loss, train_error)) #if args.verbose or (curr_batch == n_batch_ele - 1): #verbose for each iteration, batch for epoch # use sample of 1000 to evaluate training, validation and tests losses/errors if curr_batch == n_batch_ele - 1: #last batch #TODO: #if i==T-1: #use deterministic option for BN layers if curr_epoch == epochs-1: #if it's also the last epoch, do a bigger test n_eval = 5000 else: n_eval = 1000 #temp_idx = ele_perm[:n_eval] #same 1000 each time? n_ele_eval_batches = n_ele // n_eval temp_idx = ele_perm[(curr_epoch % n_ele_eval_batches)*n_eval: ((curr_epoch % n_ele_eval_batches)+1)*n_eval] batch_x, batch_y = X_elementary[temp_idx], Y_elementary[temp_idx] tmp_y = np.zeros((n_eval, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y train_loss, unreg_train_loss, train_pred = func_eval_train(batch_x, batch_y) wrong = 0 for e1, e2 in zip(train_pred, Y_elementary[temp_idx]): if e1 != e2: wrong += 1 train_error = 100. * wrong / n_eval print("Eval on Train Set: Epoch %d, batch %d, time = %ds, loss = %.4f, unreg loss = %.4f, error = %.4f" % (curr_epoch, curr_batch, time() - t_start, train_loss, unreg_train_loss, train_error)) unreg_train_losses[-1].append(float(unreg_train_loss)) train_losses[-1].append(float(train_loss)) train_errors[-1].append(train_error) #temp_idx = hyper_perm[:n_eval] #same 1000 each time? n_hyper_eval_batches = n_hyper // n_eval temp_idx = hyper_perm[(curr_epoch % n_hyper_eval_batches)*n_eval: ((curr_epoch % n_hyper_eval_batches)+1)*n_eval] batch_x, batch_y = X_hyper[temp_idx], Y_hyper[temp_idx] tmp_y = np.zeros((n_eval, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y valid_loss, valid_pred = func_eval(batch_x, batch_y) wrong = 0 for e1, e2 in zip(valid_pred, Y_hyper[temp_idx]): if e1 != e2: wrong += 1 valid_error = 100. * wrong / n_eval print("Eval on Valid Set: Epoch %d, batch %d, time = %ds, loss = %.4f, error = %.4f" % (curr_epoch, curr_batch, time() - t_start, valid_loss, valid_error)) valid_losses[-1].append(float(valid_loss)) valid_errors[-1].append(valid_error) #temp_idx = test_perm[:n_eval] #same 1000 each time? n_test_eval_batches = n_test // n_eval temp_idx = test_perm[(curr_epoch % n_test_eval_batches)*n_eval: ((curr_epoch % n_test_eval_batches)+1)*n_eval] batch_x, batch_y = X_test[temp_idx], Y_test[temp_idx] tmp_y = np.zeros((n_eval, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y test_loss, test_pred = func_eval(batch_x, batch_y) wrong = 0 for e1, e2 in zip(test_pred, Y_test[temp_idx]): if e1 != e2: wrong += 1 # eval_error = 1. * sum(int(Y_test[temp_idx] != batch_y)) / n_eval test_error = 100. * wrong / n_eval print("Eval on Test Set: Epoch %d, batch %d, time = %ds, loss = %.4f, error = %.4f" % (curr_epoch, curr_batch, time() - t_start, test_loss, test_error)) test_losses[-1].append(float(test_loss)) test_errors[-1].append(test_error) #Save and save plots save_errors_losses_norms(outdir, unreg_train_losses, train_losses, valid_losses, test_losses, train_errors, valid_errors, test_errors, norms, distances_from_final, velocity_norms, theta_names) # save the model parameters after T1 into theta_final if not theta_final: theta_final = [param.get_value() for param in model.params_theta] f=open(outdir+'/theta_final.pckl', 'wb') # Python 2: open(..., 'w') pickle.dump(theta_final, f) f.close() return fix_weight, theta_final
def map_simple(asker, to_apply, head, tail): return asker.reply(answer=T.cons( updates.update(to_apply, head), updates.update(update_map(to_apply), tail) ))
def increment_odd(asker, x): return asker.reply(answer=T.double(updates.update(increment(), x)))
def update(): """ Forces an update of the server's data. """ updates.update() return ''
def has_pointer_now(asker, object): if convert.check_hard(asker, has_pointer(), object): result = updates.update(updates.remove_modifier(is_pointer()), object) else: result = properties.simple_add_modifier(result, has_pointer()) return asker.reply(answer=result)
def plus_one(x): return updates.update(increment(), x)
def map_concat(asker, to_apply, a, b): return asker.reply(answer=concat( updates.update(update_map(to_apply), a), updates.update(update_map(to_apply), b) ))
def setup_wxdb(self): """Create tables this module.""" tables_dict, default_data = updates.update(tables.SETTINGS, DEFAULT_DATA) self.db.setup(tables_dict, tables.get_columns_names, default_data)
def map_last(asker, to_apply, init, last): return asker.reply(answer=snoc( updates.update(update_map(to_apply), init), updates.update(to_apply, last) ))
def run_exp(args, update_lambda, fix_weight): if args.predata is False: X_elementary, Y_elementary, X_hyper, Y_hyper, X_valid, Y_valid, X_test, Y_test = read_preprocess(params=args) np.savez(args.processedDataName, X_elementary=X_elementary, Y_elementary=Y_elementary, X_hyper=X_hyper, Y_hyper=Y_hyper, X_v=X_valid, Y_v=Y_valid, X_test=X_test, Y_test=Y_test) else: tmpload = np.load(args.processedDataName) X_elementary, Y_elementary, X_hyper, Y_hyper, X_valid, Y_valid, X_test, Y_test = \ tmpload['X_elementary'], tmpload['Y_elementary'], tmpload['X_hyper'], tmpload['Y_hyper'],\ tmpload['X_v'], tmpload['Y_v'], tmpload['X_test'], tmpload['Y_test'] """ Build Theano functions """ if args.model == 'convnet': x = T.ftensor4('x') elif args.model == 'mlp': x = T.matrix('x') else: raise AttributeError y = T.matrix('y') lr_ele = T.fscalar('lr_ele') lr_ele_true = np.array(args.lrEle, theano.config.floatX) mom = 0.95 # Michael: momentum lr_hyper = T.fscalar('lr_hyper') grad_valid_weight = T.tensor4('grad_valid_weight') if args.model == 'mlp': model = MLP(x=x, y=y, args=args) elif args.model == 'convnet': model = ConvNet(x=x, y=y, args=args) #Michael: check here for model.params_theta if args.dataset == 'mnist': nc = 1 nPlane = 28 else: nc = 3 nPlane = 32 X_elementary = X_elementary.reshape(-1, nc, nPlane, nPlane) X_hyper = X_hyper.reshape(-1, nc, nPlane, nPlane) X_valid = X_valid.reshape(-1, nc, nPlane, nPlane) X_test = X_test.reshape(-1, nc, nPlane, nPlane) else: raise AttributeError # Michael: this computes the updated parameters # Michael: these aren't the new parameters themselves, but the update functions update_ele, update_valid, output_valid_list, share_var_dloss_dweight = update(model.params_theta, model.params_lambda, model.params_weight, model.loss, model.penalty, model.lossWithPenalty, lr_ele, lr_hyper, mom) if update_lambda: for up, origin in zip(update_lambda, model.params_lambda): origin.set_value(np.array(up)) boo = origin.get_value() # print 'update', type(up), type(boo), boo[1] # TIME.sleep(20) if fix_weight: for fix, origin in zip(fix_weight, model.params_weight): origin.set_value(np.array(fix)) else: fix_weight = [] for origin in model.params_weight: fix_weight.append(origin.get_value()) # Phase 1 # Michael: ??? func_elementary = theano.function( inputs=[x, y, lr_ele], outputs=[model.lossWithPenalty, model.prediction], updates=update_ele, #Michael: update_ele is the updating function, not the new parameters on_unused_input='ignore', allow_input_downcast=True) func_eval = theano.function( inputs=[x, y], outputs=[model.loss, model.prediction], on_unused_input='ignore', allow_input_downcast=True) # Phase 2 # actually, in the backward phase func_hyper_valid = theano.function( inputs=[x, y], outputs=[model.loss, model.prediction] + output_valid_list, updates=update_valid, on_unused_input='ignore', allow_input_downcast=True) """ Phase 1: meta-forward """ X_mix = np.concatenate((X_valid, X_test), axis=0) Y_mix = np.concatenate((Y_valid, Y_test), axis=0) print(X_valid.shape, X_mix.shape) X_valid, Y_valid = X_mix[:len(X_mix) // 2], Y_mix[:len(X_mix) // 2] X_test, Y_test = X_mix[len(X_mix) // 2:], Y_mix[len(X_mix) // 2:] n_ele, n_valid, n_test = X_elementary.shape[0], X_valid.shape[0], X_test.shape[0] # TODO: remove this override n_ele = 20000 X_elementary, Y_elementary = X_elementary[:n_ele], Y_elementary[:n_ele] print("# of ele, valid, test: ", n_ele, n_valid, n_test) n_batch_ele = n_ele // args.batchSizeEle test_perm, ele_perm = range(0, n_test), range(0, n_ele) last_iter = args.maxEpoch * n_batch_ele - 1 temp_err_ele = [] temp_cost_ele = [] eval_loss = 0. t_start = time() iter_index_cache = [] # save the model parameters into theta_initial theta_initial = [] for i, w in enumerate(model.params_theta): # Michael: doesn't actually go through parameters, only [W, b, W, b, W, b, W, b] theta_initial.append(w.get_value()) """ # Michael: pick two random parameters, construct two lists to store the paths # Michael: "i, w in enumerate(model.params_theta)", but random? # i is the layer (list index), w is the weight # model.params_theta = [W, b, W, b, W, b, W, b] # W's, b's are type theano.tensor.sharedvar.TensorSharedVariable # model.params_theta[0].get_value()[0][0][0][0] gives a weight, possibly repeated/shared? # model.params_theta[i1].get_value()[i2][i3][i4][i5] # Get coordinates of first weight coords1 = [np.random.randint(0, len(model.params_theta))] #[len(model.params_theta)-2] #[0] #[np.random.randint(0, len(model.params_theta))] layer_value = model.params_theta[coords1[0]].get_value() while not isinstance(layer_value, (int, float, np.float32, np.float64)): #while we haven't gotten to a weight coords1.append(np.random.randint(0, len(layer_value))) layer_value = layer_value[coords1[-1]] # Access and create list initialized with first value layer_value = model.params_theta[coords1[0]].get_value() for l in range(1, len(coords1)): layer_value = layer_value[coords1[l]] w_1 = [layer_value] #for l in range(1, len(coords1)-1): # layer_value = layer_value[coords1[l]] #layer_value[coords1[-1]] = 1.0 #w_1 = [1.0] # Get coordinates of second weight coords2 = [np.random.randint(0, len(model.params_theta))] #[len(model.params_theta)-2] #[0] # layer_value = model.params_theta[coords2[0]].get_value() while not isinstance(layer_value, (int, float, np.float32, np.float64)): #while we haven't gotten to a weight coords2.append(np.random.randint(0, len(layer_value))) layer_value = layer_value[coords2[-1]] # Access and create list initialized with first value layer_value = model.params_theta[coords2[0]].get_value() for l in range(1, len(coords2)): layer_value = layer_value[coords2[l]] w_2 = [layer_value]""" #for l in range(1, len(coords2)-1): # layer_value = layer_value[coords2[l]] #layer_value[coords2[-1]] = 1.30 #w_2 = [1.30] for i in range(0, args.maxEpoch * n_batch_ele): # Michael: SGD steps curr_epoch = i // n_batch_ele curr_batch = i % n_batch_ele """ Learning rate and momentum schedules. """ t = 1. * i // (args.maxEpoch * n_batch_ele) #Michael: never used? """ Update """ sample_idx_ele = ele_perm[(curr_batch * args.batchSizeEle):((curr_batch + 1) * args.batchSizeEle)] #Michael: batch indices iter_index_cache.append(sample_idx_ele) batch_x, batch_y = X_elementary[sample_idx_ele], Y_elementary[sample_idx_ele] #Michael: batch data if i == 399: print("399!!!!!!!!!!!", batch_y) #Michael: ??? # TODO: last elementary step before hyperparameter update? #Michael: what's this for? tmp_y = np.zeros((args.batchSizeEle, 10)) #Michael: 10 for 10 classes; put a 1 in row=idx and column=class=element of idx for idx, element in enumerate(batch_y): #Michael: idx = index, element = element at that index tmp_y[idx][element] = 1 batch_y = tmp_y # Michael: This where the elementary parameters are updated res = func_elementary(batch_x, batch_y, lr_ele_true) (cost_ele, pred_ele, debugs) = (res[0], res[1], res[2:]) # Michael: add new parameters to lists """layer_value = model.params_theta[coords1[0]].get_value() for l in range(1, len(coords1)): layer_value = layer_value[coords1[l]] w_1.append(layer_value) layer_value = model.params_theta[coords2[0]].get_value() for l in range(1, len(coords2)): layer_value = layer_value[coords2[l]] w_2.append(layer_value)""" # Michael: plot them right away if i%20 == 0: #only every 20 #plt.plot(w_1, w_2, marker='o', ms=3.) #plt.plot(w_1[0], w_2[0], marker='o') #plt.plot(w_1[len(w_1)-1], w_2[len(w_1)-1], marker='o', ms=10.) #plt.show() print(i) # print("Epoch %d, batch %d, time = %ds, train_loss = %.4f" % # (curr_epoch, curr_batch, time() - t_start, cost_ele)) # temp_err_ele += [1. * sum(batch_y != pred_ele) / args.batchSizeEle] temp_cost_ele += [cost_ele] eval_error = 0. # if np.isnan(cost_ele): # print 'NANS', cost_ele """ Evaluate """ if args.verbose or (curr_batch == n_batch_ele - 1): if args.model == 'mlp': n_eval = n_test else: n_eval = 1000 temp_idx = test_perm[:n_eval] batch_x, batch_y = X_test[temp_idx], Y_test[temp_idx] tmp_y = np.zeros((n_eval, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y eval_loss, y_test = func_eval(batch_x, batch_y) wrong = 0 for e1, e2 in zip(y_test, Y_test[temp_idx]): if e1 != e2: wrong += 1 # eval_error = 1. * sum(int(Y_test[temp_idx] != batch_y)) / n_eval eval_error = 100. * wrong / n_eval print("test sample", n_eval) print("Valid on Test Set: Epoch %d, batch %d, time = %ds, eval_loss = %.4f, eval_error = %.4f" % (curr_epoch, curr_batch + 1, time() - t_start, eval_loss, eval_error)) # save the model parameters after T1 into theta_final theta_final = [] for i, w in enumerate(model.params_theta): theta_final.append(w.get_value()) # Michael: plot paths #plt.plot(w_1, w_2, marker='o', ms=3.) #plt.plot(w_1[0], w_2[0], marker='o') #plt.plot(w_1[len(w_1)-1], w_2[len(w_1)-1], marker='o', ms=10.) #plt.show() # Michael: plot paths #plt.plot(range(0,len(w_2)), w_2, marker='o', ms=3.) #plt.plot(0, w_2[0], marker='o') #plt.plot(len(w_2)-1, w_2[len(w_1)-1], marker='o', ms=10.) #plt.show() """ Phase 2: Validation on Hyper set """ n_hyper = X_hyper.shape[0] n_batch_hyper = n_hyper // args.batchSizeHyper hyper_perm = range(0, n_hyper) # np.random.shuffle(hyper_perm) err_valid = 0. cost_valid = 0. t_start = time() grad_l_theta = [] for i in range(0, n_batch_hyper): sample_idx = hyper_perm[(i * args.batchSizeHyper):((i + 1) * args.batchSizeHyper)] batch_x, batch_y = X_elementary[sample_idx], Y_elementary[sample_idx] # TODO: refactor, too slow tmp_y = np.zeros((args.batchSizeEle, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y res = func_hyper_valid(batch_x, batch_y) valid_cost, pred_hyper, grad_temp = res[0], res[1], res[2:] err_tmp = 0. # err_tmp = 1. * sum(batch_y != pred_hyper) / args.batchSizeHyper err_valid += err_tmp # print "err_temp", err_tmp cost_valid += valid_cost # accumulate gradient and then take the average if i == 0: for grad in grad_temp: grad_l_theta.append(np.asarray(grad)) else: for k, grad in enumerate(grad_temp): grad_l_theta[k] += grad err_valid /= n_batch_hyper cost_valid /= n_batch_hyper # get average grad of all iterations on validation set for i, grad in enumerate(grad_l_theta): print(grad.shape) grad_l_theta[i] = grad / (np.array(n_hyper * 1., dtype=theano.config.floatX)) print("Valid on Hyper Set: time = %ds, valid_err = %.2f, valid_loss = %.4f" % (time() - t_start, err_valid * 100, cost_valid)) """ Phase 3: meta-backward """ # updates for phase 3 update_hyper, output_hyper_list, phase_3_input = updates_hyper(model.params_lambda, model.params_weight, model.lossWithPenalty, grad_l_theta, output_valid_list) # Phase 3 # dloss_dpenalty = T.grad(model.loss, model.params_lambda) func_hyper = theano.function( inputs=[x, y], outputs=output_hyper_list + output_valid_list, updates=update_hyper, on_unused_input='ignore', allow_input_downcast=True) # Michael: this is the backwards approximating path # init for pseudo params pseudo_params = [] for i, v in enumerate(model.params_theta): pseudo_params.append(v.get_value()) def replace_pseudo_params(ratio): for i, param in enumerate(model.params_theta): pseudo_params[i] = (1 - ratio) * theta_initial[i] + ratio * theta_final[i] param.set_value(pseudo_params[i]) n_backward = len(iter_index_cache)//10 print("n_backward", n_backward) rho = np.linspace(0.001, 0.999, n_backward) # initialization up_lambda, up_v = [], [] for param in model.params_lambda: temp_param = np.zeros_like(param.get_value() * 0., dtype=theano.config.floatX) up_lambda += [temp_param] for param in model.params_weight: temp_v = np.zeros_like(param.get_value() * 0., dtype=theano.config.floatX) up_v += [temp_v] # time.sleep(20) up_theta = grad_l_theta iter_index_cache = iter_index_cache[:n_backward] for iteration in range(n_backward)[::-1]: # Michael: this is the backwards approximating path replace_pseudo_params(rho[iteration]) # line 4 curr_epoch = iteration // n_batch_ele curr_batch = iteration % n_batch_ele if iteration % 40 == 0: print("Phase 3, ep{} iter{}, total{}".format(curr_epoch, curr_batch, iteration)) sample_idx_ele = iter_index_cache[iteration] # sample_idx_ele = ele_perm[(curr_batch * args.batchSizeEle):((curr_batch + 1) * args.batchSizeEle)] batch_x, batch_y = X_elementary[sample_idx_ele], Y_elementary[sample_idx_ele] if curr_batch == 399: print("399!!!!!!!!!!!", batch_y) tmp_y = np.zeros((args.batchSizeEle, 10)) for idx, element in enumerate(batch_y): tmp_y[idx][element] = 1 batch_y = tmp_y if args.model == 'mlp': for p3, p1, input_p in zip(up_v, up_theta, phase_3_input): # print p3.shape, p1.shape p3 += lr_ele_true * p1 input_p.set_value(p3) tmp = input_p.get_value() # print 'set up_v to obtain hypergrad', tmp[1][1] # TIME.sleep(2) else: for p3, p1, input_p in zip(up_v, up_theta, phase_3_input): p3 += lr_ele_true * p1 input_p.set_value(p3) # hessian vector product HVP_value = func_hyper(batch_x, batch_y) HVP_weight_value = HVP_value[:4] HVP_lambda_value = HVP_value[4:8] debug_orz = HVP_value[8:] # return cnt = 0 for p1, p2, p3, hvp1, hvp2 in zip(up_theta, up_lambda, up_v, HVP_weight_value, HVP_lambda_value): # this code is to monitor the up_lambda if cnt == 3: tmp2 = np.array(hvp2) tmp1 = np.array(hvp1) if iteration % 40 == 0: print("up_lambda", p2[3][0]) else: cnt += 1 p1 -= (1. - mom) * np.array(hvp1) p2 -= (1. - mom) * np.array(hvp2) p3 *= mom # print up_lambda[2][0][0] return model.params_lambda, up_lambda, fix_weight, eval_loss, eval_error
def set_last_simple(asker, l, new_value): return asker.reply(answer=l.simple_update(tail=updates.update( updates.set_field(last(), new_value), l['tail'] )))
import convert import representations import properties import updates import outlines import builtins import computations import term import termtypes import state import views import functions import strings from term import Term as T k = updates.update(updates.trivial(), T.from_int(0)) d = T('test [a]', a=k) Q = T('a question about [this]', this=k) asker = ask.Asker(Q) a = T('a') b = T('b') c = T('c') l1 = T.from_list([a, b, c]) l2 = T.from_list([a, a, a]) l12 = lists.zip(l1, l2) d = dictionaries.from_items(l12)
def toggle_expanded(asker, object): if convert.check_hard(asker, is_expanded(), object): result = updates.update(updates.remove_modifier(is_expanded()), object) else: result = properties.simple_add_modifier(object, is_expanded()) return asker.reply(answer=result)