Re-compose a Tensor after tensor factorization
Asked Answered
I

2

13

I am trying to decompose a 3D matrix using python library scikit-tensor. I managed to decompose my Tensor (with dimensions 100x50x5) into three matrices. My question is how can I compose the initial matrix again using the decomposed matrix produced with Tensor factorization? I want to check if the decomposition has any meaning. My code is the following:

import logging
from scipy.io.matlab import loadmat
from sktensor import dtensor, cp_als
import numpy as np

//Set logging to DEBUG to see CP-ALS information
logging.basicConfig(level=logging.DEBUG)
T = np.ones((400, 50))
T = dtensor(T)
P, fit, itr, exectimes = cp_als(T, 10, init='random')
// how can I re-compose the Matrix T? TA = np.dot(P.U[0], P.U[1].T)

I am using the canonical decomposition as provided from the scikit-tensor library function cp_als. Also what is the expected dimensionality of the decomposed matrices?

Iatrogenic answered 28/9, 2016 at 12:58 Comment(2)
cp.py says: $A\approx\sum_{r=1}^{rank} \\vec{u}_r^{(1)} \outer \cdots \outer \\vec{u}_r^{(N)}$. Have you tried that? This should be identical to "P.totensor()"Fitted
@Fitted you mean the lines 145 and 146 of cp.py?Iatrogenic
T
7

The CP product of, for example, 4 matrices

X_{abcd} = \displaystyle\sum_{z=0}^{Z}{A_{az} B_{bz} C_{cz} D_{dz} + \epsilon_{abcd}}

can be expressed using Einstein notation as

X_{abcd} = A_{az} B_{bz} C_{cz} D_{dz} + \epsilon_{abcd}

or in numpy as

numpy.einsum('az,bz,cz,dz -> abcd', A, B, C, D)

so in your case you would use

numpy.einsum('az,bz->ab', P.U[0], P.U[1])

or, in your 3-matrix case

numpy.einsum('az,bz,cz->abc', P.U[0], P.U[1], P.U[2])

sktensor.ktensor.ktensor also have a method totensor() that does exactly this:

np.allclose(np.einsum('az,bz->ab', P.U[0], P.U[1]), P.totensor())
>>> True
Traditional answered 30/9, 2016 at 14:57 Comment(7)
Hey Nils thanks for the reply. Are you sure about this? I tried to re-compose the matrix and the result was not close to the intial tensor.Iatrogenic
Well, it depends on how nicely you can decompose the tensor in the first place. If your epsilon is big the two will be noticeably different.Traditional
What are the parameters except the number of latent dimension I can handle in the case of cp_als algorithm?Iatrogenic
I don't understand your question, sorry.Traditional
I am trying to figure out what are the parameters I can tune during the decomposition except the tensor rank.Iatrogenic
The parameters are in the docstring.Traditional
Does this answer your question?Traditional
M
0

See an explanation of CP here. You may also use tensorlearn package to rebuild the tensor.

Maurilla answered 20/11, 2022 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.