def mutate(outputs, user_vs, all_vs, mutatable_fn, search_space_fn):
    mutate_candidates = []
    new_vs = list(user_vs)
    for i, h in enumerate(
            co.unassigned_independent_hyperparameter_iterator(outputs)):
        if mutatable_fn(h):
            mutate_candidates.append(h)
        h.assign_value(all_vs[i])

    # mutate a random hyperparameter
    assert len(mutate_candidates) == len(user_vs)
    m_ind = random.randint(0, len(mutate_candidates) - 1)
    m_h = mutate_candidates[m_ind]
    v = m_h.vs[random.randint(0, len(m_h.vs) - 1)]

    # ensure that same value is not chosen again
    while v == user_vs[m_ind]:
        v = m_h.vs[random.randint(0, len(m_h.vs) - 1)]
    new_vs[m_ind] = v
    if 'sub' in m_h.get_name():
        new_vs = new_vs[:m_ind + 1]

    inputs, outputs = search_space_fn()
    all_vs = specify_evolution(outputs, mutatable_fn, new_vs)
    return inputs, outputs, new_vs, all_vs
def draw_graph_evolution(outputs,
                         hyperp_value_lst,
                         out_folderpath,
                         graph_name='graph',
                         draw_hyperparameters=True,
                         draw_io_labels=True,
                         draw_module_hyperparameter_info=True):
    def draw_fn(i):
        return draw_graph(
            outputs,
            draw_hyperparameters=draw_hyperparameters,
            draw_io_labels=draw_io_labels,
            draw_module_hyperparameter_info=draw_module_hyperparameter_info,
            out_folderpath=out_folderpath,
            graph_name=graph_name + '-%d' % i,
            print_to_screen=False)

    draw_fn(0)
    h_iter = co.unassigned_independent_hyperparameter_iterator(outputs)
    for i, v in enumerate(hyperp_value_lst):
        h = next(h_iter)
        h.assign_value(v)
        draw_fn(i + 1)

    filepath_lst = [
        ut.join_paths([out_folderpath, graph_name + '-%d.pdf' % i])
        for i in range(len(hyperp_value_lst) + 1)
    ]
    out_filepath = ut.join_paths([out_folderpath, graph_name + '.pdf'])

    cmd = " ".join(["pdftk"] + filepath_lst + ["cat", "output", out_filepath])
    ut.run_bash_command(cmd)

    for fp in filepath_lst:
        ut.delete_file(fp)
def random_specify_evolution(outputs, mutatable_fn):
    user_vs = []
    all_vs = []
    for h in co.unassigned_independent_hyperparameter_iterator(outputs):
        v = random_specify_hyperparameter(h)
        if mutatable_fn(h):
            user_vs.append(v)
        all_vs.append(v)
    return user_vs, all_vs
def specify_evolution(outputs, mutatable_fn, user_vs):
    vs_idx = 0
    vs = []
    for i, h in enumerate(
            co.unassigned_independent_hyperparameter_iterator(outputs)):
        if mutatable_fn(h):
            if vs_idx >= len(user_vs):
                user_vs.append(h.vs[random.randint(0, len(h.vs) - 1)])
            h.assign_value(user_vs[vs_idx])
            vs.append(user_vs[vs_idx])
            vs_idx += 1
        else:
            v = random_specify_hyperparameter(h)
            vs.append(v)
    return vs
Beispiel #5
0
def random_specify(outputs):
    """Chooses random values to all the unspecified hyperparameters.

    The hyperparameters will be specified after this call, meaning that the
    compile and forward functionalities will be available for being called.

    Args:
        outputs (dict[str, deep_architect.core.Output]): Dictionary of named
            outputs which by being traversed back will reach all the modules
            in the search space, and correspondingly all the current
            unspecified hyperparameters of the search space.
    """
    hyperp_value_lst = []
    for h in co.unassigned_independent_hyperparameter_iterator(outputs):
        v = random_specify_hyperparameter(h)
        hyperp_value_lst.append(v)
    return hyperp_value_lst
