https://github.com/tensorly/tensorly
Revision 4f73baf20af94e1f1a3a54fa59d4476ac68512e3 authored by wumming on 30 August 2018, 20:15:08 UTC, committed by wumming on 30 August 2018, 20:15:08 UTC
1 parent 3448786
Raw File
Tip revision: 4f73baf20af94e1f1a3a54fa59d4476ac68512e3 authored by wumming on 30 August 2018, 20:15:08 UTC
merge2
Tip revision: 4f73baf
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
import tensorly.backend as T

###########################################################################
# A tensor is simply a numpy array
tensor = T.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)
    T.assert_array_equal(folded, tensor)
back to top