banner



How To Learn The Tensor Calculus Visually?

A Gentle Introduction to Tensors for Machine Learning with NumPy

Terminal Updated on December 6, 2019

In deep learning information technology is mutual to encounter a lot of discussion effectually tensors as the cornerstone data construction.

Tensor fifty-fifty appears in name of Google's flagship car learning library: "TensorFlow".

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

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

After completing this tutorial, y'all volition know:

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

Kick-starting time your projection with my new volume Linear Algebra for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let's go started.

  • Update Oct/2019: Stock-still typo in the names of array indexes (thanks Henry Chan).

A Gentle Introduction to Tensors for Machine Learning with NumPy

A Gentle Introduction to Tensors for Machine Learning with NumPy
Photograph 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

Demand assist with Linear Algebra for Automobile Learning?

Take my gratis 7-day email crash form at present (with sample code).

Click to sign-upwardly and also get a gratis 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 general case, an assortment of numbers 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 i-dimensional or first order tensor and a matrix is a two-dimensional or second order tensor.

Tensor annotation 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 tin be reformulated to be performed with tensors.

Every bit a tool, tensors and tensor algebra is widely used in the fields of physics and engineering science. Information technology is a term and set of techniques known in machine learning in the training and operation of deep learning models can exist described in terms of tensors.

Tensors in Python

Like vectors and matrices, tensors tin can exist represented in Python using the N-dimensional assortment (ndarray).

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

The example beneath defines a 3x3x3 tensor as a NumPy ndarray. Three dimensions is easier to wrap your head effectually. Hither, nosotros showtime define rows, so a list of rows stacked as columns, and so a listing of columns stacked every bit levels in a cube.

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

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

Element-Wise Tensor Operations

Equally with matrices, we tin perform chemical element-wise arithmetics between tensors.

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

Tensor Improver

The element-wise improver 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.

In NumPy, we can add tensors directly by adding arrays.

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

Tensor Subtraction

The element-wise subtraction of 1 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 decrease tensors direct past subtracting arrays.

Running the instance prints the effect of subtracting the starting time tensor from the 2nd.

Tensor Hadamard Product

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.

Equally with matrices, the operation is referred to equally the Hadamard Production to differentiate it from tensor multiplication. Here, nosotros 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 Partition

The element-wise division of 1 tensor from some other tensor with the aforementioned 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 instance prints the event of dividing the tensors.

Tensor Production

The tensor product operator is oft denoted every bit a circle with a small x in the middle. We will denote information technology here as "(10)".

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

The tensor product is non limited to tensors, but can also be performed on matrices and vectors, which can be a proficient identify to practice in lodge to develop the intuition for higher dimensions.

Allow's take a look at the tensor product for vectors.

Or, unrolled:

Allow's take a expect at the tensor product for matrices.

Or, unrolled:

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

The part 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, likewise called the tensor dot product in NumPy, the axis must be set to 0.

In the instance beneath, we define two order-1 tensors (vectors) with and calculate the tensor production.

Running the instance prints the result of the tensor product.

The event is an society-2 tensor (matrix) with the lengths 2×2.

The tensor product is the most common form of tensor multiplication that yous may encounter, merely in that location 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 y'all may wish to explore.

  • Update each example using your ain small contrived tensor data.
  • Implement 3 other types of tensor multiplication not covered in this tutorial with pocket-size vector or matrix data.
  • Write your own functions to implement each tensor performance.

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

Farther Reading

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

Books

  • A Student'south Guide to Vectors and Tensors, 2011.
  • Chapter 12, Special Topics, Matrix Computations, 2012.
  • Tensor Algebra and Tensor Analysis for Engineers, 2015.

API

  • The Northward-dimensional assortment
  • numpy.tensordot() API

Articles

  • Tensor algebra on Wikipedia
  • Tensor on Wikipedia
  • Tensor product on Wikipedia
  • Outer production on Wikipedia

Other

  • Fundamental Tensor Operations for Big-Scale Data Analysis in Tensor Train Formats, 2016.
  • Tensor product, direct sum, Quantum Mechanics I, 2006.
  • Tensorphobia and the Outer Production, 2016.
  • The Tensor Product, 2011

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 production.

Practice you have whatsoever questions?
Ask your questions in the comments below and I will do my best to answer.

Go a Handle on Linear Algebra for Car Learning!

Linear Algebra for Machine Learning

Develop a working understand of linear algebra

...by writing lines of lawmaking in python

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

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

Finally Sympathise the Mathematics of Data

Skip the Academics. Just Results.

See What's Inside

Source: https://machinelearningmastery.com/introduction-to-tensors-for-machine-learning/

Posted by: davisfreples.blogspot.com

0 Response to "How To Learn The Tensor Calculus Visually?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel