2向数组的张量分解

问题描述

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

cp可以很好地用于三路阵列。

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]]
>      [,] -0.5
> [2,] -0.5

> $U[[3]]
>      [,] -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

解决方法

一种解决方法是将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]]
>      [,] -0.5
> [2,] -0.5

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