-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestimate_l1_sensitivity_genome.py
80 lines (58 loc) · 2.1 KB
/
estimate_l1_sensitivity_genome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from util import Embedder
import numpy as np
import argparse
import random
from scipy.stats import describe
from tqdm import tqdm
parser = argparse.ArgumentParser(description='Genome Attack Aux.')
parser.add_argument("-a", type=str, default='bert', help = 'targeted architecture')
parser.add_argument("-p", type=int, default= 5555, help = 'the comm port the client will use')
ARGS = parser.parse_args()
ARCH = ARGS.a
TOTAL_LEN = 20
embedder = Embedder(ARGS.p)
embedding = embedder.embedding # export the functional port
TABLE = {
"A": 0,
"G": 1,
"C": 2,
"T": 3
}
REVERSE_TABLE = ["A", "G", "C", "T"]
def gen(target = 0):
local_reverse_table = REVERSE_TABLE.copy()
seq = [random.choice(REVERSE_TABLE) for i in range(TOTAL_LEN)]
local_reverse_table.remove(seq[target])
diff_seq = seq[:target] + [random.choice(local_reverse_table)] + seq[target+1:]
return "".join(seq), "".join(diff_seq)
def get_samples(sample_size):
batch = []
TARGETS = list(range(TOTAL_LEN))
for i in range(sample_size):
target = random.choice(TARGETS)
batch.append(gen(target))
# print([b[0] for b in batch])
# print([b[1] for b in batch])
z = embedding([b[0] for b in batch], "genome.dp.0", ARCH, cached = False)
z_prime = embedding([b[1] for b in batch], "genome.dp.1", ARCH, cached = False)
return z, z_prime
def estimate_sensitivity_mean(z, z_prime):
delta = 0.0
for i in range(z.shape[0]):
delta += np.linalg.norm(z[i, :] - z_prime[i, :], ord = 1)
return delta / z.shape[0]
def estimate_sensitivity_max(z, z_prime):
max_delta = 0.0
for i in range(z.shape[0]):
delta = np.linalg.norm(z[i, :] - z_prime[i, :], ord = 1)
max_delta = max(max_delta, delta)
return max_delta
if __name__ == '__main__':
rep_time = 10
l1_senses = []
print("ESTIMATE {}".format(ARCH))
for i in tqdm(range(rep_time)):
z, z_prime = get_samples(1000)
l1_sense = estimate_sensitivity_mean(z, z_prime)
l1_senses.append(l1_sense)
print(describe(l1_senses))