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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 | def fit(
self,
X: Float[torch.Tensor, "n_samples n_manifolds"],
y: Int[torch.Tensor, "n_samples"],
) -> ProductSpaceSVM:
"""Fit one-vs-rest SVMs on the product manifold data.
Args:
X: Training points tensor.
y: Integer class labels tensor.
Returns:
self: Fitted ProductSpaceSVM instance.
"""
# unique classes
self._store_classes(y)
n = X.shape[0]
# aggregated kernel
Ks, _ = product_kernel(self.pm, X, None)
K_sum = torch.ones((n, n), dtype=X.dtype, device=X.device)
for K_m, w in zip(Ks, self.weights, strict=False):
K_sum += w * K_m
X_np = X.detach().cpu().numpy()
K_np = K_sum.detach().cpu().numpy()
def sqrtm_psd(P: np.ndarray) -> Any:
w, V = np.linalg.eigh(P)
w_s = np.sqrt(np.clip(w, 0, None))
B = V @ np.diag(w_s) @ V.T
return (B + B.T) * 0.5
# containers
self.beta = {}
self.zeta = {}
self.epsilon = {}
self.b = {}
for cls in self.classes_:
cls_item = cls.item() if isinstance(cls, torch.Tensor) else cls
# one-vs-rest labels: +1 for cls, -1 for others
y_bin = torch.where(y == cls_item, 1, -1)
Y = torch.diagflat(y_bin).detach().cpu().numpy()
# variables
beta_var = cp.Variable(n)
zeta = cp.Variable(n, nonneg=True)
eps_var = cp.Variable(1)
b_var = cp.Variable(1)
# base constraints
constraints = [eps_var >= 0]
constraints.append(Y @ (K_np @ beta_var + b_var) >= eps_var - zeta)
# per-manifold SOC
for M, K_comp in zip(self.pm.P, Ks, strict=False):
P_np = K_comp.detach().cpu().numpy()
if M.type == "E" and self.e_constraints:
B = sqrtm_psd(P_np)
constraints.append(cp.norm(B @ beta_var, 2) <= 1.0)
elif M.type == "S" and self.s_constraints:
B = sqrtm_psd(P_np)
constraints.append(cp.norm(B @ beta_var, 2) <= np.sqrt(np.pi / 2))
elif M.type == "H" and self.h_constraints:
# PSD split
eigvals, eigvecs = np.linalg.eigh(P_np)
plus = np.clip(eigvals, 0, None)
minus = np.clip(-eigvals, 0, None)
Kp = (eigvecs @ np.diag(plus) @ eigvecs.T + (eigvecs @ np.diag(plus) @ eigvecs.T).T) * 0.5
Km = (eigvecs @ np.diag(minus) @ eigvecs.T + (eigvecs @ np.diag(minus) @ eigvecs.T).T) * 0.5
Bp = sqrtm_psd(Kp)
Bm = sqrtm_psd(Km)
C_H = abs(M.curvature)
R = -M.scale
r_h = abs(np.arcsinh(-(R**2) * C_H))
r = self.eps
constraints.append(cp.norm(Bm @ beta_var, 2) <= np.sqrt(max(r, 0.0)))
constraints.append(cp.norm(Bp @ beta_var, 2) <= np.sqrt(max(r + r_h, 0.0)))
# solve
prob = cp.Problem(cp.Minimize(-eps_var + cp.sum(zeta)), constraints)
prob.solve(solver="SCS")
# save results
self.beta[cls_item] = np.ravel(beta_var.value)
self.zeta[cls_item] = zeta.value
self.epsilon[cls_item] = float(eps_var.value)
self.b[cls_item] = float(b_var.value)
# store training data
self.X_train_ = torch.tensor(X_np, dtype=torch.float32)
self.is_fitted_ = True
return self
|