# Understanding Tensors and Tensor Decompositions: Part 1

This is a three-part blog post about tensors. The first part introduces tensors; the second and third part discuss tensor decomposition. So a natural question that you might have is what is a tensor and why should I be interested in them? Well read on to get your answer.

Simply stated, a tensor is a multidimensional or N-way array. The array dimensionality, the value of N, specifies the tensor order or the number of tensor modes.   A one-dimensional array a of p elements, that is a p ${\times }$ 1 vector, is considered a tensor of order one, and a two-dimensional array or matrix A of size p ${\times }$ q is a tensor of order two. Tensors of order three or more are generally called high-order tensors. The visualization of tensors of order zero to five shown below tells us that you can view high-order tensors as  organized arrangement of low-order tensors or a higher order representation of vector/matrix concepts. ##### Why tensors?

There are many reasons for us to be interested in tensors.  First, high-order tensors are a natural mathematical representation in many instances.  For example, RGB images are tensors of order three and videos are tensors of order four. In medical imaging, the multimodality images of patients captured under different conditions produce high order tensors. Second, vectors and matrices of large-size converted to high-order tensors lead to efficient computation as in deep learning libraries. Finally, you can stack matrices in many  tasks to form tensors to take advantages of data compression and analysis via tensor decomposition, similar to decomposing matrices via singular value decomposition (SVD) and PCA. This, for example, has been used for the face recognition task by stacking face images of different subjects under different lighting conditions, different facial expressions, and different view angles to form a tensor representation of the data. ##### Tensor Terminology

We access the elements of a real-valued tensor ${\boldsymbol{\mathcal{A}}\in {\mathbb{R}^{I_1\times I_2 \times \cdots I_K}}}$ of order K using K indices as in ${a_{i_1 , i_2 , \cdots , i_K}}$. You can access a tensor subarray by keeping a subset of indices fixed. In tensors, the term fiber is used to refer to a tensor subarray with all but one fixed index.  Thus, a fiber is just like a row/column in a matrix. For a three-mode tensor, we can form three different sets of fibers; we reference them as column (mode-1), row (mode-2), and tube (mode-3) fibers as shown below. A tensor subarray with all but two indices fixed is termed a slice, which is nothing but a matrix. Again, the slices are known with different names based on the indices kept fixed. You may recall the rank of a matrix is the number of independent columns or rows. For example, the matrix $\bf A = \begin{bmatrix}1 & 2\\3 & 4\end {bmatrix}$
has rank 2 as its both columns are independents, i.e. one column/row is not a scaled version of the other column. On the other hand, the following matrix $\bf A = \begin{bmatrix}1 & 2\\2 & 4\end {bmatrix}$
is a matrix of rank 1 as the second column/row is same as the first column multiplied by 2.
You may also recall that given two vectors, we can multiply them in three different ways. In inner or dot product, the result is just a number. The second kind of multiplication, known as the vector product, results in a vector. The third kind of product, the outer product and denoted by ( $\circ$), results in a matrix.  It is the outer product that is of interest to us right now. Thus, given two column vectors $\bf{a} = \begin{bmatrix}1 \\ 2\end {bmatrix}$ and $\bf{b} =\begin{bmatrix}2 \\ 3\end{bmatrix}$, the outer product results in the following matrix: $\bf {a}\,\circ\, \bf{b}= \bf {a}\bf{b}^t =\begin{bmatrix}2 & 3\\4 & 6\end {bmatrix}$

You will notice that the result of outer product of two vectors is always a matrix of rank 1. If we do outer product of three vectors, then the result is a rank 1 tensor of order three. To put it in other way, a tensor ${\boldsymbol{\mathcal{A}}\in {\mathbb{R}^{I_1\times I_2 \times \cdots I_K}}}$, of order K is of rank 1 if it can be expressed as the outer product of K vectors, i.e. ${\boldsymbol{\mathcal{A}}}= \bf{a^1}\,\circ\, \bf{a^2}\,\circ\,\cdots \,\circ\,\bf{a^K}$, where the superscripts denote different vectors. This implies that every tensor element can be expressed as the product of corresponding vector elements. For a tensor of order 3, we can thus write its (i, j, k) element as $a^{1}_ia^{2}_ja^{3}_k$, the product of multiplying the i-th element of the first vector with the j-th element of the second vector and the k-th element of the third vector as illustrated below, where the small white rectangles denote the vector elements whose product defines the shown tensor element. Since a numerical example always helps, take a look below at an example of a rank 1 tensor of order 3 created by performing outer product (denoted by %o% in R) of three vectors.

<br>
&gt; a1 = c(1,2,3)<br>
&gt; a2 = c(4,5,6)<br>
&gt; a3 = c(7, 8, 9)<br>
&gt; T = a1%o%a2%o%a3<br>
&gt; print(T)<br>
, , 1</p>
<p>     [,1] [,2] [,3]<br>
[1,]   28   35   42<br>
[2,]   56   70   84<br>
[3,]   84  105  126</p>
<p>, , 2</p>
<p>     [,1] [,2] [,3]<br>
[1,]   32   40   48<br>
[2,]   64   80   96<br>
[3,]   96  120  144</p>
<p>, , 3</p>
<p>     [,1] [,2] [,3]<br>
[1,]   36   45   54<br>
[2,]   72   90  108<br>
[3,]  108  135  162<br>


Note that the result above is showing the three frontal slices of the tensor. You can view the horizontal and lateral slices by changing the fixed index as shown below.

<br>
&gt; T[1,,]<br>
[,1] [,2] [,3]<br>
[1,]   28   32   36<br>
[2,]   35   40   45<br>
[3,]   42   48   54<br>
&gt; T[2,,]<br>
[,1] [,2] [,3]<br>
[1,]   56   64   72<br>
[2,]   70   80   90<br>
[3,]   84   96  108<br>
&gt; T[3,,]<br>
[,1] [,2] [,3]<br>
[1,]   84   96  108<br>
[2,]  105  120  135<br>
[3,]  126  144  162<br>
&gt; T[,1,]<br>
[,1] [,2] [,3]<br>
[1,]   28   32   36<br>
[2,]   56   64   72<br>
[3,]   84   96  108<br>
&gt; T[,2,]<br>
[,1] [,2] [,3]<br>
[1,]   35   40   45<br>
[2,]   70   80   90<br>
[3,]  105  120  135<br>
&gt; T[,3,]<br>
[,1] [,2] [,3]<br>
[1,]   42   48   54<br>
[2,]   84   96  108<br>
[3,]  126  144  162<br>


Another term that you are likely to come across while working with tensors is unfolding or flattening. What it means is to arrange a tensor as a matrix. When you unfold a tensor along its one of the modes, say mode m, then we say we are doing mode-m flattening. The mode-m flattened matrix is formed simply by arranging the mode-m fibers as its columns. For example, the mode-1 flattening of the order 3 tensor of the above example will be:

<br>
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]<br>
[1,]   28   35   42   32   40   48   36   45   54<br>
[2,]   56   70   84   64   80   96   72   90  108<br>
[3,]   84  105  126   96  120  144  108  135  162<br>


With this brief intro to tensors, we are ready to look at tensor decomposition and its applications. But you will have to wait for Parts 2 and 3 coming in next few weeks.

Benson Muite has created a Git repository for the code used in this and the next two blog posts on tensors. Pl. visit the repository at https://gitlab.com/bkmgit/understanding-tensors-and-tensor-decomposition/. Many thanks to him.