I am using the qndiag library to try to find a diagonalisation for 2 given matrices.
The github is here : qndiag libray
I am using this Python script to compute these 2 diagonalisation as closed as possible :
import os, sys
import numpy as np
from qndiag import qndiag
# dimension
m=7
# number of matrices
n=2
# Load spectro and WL+GCph+XC
FISH_GCsp = np.loadtxt('Fisher_GCsp_flat.txt')
FISH_XC = np.loadtxt('Fisher_XC_GCph_WL_flat.txt')
# Marginalizing over uncommon parameters between the two matrices
COV_GCsp_first = np.linalg.inv(FISH_GCsp)
COV_XC_first = np.linalg.inv(FISH_XC)
COV_GCsp = COV_GCsp_first[0:m,0:m]
COV_XC = COV_XC_first[0:m,0:m]
# Invert to get Fisher matrix
FISH_sp = np.linalg.inv(COV_GCsp);
FISH_xc = np.linalg.inv(COV_XC);
# Drawing a random set of commuting matrices
C=np.zeros((n,m,m));
C[0,:,:] = FISH_sp
C[1,:,:] = FISH_xc
[D, B] = qndiag(C, 'max_iter', 1000, 'tol', 1e-3);
# Print expected diagonal matrices
B*C[0,:,:]*B.T
B*C[1,:,:]*B.T
Given my 2 matrices 7x7 FISH_sp
and FISH_xc
, I get an error of kind :
approximate_joint_diagonalization_qndiag/qndiag/qndiag/qndiag.py", line 90, in qndiag
D = transform_set(B, C)
and following
approximate_joint_diagonalization_qndiag/qndiag/qndiag/qndiag.py", line 151, in transform_set
op[i] = M.dot(d.dot(M.T))
AttributeError: 'str' object has no attribute 'dot'
Here the concerned function transform_set
:
def transform_set(M, D, diag_only=False):
n, p, _ = D.shape
if not diag_only:
op = np.zeros((n, p, p))
for i, d in enumerate(D):
op[i] = M.dot(d.dot(M.T))
and the main function that is called in my initialization :
def qndiag(C, B0=None, weights=None, max_iter=1000, tol=1e-6,
lambda_min=1e-4, max_ls_tries=10, diag_only=False,
return_B_list=False, verbose=False):
"""Joint diagonalization of matrices using the quasi-Newton method
Parameters
----------
C : array-like, shape (n_samples, n_features, n_features)
Set of matrices to be jointly diagonalized. C[0] is the first matrix,
etc...
B0 : None | array-like, shape (n_features, n_features)
Initial point for the algorithm. If None, a whitener is used.
weights : None | array-like, shape (n_samples,)
Weights for each matrix in the loss:
L = sum(weights * KL(C, C')) / sum(weights).
No weighting (weights = 1) by default.
max_iter : int, optional
Maximum number of iterations to perform.
tol : float, optional
A positive scalar giving the tolerance at which the
algorithm is considered to have converged. The algorithm stops when
|gradient| < tol.
lambda_min : float, optional
A positive regularization scalar. Each eigenvalue of the Hessian
approximation below lambda_min is set to lambda_min.
max_ls_tries : int, optional
Maximum number of line-search tries to perform.
diag_only : bool, optional
If true, the line search is done by computing only the diagonals of the
dataset. The dataset is then computed after the line search.
Taking diag_only = True might be faster than diag_only=False
when the matrices are large (n_features > 200)
return_B_list : bool, optional
Chooses whether or not to return the list of iterates.
verbose : bool, optional
Prints informations about the state of the algorithm if True.
Returns
-------
D : array-like, shape (n_samples, n_features, n_features)
Set of matrices jointly diagonalized
B : array, shape (n_features, n_features)
Estimated joint diagonalizer matrix.
infos : dict
Dictionnary of monitoring informations, containing the times,
gradient norms and objective values.
References
----------
P. Ablin, J.F. Cardoso and A. Gramfort. Beyond Pham's algorithm
for joint diagonalization. Proc. ESANN 2019.
https://www.elen.ucl.ac.be/Proceedings/esann/esannpdf/es2019-119.pdf
https://hal.archives-ouvertes.fr/hal-01936887v1
https://arxiv.org/abs/1811.11433
"""
t0 = time()
n_samples, n_features, _ = C.shape
if B0 is None:
C_mean = np.mean(C, axis=0)
d, p = np.linalg.eigh(C_mean)
B = p.T / np.sqrt(d[:, None])
else:
B = B0
if weights is not None: # normalize
weights_ = weights / np.mean(weights)
else:
weights_ = None
D = transform_set(B, C)
Why this error on dot operator ? it seems to be matricial product (like np.dot
) but the way it is used lets think that's not the case.
The issue lies in [D, B] = qndiag(C, 'max_iter', 1000, 'tol', 1e-3)
, B0
(which is the second param) gets assigned as a string not as an array! Then eventually B
would be a string and hence the error message str object has no attribute 'dot' !, if you are only passing C
matrix as parameter, just do [D, B] = qndiag(C)
.