【问题标题】:Why attribute tensor_dot is not found in tensorly为什么在tensorly中找不到属性tensor_dot
【发布时间】:2022-12-29 04:59:03
【问题描述】:

考虑以下代码,我在其中定义了一个函数来计算矩阵的克罗内克积。目标是使用 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
# https://sparrow.dev/numpy-norm/

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(datas), size=(data.shape))

在倒数第二行,Python 抛出一个异常,说

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'
(1 line skipped)

当我查看 the official documentationtensorly 时,tensorly.tenalg 实际上具有这样的属性。为什么 Python 会出现这个问题?也许我应该使用来自 GitHub 而不是来自 piptensorly 版本?如果有帮助的话,我的代码来自here,其中代码块 4 定义了感兴趣的函数。

【问题讨论】:

    标签: python tensorly


    【解决方案1】:

    我们切换到tensordot(而不是tensor_dot)以遵循 NumPy API,并添加了可选的批处理模式,请参阅此处的文档:http://tensorly.org/dev/modules/generated/tensorly.tenalg.tensordot.html#tensorly.tenalg.tensordot

    作为旁注,TensorLy 还在 tenalg 子模块中包含一个 Kronecker 函数,您可以直接使用它来获取矩阵列表的克罗内克积。

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      相关资源
      最近更新 更多