Развертывание трехмерного тензора с неизвестным размером (размер пакета) - PullRequest
0 голосов
/ 08 февраля 2020

Я хочу развернуть трехмерный тензор формы (?, 100,80) в пользовательском слое в керасе. Мы получим три развернутые матрицы, соответствующие трем модам. Доступные функции в Tensorly и других наборах инструментов не поддерживают неизвестное измерение. Буду признателен за любую помощь в этом.

Развернуть функции доступны онлайн -

def f_unfold(tensor, mode=0):
"""Unfolds a tensors following the Kolda and Bader definition

    Moves the `mode` axis to the beginning and reshapes in Fortran order
"""
return np.reshape(np.moveaxis(tensor, mode, 0), 
                  (tensor.shape[mode], -1), order='F')

другая реализация -

def unfold(X, mode):
"""This is a tool function to unfold a 3d tensor. Doesnt acept tensors of higher dimentions
mode = 0 - Mode of unfolding the tensor. Can have values 0,1,2 - as an input tensor has 3 dimensions.
This approach deffers from the matematical theory, where modes start from 1.
"""
z, x, y = X.shape

if mode == 0:
    G = np.zeros((x,0), dtype=float)
    for i in range(z):
        G = np.concatenate((G, X[i,:,:]), axis=1)

if mode == 1:
    G = np.zeros((y,0), dtype=float)
    for i in range(z):
        G = np.concatenate((G, X[i,:,:].T), axis=1)

if mode == 2:
    G = np.zeros((z,0), dtype=float)
    for i in range(y):
        G = np.concatenate((G, X[:,:,i]), axis=1)
return  G
...