I am trying to decompose the tensor (m, n, o) into the matrices A (m, r), B (n, r) and C (k, r). This is known as PARAFAC decomposition. Tensorly already performs this kind of decomposition.
An important step is to multiply A, B, and C to get the tensor of the form (m, n, o).
Tessno does it as follows:
def kt_to_tensor(A, B, C):
factors = [A, B, C]
for r in range(factors[0].shape[1]):
vecs = np.ix_(*[u[:, r] for u in factors])
if r:
res += reduce(np.multiply, vecs)
else:
res = reduce(np.multiply, vecs)
return res
However, the package I'm using (Autograd) does not support operations np.ix_. Thus, I wrote a simpler definition as follows:
def new_kt_to_tensor(A, B, C):
m, n, o = A.shape[0], B.shape[0], C.shape[0]
out = np.zeros((m, n, o))
k_max = A.shape[1]
for alpha in range(0, m):
for beta in range(0, n):
for delta in range(0, o):
for k in range(0, k_max):
out[alpha, beta, delta]=out[alpha, beta, delta]+ A[alpha, k]*B[beta, k]*C[delta, k]
return out
However, it turns out that this implementation also has some aspects that the city does not support. However, autograph support np.tensordot.
, np.tensordot . , Tensorflow tf.tensordot .
:
def tensordot_multplication(A, B, C):
"""
use np.tensordot
"""