In deep learning it is common to see a lot of discussion around as the cornerstone data structure.

Tensor even appears in name of Google’s flagship learning library: “TensorFlow”.

Tensors are a type of data structure used in linear algebra, and like vectors and matrices, you can calculate arithmetic operations with tensors.

In this tutorial, you will discover what tensors are and how to manipulate them in Python with NumPy

After completing this tutorial, you will know:

• That tensors are a generalization of matrices and are represented using n-dimensional arrays.
• How to implement element-wise operations with tensors.
• How to perform the tensor product.

Let’s get started.

A Introduction to Tensors for Machine Learning with NumPy
Photo by Daniel Lombraña González, some rights reserved.

## Tutorial Overview

This tutorial is divided into 3 parts; they are:

1. What are Tensors?
2. Tensors in Python
3. Element-Wise Tensor Operations
4. Tensor Product

### Need help with Linear Algebra for Machine Learning?

Take my free 7-day email crash course now (with sample ).

Click to sign-up and also get a free PDF Ebook version of the course.

## What are Tensors?

A tensor is a generalization of vectors and matrices and is easily understood as a multidimensional array.

In the case, an array of arranged on a regular grid with a variable number of axes is known as a tensor.

— Page 33, Deep Learning, 2016.

A vector is a one-dimensional or first order tensor and a matrix is a two-dimensional or second order tensor.

Tensor notation is much like matrix notation with a capital letter representing a tensor and lowercase letters with subscript integers representing scalar values within the tensor.

Many of the operations that can be performed with scalars, vectors, and matrices can be reformulated to be performed with tensors.

As a tool, tensors and tensor algebra is widely used in the fields of physics and engineering. It is a term and set of techniques known in machine learning in the and operation of deep learning models can be described in terms of tensors.

## Tensors in Python

Like vectors and matrices, tensors can be represented in Python using the N-dimensional array (ndarray).

A tensor can be defined in-line to the constructor of array() as a list of lists.

The example below defines a 3x3x3 tensor as a NumPy ndarray. Three dimensions is easier to wrap your head around. Here, we first define rows, then a list of rows stacked as columns, then a list of columns stacked as levels in a cube.

Running the example first prints the shape of the tensor, then the values of the tensor itself.

You can see that, at least in three-dimensions, the tensor is printed as a series of matrices, one for each layer. For this 3D tensor, axis 0 specifies the level, axis 1 specifies the column, and axis 2 specifies the row.

## Element-Wise Tensor Operations

As with matrices, we can perform element-wise arithmetic between tensors.

In this section, we will work through the four main arithmetic operations.

The element-wise addition of two tensors with the same dimensions results in a new tensor with the same dimensions where each scalar value is the element-wise addition of the scalars in the parent tensors.

Running the example prints the addition of the two parent tensors.

### Tensor Subtraction

The element-wise subtraction of one tensor from another tensor with the same dimensions results in a new tensor with the same dimensions where each scalar value is the element-wise subtraction of the scalars in the parent tensors.

In NumPy, we can subtract tensors directly by subtracting arrays.

Running the example prints the result of subtracting the first tensor from the second.

The element-wise multiplication of one tensor from another tensor with the same dimensions results in a new tensor with the same dimensions where each scalar value is the element-wise multiplication of the scalars in the parent tensors.

As with matrices, the operation is referred to as the Hadamard Product to differentiate it from tensor multiplication. Here, we will use the “o” operator to indicate the Hadamard product operation between tensors.

In NumPy, we can multiply tensors directly by multiplying arrays.

Running the example prints the result of multiplying the tensors.

### Tensor Division

The element-wise division of one tensor from another tensor with the same dimensions results in a new tensor with the same dimensions where each scalar value is the element-wise division of the scalars in the parent tensors.

In NumPy, we can divide tensors directly by dividing arrays.

Running the example prints the result of dividing the tensors.

## Tensor Product

The tensor product operator is often denoted as a circle with a small x in the middle. We will denote it here as “(x)”.

Given a tensor A with q dimensions and tensor B with r dimensions, the product of these tensors will be a new tensor with the order of q + r or, said another way, q + r dimensions.

The tensor product is not limited to tensors, but can also be performed on matrices and vectors, which can be a good place to practice in order to develop the intuition for higher dimensions.

Let’s take a look at the tensor product for vectors.

Or, unrolled:

Let’s take a look at the tensor product for matrices.

Or, unrolled:

The tensor product can be implemented in NumPy using the tensordot() function.

The function takes as arguments the two tensors to be multiplied and the axis on which to sum the products over, called the sum reduction. To calculate the tensor product, also called the tensor dot product in NumPy, the axis must be set to 0.

In the example below, we define two order-1 tensors (vectors) with and calculate the tensor product.

Running the example prints the result of the tensor product.

The result is an order-2 tensor (matrix) with the lengths 2×2.

The tensor product is the most common form of tensor multiplication that you may encounter, but there are many other types of tensor multiplications that exist, such as the tensor dot product and the tensor contraction.

## Extensions

This section lists some ideas for extending the tutorial that you may wish to explore.

• Update each example using your own small contrived tensor data.
• Implement three other types of tensor multiplication not covered in this tutorial with small vector or matrix data.
• Write your own functions to implement each tensor operation.

If you explore any of these extensions, I’d love to know.

This section provides more resources on the topic if you are looking to go deeper.

## Summary

In this tutorial, you discovered what tensors are and how to manipulate them in Python with NumPy.

Specifically, you learned:

• That tensors are a generalization of matrices and are represented using n-dimensional arrays.
• How to implement element-wise operations with tensors.
• How to perform the tensor product.

Do you have any questions?

## Get a Handle on Linear Algebra for Machine Learning!

#### Develop a working understand of linear algebra

…by writing lines of code in python

Discover how in my new Ebook:
Linear Algebra for Machine Learning

It provides self-study tutorials on topics like:
Vector Norms, Matrix Multiplication, Tensors, Eigendecomposition, SVD, PCA and much more…