TENSOR - The n-dimension matrix

kipngeno koech
3 min readJul 21, 2024

--

What is a Tensor and where is it used; Tensor is an n-dimension matrix and is used in various fields of machine learning, deep learning, neural networks (CNNs, RNNs) and scientific computing due to their ability to handle multi-dimensional data efficiently

In simplest terms, it is a data structure, an advanced one, that can store a data with multiple attributes. Arrays, a popular data structure stores data in one dimension, so is lists. this means Arrays are 1-dimension tensors.

Matrices are common 2-dimension (2D) arrays alongside data frames. Here you store data in grid like formats or tables, it has rows and columns. Matrix is an array of arrays. Each element in itself is an array inside another array.

The key here is that it has rows & columns, and we can represent data with more than one feature, let us say an image with image name and image locations or the type of image

NB: both arrays and matrices (an array of arrays) are tensors, the first being a 1D tensor and the latter being a 2D tensor

There is 3D, 4D, 5D,…..N-dimension tensors depending on the attributes of the data you’re trying to represent, for example this is how a 3D tensor will look like, it has rows, columns and the depth (like how a box is represented)

3D tensors are just a stack of 2D matrices, Each layer in the stack represent a 2D matrix, and the depth indicates the number of this layers

RANKS

Tensors are n-dimension matrix, they have ranks depending on the value of N

“rank” refers to the number of dimensions or axes of the tensor. In the context of tensors and multi-dimensional arrays, “axes” (singular: “axis”) refer to the different dimensions along which data is organized. Each axis represents a specific way of indexing into the data structure.

For a 1D tensor (vector), it can be conceptually thought of as either a row or a column, but it is fundamentally a single sequence of numbers without any explicit direction.

For 2-dimension arrays you have the rows and columns, that means data is arranged in rows and columns, and thus is of rank 2

Practical Example

  • Rank 0: A single temperature reading.
  • Rank 1: A list of daily temperatures.
  • Rank 2: A table of temperatures for a week (days vs. times).
  • Rank 3: A stack of such tables for different cities.

CREATING A TENSOR WITH TENSORFLOW

to create a one dimension tensor:

import tensorflow as tf
tensor1D = tf.constant([10, 20, 30])
print(tensor1D)

to create a two dimension tensor:

import tensorflow as tf
tensor2D = tf.constant([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
print(tensor2D)

to create a three dimension tensor:

import tensorflow as tf
tensor3D = tf.constant([
[
[1, 2, 3],
[4, 5, 6]
],
[
[7, 8, 9],
[10, 11, 12]
],
[
[13, 14, 15],
[16, 17, 18]
]
])
print(tensor3D)

as you can see above, 3D tensor is just a layers of 2D

Curtains closing: When we represent data for machine learning, we typically do so in numerical form, that is why we need tensors

--

--

kipngeno koech
kipngeno koech

Written by kipngeno koech

Step into Kipngeno Koech's world, where imagination & reality collide. His writing transports you to new realms, captivating & inspiring. Experience the magic