Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/structsvm/hamming_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ def __init__(

self.set_coefficients(coefficients)
self.set_offset(offset)
self.set_scaling_factor(np.sum(mask) if mask is not None else ground_truth.size)
6 changes: 6 additions & 0 deletions src/structsvm/linear_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ def get_coefficients(self) -> np.ndarray:

def get_offset(self) -> float:
return self.offset

def set_scaling_factor(self, scaling_factor: float) -> None:
self.scaling_factor = scaling_factor

def get_scaling_factor(self) -> float:
return self.scaling_factor
10 changes: 8 additions & 2 deletions src/structsvm/soft_margin_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ def __init__(
self._costs = costs
self._b = self._costs.get_offset()
self._g = self._costs.get_coefficients()
self._scale = self._costs.get_scaling_factor()

self._b = self._b / self._scale
self._g = self._g / self._scale
self._features = self._features / self._scale

# combined features of the ground truth and current y*
self._d = features @ ground_truth
self._d = self._features @ ground_truth

# setup solver
self._solver = ilpy.Solver(self._num_variables, ilpy.VariableType.Binary)
Expand Down Expand Up @@ -106,7 +111,8 @@ def value_and_gradient(self, w: np.ndarray) -> tuple[float, np.ndarray]:
solution = self._solver.solve()

# read optimal value L(w)
value = solution.get_value()
# get_value() excludes the constant term, so add it back
value = solution.get_value() + a + self._b

# ∂L(w)/∂w = φ(x')y' - φ(x')y*
# = d - e
Expand Down
Loading