def load_er_data():
    frac_train = 0.9
    pattern = 'nrange-%d-%d-n_graph-%d-p-%.2f' % (
        cmd_args.min_n, cmd_args.max_n, cmd_args.n_graphs, cmd_args.er_p)

    num_train = int(frac_train * cmd_args.n_graphs)

    train_glist = []
    test_glist = []
    label_map = {}
    for i in range(cmd_args.min_c, cmd_args.max_c + 1):
        cur_list = load_pkl(
            '%s/ncomp-%d-%s.pkl' % (cmd_args.data_folder, i, pattern),
            cmd_args.n_graphs)
        assert len(cur_list) == cmd_args.n_graphs
        train_glist += [S2VGraph(cur_list[j], i) for j in range(num_train)]
        test_glist += [
            S2VGraph(cur_list[j], i) for j in range(num_train, len(cur_list))
        ]
        label_map[i] = i - cmd_args.min_c
    cmd_args.num_class = len(label_map)
    cmd_args.feat_dim = 1
    print('# train:', len(train_glist), ' # test:', len(test_glist))

    return label_map, train_glist, test_glist
def load_graphs():
    frac_train = 0.9
    pattern = 'nrange-%d-%d-n_graph-%d-p-%.2f' % (
        cmd_args.min_n, cmd_args.max_n, cmd_args.n_graphs, cmd_args.er_p)

    num_train = int(frac_train * cmd_args.n_graphs)

    train_glist = []
    test_glist = []
    label_map = {}
    for i in range(cmd_args.min_c, cmd_args.max_c + 1):
        cur_list = load_pkl(
            '%s/ncomp-%d-%s.pkl' % (cmd_args.data_folder, i, pattern),
            cmd_args.n_graphs)
        print('Input filename:\n',
              '%s/ncomp-%d-%s.pkl' % (cmd_args.data_folder, i, pattern))
        assert len(cur_list) == cmd_args.n_graphs
        print('----- type(cur_list) =\n', type(cur_list))
        print('----- type(cur_list[0]) =\n', type(cur_list[0]))
        train_glist += [S2VGraph(cur_list[j], i) for j in range(num_train)]
        test_glist += [
            S2VGraph(cur_list[j], i) for j in range(num_train, len(cur_list))
        ]
        label_map[i] = i - cmd_args.min_c

    print('# train:', len(train_glist), ' # test:', len(test_glist))

    return label_map, train_glist, test_glist
예제 #3
0
def load_graphs(graph_tuples, n_graphs, frac_train=None):
    
    if (frac_train is not None):
        frac_train = frac_train
    else:
        frac_train = 0.8
        
    num_train = int(frac_train * n_graphs)
    
    train_glist = [S2VGraph(graph_tuples[j]) for j in range(num_train)]
    test_glist = [S2VGraph(graph_tuples[j]) for j in range(num_train, n_graphs)]
    
    print('# train:', len(train_glist), ' # test:', len(test_glist))

    return train_glist, test_glist
    def step(self, actions):
        if self.first_nodes is None:  # pick the first node of edge
            assert self.n_steps % 2 == 0
            self.first_nodes = actions
            self.banned_list = []
            for i in range(len(self.g_list)):
                self.banned_list.append(
                    self.bannedActions(self.g_list[i].to_networkx(),
                                       self.first_nodes[i]))
        else:  # edge picked
            self.added_edges = []
            for i in range(len(self.g_list)):
                g = self.g_list[i].to_networkx()
                g.add_edge(self.first_nodes[i], actions[i])
                self.added_edges.append((self.first_nodes[i], actions[i]))
                self.g_list[i] = S2VGraph(g, label=self.g_list[i].label)
            self.first_nodes = None
            self.banned_list = None
        self.n_steps += 1

        if self.isTerminal():
            logits, _, acc = self.classifier(self.g_list)
            pred = logits.data.max(1, keepdim=True)[1]
            self.pred = pred.view(-1).cpu().numpy()
            self.rewards = (acc.view(-1).numpy() * -2.0 + 1.0).astype(
                np.float32)
