二维数组的张量分解

Tensor decomposition of 2-way array

我可以使用 rTensor::cp 分解 R 中的 3 路数组。但是,分解 2-way 数组 L[[i]] : subscript out of bounds 时会发生错误。如何分解二维数组?谢谢。

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

二维数组出错。

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

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

一种解决方法是将 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