AttributeError: 'str' object has no attribute 'dot'

AttributeError: 'str' object has no attribute 'dot'

我正在使用 qndiag 库尝试找到 2 个给定矩阵的对角化。

github 在这里:qndiag libray

我正在使用这个 Python 脚本来计算这两个尽可能闭合的对角化:

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

鉴于我的 2 个矩阵 7x7 FISH_spFISH_xc,我得到了一个错误:

approximate_joint_diagonalization_qndiag/qndiag/qndiag/qndiag.py", line 90, in qndiag
    D = transform_set(B, C)

及以下

 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'

这里关注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))

以及在我的初始化中调用的主函数:

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)

为什么点运算符出现这个错误?它似乎是矩阵乘积(如 np.dot),但它的使用方式让我们认为情况并非如此。

问题出在[D, B] = qndiag(C, 'max_iter', 1000, 'tol', 1e-3)B0(第二个参数)被分配为字符串而不是数组!然后最终 B 将是一个字符串,因此错误消息 str object has no attribute 'dot' !, 如果您只传递 C 矩阵作为参数,只需执行 [D, B] = qndiag(C)