def _load_graphs(graph_fname, graph_type, num_graph):
    cur_list = load_pkl('%s/%s' % (cmd_args.data_folder, graph_fname), num_graph)
    assert len(cur_list) == num_graph
    test_glist = [S2VGraph(g, graph_type) for g in cur_list]
    label_map = {i: i-1 for i in range(cmd_args.min_c, cmd_args.max_c+1)}
    print('# test:', len(test_glist))
    return label_map, test_glist
예제 #6
0
def propose_attack(model, s2v_g, num_added=1):
    g = s2v_g.to_networkx()
    comps = [c for c in nx.connected_component_subgraphs(g)]
    set_id = {}

    for i in range(len(comps)):
        for j in comps[i].nodes():
            set_id[j] = i

    node_feat, edge_feat, labels = model.PrepareFeatureLabel([s2v_g])
    if cmd_args.ctx == 'gpu':
        node_feat = node_feat.cuda()
        labels = labels.cuda()

    cand_list = [s2v_g]
    for l in range(len(model.label_map)):
        if l == s2v_g.label:
            continue
        labels[0] = l
        model.zero_grad()
        (_, embed), sp_dict = model.s2v([s2v_g],
                                        node_feat,
                                        edge_feat,
                                        pool_global=True,
                                        n2n_grad=True)
        _, loss, _ = model.mlp(embed, labels)
        loss.backward()
        grad = sp_dict['n2n'].grad.data.numpy().flatten()
        idxes = np.argsort(grad)
        added = []

        for p in idxes:
            x = p // s2v_g.num_nodes
            y = p % s2v_g.num_nodes
            if set_id[x] != set_id[y] or x == y or grad[p] > 0:
                continue
            added.append((x, y))
            if len(added) >= num_added:
                break
        if len(added) == 0:
            continue
        g2 = g.copy()
        g2.add_edges_from(added)

        cand_list.append(S2VGraph(g2, s2v_g.label))

    _, _, acc = model(cand_list)
    acc = acc.double().cpu().numpy()
    for i in range(len(cand_list)):
        if acc[i] < 1.0:
            return cand_list[i]
    return cand_list[0]
예제 #7
0
    def step(self, actions, _type=0):

        # if edge stub is none
        if self.first_nodes is None:  # pick the first node of edge
            assert self.n_steps % 2 == 0

            # set edge stub to action
            self.first_nodes = actions
            self.banned_list = []

            for i in range(len(self.g_list)):
                self.banned_list.append(
                    self.bannedActions(self.g_list[i].to_networkx(),
                                       self.first_nodes[i]))

        # if edge stub is not None
        else:

            #self.added_edges = []

            for i in range(len(self.g_list)):

                g = self.g_list[i].to_networkx()

                if _type:
                    # remove edge between edge stub and action

                    if g.has_edge(self.first_nodes[i], actions[i]):
                        g.remove_edge(self.first_nodes[i], actions[i])

                else:
                    # create edge between edge stub and action
                    g.add_edge(self.first_nodes[i], actions[i])

                self.added_edges.append((self.first_nodes[i], actions[i]))

                self.g_list[i] = S2VGraph(g, label=self.g_list[i].label)

            # set edge stub to none
            self.first_nodes = None
            self.banned_list = None

        self.n_steps += 1

        if self.isTerminal():
            logits, _, acc = self.classifier(self.g_list)
            pred = logits.data.max(1, keepdim=True)[1]
            self.pred = pred.view(-1).cpu().numpy()
            self.rewards = (acc.view(-1).numpy() * -2.0 + 1.0).astype(
                np.float32)
