Guide To TensorLy: A Python Library For Tensor Learning


TensorLy is an open-source Python library that eases the task of performing tensor operations. It provides a high-level API for dealing with deep tensorized neural networks and tensor methods. It was created in 2015 by a senior research scientist at NVIDIA Research Group named Jean Kossaifi. It was presented at the NeurIPS workshop and was later published byJean Kossaifi, Yannis Panagakis, Anima Anandkumar and Maja Pantic in a JMLR paper in February 2019.

The project is supported by the following universities and organizations:

TensorLy makes it easy to handle tensor decomposition, tensor learning and tensor algebra. It’s robust backend system enables users to perform computations with NumPy, TensorFlow, PyTorch, MXNet, CuPy or JAX. It can run methods that can scale on multiple CPUs or GPUs.

Subscribe to our Newsletter

Join our editors every weekday evening as they steer you through the most significant news of the day, introduce you to fresh perspectives, and provide unexpected moments of joy
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.

TensorLy was initially built on the top of NumPy and SciPy with a soft dependency on plotting library Matplotlib. A backend system was added to it later on for combining tensor methods with deep learning and for enabling transparent switching between various libraries and platforms.. It provides a simple and efficient API such as that of scikit-learn. However, scikit-learn deals with observations in terms of vectors whereas TensorLy represent them as higher-order arrays called tensors. 

The methods, functionalities and libraries supported be summarized in the following diagrams:

Installation of TensorLy

NOTE: TensorLy has been developed and tested only on Python 3 version.

  1. Install the library using pip command:

pip install -U tensorly 

(option -U above is optional; use it for packages updates)

  1. Install it using conda from tensorly channel:

conda install -c tensorly tensorly

Practical implementation

Here, we demonstrate how to use this library for basic tensor operations, tensor decomposition and tensor regression. Step-wise explanation for code of each of the use cases is as follows: 

Some basic tensor operations

  1. Import required libraries
 import numpy as np
 import tensorly as tl
 from tensorly.testing import assert_array_equal
 #assert_array_equal() raises an error if two array-like objects are unequal 
  1. Create a tensor (it is nothing but a numpy array)

orig_tensor = tl.tensor(np.arange(40).reshape((5, 2, 4)))

A 3D array containing elements from 0 to 39 will be created’

Print the created tensor

 orig_tensor = tl.tensor(np.arange(40).reshape((5, 2, 4)))
 print('Original tensor:\n{}'.format(orig_tensor)) 
  1. Unfolding the tensor
 for m in range(tensor.ndim):  #for each dimension of the tensor
      print('Mode-{} unfolding:\n{}'.format(m, tl.unfold(tensor, m))) 

unfold() method performs unfolding starting from mode 0, and unfolds till (n-1)th dimension (where, ‘n’ is the dimension of the tensor).

  1. Refolding the tensor
 for mode in range(tensor.ndim):  #for each dimension of the tensor
     unf = tl.unfold(tensor, mode) #unfold the tensor first
     f = tl.fold(unf, mode, tensor.shape) #re-fold the tensor
 #give an error if the refolded tensor and original tensor are unequal
     assert_array_equal(f, tensor)  

The unfolded tensor after executing above lines of code:

The re-folded tensor will be as follows:

Tensor basic operations

Tensor decomposition example

Here, we demonstrate compression of colored image of a raccoon using parafac decomposition and Tucker decomposition techniques.

  1. Install required libraries
 import matplotlib.pyplot as plt
 import tensorly as tl
 import numpy as np
 from scipy.misc import face #for the raccoon’s face image
 #to zoom the image represented in the form of an array
 from scipy.ndimage import zoom 
 from tensorly.decomposition import parafac
 from tensorly.decomposition import tucker
 from math import ceil 
  1. Load the raccoon face image
 img = face()
 #Represent the image as a tensor
 img = tl.tensor(zoom(face(), (0.3, 0.3, 1)), dtype='float64') 
  1. Define a function to convert image tensor’s datatype from float to unsigned integer
 def to_img(tensor):
     image = tl.to_numpy(tensor) #Convert the tensor to a numpy array
  #Subtract minimum element from the array from each of the elements
     image -= image.min() 
 #Divide the modified elements by the maximum array element
     image /= image.max()
 #Multiply the elements by 255 (0-255 is pixel range for colored images)
     image *= 255
 #Change the datatype to unit8 (unsigned integer)
     return image.astype(np.uint8) 
  1. Define ranks of decomposition
 # Rank of CP decomposition
 cp_rank = 25
 # Rank of Tucker decomposition
 tucker_rank = [100, 100, 2] 

