def main(emb_weights, training_file, validation_file, n_diagnosis_codes,
         n_labels, batch_size, dropout_rate, L2_reg, n_epoch, log_eps,
         n_claims, visit_size, hidden_size, model_name):
    options = locals().copy()
    print("Loading dataset...", file=log_f, flush=True)
    test = rnn_tools.load_data(validation_file)

    n_samples = NUM_ATTACK_SAMPLES

    attacker = Attacker(options, emb_weights)

    n_success = 0
    n_fail = 0

    total_node_change = 0

    n_iteration = 0

    saving_time = {}

    attack_code_dict = {}

    for i in range(n_samples):
        print("-------- %d ---------" % (i), file=log_f, flush=True)

        sample = test[0][i]

        y = test[1][i]

        print('* Processing:%d/%d samples' % (i, n_samples),
              file=log_f,
              flush=True)

        print("* Original: " + str(sample), file=log_f, flush=True)

        print("  Original label: %d" % (y), file=log_f, flush=True)

        st = time.time()

        maxCy, S_t, Code_S_t, success_flag, iteration = attacker.attack(
            sample, y)

        et = time.time()
        all_t = et - st

        if success_flag == 1:
            n_success += 1
            n_iteration += iteration
            total_node_change += len(Code_S_t)
            saving_time[i] = all_t
            attack_code_dict[i] = Code_S_t
        elif success_flag == -1:
            n_fail += 1

        print("* Result: ", file=log_f, flush=True)

        print(Code_S_t, file=log_f, flush=True)
        print(S_t, file=log_f, flush=True)
        print(maxCy, file=log_f, flush=True)

        print("  Nnumber of changed codes: %d" % (len(Code_S_t)),
              file=log_f,
              flush=True)

        print("  Number of iterations for this: " + str(iteration),
              file=log_f,
              flush=True)

        print(" Time: " + str(all_t), file=log_f, flush=True)

        print("* SUCCESS Number NOW: %d " % (n_success),
              file=log_f,
              flush=True)
        print("* Failure Number NOW: %d " % (n_fail), file=log_f, flush=True)

        if n_success:
            print("  Average Number of success changed codes: " +
                  str(float(total_node_change) / float(n_success)),
                  file=log_f,
                  flush=True)
            print("  Average Number of success iterations: " +
                  str(float(n_iteration) / float(n_success)),
                  file=log_f,
                  flush=True)

        if i % 20 == 0:
            pickle.dump(
                attack_code_dict,
                open(
                    './AttackCodes/codes_GB_%s_k=%d_t=%s_s=%d.pickle' %
                    (MODEL_TYPE, INDICE_K, str(TAU), SECONDS), 'wb'))
            pickle.dump(
                saving_time,
                open(
                    'Each_TimeNumber/time_GB_%s_k=%d_t=%s_s=%d.pickle' %
                    (MODEL_TYPE, INDICE_K, str(TAU), SECONDS), 'wb'))

    pickle.dump(
        attack_code_dict,
        open(
            './AttackCodes/codes_GB_%s_k=%d_t=%s_s=%d.pickle' %
            (MODEL_TYPE, INDICE_K, str(TAU), SECONDS), 'wb'))
    pickle.dump(
        saving_time,
        open(
            'Each_TimeNumber/time_GB_%s_k=%d_t=%s_s=%d.pickle' %
            (MODEL_TYPE, INDICE_K, str(TAU), SECONDS), 'wb'))

    print("--- Total Success Number: " + str(n_success) + " ---",
          file=log_f,
          flush=True)
    print("--- Total Attack Success Rate: " +
          str(float(n_success) / float(n_success + n_fail)),
          file=log_f,
          flush=True)

    print(TITLE, file=log_f, flush=True)
    print(TITLE)