def propose_attack(model, s2v_g, num_added=1):
    g = s2v_g.to_networkx()
    comps = [c for c in nx.connected_component_subgraphs(g)]
    set_id = {}

    for i in range(len(comps)):
        for j in comps[i].nodes():
            set_id[j] = i

    cand = []
    for i in range(len(g) - 1):
        for j in range(i + 1, len(g)):
            if set_id[i] != set_id[j] or i == j:
                continue
            cand.append('%d %d' % (i, j))

    if cmd_args.rand_att_type == 'random':
        added = np.random.choice(cand, num_added)
        added = [(int(w.split()[0]), int(w.split()[1])) for w in added]
        g.add_edges_from(added)
        return S2VGraph(g, s2v_g.label)
    elif cmd_args.rand_att_type == 'exhaust':
        g_list = []
        for c in cand:
            x, y = [int(w) for w in c.split()]
            g2 = g.copy()
            g2.add_edge(x, y)
            g_list.append(S2VGraph(g2, s2v_g.label))
        _, _, acc = model(g_list)
        ans = g_list[0]
        for i in range(len(g_list)):
            if acc.numpy()[i] < 1:
                ans = g_list[i]
                break
        return ans
    else:
        raise NotImplementedError
    def get_fitness(self):
        g_list = []
        g = self.s2v_g.to_networkx()
        for edges in self.population:
            g2 = g.copy()
            g2.add_edge(edges[0][0], edges[0][1])
            #        g2.add_edges_from(edges)
            assert nx.number_connected_components(g2) == self.s2v_g.label
            g_list.append(S2VGraph(g2, self.s2v_g.label))

        log_ll, _, acc = self.classifier(g_list)
        acc = acc.cpu().double().numpy()
        if self.solution is None:
            for i in range(len(self.population)):
                if acc[i] < 1.0:
                    self.solution = self.population[i]
                    break
        nll = -log_ll[:, self.classifier.label_map[self.s2v_g.label]]
        return nll
예제 #10
0
def attackable(classifier, s2v_g, x = None, y = None):
    g = s2v_g.to_networkx()
    comps = [c for c in nx.connected_component_subgraphs(g)]
    set_id = {}

    for i in range(len(comps)):
        for j in comps[i].nodes():
            set_id[j] = i
    
    if x is not None:
        r_i = [x]
    else:
        r_i = range(len(g) - 1)

    g_list = []    
    for i in r_i:
        if y is not None:
            assert x is not None
            r_j = [y]
        else:
            if x is not None:
                r_j = range(len(g) - 1)
            else:
                r_j = range(i + 1, len(g))
        for j in r_j:
            if set_id[i] != set_id[j]:
                continue
            g2 = g.copy()
            g2.add_edge(i, j)
            assert nx.number_connected_components(g2) == s2v_g.label
            g_list.append(S2VGraph(g2, s2v_g.label))
    if len(g_list) == 0:
        print(x, y)
        print(g.edges(), s2v_g.label)
        print(set_id)
    assert len(g_list)
    _, _, acc = classifier(g_list)

    return np.sum(acc.view(-1).numpy()) < len(g_list)
예제 #11
0
    def run_simulation(self):

        self.env.setup(g_list)
        avg_rewards = []

        t_a, t_s = 0, 0

        for asdf in range(GLOBAL_EPISODE_STEPS):

            if asdf % 2 == 0:
                assert self.env.first_nodes == None

            for i in range(len(self.g_list)):

                g = self.g_list[i].to_networkx()

                con_nodes = list(set(list(sum(g.edges, ()))))
                for j in range(20):
                    if (j not in con_nodes):
                        rand_num = np.random.randint(0, 20)
                        g.add_edge(j, rand_num)
                        self.env.added_edges.append((j, rand_num))

                self.g_list[i] = S2VGraph(g, label=self.g_list[i].label)

            action_type = (asdf % 4) // 2

            # get Actions
            list_at, _ = self.make_actions(_type=action_type)

            # save State
            list_st = self.env.cloneState()

            cur_state = self.env.getStateRef()

            _, predicted_Q = self.net(cur_state,
                                      None,
                                      greedy_acts=False,
                                      _type=action_type)

            # get Rewards
            if self.env.first_nodes is not None:
                rewards = self.env.get_rewards(list_at, _type=action_type)
                avg_rewards.append(sum(rewards) / len(rewards))
            else:
                rewards = [0] * len(g_list)

            # Update graph to get S'
            self.env.step(list_at, _type=action_type)

            # get next state
            if env.isTerminal():
                s_prime = None
            else:
                s_prime = self.env.cloneState()

            # get S'and A' values
            try:
                sprime_at, q_primes = self.make_actions(_type=action_type)

            except:
                continue

            # Calculate Q(S', A')
            actual_Q = torch.Tensor(rewards) + torch.Tensor(q_primes)

            # Pass loss to network
            loss = F.mse_loss(predicted_Q, actual_Q)
            self.optimizer.zero_grad()
            loss.backward()
            self.optimizer.step()

        return avg_rewards