-
Notifications
You must be signed in to change notification settings - Fork 5
/
objective.py
67 lines (53 loc) · 1.69 KB
/
objective.py
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
import numpy as np
from benchopt import BaseObjective
def amari_distance(W, A):
"""
Computes the Amari distance between two matrices W and A.
It cancels when WA is a permutation and scale matrix.
Parameters
----------
W : ndarray, shape (n_features, n_features)
Input matrix
A : ndarray, shape (n_features, n_features)
Input matrix
Returns
-------
d : float
The Amari distance
"""
P = np.dot(W, A)
def s(r):
return np.sum(np.sum(r ** 2, axis=1) / np.max(r ** 2, axis=1) - 1)
return (s(np.abs(P)) + s(np.abs(P.T))) / (2 * P.shape[0])
class Objective(BaseObjective):
min_benchopt_version = "1.5"
name = "Joint Diagonalization"
parameters = {
'ortho': [False, True]
}
def __init__(self, ortho=False):
self.ortho = ortho
def set_data(self, C, A):
self.C = C
self.A = A
def get_one_result(self):
return np.eye((self.C.shape[1]))
def evaluate_result(self, B):
if self.ortho:
is_ortho = np.linalg.norm(
B @ B.T - np.eye(len(B)), ord=np.inf) < 1e-6
if not is_ortho:
return np.inf # constraint is violated
D = B @ self.C @ B.T # diagonalization with B
n_matrices, _, _ = D.shape
diagonals = np.diagonal(D, axis1=1, axis2=2)
nll = np.sum(np.log(diagonals))
for Di in D:
nll -= np.linalg.slogdet(Di)[1]
nll /= 2 * n_matrices
return {
'value': nll, # negative log-likelihood
'Amari distance': amari_distance(B, self.A)
}
def get_objective(self):
return dict(C=self.C, ortho=self.ortho)