Beispiel #2
0
def main(emb_weights, training_file, validation_file, testing_file,
         n_diagnosis_codes, n_labels, batch_size, dropout_rate, L2_reg,
         n_epoch, log_eps, n_claims, visit_size, hidden_size, use_gpu,
         model_name):
    options = locals().copy()
    print("Loading dataset...", file=log_f, flush=True)
    test = rnn_tools.load_data(training_file, validation_file, testing_file)

    n_people = NUM_ATTACK_SAMPLES

    attacker = Attacker(options, emb_weights)

    n_success = 89

    total_node_change = n_success * 1.101123595505618

    n_iteration = n_success * 1.101123595505618

    saving_time = {}

    attack_code_dict = {}

    # ****** #
    file1 = './AttackCodes/codes_Prim_sub_t=0.7_s=3600.pickle'

    attack_code_dict = pickle.load(open(file1, 'rb'))

    file2 = './Each_TimeNumber/time_Prim_sub_t=0.7_s=3600.pickle'

    saving_time = pickle.load(open(file2, 'rb'))
    # ****** #

    for i in range(100, n_people):
        print("-------- %d ---------" % (i), file=log_f, flush=True)

        person = test[0][i]

        y = test[1][i]

        n_visit = len(person)

        print(
            '* Processing:%d/%d person, number of visit for this person: %d' %
            (i, n_people, n_visit),
            file=log_f,
            flush=True)

        print("* Original: " + str(person), file=log_f, flush=True)

        print("  Original label: %d" % (y), file=log_f, flush=True)

        st = time.time()
        greedy_set, greedy_set_best_temp_person, best_Cy, num_changed, success_flag, iteration = attacker.attack(
            person, y)

        et = time.time()
        all_t = et - st

        if success_flag:
            n_success += 1
            n_iteration += iteration
            total_node_change += num_changed

            saving_time[i] = all_t
            attack_code_dict[i] = list(greedy_set)

        print("* Result: ", file=log_f, flush=True)
        print(greedy_set, file=log_f, flush=True)
        print(greedy_set_best_temp_person, file=log_f, flush=True)
        print(best_Cy, file=log_f, flush=True)

        print("  Nnumber of changed codes: %d" % (num_changed),
              file=log_f,
              flush=True)

        print("  Number of iterations for this: " + str(iteration),
              file=log_f,
              flush=True)

        print(" Time: " + str(all_t), file=log_f, flush=True)

        print("* SUCCESS Number NOW: %d " % (n_success),
              file=log_f,
              flush=True)

        if n_success:
            print("  Average Number of success changed codes: " +
                  str(float(total_node_change) / float(n_success)),
                  file=log_f,
                  flush=True)
            print("  Average Number of success iterations: " +
                  str(float(n_iteration) / float(n_success)),
                  file=log_f,
                  flush=True)

            if i % 20 == 0:
                pickle.dump(
                    attack_code_dict,
                    open(
                        './AttackCodes/codes_Prim_%s_t=%s_s=%d.pickle' %
                        (MODEL_TYPE, str(TAU), SECONDS), 'wb'))
                pickle.dump(
                    saving_time,
                    open(
                        './Each_TimeNumber/time_Prim_%s_t=%s_s=%d.pickle' %
                        (MODEL_TYPE, str(TAU), SECONDS), 'wb'))

        pickle.dump(
            attack_code_dict,
            open(
                './AttackCodes/codes_Prim_%s_t=%s_s=%d.pickle' %
                (MODEL_TYPE, str(TAU), SECONDS), 'wb'))
        pickle.dump(
            saving_time,
            open(
                './Each_TimeNumber/time_Prim_%s_t=%s_s=%d.pickle' %
                (MODEL_TYPE, str(TAU), SECONDS), 'wb'))

    print("--- Total Success Number: " + str(n_success) + " ---",
          file=log_f,
          flush=True)
    print(TITLE)
    print(TITLE, file=log_f, flush=True)
