In the world of data science and machine learning, tensors are everywhere. So, what exactly is a tensor? At its center, a tensor is just a way of saying “multi-dimensional array.” But they go beyond the traditional array we used to in programming. Tensors can be used to represent data structures like images, audio signals, and time-series data.
Let’s take a look at some examples of tensors.
Example 1: A Scalar
A scalar is just a way of saying “a single value.” In Python, you can represent a scalar as a tensor with an order of 0, like this:
import tensorflow as tfscalar = tf.constant(42)
print(scalar)
Output:
tf.Tensor(42, shape=(), dtype=int32)
Here, I used TensorFlow, a popular library for working with tensors in Python, to create a tensor representing the value 42. Notice that the shape of the tensor is empty, since it’s just a single value.
Example 2: A Vector
A vector is a 1D array of values. In Python, we can represent a vector as a tensor with an order of 1, like this:
import tensorflow as tfvector = tf.constant([1, 2, 3])
print(vector)
Output:
tf.Tensor([1 2 3], shape=(3,), dtype=int32)
Here, I created a tensor representing the vector [1,2,3]. Notice that the shape of the tensor is (3,), indicating that it has three elements along the 1st axis.
Example 3: A Matrix
A matrix is a 2D array of values. In Python, we can represent a matrix as a tensor with an order of 2, like this:
import tensorflow as tfmatrix = tf.constant([[1, 2], [3, 4], [5, 6]])
print(matrix)
Output:
tf.Tensor(
[[1 2]
[3 4]
[5 6]], shape=(3, 2), dtype=int32)
Here, we’ve created a tensor representing a matrix. Notice that the shape of the tensor is (3,2), indicating that it has three rows and two columns.
Example 4: A Tensor with More Than Two Dimensions
Tensors can have more than two dimensions. For example, we can represent a color image as a tensor with an order of 3, where the 1st axis represents the height of the image, the second axis represents the width of the image, and the third axis represents the color channels (red, green, and blue). Here’s an example:
import tensorflow as tfimage = tf.constant([
[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
[[255, 255, 0], [255, 0, 255], [0, 255, 255]],
[[255, 255, 255], [0, 0, 0], [128, 128, 128]]])
print(image)
Output:
tf.Tensor(
[[[255 0 0]
[ 0 255 0]
[ 0 0 255]][[255 255 0]
[255 0 255]
[ 0 255 255]]
[[255 255 255]
[ 0 0 0]
[128 128 128]]], shape=(3, 3, 3), dtype=int32)
Here, I’ve created a tensor representing a small 3×3 color image. Notice that the shape of the tensor is (3,3,3), indicating that it has three rows, three columns, and three color channels
Working with Tensors in Python
As we’ve seen in the examples above, TensorFlow is a popular library for working with tensors in Python. To get started with TensorFlow, you can install it using pip.
Once you’ve installed TensorFlow, you can start creating tensors using the tf.constant()
function, as we’ve seen in the examples above. You can also perform operations on tensors, like addition, multiplication, and more.
You can also perform more complex operations on tensors, like matrix multiplication and convolution. These operations are commonly used in machine learning and deep learning, where you’re working with large datasets and complex models.
For more information on TensorFlow, visit https://www.tensorflow.org/overview
Conclusion
Tensors are a powerful concept in data science and machine learning. By representing complex data structures as tensors, you can perform powerful operations and build sophisticated models that can learn from data. So next time you’re working with a complex dataset, remember: tensors are your friend!
For complete code: https://github.com/iamsaicharan/blog/blob/main/what_is_tensor.ipynb