实现HOSVD(高阶奇异值分解)推荐系统
https://blog.csdn.net/zd836614437/article/details/51193359
对上面文章内容用matlab实现
1、创建一个如下的张量
代码:
A(: , : ,1)=[1,0,0;1,0,0;0,0,0];
A(: , : ,2)=[0,0,0;0,1,0;0,0,0];
A(: , : ,3)=[0,0,0;0,0,0;0,0,1];
A=tensor(A)
输出:
A is a tensor of size 3 x 3 x 3
A(:,:,1) =
1 0 0
1 0 0
0 0 0
A(:,:,2) =
0 0 0
0 1 0
0 0 0
A(:,:,3) =
0 0 0
0 0 0
0 0 1
图形如下图:
2、将张量按照mode-n模展开
A1=tenmat(A,1); %模-1展开
A2=tenmat(A,2); %模-2展开
A3=tenmat(A,3); %模-3展开
输出为:
A1 is a matrix corresponding to a tensor of size 3 x 3 x 3
A1.rindices = [ 1 ] (modes of tensor corresponding to rows)
A1.cindices = [ 2 3 ] (modes of tensor corresponding to columns)
A1.data =
1 0 0 0 0 0 0 0 0
1 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1
A2 is a matrix corresponding to a tensor of size 3 x 3 x 3
A2.rindices = [ 2 ] (modes of tensor corresponding to rows)
A2.cindices = [ 1 3 ] (modes of tensor corresponding to columns)
A2.data =
1 1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1
A3 is a matrix corresponding to a tensor of size 3 x 3 x 3
A3.rindices = [ 3 ] (modes of tensor corresponding to rows)
A3.cindices = [ 1 2 ] (modes of tensor corresponding to columns)
A3.data =
1 1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 1
展开图为:
3、对展开后的矩阵进行奇异值分解
[U1,V1,W1]=svd(A1.data);
[U2,V2,W2]=svd(A2.data);
[U3,V3,W3]=svd(A3.data);
输出的A1分解值为:
U1 =
0.5257 0 0.8507
0.8507 0 -0.5257
0 -1.0000 0
V1 =
列 1 至 6
1.6180 0 0 0 0 0
0 1.0000 0 0 0 0
0 0 0.6180 0 0 0
列 7 至 9
0 0 0
0 0 0
0 0 0
W1 =
列 1 至 6
0.8507 0 0.5257 0 0 0
0 0 0 0 -1.0000 0
0 0 0 0 0 0
0 0 0 1.0000 0 0
0.5257 0 -0.8507 0 0 0
0 0 0 0 0 1.0000
0 0 0 0 0 0
0 0 0 0 0 0
0 -1.0000 0 0 0 0
列 7 至 9
0 0 0
0 0 0
0 0 -1.0000
0 0 0
0 0 0
0 0 0
1.0000 0 0
0 1.0000 0
0 0 0
4、对奇异值减噪,去掉U1第3列
U1(:,3)=[] %删除U1第3列
输出:
U1 =
0.5257 0
0.8507 0
0 -1.0000
5、构造核心张量
S=ttm(A,{U1',U2',U3'}) %减噪后的数据重新构造核心张量S
输出:
S is a tensor of size 2 x 3 x 3
S(:,:,1) =
1.3764 0 0
0 0 0
S(:,:,2) =
0 0 0
0 -1 0
S(:,:,3) =
0 0 0.8507
0 0 0
6、重构目标张量
A=ttm(S,{U1,U2,U3})
输出:
A is a tensor of size 3 x 3 x 3
A(:,:,1) =
0.7236 0 0
1.1708 0 0
0 0 0
A(:,:,2) =
0 0.4472 0
0 0.7236 0
0 0 0
A(:,:,3) =
0 0 0
0 0 0
0 0 1