‘Rank’ here refers to the number of directions required to describe it.

  1. Perform CP decomposition
 weights, factors = parafac(img, rank=cp_rank, init='random', tol=10e-6)
 # Reconstruct the image from the factors
 cp_rec = tl.cp_to_tensor((weights, factors)) 
  1. Perform Tucker decomposition
 core, factors = tucker(img, rank=tucker_rank, init='random', tol=10e-5, 
 #Convert the tucker tensor into a full tensor
 tucker_rec = tl.tucker_to_tensor((core, factors)) 
  1. Plot the original image
 fig = plt.figure()
 ax = fig.add_subplot(1, 3, 1) 
 #arguments represent (number of rows, number of columns, index)
 ax.set_axis_off() #Turn the X and Y axes off
 ax.imshow(to_img(img))  #Display the plot
 ax.set_title('Original Image') #Title of the plot 
  1. Similarly, plot the image compressed using CP decomposition and Tucker decomposition.
 ax = fig.add_subplot(1, 3, 2)
 ax.set_title('CP decomposition')
 ax = fig.add_subplot(1, 3, 3)
 ax.set_title('Tucker Decomposition')
 #Adjust the padding between and around the subplots
 #Display the plots 


Tensor decomposition output

Tensor regression example

  1. Import required libraries
 import matplotlib.pyplot as plt
 from tensorly.base import tensor_to_vec, partial_tensor_to_vec
 from tensorly.datasets.synthetic import gen_image
 from tensorly.random import check_random_state
 from tensorly.regression.cp_regression import CPRegressor
 import tensorly as tl 
  1. Set image parameters
 img_ht = 25 #height of the image
 img_width = 25 #width of the image
 # shape of the images
 patterns = ['swiss', 'circle']
 # ranks for testing
 ranks = [1, 2, 3, 4, 5] 
  1. Generate random samples
 r = check_random_state(1)
 #Tensor representation of the samples
 X = tl.tensor(r.normal(size=(1000, img_ht, img_width), loc=0,  
 #numpy.random.RandomState.normal() draws random samples from a  
 normal distribution 
  1. Parameters of the plot
 n_rows = len(patterns)
 n_columns = len(ranks) + 1 
  1. Plot the images before and after applying regression
 fig = plt.figure()
 for i, pattern in enumerate(patterns):
      # Generate the original image for regression testing
        wt_img = gen_image(region=pattern, image_height=img_ht,    
               wt_img = tl.tensor(wt_img) #Convert the image into tensor
        # Generate the labels
        y =, skip_begin=1),    
        # Plot the original weights
        ax = fig.add_subplot(n_rows, n_columns, i*n_columns + 1)
        if i == 0:
       for j, rank in enumerate(ranks):
    # Create a tensor Regressor estimator
         est = CPRegressor(weight_rank=rank, tol=10e-7, n_iter_max=100,   
          reg_W=1, verbose=0)
          # Fit the estimator to the data
, y)
          #Plot the images after regression
 ax = fig.add_subplot(n_rows, n_columns, i*n_columns + j + 2)
          if i == 0:
              ax.set_title('Learned\nrank = {}'.format(rank))
 plt.suptitle("CP tensor regression") #Add a centered title to the figure #Display the plot 


Tensor regression output
  • Google colab notebooks of the above implementations:


Refer to the following sources to get an in-depth understanding of TensorLy Python library:

Nikita Shiledarbaxi
A zealous learner aspiring to advance in the domain of AI/ML. Eager to grasp emerging techniques to get insights from data and hence explore realistic Data Science applications as well.

Download our Mobile App


AI Hackathons, Coding & Learning

Host Hackathons & Recruit Great Data Talent!

AIM Research

Pioneering advanced AI market research

Request Customised Insights & Surveys for the AI Industry

The Gold Standard for Recognizing Excellence in Data Science and Tech Workplaces

With Best Firm Certification, you can effortlessly delve into the minds of your employees, unveil invaluable perspectives, and gain distinguished acclaim for fostering an exceptional company culture.

AIM Leaders Council

World’s Biggest Community Exclusively For Senior Executives In Data Science And Analytics.

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.