## Issue

I want to decompose a 3-dimensional tensor using SVD.

I am not quite sure if and, how following decomposition can be achieved.

I already know how I can split the tensor horizontally from this tutorial: tensors.org Figure 2.2b

`d = 10; A = np.random.rand(d,d,d) Am = A.reshape(d**2,d) Um,Sm,Vh = LA.svd(Am,full_matrices=False) U = Um.reshape(d,d,d); S = np.diag(Sm)`

## Solution

Matrix methods can be naturally extended to higher-orders. SVD, for instance, can be generalized to tensors e.g. with the Tucker decomposition, sometimes called a higher-order SVD.

We maintain a Python library for tensor methods, TensorLy, which lets you do this easily. In this case you want a partial Tucker as you want to leave one of the modes uncompressed.

Let’s import the necessary parts:

```
import tensorly as tl
from tensorly import random
from tensorly.decomposition import partial_tucker
```

For testing, let’s create a 3rd order tensor of size (10, 10, 10):

```
size = 10
order = 3
shape = (size, )*order
tensor = random.random_tensor(shape)
```

You can now decompose the tensor using the tensor decomposition. In your case, you want to leave one of the dimensions untouched, so you’ll only have two factors (your U and V) and a core tensor (your S):

```
core, factors = partial_tucker(tensor, rank=size, modes=[0, 2])
```

You can reconstruct the original tensor from your approximation using a series of n-mode products to contract the core with the factors:

```
from tensorly import tenalg
rec = tenalg.multi_mode_dot(core, factors, modes=[0, 2])
rec_error = tl.norm(rec - tensor)/tl.norm(tensor)
print(f'Relative reconstruction error: {rec_error}')
```

In my case, I get

```
Relative reconstruction error: 9.66027176805661e-16
```

Answered By – Jean

Answer Checked By – Mary Flores (BugsFixing Volunteer)