Example #1
0
def cnn_arg_predictor(concept,
                      prediction_data,
                      all_programs,
                      all_or_nothing_probs=True):
    arg_p = prediction_data[concept]
    command_dict = create_commands_dict()
    valid_atoms = list(command_dict.keys())
    max_instr = max(valid_atoms)
    arg_log_p = {}
    for instr in xrange(max_instr + 1):
        if instr in arg_p:
            # this value should not contain all -inf, threshold if that happens
            log_p = np.log(zip(*arg_p[instr])[1])
            if (log_p < np.log(PROB_THRESHOLD)).all():
                valid_atoms.remove(instr)
            if all_or_nothing_probs:
                log_p[log_p < np.log(PROB_THRESHOLD)] = -1e10
                log_p /= 100
            log_p -= logsumexp(log_p)
            arg_log_p[instr] = zip(zip(*arg_p[instr])[0], log_p)
        elif instr not in valid_atoms:
            arg_log_p[instr] = [(None, 0.0)]
        else:
            args = command_dict[instr][1]
            if args is None:  # correct for a None outside of list
                args = [None]
            log_p = -np.log(len(args))
            arg_log_p[instr] = [(arg, log_p) for arg in args]
    return arg_log_p, valid_atoms
Example #2
0
def uniform_arg_predictor(concept, prediction_data, all_programs):
    command_dict = create_commands_dict()
    valid_atoms = list(command_dict.keys())
    max_instr = max(valid_atoms)
    arg_log_p = {}
    for instr in xrange(max_instr + 1):
        args = command_dict.get(instr, (None, None))[1]
        if args is None:  # correct for a None outside of list
            args = [None]
        log_p = -np.log(len(args))
        arg_log_p[instr] = [(arg, log_p) for arg in args]
    return arg_log_p, valid_atoms
def predict_arg_order_all_tasks_with_prob(concepts_dict,
                                          depth=5,
                                          is_complete_dep_graph=True):
    """
    Similar to the function predict_arg_order_all_tasks, but output arg
    choices with probabilities
    """
    features, task_names = convert_all_examples_to_features(concepts_dict)
    num_tasks = features.shape[0]

    target_funcs = [5, 6, 13]

    arg_order_with_task_names = {}
    for task_name in task_names:
        arg_order_with_task_names[task_name] = {}

    for target_func in target_funcs:
        cmds = pu.create_commands_dict()
        args = cmds[target_func][1]

        y_pred_all_args = np.zeros((num_tasks, len(args)))

        for arg_idx in xrange(len(args)):
            model_path = ARG_PREDICTION_MODEL_PATH + \
                '/model_func={' \
                '}_numFeatures={' \
                '}_imgResolution={' \
                '}_depth={' \
                '}_isCompleteDepGraph={' \
                '}_arg={}'.format(
                    target_func,
                    NUM_FEATURES,
                    IMG_RESOLUTION,
                    depth,
                    is_complete_dep_graph,
                    args[arg_idx])
            loaded_model = load_model(model_path)
            y_pred_one_arg = loaded_model.predict(features)
            y_pred_all_args[:, arg_idx:(arg_idx + 1)] = y_pred_one_arg
            backend.clear_session()

        for task_idx in xrange(num_tasks):
            task_name = task_names[task_idx]

            args_with_prob = []
            for arg_idx in xrange(len(args)):
                prob = y_pred_all_args[task_idx][arg_idx]

                args_with_prob.append((args[arg_idx], prob))

            arg_order_with_task_names[task_name][target_func] = args_with_prob
    return arg_order_with_task_names
Example #4
0
def ground_truth_arg_predictor(concept, prediction_data, all_programs):
    program = all_programs[concept]
    command_dict = create_commands_dict()
    command_set = set(command_dict.keys())
    max_instr = max(command_set)
    arg_log_p = {}
    for instr in xrange(max_instr + 1):
        args = set([arg for i, arg in program if i == instr])
        if not args:
            args = [None]
        log_p = -np.log(len(args))
        arg_log_p[instr] = [(arg, log_p) for arg in args]

    prog_instrs = zip(*program)[0]
    valid_atoms = []

    for instr in command_set:
        if instr in prog_instrs or command_dict[instr][1] is None:
            valid_atoms.append(instr)
    return arg_log_p, valid_atoms
