Revision a7b3723caa9581b976f3a5cca5084bfd33807ef9 authored by JeanKossaifi on 24 October 2016, 22:25:19 UTC, committed by JeanKossaifi on 24 October 2016, 22:25:19 UTC
1 parent f116292
Raw File
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):
    unfolded = unfold(tensor, mode)
    print('* mode-{} unfolding:\n{}'.format(mode, fold(unfolded, mode, tensor.shape)))
back to top