https://github.com/tensorly/tensorly
Revision f11629209c39a644c949c4e2823c4c005b386f95 authored by JeanKossaifi on 24 October 2016, 11:23:18 UTC, committed by JeanKossaifi on 24 October 2016, 11:23:18 UTC
1 parent 0967e0c
Raw File
Tip revision: f11629209c39a644c949c4e2823c4c005b386f95 authored by JeanKossaifi on 24 October 2016, 11:23:18 UTC
DOC: minor changes.
Tip revision: f116292
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