swh:1:snp:4e3e7077647a709f15b8c1b32ce7100175d0580b
Raw File
Tip revision: 23c6d135ae81f12205d4d57c22652628ecc2adf1 authored by Jean Kossaifi on 06 January 2017, 13:34:08 UTC
Better github ribbon (especially for mobiles)
Tip revision: 23c6d13
plot_tensor.py
# -*- coding: utf-8 -*-
"""
Basic tensor operations
=======================

Example on how to use :mod:`tensorly.base` to perform basic tensor operations.

"""

import matplotlib.pyplot as plt
from tensorly.base import unfold, fold
import numpy as np

###########################################################################
# A tensor is simply a numpy array
tensor = np.arange(24).reshape((3, 4, 2))
print('* original tensor:\n{}'.format(tensor))

###########################################################################
# Unfolding a tensor is easy
for mode in range(tensor.ndim):
    print('* mode-{} unfolding:\n{}'.format(mode, unfold(tensor, mode)))

###########################################################################
# Re-folding the tensor is as easy:
for mode in range(tensor.ndim):
    unfolding = unfold(tensor, mode)
    folded = fold(unfolding, mode, tensor.shape)
    print(np.all(folded == tensor))
back to top