Tensor reshaping explained

In multilinear algebra, a reshaping of tensors is any bijection between the set of indices of an order-

M

tensor and the set of indices of an order-

L

tensor, where

L<M

. The use of indices presupposes tensors in coordinate representation with respect to a basis. The coordinate representation of a tensor can be regarded as a multi-dimensional array, and a bijection from one set of indices to another therefore amounts to a rearrangement of the array elements into an array of a different shape. Such a rearrangement constitutes a particular kind of linear map between the vector space of order-

M

tensors and the vector space of order-

L

tensors.

Definition

Given a positive integer

M

, the notation

[M]

refers to the set

\{1,...,M\}

of the first positive integers.

For each integer

m

where

1\lem\leM

for a positive integer

M

, let

Vm

denote an

Im

-dimensional vector space over a field

F

. Then there are vector space isomorphisms (linear maps)

\beginV_1 \otimes \cdots \otimes V_M & \simeq F^ \otimes \cdots \otimes F^ \\& \simeq F^ \otimes \cdots \otimes F^ \\& \simeq F^ \otimes F^ \otimes \cdots \otimes F^ \\& \simeq F^ \otimes F^ \otimes F^ \otimes \cdots \otimes F^ \\& \,\,\,\vdots \\& \simeq F^,\end

where

\pi\inak{S}M

is any permutation and

ak{S}M

is the symmetric group on

M

elements. Via these (and other) vector space isomorphisms, a tensor can be interpreted in several ways as an order-

L

tensor where

L\leM

.

Coordinate representation

The first vector space isomorphism on the list above,

V1VM\simeq

I1
F

IM
F
, gives the coordinate representation of an abstract tensor. Assume that each of the

M

vector spaces

Vm

has a basis

\{

m,
v
1
m,
v
2

\ldots,

m
v
Im

\}

. The expression of a tensor with respect to this basis has the form \mathcal = \sum_^\ldots\sum_^ a_ v_^1 \otimes v_^2 \otimes \cdots \otimes v_^, where the coefficients
a
i1,i2,\ldots,iM
are elements of

F

. The coordinate representation of

l{A}

is \sum_^\ldots\sum_^ a_ \mathbf_^1 \otimes \mathbf_^2 \otimes \cdots \otimes \mathbf_^M,where
m
e
i
is the

ith

standard basis vector of
Im
F
. This can be regarded as a M-way array whose elements are the coefficients
a
i1,i2,\ldots,iM
.

General flattenings

For any permutation

\pi\inak{S}M

there is a canonical isomorphism between the two tensor products of vector spaces

V1V2VM

and

V\pi(1)V\pi(2)V\pi(M)

. Parentheses are usually omitted from such products due to the natural isomorphism between

Vi(VjVk)

and

(ViVj)Vk

, but may, of course, be reintroduced to emphasize a particular grouping of factors. In the grouping,(V_ \otimes \cdots \otimes V_)\otimes(V_ \otimes \cdots \otimes V_)\otimes\cdots\otimes(V_ \otimes \cdots \otimes V_),there are

L

groups with

rl-rl-1

factors in the

lth

group (where

r0=0

and

rL=M

).

Letting

Sl=(\pi(rl-1+1),\pi(rl-1+2),\ldots,\pi(rl))

for each

l

satisfying

1\lel\leL

, an

(S1,S2,\ldots,SL)

-flattening of a tensor

l{A}

, denoted
l{A}
(S1,S2,\ldots,SL)
, is obtained by applying the two processes above within each of the

L

groups of factors. That is, the coordinate representation of the

lth

group of factors is obtained using the isomorphism
(V
\pi(rl-1+1)

V
\pi(rl-1+2)

V
\pi(rl)
I
\pi(rl-1+1)
)\simeq(F

I
\pi(rl-1+2)
F

⊗ … ⊗

I
\pi(rl)
F

)

, which requires specifying bases for all of the vector spaces

Vk

. The result is then vectorized using a bijection

\mul:[I

\pi(rl-1+1)
] x [I
\pi(rl-1+2)
] x … x [I
\pi(rl)
]\to[I
Sl

]

to obtain an element of
I
Sl
F
, where I_ := \prod_^ I_, the product of the dimensions of the vector spaces in the

lth

group of factors. The result of applying these isomorphisms within each group of factors is an element of
I
S1
F

I
SL
F
, which is a tensor of order

L

.

Vectorization

By means of a bijective map

\mu:[I1] x x [IM]\to[I1 … IM]

, a vector space isomorphism between
I1
F

IM
F

and
I1IM
F

is constructed via the mapping
1
e
i1

m
e
im

M
e
iM

\mapsto

e
\mu(i1,i2,\ldots,iM)

,

where for every natural number

i

such that

1\lei\leI1IM

, the vector

ei

denotes the ith standard basis vector of
i1iM
F

. In such a reshaping, the tensor is simply interpreted as a vector in
I1IM
F
. This is known as vectorization, and is analogous to vectorization of matrices. A standard choice of bijection

\mu

is such that

\operatorname(\mathcal) = \begin a_ & a_ & \cdots & a_ & a_ & \cdots & a_ \end^T,

which is consistent with the way in which the colon operator in Matlab and GNU Octave reshapes a higher-order tensor into a vector. In general, the vectorization of

l{A}

is the vector

[

a
\mu-1(i)
I1IM
]
i=1

.

The vectorization of

l{A}

denoted with

vec(l{A})

or

l{A}[:]

is an

[S1,S2]

-reshaping where

S1=(1,2,\ldots,M)

and

S2=\empty

.

Mode-m Flattening / Mode-m Matrixization

Let

l{A}\in

I1
F

I2
F

IM
F
be the coordinate representation of an abstract tensor with respect to a basis.Mode-m matrixizing (a.k.a. flattening) of

l{A}

is an

[S1,S2]

-reshaping in which

S1=(m)

and

S2=(1,2,\ldots,m-1,m+1,\ldots,M)

. Usually, a standard matrixizing is denoted by

_ = \mathcal_

This reshaping is sometimes called matrixizing, matricizing, flattening or unfolding in the literature. A standard choice for the bijections

\mu1,\mu2

is the one that is consistent with the reshape function in Matlab and GNU Octave, namely

_ := \begin a_ & a_ & \cdots & a_ \\a_ & a_ & \cdots & a_ \\\vdots & \vdots & & \vdots \\a_ & a_ & \cdots & a_\end

Definition Mode-m Matrixizing:[{\mathbf A}_{[m]}]_ = a_, \;\; \text j = i_m \text k=1+\sum_^M(i_n - 1) \prod_^ I_l.The mode-m matrixizing of a tensor

{lA}\in

I1 x ...IM
F

,

is defined as the matrix

{A}[m]\in

Im x (I1...Im-1Im+1...IM)
F
. As the parenthetical ordering indicates, the mode-m column vectors are arranged bysweeping all the other mode indices through their ranges,with smaller mode indexes varying more rapidly than larger ones; thu