-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathHuberLoss.py
More file actions
164 lines (124 loc) · 4.02 KB
/
Copy pathHuberLoss.py
File metadata and controls
164 lines (124 loc) · 4.02 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import numpy as np
import warnings
from numbers import Number
from cil.optimisation.functions import Function
from cil.optimisation.operators import DiagonalOperator, LinearOperator
from cil.framework import DataContainer
class HuberLoss(Function):
r"""
(Weighted) Huber loss
For residual r = Ax - b:
phi_delta(r) =
0.5 * r^2 if |r| <= delta
delta * (|r| - 0.5*delta) otherwise
Parameters
----------
A : LinearOperator
b : Data, DataContainer
huber_delta : float
Transition point between L2 and L1 behaviour
c : float, default 1.0
Scaling constant
weight : DataContainer, optional
Positive diagonal weights
"""
def __init__(self, A, b, huber_delta, c=1.0, weight=None):
super(HuberLoss, self).__init__()
if huber_delta <= 0:
raise ValueError("huber_delta must be positive")
self.A = A
self.b = b
self.c = c
self.huber_delta = huber_delta
self.weight = weight
self._weight_norm = None
if weight is not None:
if (self.weight < 0).any():
raise ValueError("Weight contains negative values")
def __call__(self, x):
r = self.A.direct(x)
r.subtract(self.b, out=r)
abs_r = r.abs()
# m = min(|r|, delta)
m = abs_r.copy()
m.minimum(self.huber_delta, out=m)
# 0.5 * m^2
val = m.power(2)
val.multiply(0.5, out=val)
# delta * (|r| - m)
lin = abs_r.copy()
lin.subtract(m, out=lin)
lin.multiply(self.huber_delta, out=lin)
val.add(lin, out=val)
if self.weight is not None:
val.multiply(self.weight, out=val)
return self.c * val.sum()
def gradient(self, x, out=None):
if out is None:
out = x * 0.0
r = self.A.direct(x)
r.subtract(self.b, out=r)
abs_r = r.abs()
# m = min(|r|, delta)
m = abs_r.copy()
m.minimum(self.huber_delta, out=m)
# grad wrt residual: sign(r) * m
grad_r = r.sign()
grad_r.multiply(m, out=grad_r)
if self.weight is not None:
grad_r.multiply(self.weight, out=grad_r)
self.A.adjoint(grad_r, out=out)
out.multiply(self.c, out=out)
return out
@property
def L(self):
if self._L is None:
self.calculate_Lipschitz()
return self._L
@L.setter
def L(self, value):
warnings.warn("You should set the Lipschitz constant with calculate_Lipschitz().")
if isinstance(value, Number) and value >= 0:
self._L = value
else:
raise TypeError("The Lipschitz constant must be non-negative")
def calculate_Lipschitz(self):
"""
Lipschitz constant of gradient.
For Huber:
max phi'' = 1
so:
L = c * ||A||^2
(weighted: multiplied by ||W||)
"""
try:
self._L = np.abs(self.c) * (self.A.norm() ** 2)
except AttributeError:
if self.A.is_linear():
Anorm = LinearOperator.PowerMethod(self.A, 10)[0]
self._L = np.abs(self.c) * (Anorm * Anorm)
else:
warnings.warn(
f"{self.__class__.__name__} could not calculate Lipschitz Constant."
)
if self.weight is not None:
self._L *= self.weight_norm
@property
def weight_norm(self):
if self.weight is not None:
if self._weight_norm is None:
D = DiagonalOperator(self.weight)
self._weight_norm = D.norm()
else:
self._weight_norm = 1.0
return self._weight_norm
def __rmul__(self, other):
if not isinstance(other, Number):
raise NotImplemented
return HuberLoss(
A=self.A,
b=self.b,
huber_delta=self.huber_delta,
c=self.c * other,
weight=self.weight
)