Linear Algebra: Tensor in Deep Learning

Raajeev H Dave (AI Man)
4 min readDec 7, 2024

What are Tensors in Linear Algebra?

A tensor is like a generalization of scalars, vectors, and matrices. It organizes numbers in multiple dimensions (or axes). If you think of a scalar as a single number, a vector as a 1D list, and a matrix as a 2D table, a tensor extends this idea to any number of dimensions.

Understanding Tensors with Real-Life Examples

1. Scalar (0D Tensor)

A scalar is just a single number.

  • Example: The temperature at a spot: 30°C.

2. Vector (1D Tensor)

A vector is a list of numbers (1D).

  • Example: The high temperatures over a week:

3. Matrix (2D Tensor)

A matrix is a table of numbers (2D).

  • Example: Temperatures for a week in two cities:

4. Tensor (3D and Beyond)

A tensor extends matrices into higher dimensions.

  • Example: Temperatures for a week in 3 cities at 3 different times of the day: This would be a 3D tensor with dimensions (3 cities × 7 days × 3 times).

Each element represents the temperature for:

  • A specific city,
  • On a specific day,
  • At a specific time.

Visualizing a Tensor

Think of a tensor like a stack of matrices:

  1. A vector is a single row or column (1D).
  2. A matrix is a table (2D).
  3. A 3D tensor is a “cube” of numbers (layers of matrices stacked together).

For even higher dimensions (4D, 5D…), it becomes hard to visualize but follows the same logic.

Relation to Deep Learning

Tensors are foundational in deep learning because they are the data structures used to represent inputs, outputs, and intermediate data during computations. Here’s how they relate:

1. Representing Input Data

  • For a grayscale image: Each pixel’s intensity forms a 2D matrix.
  • For a color image: Each pixel has Red, Green, and Blue (RGB) values. This is a 3D tensor:

Example: An image with dimensions 64×64×3 has:

  • 64 pixels in height,
  • 64 pixels in width,
  • 3 channels (RGB).

2. Neural Network Weights

Each layer of a neural network has weights represented as tensors. For example:

  • A simple connection between input and output is a matrix (2D).
  • Complex layers, like convolutional layers in image processing, use higher-dimensional tensors.

3. Tensor Operations

Tensors allow:

  • Efficient data manipulation (e.g., resizing, rotating images).
  • Matrix multiplications in higher dimensions for learning patterns.

Simplified Deep Learning Example with Tensors

Imagine you’re building an AI to recognize handwritten digits (like 0–9):

  • Input Tensor:
  • Each digit is an image of 28×28 pixels.
  • A dataset of 100 images is a 3D tensor
  • Weights Tensor:
  • The neural network uses weights to process the input. These weights are stored as tensors.
  • Output Tensor:
  • The network outputs a probability for each digit (0–9) for every image. The output is a 2D tensor:

Why Tensors Are Important in Deep Learning

  1. Handle Multi-Dimensional Data: Tensors represent data with multiple features, dimensions, or attributes (e.g., videos, text, images).
  2. Efficient Computation: Operations on tensors (like multiplication, addition) are optimized for hardware (e.g., GPUs).
  3. Versatility: Almost all inputs in AI — text, images, audio — can be represented as tensors.

By understanding tensors, you unlock the ability to represent and manipulate complex data structures, which is a cornerstone of deep learning!

--

--

No responses yet