You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tensorly 0.8.1使用PARAFAC2报错'svd'属性缺失的解决求助

Tensorly 0.8.1中运行PARAFAC2代码报错AttributeError: module 'tensorly' has no attribute 'svd'

问题背景

尝试运行Tensorly的PARAFAC2示例代码,当前使用Tensorly版本为0.8.1,运行环境为Jupyter Notebook,目的是先验证示例代码可正常运行,后续将用Tensorly的PARAFAC完成其他任务。

运行代码

import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt
import tensorly as tl
from tensorly.decomposition import parafac2
from scipy.optimize import linear_sum_assignment

# Set parameters
true_rank = 3
I, J, K = 30, 40, 20
noise_rate = 0.1
np.random.seed(0)

# Generate random matrices
A_factor_matrix = np.random.uniform(1, 2, size=(I, true_rank))
B_factor_matrix = np.random.uniform(size=(J, true_rank))
C_factor_matrix = np.random.uniform(size=(K, true_rank))

# Normalised factor matrices
A_normalised = A_factor_matrix/la.norm(A_factor_matrix, axis=0)
B_normalised = B_factor_matrix/la.norm(B_factor_matrix, axis=0)
C_normalised = C_factor_matrix/la.norm(C_factor_matrix, axis=0)

# Generate the shifted factor matrix
B_factor_matrices = [np.roll(B_factor_matrix, shift=i, axis=0) for i in range(I)]
Bs_normalised = [np.roll(B_normalised, shift=i, axis=0) for i in range(I)]

# Construct the tensor
tensor = np.einsum('ir,ijr,kr->ijk', A_factor_matrix, B_factor_matrices, C_factor_matrix)

# Add noise
noise = np.random.standard_normal(tensor.shape)
noise /= np.linalg.norm(noise)
noise *= noise_rate*np.linalg.norm(tensor)
tensor += noise


best_err = np.inf
decomposition = None

for run in range(10):
    print(f'Training model {run}...')
    trial_decomposition, trial_errs = parafac2(tensor, true_rank, return_errors=True, tol=1e-8, n_iter_max=500, random_state=run)
    print(f'Number of iterations: {len(trial_errs)}')
    print(f'Final error: {trial_errs[-1]}')
    if best_err > trial_errs[-1]:
        best_err = trial_errs[-1]
        err = trial_errs
        decomposition = trial_decomposition
    print('-------------------------------')
print(f'Best model error: {best_err}')

报错信息

Training model 0...
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-86-c44d0dc18878> in <module>
     52 for run in range(10):
     53     print(f'Training model {run}...')
---> 54     trial_decomposition, trial_errs = parafac2(tensor, true_rank, return_errors=True, tol=1e-8, n_iter_max=500, random_state=run)
     55     print(f'Number of iterations: {len(trial_errs)}')
     56     print(f'Final error: {trial_errs[-1]}')

~/env/lib64/python3.6/site-packages/tensorly/decomposition/_parafac2.py in parafac2(tensor_slices, rank, n_iter_max, init, svd, normalize_factors, tol, absolute_tol, nn_modes, random_state, verbose, return_errors, n_iter_parafac)
    312         weights = T.ones(weights.shape, **tl.context(tensor_slices[0]))
    313 
--> 314         projections = _compute_projections(tensor_slices, factors, svd)
    315         projected_tensor = _project_tensor_slices(tensor_slices, projections)
    316         factors = parafac_updates(projected_tensor, weights, factors)

~/env/lib64/python3.6/site-packages/tensorly/decomposition/_parafac2.py in _compute_projections(tensor_slices, factors, svd)
     94         lhs = T.dot(factors[1], T.transpose(A * factors[2]))
     95         rhs = T.transpose(tensor_slice)
--> 96         U, _, Vh = svd_interface(T.dot(lhs, rhs), n_eigenvecs=n_eig, method=svd)
     97 
     98         out.append(T.transpose(T.dot(U, Vh)))

~/env/lib64/python3.6/site-packages/tensorly/tenalg/svd.py in svd_interface(matrix, method, n_eigenvecs, flip_sign, u_based_flip_sign, non_negative, mask, n_iter_mask_imputation, **kwargs)
    416         )
    417 
--> 418     U, S, V = svd_fun(matrix, n_eigenvecs=n_eigenvecs, **kwargs)
    419 
    420     if mask is not None:

~/env/lib64/python3.6/site-packages/tensorly/tenalg/svd.py in truncated_svd(matrix, n_eigenvecs, **kwargs)
    224     full_matrices = True if n_eigenvecs > min_dim else False
    225 
--> 226     U, S, V = tl.svd(matrix, full_matrices=full_matrices)
    227     return U[:, :n_eigenvecs], S[:n_eigenvecs], V[:n_eigenvecs, :]
    228 

AttributeError: module 'tensorly' has no attribute 'svd'

已尝试从tensorly和tensorly.decomposition中导入svd,但均无效。

期望输出

Training model 0...
Number of iterations: 500
Final error: 0.09204720575424472
-------------------------------
Training model 1...
Number of iterations: 500
Final error: 0.09204726856012718
-------------------------------
Training model 2...
Number of iterations: 500
Final error: 0.09269711804187236
-------------------------------
Training model 3...
Number of iterations: 392
Final error: 0.09204692795621944
-------------------------------
Training model 4...
Number of iterations: 415
Final error: 0.09204692959223097
-------------------------------
Training model 5...
Number of iterations: 500
Final error: 0.09291065541285955
-------------------------------
Training model 6...
Number of iterations: 364
Final error: 0.09204692769766268
-------------------------------
Training model 7...
Number of iterations: 424
Final error: 0.09204692883956121
-------------------------------
Training model 8...
Number of iterations: 481
Final error: 0.09204693125447479
-------------------------------
Training model 9...
Number of iterations: 500
Final error: 0.0920563578975846
-------------------------------
Best model error: 0.09204692769766268

解决办法

原因分析

Tensorly 0.8.1版本中,tl.svd并未直接暴露在tensorly主模块下,而是位于tensorly.tenalg子模块中,示例代码的内部调用逻辑在该版本存在路径问题。

方案1:升级Tensorly到稳定版

运行以下命令升级到最新稳定版:

pip install --upgrade tensorly

新版本已将tensorly.svd正确暴露,且修复了内部调用逻辑,升级后可直接运行原代码。

方案2:兼容Tensorly 0.8.1的临时修复

若无法升级版本,可在代码导入部分添加以下内容,将svd函数绑定到tensorly主模块:

from tensorly.tenalg import svd
tl.svd = svd

添加后再运行原代码即可解决报错问题。


内容的提问来源于stack exchange,提问作者A55i57

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 14:57:01