Beispiel #6
0
    def sample(self):
        while True:
            try:
                inputs, outputs = self.search_space_fn()

                h_it = co.unassigned_independent_hyperparameter_iterator(
                    outputs.values())
                tree_hist, tree_vs = self._tree_walk(h_it)
                rollout_hist, rollout_vs = self._rollout_walk(h_it)
                vs = tree_vs + rollout_vs
                searcher_eval_token = {
                    'tree_hist': tree_hist,
                    'rollout_hist': rollout_hist
                }

                return inputs, outputs, vs, searcher_eval_token
            except ValueError:
                pass
    def eval(self, vs):
        _, outputs = self.ssf.get_search_space()
        node_ops = [None] * self.num_nodes_per_cell
        node_ops[0] = INPUT
        node_ops[-1] = OUTPUT
        matrix = [[0] * self.num_nodes_per_cell
                  for _ in range(self.num_nodes_per_cell)]
        for i, h in enumerate(
                co.unassigned_independent_hyperparameter_iterator(outputs)):
            h_name = h.get_name().split('-')[-2]
            if 'node' in h_name:
                node_num = int(h_name.split('_')[-1])
                node_ops[node_num + 1] = node_op_names[vs[i]]
            elif 'in' in h_name:
                h_name = h_name.split('_')
                matrix[int(h_name[1])][int(h_name[2])] = vs[i]

            h.assign_value(vs[i])
        model_spec = api.ModelSpec(matrix=matrix, ops=node_ops)
        data = self.nasbench.query(model_spec, epochs=self.epochs)
        return data
Beispiel #8
0
def specify(outputs, hyperp_value_lst):
    """Specify the parameters in the search space using the sequence of values
    passed as argument.

    .. note::
        This functionality is useful to replay the sequence of steps that were
        used to sample a model from the search space. This is typically used if
        it is necessary to replicate a model that was saved to disk by the
        logging functionality. Using the same sequence of values will yield the
        same model as long as the sequence of values takes the search space
        from fully unspecified to fully specified. Be careful otherwise.

    Args:
        outputs (dict[str, deep_architect.core.Output]): Dictionary of named
            outputs which by being traversed back will reach all the modules
            in the search space, and correspondingly all the current
            unspecified hyperparameters of the search space.
        hyperp_value_lst (list[object]): List of values used to specify the hyperparameters.
    """
    for i, h in enumerate(
            co.unassigned_independent_hyperparameter_iterator(outputs)):
        h.assign_value(hyperp_value_lst[i])
Beispiel #9
0
    def sample(self):
        arc = self._sample()
        idx = 0
        hyp_values = {}
        for i in range(1, self.num_layers + 1):
            hyp_values['op_' + str(i)] = arc[idx]
            idx += 1
            for j in range(i - 1):
                hyp_values['H.skip_' + str(j) + '_' + str(i) + '-0'] = arc[idx]
                idx += 1

        inputs, outputs = self.search_space_fn()
        vs = []
        for i, h in enumerate(
                unassigned_independent_hyperparameter_iterator(
                    outputs, list(hs.values()))):
            if h.get_name() in hyp_values:
                v = h.vs[hyp_values[h.get_name()]]
                h.assign_value(v)
                vs.append(v)
            else:
                v = random_specify_hyperparameter(h)
                vs.append(v)
        return inputs, outputs, vs, {'arc': arc}
Beispiel #10
0
    name_to_hyperp = {
        ut.json_object_to_json_string({
            "node_id": i,
            "in_node_id": j
        }): D([0, 1]) for i in range(1, num_nodes) for j in range(i - 1)
    }
    return mo.substitution_module(
        "Motif", substitution_fn, name_to_hyperp, ["in"], ["out"], scope=None)


(inputs, outputs) = mo.SearchSpaceFactory(
    lambda: motif(lambda: motif(batch_normalization, 4), 4)).get_search_space()
# (inputs, outputs) = mo.SearchSpaceFactory(
#     lambda: motif(batch_normalization, 4)).get_search_space()
# random_specify(outputs)
for h in co.unassigned_independent_hyperparameter_iterator(outputs):
    h.assign_value(1)

vi.draw_graph(outputs, draw_module_hyperparameter_info=False)

# inputs_val = Input((32, 32, 3))
# co.forward({inputs["in"]: inputs_val})
# outputs_val = outputs["out"].val

# model = Model(inputs=inputs_val, outputs=outputs_val)
# model.summary()

### NOTE: these are done.

# TODO: finish the model