gpt4 book ai didi

python - 使用 HOSVD 分解后 sktensor/scikit-tensor 中的张量重建

转载 作者:太空狗 更新时间:2023-10-29 21:45:20 26 4
gpt4 key购买 nike

我目前正在分解 3-D 张量,例如 [user,item,tags]=rating。我在 python 中使用 sktensor 库进行分解。例如。

T = np.zeros((3, 4, 2))
T[:, :, 0] = [[ 1, 4, 7, 10], [ 2, 5, 8, 11], [3, 6, 9, 12]]
T[:, :, 1] = [[13, 16, 19, 22], [14, 17, 20, 23], [15, 18, 21, 24]]
T = dtensor(T)
Y = hooi(T, [2, 3, 1], init='nvecs')

现在函数 hooi 返回的是什么以及如何从中重建张量???

最佳答案

首先,函数 tucker_hooi 使用高阶正交计算张量的 Tucker 分解 迭代。


函数如下:

hooi(X, rank, init)

哪里:

  • X是要分解的张量
  • rank 是张量每个模式的分解秩
  • init: {'random', 'nvecs'} 是要使用的初始化方法(随机或 HOSVD)

示例:

from sktensor.tucker import hooi
import numpy as np
from sktensor import dtensor

T = np.zeros((3, 4, 2))
T[:, :, 0] = [[ 1, 4, 7, 10], [ 2, 5, 8, 11], [3, 6, 9, 12]]
T[:, :, 1] = [[13, 16, 19, 22], [14, 17, 20, 23], [15, 18, 21, 24]]
T = dtensor(T)
print(T.shape)
#(3, 4, 2)

Y = hooi(T, [2, 3, 1], init='nvecs')

core_S = Y[0]
core_S = np.array(core_S)
print(core_S.shape)
#(2, 3, 1)

U1 = Y[1][0]
U2 = Y[1][1]
U3 = Y[1][2]

print(U1)

[[ 0.54043979 0.7357025 ]
[ 0.57659506 0.02952065]
[ 0.61275033 -0.67666119]]

结果解读

  • core_S是核心张量S
  • Ux 是模式 x 的左奇异矩阵 Ux

可视化

enter image description here


编辑 1:

要重建原始张量 T,请执行以下操作:

from sktensor.core import ttm

core, U = hooi(T,rank= [2, 3, 1])
Trec = ttm(core, U)

print(Trec.shape)
#(3, 4, 2)

来源:https://github.com/mnick/scikit-tensor/blob/master/sktensor/tests/test_tucker_hooi.py

关于python - 使用 HOSVD 分解后 sktensor/scikit-tensor 中的张量重建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43509087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com