swh:1:snp:5571f7976ca4ac18a2bf37a76e1d016d6295b377
Tip revision: 1bb217a077d6fa1d507f963a60da81adfd099d79 authored by Jean Kossaifi on 14 July 2017, 03:03:33 UTC
Merge pull request #7 from chubei/master
Merge pull request #7 from chubei/master
Tip revision: 1bb217a
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))