forked from TrustAI/DeepConcolic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorms.py
70 lines (43 loc) · 991 Bytes
/
norms.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
from abc import abstractmethod
from engine import Metric
import numpy as np
# ---
class Norm (Metric):
'''
Just an alias for norms.
'''
def close_to(self, refs, x):
for r in refs:
if self.distance (r, x) <= self.factor:
return True;
return False
# ---
class L0 (Norm):
'''
L0 norm.
'''
def __init__(self, scale = 255, **kwds):
super().__init__(scale = scale, **kwds)
def __repr__(self):
return 'L0'
@property
def is_int(self):
return True
def distance(self, x, y):
return np.count_nonzero (np.abs (x - y) * self.scale > 1)
def close_to(self, refs, x):
size = refs[0].size * self.factor
for diff in refs - x:
if np.count_nonzero (np.abs (diff) * self.scale > 1) <= size:
return True
return False
# ---
class LInf (Norm):
'''
L-inf norm.
'''
def __repr__(self):
return 'Linf'
def distance(self, x, y):
return np.amax (np.absolute (x - y) * self.scale)
# ---