Using GPU with Cupy
Tensor Computations
For the numpy
implementation, it is not difficult to adapt it to the cupy
implementation with GPU. Below are the two functions for ten2mat
(converting tensor to matrix) and mat2ten
(converting matrix to tensor).
import cupy as np
def ten2mat(tensor, mode):
return np.reshape(np.moveaxis(tensor, mode, 0), (tensor.shape[mode], -1), order = 'F')
def mat2ten(mat, tensor_size, mode):
index = list()
index.append(mode)
for i in range(int(tensor_size.shape[0])):
if i != mode:
index.append(int(i))
size = []
for i in index:
size.append(int(tensor_size[i]))
return np.moveaxis(np.reshape(mat, size, order = 'F'), 0, mode)