Skip to content

Commit aedfd61

Browse files
authored
smr_alignment.MetricAlignmentOptimizer: Make entropy penalty convex (#19)
Previously, `MetricAlignmentOptimizer.optimize_weights()` used the `cvxpy` built-in `cvxpy.entr(x)` entropy penalty. What I didn't realise until I ran the optimizer on real data, is that this function is actually concave. Therefore, it is possible, that the overall optimization problem is not convex anymore, messing up our convex solver. This adapts the entropy penalty to be a basic L2 penalty, which is convex. Fixes #16
1 parent 8d99d79 commit aedfd61

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

smr_alignment/src/smr_alignment/metric_alignment.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ def optimize_weights(
147147
loss = cp.sum(cp.pos(margins)) # piecewise-linear convex
148148

149149
if entropy_lambda > 0:
150-
# cp.entr(x) = x*log(x); we add small offset to keep it defined near zero
151-
loss += entropy_lambda * cp.sum(cp.entr(w + 1e-16))
150+
# we add small offset to keep it defined near zero
151+
n = len(metrics)
152+
u = np.full(n, 1.0 / n)
153+
loss += entropy_lambda * cp.sum_squares(w - u)
152154

153155
constraints = [cp.sum(w) == 1.0] # cp.sum(w) == 1.0 in CVXPY doesn’t return a plain bool at runtime,
154156
# it returns a Constraint object, confusing type checkers. Therefore, the "type: ignore" below.

0 commit comments

Comments
 (0)