def get_feed_dicts(outputs):
    """Get the training and evaluation dictionaries that map placeholders to the
    values that they should take during training and evaluation, respectively
    (e.g., used for dropout or batch normalization).

    Args:
        outputs (dict[str, deep_architect.core.Output]): Dictionary of named
            outputs of the model (i.e., with no unspecified hyperparameters
            available) sampled from the search space.

    Returns:
        (dict, dict):
            Training and evaluation dictionaries where the keys are placeholders
            and the values are the values the placeholders should take during
            each of these stages.
    """
    train_feed = {}
    eval_feed = {}

    def fn(x):
        if hasattr(x, 'train_feed'):
            train_feed.update(x.train_feed)
        if hasattr(x, 'eval_feed'):
            eval_feed.update(x.eval_feed)
        return False

    co.traverse_backward(outputs, fn)
    return (train_feed, eval_feed)
Exemple #2
0
def setTraining(output_lst, isTraining):

    def fn(mx):
        if hasattr(mx, 'isTraining'):
            mx.isTraining = isTraining

    co.traverse_backward(output_lst, fn)
Exemple #3
0
def setRecompile(output_lst, recompile):

    def fn(mx):
        mx._is_compiled = not recompile

    co.traverse_backward(output_lst, fn)
    logger.debug('set_recompile')
Exemple #4
0
def extract_features(inputs, outputs):
    """Extract a feature representation of a model represented through inputs and
    outputs.

    This function has been mostly used for performance prediction on fully
    specified models, i.e., after all the hyperparameters in the search space
    have specified. After this, there is a single model for which we can compute
    an appropriate feature representation containing information about the
    connections that the model makes and the values of the hyperparameters.

    Args:
        inputs (dict[str, deep_architect.core.Input]): Dictionary mapping names
            to inputs of the architecture.
        outputs (dict[str, deep_architect.core.Output]): Dictionary mapping names to outputs
            of the architecture.

    Returns:
        dict[str, list[str]]:
            Representation of the architecture as a dictionary where each
            key is associated to a list with different types of features.
    """
    module_memo = co.OrderedSet()

    module_feats = []
    connection_feats = []
    module_hyperp_feats = []

    # getting all the modules
    module_memo = []

    def fn(m):
        module_memo.append(m)

    co.traverse_backward(outputs, fn)

    for m in module_memo:
        # module features
        m_feats = m.get_name()
        module_feats.append(m_feats)

        for ox_localname, ox in iteritems(m.outputs):
            if ox.is_connected():
                ix_lst = ox.get_connected_inputs()
                for ix in ix_lst:
                    # connection features
                    c_feats = "%s |-> %s" % (ox.get_name(), ix.get_name())
                    connection_feats.append(c_feats)

        # module hyperparameters
        for h_localname, h in iteritems(m.hyperps):
            mh_feats = "%s/%s : %s = %s" % (m.get_name(), h_localname,
                                            h.get_name(), h.get_value())
            module_hyperp_feats.append(mh_feats)

    return {
        'module_feats': module_feats,
        'connection_feats': connection_feats,
        'module_hyperp_feats': module_hyperp_feats,
    }
Exemple #5
0
def _call_fn_on_pytorch_module(outputs, fn):
    def fn_iter(mx):
        if hasattr(mx, 'pyth_modules'):
            for pyth_m in mx.pyth_modules:
                fn(pyth_m)
        return False

    co.traverse_backward(outputs, fn_iter)
