https://github.com/tensorly/tensorly
Revision c53b8a619956697bfcf07576328bf2d745338c9d authored by Jean KOSSAIFI on 30 October 2016, 17:56:52 UTC, committed by Jean KOSSAIFI on 30 October 2016, 17:56:52 UTC
1 parent 3438c5a
Raw File
Tip revision: c53b8a619956697bfcf07576328bf2d745338c9d authored by Jean KOSSAIFI on 30 October 2016, 17:56:52 UTC
Updated README and setup.py, removed setup.cfg.
Tip revision: c53b8a6
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