首页 > 解决方案 > 2路数组的张量分解

问题描述

我可以rTensor::cp用来分解R. 但是,分解 2-way array 会发生错误L[[i]] : subscript out of bounds。如何分解2路数组?谢谢。

cp适用于 3 路阵列。

library(rTensor)
a <- c(0.1,0.9)
b <- c(0.5,0.5)
c <- c(0.7,0.3)
tnsr <- as.tensor(outer(outer(a,b),c))
cpD <- cp(tnsr, num_components=1)

> $U[[1]]
>      [,1]
> [1,]  0.1
> [2,]  0.9

> $U[[2]]
>      [,1]
> [1,] -0.5
> [2,] -0.5

> $U[[3]]
>      [,1]
> [1,] -0.7
> [2,] -0.3

2 路阵列发生错误。

tnsr <- as.tensor(outer(a,b))
cpD <- cp(tnsr, num_components=1)

> Error in L[[i]] : subscript out of bounds

标签: rtensordecomposition

解决方案


一种解决方法是将 2 路数组乘以 1 以使其成为 3 路数组。

tnsr = as.tensor(outer(outer(a,b),1))
cpD = cp(tnsr, num_components=1)

> $U[[1]]
>      [,1]
> [1,]  0.1
> [2,]  0.9

> $U[[2]]
>      [,1]
> [1,] -0.5
> [2,] -0.5

> $U[[3]]
>      [,1]
> [1,]   -1

推荐阅读