-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
40 lines (30 loc) · 1.56 KB
/
utils.py
File metadata and controls
40 lines (30 loc) · 1.56 KB
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
import tensorflow as tf
# Ref - https://github.com/xingjunm/lid_adversarial_subspace_detection
# Ref - "Characterizing Adversarial Subspaces Using Local Intrinsic Dimensionality". ICLR 2018
def get_lid_score(data, k=21):
# pairwise distance
r = tf.reduce_sum(data ** 2, axis=1)
r = tf.reshape(r, [-1, 1])
distance = tf.sqrt(r - 2 * tf.matmul(data, tf.transpose(data)) + tf.transpose(r) + 1e-9)
# find the k nearest neighbor
temp, _ = tf.nn.top_k(-distance, k=k, sorted=True)
top_k_distance = -temp[:, 1:]
m = tf.transpose(tf.multiply(tf.transpose(top_k_distance), 1.0 / top_k_distance[:, -1]))
lids = (1 - k) / tf.reduce_sum(tf.math.log(m + 1e-9), axis=1)
return tf.reduce_mean(lids)
def euclidean_distance(x, y):
return tf.broadcast_to(tf.reshape(tf.reduce_sum(x ** 2, axis=-1), (1, x.shape[0])), (y.shape[0], x.shape[0])) + \
tf.broadcast_to(tf.reshape(tf.reduce_sum(y ** 2, axis=-1), (y.shape[0], 1)), (y.shape[0], x.shape[0])) - \
2 * tf.matmul(y, tf.transpose(x))
def accuracy(y_true, y_pred):
same = tf.equal(tf.argmax(y_true, axis=-1), tf.argmax(y_pred, axis=-1))
return tf.reduce_mean(tf.cast(same, tf.float32))
def generate_exp_string(profile: dict, data_type=True):
exp_string = ''
for x in list(profile):
value = profile[x].__name__ if hasattr(profile[x], '__name__') else str(profile[x])
if data_type:
exp_string += '{}:{}:{}-'.format(x, type(profile[x]).__name__, value)
else:
exp_string += '{}:{}-'.format(x, value)
return exp_string[:-1]