Beispiel #1
0
    def __init__(self, index, modules, apollo_net):
        self.index = index
        self.apollo_net = apollo_net

        # TODO eww
        counter = [0]

        def number(tree):
            r = counter[0]
            counter[0] += 1
            return r

        numbered = util.tree_map(number, modules)
        assert util.flatten(numbered) == range(len(util.flatten(numbered)))

        def children(tree):
            if not isinstance(tree, tuple):
                return str(())
            # TODO nasty hack to make flatten behave right
            return str(
                tuple(c[0] if isinstance(c, tuple) else c for c in tree[1:]))

        child_annotated = util.tree_map(children, numbered)

        self.modules = util.flatten(modules)
        self.children = [eval(c) for c in util.flatten(child_annotated)]
Beispiel #2
0
    def __init__(self, index, modules, apollo_net):
        self.index = index
        self.apollo_net = apollo_net

        # TODO eww
        counter = [0]

        def number(tree):
            r = counter[0]
            counter[0] += 1
            return r

        numbered = util.tree_map(number, modules)
        assert util.flatten(numbered) == range(len(util.flatten(numbered)))

        def children(tree):
            if not isinstance(tree, tuple):
                return str(())
            # TODO nasty hack to make flatten behave right
            return str(
                tuple(c[0] if isinstance(c, tuple) else c for c in tree[1:]))

        child_annotated = util.tree_map(children, numbered)

        self.modules = util.flatten(modules)
        self.children = [eval(c) for c in util.flatten(child_annotated)]
Beispiel #3
0
def featurize_layouts(datum, max_layouts):
    # TODO pre-fill module type index
    layout_reprs = np.zeros((max_layouts, len(MODULE_INDEX) + 7))
    for i_layout in range(len(datum.layouts)):
        layout = datum.layouts[i_layout]
        labels = util.flatten(layout.labels)
        modules = util.flatten(layout.modules)
        for i_mod in range(len(modules)):
            if isinstance(modules[i_mod], MLPFindModule) or isinstance(modules[i_mod], MultiplicativeFindModule):
                layout_reprs[i_layout, labels[i_mod]] += 1
            mt = MODULE_TYPE_INDEX.index(modules[i_mod])
            layout_reprs[i_layout, len(MODULE_INDEX) + mt] += 1
    return layout_reprs
Beispiel #4
0
def featurize_layouts(datum, max_layouts):
    # TODO pre-fill module type index
    layout_reprs = np.zeros((max_layouts, len(MODULE_INDEX) + 7))
    for i_layout in range(len(datum.layouts)):
        layout = datum.layouts[i_layout]
        labels = util.flatten(layout.labels)
        modules = util.flatten(layout.modules)
        for i_mod in range(len(modules)):
            if isinstance(modules[i_mod], MLPFindModule) or isinstance(
                    modules[i_mod], MultiplicativeFindModule):
                layout_reprs[i_layout, labels[i_mod]] += 1
            mt = MODULE_TYPE_INDEX.index(modules[i_mod])
            layout_reprs[i_layout, len(MODULE_INDEX) + mt] += 1
    return layout_reprs
Beispiel #5
0
    def forward(self, label_data, features, rel_features, dropout):

        flat_data = [util.flatten(d) for d in label_data]
        flat_data = np.asarray(flat_data)
        outputs = [None for i in range(len(self.modules))]
        for i in reversed(range(len(self.modules))):
            bottoms = [outputs[j] for j in self.children[i]]
            assert None not in bottoms
            mod_index = self.index * 100 + i
            output = self.modules[i].forward(mod_index, flat_data[:, i],
                                             bottoms, features, rel_features,
                                             dropout, self.apollo_net)
            outputs[i] = output

        self.outputs = outputs
        return outputs[0]
Beispiel #6
0
    def forward(self, label_data, features, rel_features, dropout):

        flat_data = [util.flatten(d) for d in label_data]
        flat_data = np.asarray(flat_data)
        outputs = [None for i in range(len(self.modules))]
        for i in reversed(range(len(self.modules))):
            bottoms = [outputs[j] for j in self.children[i]]
            assert None not in bottoms
            mod_index = self.index * 100 + i
            output = self.modules[i].forward(mod_index, flat_data[:, i],
                                             bottoms, features, rel_features,
                                             dropout, self.apollo_net)
            outputs[i] = output

        self.outputs = outputs
        return outputs[0]