为何tensorly.tenalg模块中不存在tensor_dot属性?
Tensorly
tl.tenalg.tensor_dot 属性不存在的报错分析 问题场景
编写了计算三个矩阵Kronecker积的函数matrix_outer,目标是通过Tensorly库的tensor_dot实现张量外推算法,但运行时抛出属性不存在的异常,而官方文档显示该函数存在,需排查原因及解决办法。
代码实现
import numpy as np import numpy.linalg as la #%pip install tensorly import pandas as pd import tensorly as tl import warnings from scipy.stats import norm def matrix_outer (A, B, C ) : """ Calculates Kronecker product of Matrices A, B, C """ n_A = A.shape[0] n_B = B.shape[0] n_C = C.shape[0] k = A.shape[1] tensor = tl.zeros(shape=(n_A, n_B, n_C)) for i in range(k): coef_a = A[:,i] coef_b = B[:,i] coef_c = C[:,i] tensor += tl.tenalg.tensor_dot(tl.tenalg.tensor_dot(coef_a, coef_b).reshape(n_A, n_B), coef_c).reshape(n_A, n_B, n_C) return tensor """ Okay now we've defined our functions. The next step is data generation. """ # Tensor size R = 4 n_user = 160 # We have 100 units/users. This is a marketing application. n_prod = 120 # Sales data, for example, of different products n_time = 100 # Here are our time periods. # Auxiliary function for normalizing vectors normalize_vec = lambda vec: vec/la.norm(vec) """ User participation is shown in matrix A. """ user_index = np.linspace(-3, 3, num=n_user) user_bell = normalize_vec(norm.pdf(user_index, loc = 0, scale = 0.5)) user_bell2 = np.roll(user_bell,120) user_bell3 = np.roll(user_bell,80 ) user_bell4 = np.roll(user_bell,40 ) A = np.c_[user_bell, user_bell2, user_bell3, user_bell4] """ Product participation is shown in matrix B. """ productl = normalize_vec(np.repeat([1, 2, 3 , 4], 30, axis=0)) product2 = np.roll(productl, 90 ) product3 = np.roll(productl, 60 ) product4 = np.roll(productl, 30 ) B = np.c_[productl, product2, product3, product4] tseriesl = normalize_vec(0.3*np.sin(np.arange(0, n_time*2, step=2*np.pi/7))[0:n_time]+0.5) tseries2 = normalize_vec(np.linspace(0, 1, n_time)) tseries3 = normalize_vec(np.repeat([0, 1], n_time/2, axis=0)) tseries4 = normalize_vec(np.repeat([1, 0], [n_time/4, 3*n_time/4], axis=0)) C = np.c_[tseriesl, tseries2, tseries3, tseries4] #Aggregate Matrices and denoise them np.random.seed(1512) data = matrix_outer(A,B, C) data_noisy= data + np.random.normal(loc=0,scale=0.5*tl.mean(data), size=(data.shape))
报错信息
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 14, in matrix_outer AttributeError: module 'tensorly.tenalg' has no attribute 'tensor_dot'
原因分析
- 版本不匹配:通过pip安装的Tensorly稳定版可能未包含
tenalg.tensor_dot——该函数是较新版本(或开发分支)中新增的,你当前安装的旧版稳定版没有这个接口。 - 导入路径差异:部分Tensorly版本中,
tensor_dot函数直接放在tensorly根模块下,而非tenalg子模块内。
解决方案
1. 升级到最新稳定版
执行以下命令升级Tensorly:
pip install --upgrade tensorly
升级完成后重新运行代码,验证tl.tenalg.tensor_dot是否可用。
2. 替换为根模块的tensor_dot(兼容旧版)
如果升级后仍无法找到该函数,可尝试将代码中的tl.tenalg.tensor_dot替换为tl.tensor_dot,旧版Tensorly的张量点积函数通常直接暴露在根模块。
3. 安装GitHub开发版(仅前两种方法无效时使用)
若上述方案都无法解决问题,再考虑安装GitHub上的开发分支版本:
pip install git+https://github.com/tensorly/tensorly.git
代码优化建议
你的matrix_outer函数可以简化,直接使用张量外积函数tl.tenalg.outer代替嵌套的tensor_dot,代码更简洁高效:
def matrix_outer(A, B, C): """Calculates Kronecker product of Matrices A, B, C""" k = A.shape[1] tensor = tl.zeros(shape=(A.shape[0], B.shape[0], C.shape[0])) for i in range(k): # 直接计算三个向量的外积,无需多次reshape tensor += tl.tenalg.outer([A[:,i], B[:,i], C[:,i]]) return tensor
内容的提问来源于stack exchange,提问作者Jared Greathouse
相关产品推荐
相关产品推荐