def program_search_example(n_programs_to_search,
                           all_programs,
                           found_programs,
                           examples,
                           prediction_data,
                           order,
                           arg_predictor,
                           concept_emulator,
                           n_examples_to_test=10,
                           explore_mode='subroutine_disable_then_enable',
                           max_iters=100,
                           debug=False):
    global global_EC_round
    global global_subroutines_enabled

    command_set = set(create_commands_dict().keys())
    n_atoms = max(command_set) + 2
    # Avoid exploring nonexistent commands
    no_pseudo_count_to = list(set(range(max(command_set) + 1)) - command_set)

    open_instr, close_instr = [25], [26]
    if order == 0:
        pseudo_counts = [15, 15]
    elif order == 1:
        pseudo_counts = [1e-1, 1e-1]

    for concept_name, prog in found_programs.iteritems():
        save_results(concept_name, prog, 0.0, 0, 0, 0, concept_emulator,
                     arg_predictor, n_examples_to_test, n_programs_to_search,
                     order)

    if debug:
        print found_programs, len(found_programs)
    # Explore - Compress steps
    start_time = time.time()

    if explore_mode == 'subroutine_enable_then_disable':
        subroutine_enable_modes = [True, False]
        pseudo_counts = [1e-1, 1e-1]
    elif explore_mode == 'subroutine_disable_then_enable':
        subroutine_enable_modes = [False, True]
        pseudo_counts = [1e-1, 1e-1]
    elif explore_mode == 'only_subroutine_disable':
        subroutine_enable_modes = [False]
        pseudo_counts = [1e-1]
    elif explore_mode == 'only_subroutine_enable':
        subroutine_enable_modes = [True]
        pseudo_counts = [1e-1]
    else:
        raise ValueError("Invalid input of exploration_mode")

    for it_outer in xrange(1):
        for subroutines_enabled_mode, pseudo_count in zip(
                subroutine_enable_modes, pseudo_counts):
            num_tasks_solved_so_far = len(found_programs)
            for it_inner in xrange(max_iters):
                global_EC_round = it_inner
                global_subroutines_enabled = subroutines_enabled_mode
                if debug:
                    print "#################### ITER ", it_inner
                # Compress
                found_progr_instr = list(
                    set([zip(*p)[0] for p in found_programs.values()]))
                model = ProgramModel(found_progr_instr,
                                     n_atoms,
                                     open_instr,
                                     close_instr,
                                     pseudo_count,
                                     no_pseudo_count_to=no_pseudo_count_to)
                max_subroutines = 100 if subroutines_enabled_mode else 0
                model.compress(order=order,
                               max_subroutines=max_subroutines,
                               debug=debug)

                np.savez_compressed(
                    'model_log_T_ordre={}_argPred={'
                    '}_maxVisitedNodes={}_numExs={'
                    '}_subroutineEnable={}_innerIt={'
                    '}.npz'.format(order, str(arg_predictor),
                                   n_programs_to_search, n_examples_to_test,
                                   subroutines_enabled_mode, it_inner),
                    model.log_T)

                longest_program = max(model.program_lengths())
                if debug:
                    print "Largest cost of programs found so far:", longest_program
                    print "Time so far", (time.time() -
                                          start_time) / 60.0, "minutes"
                # Explore
                new_programs = search_all_concepts(
                    model, examples, prediction_data, all_programs,
                    found_programs, n_programs_to_search, it_inner,
                    arg_predictor, concept_emulator, order, n_examples_to_test,
                    debug)
                if new_programs == found_programs:
                    if debug:
                        print "EC cycle saturated on iteration", it_inner, "with subroutines", "enabled" if subroutines_enabled_mode else "disabled"
                    break
                found_programs = new_programs
                if debug:
                    print "len new programs", len(new_programs)
                    print "len all programs", len(all_programs)
                    print "Time so far", (time.time() -
                                          start_time) / 60.0, "minutes"

            if len(new_programs) == num_tasks_solved_so_far:
                if debug:
                    print "No more tasks found in the mode SubroutineEnable={" \
                          "}_outerIt={}_interIt={}".format(
                              subroutines_enabled_mode, it_outer, it_inner)
                return