https://github.com/tensorly/tensorly
Revision a360a82666442bc575eac999204ed4b5c8560922 authored by JeanKossaifi on 24 October 2016, 22:28:13 UTC, committed by JeanKossaifi on 24 October 2016, 22:28:13 UTC
1 parent a7b3723
Raw File
Tip revision: a360a82666442bc575eac999204ed4b5c8560922 authored by JeanKossaifi on 24 October 2016, 22:28:13 UTC
FIX: corrected example plot_tensor
Tip revision: a360a82
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