Skip to content
164 changes: 164 additions & 0 deletions Wrappers/Python/cil/optimisation/functions/HuberLoss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
Comment thread
lauramurgatroyd marked this conversation as resolved.
Outdated

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
Comment thread
lauramurgatroyd marked this conversation as resolved.
Outdated
c : float, default 1.0
Scaling constant
weight : DataContainer, optional
Positive diagonal weights
Comment thread
lauramurgatroyd marked this conversation as resolved.
Outdated
"""

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")
Comment thread
lauramurgatroyd marked this conversation as resolved.
Outdated

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):

Comment thread
lauramurgatroyd marked this conversation as resolved.
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)
Comment on lines +248 to +252

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this is copied from Least squares but for a linear operator, calling A.norm() either accesses a stashed value or calls the power method itself. I don't understand in what cases the if statement on 157 will be triggered.

else:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, not sure what this else will pick up. If the operator is not linear and calculate_norm is not defined, the user will get a NotImplementedError. Perhaps we catch and return that with a bit more of an explanation?

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()
Comment on lines +265 to +266

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would do the same thing without building a new operator - I see arguments both ways

Suggested change
D = DiagonalOperator(self.weight)
self._weight_norm = D.norm()
self._weight_norm = self.weight.abs().max()

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
)
Loading