Exemplo n.º 1
0
 def comp_arch(self, action):
     arch = copy.deepcopy(self)
     arch.action = action
     n = arch.n
     V = arch.V
     p = 0
     for i in range(n):
         if action[p]:
             V[i].replace(layer.Identity())
         p += 1
     in_shapes = [V[i].in_shape for i in range(n)]
     out_shapes = [V[i].out_shape for i in range(n)]
     for g in self.groups:
         F = max(1, int((1.0 - action[p]) * g.F))
         for j in g.inter:
             V[j].shrink(F, F)
         for j in g.in_only:
             Fo = list(V[j].out_shape)[1]
             V[j].shrink(F, Fo)
         for j in g.out_only:
             Fi = list(V[j].in_shape)[1]
             V[j].shrink(Fi, F)
         p += 1
     for i in range(n):
         for j in range(i + 1, n):
             if out_shapes[i] == in_shapes[j]:
                 if action[p]:
                     arch.E[i][j] = True
                 p += 1
     arch.in_links, arch.out_links = gr.get_links(arch.E)
     arch.init_rep()
     arch.to(opt.device)
     return arch
Exemplo n.º 2
0
 def __init__(self, n, V, E):
     super(Architecture, self).__init__()
     self.n = n
     self.V = V
     for i in range(n):
         self.add_module('layer_%d' % (i), V[i])
     self.groups = gr.get_groups(V)
     self.E = E
     self.in_links, self.out_links = gr.get_links(E)
     self.init_rep()
Exemplo n.º 3
0
 def generate_plot(self, plots=100):
     '''Use generate and test-method to select best possible plot and
     character combination.
     '''
     all_plots = []
     for i in range(plots):
         action_list, actor1, actor2 = self.get_characters_and_actions()
         links = graph.get_links(self.action_graph, action_list)
         evaluation = self.evaluate_plot(actor1, actor2,
                                         action_list, links)
         all_plots.append((actor1, actor2, action_list, links, evaluation))
     all_plots.sort(key=lambda x: x[4], reverse=True)
     return all_plots[0]
Exemplo n.º 4
0
    def comp_arch(self, action):
        arch = copy.deepcopy(self)
        arch.action = action
        n = arch.n
        V = arch.V
        p = 0
        for i in range(n):
            if action[p]:
                V[i].replace(layer.Identity())
            p += 1
        in_shapes = [V[i].in_shape for i in range(n)]
        out_shapes = [V[i].out_shape for i in range(n)]
        flat_output = 0
        for g in self.groups:
            F = max(1, int((1.0 - action[p]) * g.F))

            for j in g.out_only:
                if (isinstance(V[j].base, (Flatten)) and
                    not action[j]):
                    if j == 0:
                        F = g.F
                    else:
                        F = flat_output
                        break

            for j in g.inter:
                V[j].shrink(F, F)
            for j in g.in_only:
                Fo = list(V[j].out_shape)[1]
                V[j].shrink(F, Fo)
            for j in g.out_only:
                Fi = list(V[j].in_shape)[1]
                V[j].shrink(Fi, F)

            for j in g.in_only:
                if (isinstance(V[j].base, (Flatten)) and
                    not action[j]):
                    flat_output = int(F*torch.prod(torch.tensor(V[j].in_shape[2:])).item())
                    break

            p += 1
        for i in range(n):
            for j in range(i + 1, n):
                if out_shapes[i] == in_shapes[j]:
                    if action[p]:
                        arch.E[i][j] = True
                    p += 1
        arch.in_links, arch.out_links = gr.get_links(arch.E)
        arch.init_rep()
        arch.to(opt.device)
        return arch
Exemplo n.º 5
0
 def __init__(self, n, V, E):
     super(Architecture, self).__init__()
     self.n = n
     self.V = V
     for i in range(n):
         self.add_module('layer_%d' % (i), V[i])
     self.groups = gr.get_groups(V)
     #for g in self.groups:
     #    print(g.F)
     #    print("in layer")
     #    print(g.in_layers)
     #    print("out layer")
     #    print(g.out_layers)
     #    print("__________________")
     self.E = E
     self.in_links, self.out_links = gr.get_links(E)
     self.init_rep()
Exemplo n.º 6
0
    def comp_arch_rand_sfn(self):
        def shrink_n(F, ratio):
            m = opt.ar_channel_mul
            return max(1, int(ceil((1.0 - ratio) * F / m))) * m

        arch = copy.deepcopy(self)
        n = arch.n
        V = arch.V

        p1 = random.choice(opt.ar_p1)
        for i in range(n):
            if (random.random() < p1 and V[i].in_shape == V[i].out_shape
                    and i not in [11, 50, 125]):
                V[i].replace(models.Identity())

        opt.ar_p2[1] = min(0.9, opt.ar_p2[1])
        for g in self.groups:
            p2 = random.uniform(*opt.ar_p2)
            for j in g.inter:
                Fi = shrink_n(list(V[j].in_shape)[1], p2)
                Fo = shrink_n(list(V[j].out_shape)[1], p2)
                V[j].shrink(Fi, Fo)
            for j in g.in_only:
                Fi = shrink_n(list(V[j].in_shape)[1], p2)
                Fo = list(V[j].out_shape)[1]
                V[j].shrink(Fi, Fo)
            for j in g.out_only:
                Fi = list(V[j].in_shape)[1]
                Fo = shrink_n(list(V[j].out_shape)[1], p2)
                V[j].shrink(Fi, Fo)

        F1 = list(V[2].out_shape)[1]
        F4 = list(V[13].out_shape)[1]
        F3 = list(V[13].in_shape)[1]
        F2 = F4 - F3
        V[11].shrink(F1, F2)
        V[12].shrink(F2, F2)

        F1 = list(V[41].out_shape)[1]
        F4 = list(V[52].out_shape)[1]
        F3 = list(V[52].in_shape)[1]
        F2 = F4 - F3
        V[50].shrink(F1, F2)
        V[51].shrink(F2, F2)

        F1 = list(V[116].out_shape)[1]
        F4 = list(V[127].out_shape)[1]
        F3 = list(V[127].in_shape)[1]
        F2 = F4 - F3
        V[125].shrink(F1, F2)
        V[126].shrink(F2, F2)

        p3 = random.choice(opt.ar_p3)
        for i in range(n):
            for j in range(i + 1, n):
                if (random.random() < p3 and V[i].out_shape == V[j].in_shape
                        and not isinstance(V[j].base,
                                           (models.Concat, models.Identity))):
                    arch.E[i][j] = True

        arch.in_links, arch.out_links = gr.get_links(arch.E)
        arch.init_rep()
        arch.to(opt.device)
        return arch