def draw_graph(outputs,
               draw_hyperparameters=True,
               draw_io_labels=True,
               draw_module_hyperparameter_info=True,
               out_folderpath=None,
               graph_name='graph',
               print_to_screen=True):
    """Draws a graph representation of the current state of the search space.

    All edges are directed. An edge between two modules represents the output of
    the first module going into the input of the second module. An edge between
    a module and an hyperparameter represents the dependency of that module on
    that hyperparameter.

    This visualization functionality is useful to verify that the description of
    the search space done through the domain-specific language encodes the
    intended search space.

    .. note::
        All drawing options are set to `True` to give a sense of all the
        information that can be displayed simultaneously. This can lead to
        cluttered graphs and slow rendering. We recommend changing the defaults
        according to the desired behavior.

        For generating a representation for a fully specified model,
        we recommend setting `draw_hyperparameters` to `False`, as if we are
        only concerned with the resulting model, the sharing structure of
        hyperparameters does not really matter. We recommend using
        `draw_hyperparameters` set to `True` when the user wishes to visualize
        the hyperparameter sharing pattern, e.g., to verify that it has been
        implemented correctly.

    Args:
        outputs (dict[str, deep_architect.core.Output]): Dictionary of named
            outputs from which we can reach all the modules in the search space
            by backwards traversal.
        draw_hyperparameters (bool, optional): Draw hyperparameter nodes in the
            graph, representing the dependencies between hyperparameters and
            modules.
        draw_io_labels (bool, optional): If `True`,
            draw edge labels connecting different modules
            with the local names of the input and output that are being connected.
        draw_module_hyperparameter_info (bool, optional): If `True`,
            draw the hyperparameters of a module alongside the module.
        graph_name (str, optional): Name of the file used to store the
            rendered graph. Only needs to be provided if we desire to output
            the graph to a file, rather than just show it.
        out_folderpath (str, optional): Folder to which to store the PDF file with
            the rendered graph. If no path is provided, no file is created.
        print_to_screen (bool, optional): Shows the result of rendering the
            graph directly to screen.
    """
    assert print_to_screen or out_folderpath is not None

    g = graphviz.Digraph()
    edge_fs = '10'
    h_fs = '10'
    penwidth = '1'

    def _draw_connected_input(ix_localname, ix):
        ox = ix.get_connected_output()
        if not draw_io_labels:
            label = ''
        else:
            ox_localname = None
            for ox_iter_localname, ox_iter in ox.get_module().outputs.items():
                if ox_iter == ox:
                    ox_localname = ox_iter_localname
                    break
            assert ox_localname is not None
            label = ix_localname + ':' + ox_localname

        g.edge(ox.get_module().get_name(),
               ix.get_module().get_name(),
               label=label,
               fontsize=edge_fs)

    def _draw_unconnected_input(ix_localname, ix):
        g.node(ix.get_name(),
               shape='invhouse',
               penwidth=penwidth,
               fillcolor='firebrick',
               style='filled')
        g.edge(ix.get_name(), ix.get_module().get_name())

    def _draw_module_hyperparameter(m, h_localname, h):
        if h.has_value_assigned():
            label = h_localname + '=' + str(h.get_value())
        else:
            label = h_localname

        g.edge(h.get_name(), m.get_name(), label=label, fontsize=edge_fs)

    def _draw_dependent_hyperparameter_relations(h_dep):
        for h_localname, h in h_dep._hyperps.items():
            if h.has_value_assigned():
                label = h_localname + '=' + str(h.get_value())
            else:
                label = h_localname

            g.edge(h.get_name(),
                   h_dep.get_name(),
                   label=label,
                   fontsize=edge_fs)

    def _draw_module_hyperparameter_info(m):
        g.node(
            m.get_name(),
            xlabel="<" + '<br align="right"/>'.join([
                '<FONT POINT-SIZE="%s">' % h_fs + h_localname +
                ('=' + str(h.get_value()) if h.has_value_assigned() else '') +
                "</FONT>" for h_localname, h in m.hyperps.items()
            ]) + ">")

    def _draw_output_terminal(ox_localname, ox):
        g.node(ox.get_name(),
               shape='house',
               penwidth=penwidth,
               fillcolor='deepskyblue',
               style='filled')
        g.edge(ox.get_module().get_name(), ox.get_name())

    nodes = set()

    def fn(m):
        """Adds the module information to the graph that is local to the module.
        """
        nodes.add(m.get_name())
        for ix_localname, ix in m.inputs.items():
            if ix.is_connected():
                _draw_connected_input(ix_localname, ix)
            else:
                _draw_unconnected_input(ix_localname, ix)

        if draw_hyperparameters:
            for h_localname, h in m.hyperps.items():
                _draw_module_hyperparameter(m, h_localname, h)

        if draw_module_hyperparameter_info:
            _draw_module_hyperparameter_info(m)
        return False

    # generate most of the graph.
    co.traverse_backward(outputs, fn)

    # drawing the hyperparameter graph.
    if draw_hyperparameters:
        hs = co.get_all_hyperparameters(outputs)

        for h in hs:
            if isinstance(h, co.DependentHyperparameter):
                _draw_dependent_hyperparameter_relations(h)

            g.node(h.get_name(),
                   fontsize=h_fs,
                   fillcolor='darkseagreen',
                   style='filled')

    # add the output terminals.
    for m in co.extract_unique_modules(list(outputs.values())):
        for ox_localname, ox in m.outputs.items():
            _draw_output_terminal(ox_localname, ox)

    # minor adjustments to attributes.
    for s in nodes:
        g.node(s,
               shape='rectangle',
               penwidth=penwidth,
               fillcolor='goldenrod',
               style='filled')

    if print_to_screen or out_folderpath is not None:
        g.render(graph_name,
                 out_folderpath,
                 view=print_to_screen,
                 cleanup=True)
Exemple #7
0
def set_is_training(outputs, is_training):
    def fn(mx):
        if hasattr(mx, 'is_training'):
            mx.is_training = is_training

    co.traverse_backward(outputs, fn)
def set_recompile(outputs, recompile):
    def fn(mx):
        mx._is_compiled = not recompile

    co.traverse_backward(outputs, fn)