def main(emb_weights, training_file, validation_file,
                                       testing_file, n_diagnosis_codes, n_labels,
                                       batch_size, dropout_rate,
                                       L2_reg, n_epoch, log_eps, n_claims, visit_size, hidden_size,
                                       use_gpu, model_name):
    options = locals().copy()

    print("Loading dataset...", file=log_f, flush=True)
    test = rnn_tools.load_data(training_file, validation_file, testing_file)

    n_people = NUM_ATTACK_SAMPLES

    attacker = Attacker(options, emb_weights)

    n_success = 0
    n_fail = 0

    total_node_change = 0

    n_iteration = 0

    saving_time = {}

    attack_code_dict = {}

    # # ****** #
    # file1 = './AttackCodes/codes_QL_sub_k=10_t=0.5_s=180.pickle'
    #
    # attack_code_dict = pickle.load(open(file1, 'rb'))
    #
    # file2 = './Each_TimeNumber/time_QL_sub_k=10_t=0.5_s=180.pickle'
    #
    # saving_time = pickle.load(open(file2, 'rb'))
    # # ****** #

    for i in range(n_people):
        print("-------- %d ---------" % (i), file=log_f, flush=True)

        person = test[0][i]

        y = test[1][i]

        n_visit = len(person)

        print('* Processing:%d/%d person, number of visit for this person: %d' % (i, n_people, n_visit), file=log_f, flush=True)

        print("* Original: "+str(person), file=log_f, flush=True)

        print("  Original label: %d" % (y), file=log_f, flush=True)

        st = time.time()
        changed_person, score, num_changed, success_flag, iteration, changed_pos = attacker.attack(person, y)

        et = time.time()
        all_t = et-st

        if success_flag == 1:
            n_success += 1
            n_iteration += iteration
            total_node_change += num_changed

            saving_time[i] = all_t
            attack_code_dict[i] = list(changed_pos)
        elif success_flag == -1:
            n_fail += 1

        print("* Result: ", file=log_f, flush=True)

        print(changed_pos, file=log_f, flush=True)
        print(changed_person, file=log_f, flush=True)

        print(score, file=log_f, flush=True)

        print("  Nnumber of changed codes: %d" % (num_changed), file=log_f, flush=True)

        print("  Number of iterations for this: " + str(iteration), file=log_f, flush=True)

        print(" Time: "+str(all_t), file=log_f, flush=True)

        print("* SUCCESS Number NOW: %d " % (n_success), file=log_f, flush=True)
        print("* Failure Number NOW: %d " % (n_fail), file=log_f, flush=True)

        if n_success:
            print("  Average Number of success changed codes: " + str(float(total_node_change)/float(n_success)), file=log_f, flush=True)
            print("  Average Number of success iterations: " + str(float(n_iteration) / float(n_success)), file=log_f, flush=True)

        if i % 20 == 0:
            pickle.dump(attack_code_dict, open('./AttackCodes/codes_QL_%s_k=%d_t=%s_s=%d.pickle' % (MODEL_TYPE, INDICE_K, str(TAU), SECONDS), 'wb'))
            pickle.dump(saving_time, open('./Each_TimeNumber/time_QL_%s_k=%d_t=%s_s=%d.pickle' % (MODEL_TYPE, INDICE_K, str(TAU), SECONDS), 'wb'))

    pickle.dump(attack_code_dict, open('./AttackCodes/codes_QL_%s_k=%d_t=%s_s=%d.pickle' % (MODEL_TYPE, INDICE_K, str(TAU), SECONDS), 'wb'))
    pickle.dump(saving_time, open('./Each_TimeNumber/time_QL_%s_k=%d_t=%s_s=%d.pickle' % (MODEL_TYPE, INDICE_K, str(TAU), SECONDS), 'wb'))

    print("--- Total Success Number: " + str(n_success) + " ---", file=log_f, flush=True)
    print("--- Total Attack Success Rate: " + str(float(n_success) / float(n_success + n_fail)), file=log_f, flush=True)

    print(TITLE, file=log_f, flush=True)
    print(TITLE)