def mttkrp(self, U, n): """ Matricized tensor times Khatri-Rao product for tensor. Calculates the matrix product of the n-mode matricization of X with the Khatri-Rao product of all entries in U except the nth. Parameters ---------- U - factorization Returns ------- out : Khatri-Rao product as a numpy array """ N = self.ndims() if len(U) != N: raise ValueError("U has the wrong length") Xn = self.permute( numpy.concatenate(([n], numpy.arange(0, n), numpy.arange(n + 1, N)))) ## use the Fortran ordering system to maintain consistent with Matlab code Xn = Xn.data.reshape(self.dimsize(n), numpy.prod(self.shape) / self.dimsize(n), order='F') Z = khatrirao.khatrirao_array([U[i] for i in range(len(U)) if i != n], reverse=True) V = numpy.dot(Xn, Z) return V
def __calculatePi(X, M, R, n, N): """ Calculate the product of all matrices but the n-th (Eq 3.6 in Chi + Kolda ArXiv paper) """ Pi = None if X.__class__ == sptensor.sptensor: Pi = np.ones((X.nnz(), R)) for nn in np.concatenate((np.arange(0, n), np.arange(n+1, N))): Pi = np.multiply(M.U[nn][X.subs[:, nn],:], Pi) else: Pi = khatrirao.khatrirao_array([M.U[i] for i in range(len(M.U)) if i != n], reverse=True) return Pi
def calculatePi(X, M, n): """ Calculate the product of all matrices but the n-th (Eq 3.6 in Chi + Kolda ArXiv paper) """ Pi = None if X.__class__ == sptensor.sptensor: Pi = np.ones((X.nnz(), M.R)) for nn in np.concatenate((np.arange(0, n), np.arange(n + 1, X.ndims()))): Pi = np.multiply(M.U[nn][X.subs[:, nn], :], Pi) else: Pi = khatrirao.khatrirao_array( [M.U[i] for i in range(len(M.U)) if i != n], reverse=True) return Pi
def mttkrp(self, U, n): """ Matricized tensor times Khatri-Rao product for tensor. Calculates the matrix product of the n-mode matricization of X with the Khatri-Rao product of all entries in U except the nth. Parameters ---------- U - factorization Returns ------- out : Khatri-Rao product as a numpy array """ N = self.ndims() if len(U) != N: raise ValueError("U has the wrong length"); Xn = self.permute(numpy.concatenate(([n], numpy.arange(0, n), numpy.arange(n+1, N)))) ## use the Fortran ordering system to maintain consistent with Matlab code Xn = Xn.data.reshape(self.dimsize(n), numpy.prod(self.shape)/self.dimsize(n), order='F'); Z = khatrirao.khatrirao_array([U[i] for i in range(len(U)) if i != n], reverse=True); V = numpy.dot(Xn,Z); return V;
def toTensor(self): """Convert this to a dense tensor""" tmp = khatrirao.khatrirao_array(self.U, True) data = np.inner(self.lmbda, tmp); return tensor.tensor(data, self.shape);