avatarRukshan Pramoditha

Summary

The provided web content introduces the creation and manipulation of tensors in TensorFlow, drawing parallels with NumPy arrays, and emphasizes the importance of data types for performance in deep learning tasks.

Abstract

The article serves as an introductory guide to tensors in TensorFlow, which are the fundamental data structures akin to multi-dimensional arrays. It outlines the process of setting up a TensorFlow environment, including both CPU and GPU support, and delves into the creation of tensors using constant() and Variable(). The author explains the main attributes of tensors such as rank, shape, and data type, and categorizes tensors into scalars, vectors, matrices, and 3D matrices, providing TensorFlow and NumPy code examples for each. The discussion extends to the default data type used in TensorFlow (32-bit floats) for efficiency in deep learning computations, contrasting with NumPy's default 64-bit floats. The article also demonstrates the conversion between tensors and NumPy arrays, ensuring compatibility and interoperability between the two libraries. The author concludes by reiterating the first steps taken in the journey of deep learning and neural networks and assures the originality of the content, maintaining a commitment to quality and uniqueness.

Opinions

  • The author positions TensorFlow as a versatile tool for both deep learning and general machine learning tasks.
  • Emphasizing the ease of installation and use, the author suggests that TensorFlow 2 simplifies the process of deep learning model development.
  • The preference for 32-bit data types in TensorFlow is presented as a deliberate design choice to optimize performance for deep learning applications.
  • By providing NumPy equivalents for tensor operations, the author implies that readers familiar with NumPy will find TensorFlow's tensor manipulation intuitive.
  • The author advocates for the creation of original content, taking personal responsibility for ensuring the uniqueness and value of their writing, as evidenced by the use of Grammarly for plagiarism checks.
  • The author expresses gratitude to Sharon McCutcheon for providing the cover image and asserts copyright over the content, code samples, and other images in the post.

Let’s Create Tensors like NumPy Arrays

Tensors are nothing but matrix representations of multi-dimensional arrays

Photo by Sharon McCutcheon on Unsplash

Every long journey starts with a simple step. Like that, this is the first step of a long journey — Deep Learning and Neural Networks. TensorFlow, which Google develops, is a deep learning library specially created for deep learning tasks. But it is not limited to deep learning tasks, it can also be used for general machine learning. It has both high-level and low-level APIs. The high-level API can be used to build complex deep learning models with a few lines of code. The low-level API allows you to get more customization on your tasks.

A great way to getting started with a new library is to examine its data structures. Like that, we’ll get started with TensorFlow 2 by examining its main data structure — Tensor. The word “Tensor” is a technical term used in deep learning. Therefore, tensors are nothing but matrix representations of multi-dimensional arrays.

(Image by author)

Using TensorFlow 2 for the first time

Let’s set up the programming environment so that you can use TensorFlow on your own computers.

Installation

According to the TensorFlow installation documentation, the TensorFlow 2 library provides CPU and GPU support in a single package via pip installation. Previously, you had to install two separate packages to get both CPU and GPU support. But, with the latest version, you don’t have to! However, TensorFlow includes GPU support only for NVIDIA CUDA-enabled graphics cards. According to NVIDIA, almost all the NVIDIA graphics cards manufactured after 2008 are CUDA-enabled!

The easiest way to install TensorFlow 2 is using the pip package.

pip install tensorflow

Test your installation using:

import tensorflow as tf

If you don’t get any error message, you’re all set.

The easiest way to use TensorFlow is using it through Google Colab. You don’t even need to install TensorFlow there. However, you need to have an internet connection. After signing in to your Googe account, click on this link to create a new notebook. After that, go to Edit tab, select Notebook settings and then choose GPU as the hardware accelerator. This will enable you to run TensorFlow with GPU support!

Import convention

We use the following import convention to import TensorFlow into your workplace:

import tensorflow as tf

When you import TensorFlow in this way, the functions such as constant() can be imported as:

tf.constant()

There are mainly two functions to create tensors

In TensorFlow, tensors can be created using the following two functions:

  • constant(): If you create a tensor using this function, the tensor cannot be changed or modified in the same programming session. So, it is a constant tensor.
  • Variable(): If you create a tensor using this function, the tensor can be changed or modified in the same programming session. So, it is a variable tensor.

We provide array-like objects as the input for the above functions.

Main attributes of a tensor

The following are the main attributes of a tensor:

  • Rank: The rank is the number of dimensions (axes) of a tensor. It can be accessed with the tf.rank() function.
  • Shape: The shape can be accessed with the shape attribute of a tensor object. It returns a tuple that represents the number of elements in each dimension.
  • Data type: This describes the type of data in a tensor. It can be accessed with the dtype attribute of a tensor object.

Types of tensors

Tensors can be multi-dimensional, meaning that they can have any number of dimensions (axes). It is hard to imagine the format of a higher dimensional tensor. For our convenience, we categorize tensors into 4 main categories:

  • Scalar (also known as 0D Tensor): As the name suggests, this is just a number without any dimension (zero-dimensional).
  • Vector (also known as 1D Tensor): This is a one-dimensional array of numbers.
  • Matrix (also known as 2D Tensor): Most of us are familiar with this. This is a two-dimensional array that contains a collection of vectors.
  • 3D Matrix (also known as 3D Tensor): It is hard to imagine this format. However, when we are working with image data, 3D matrices are often used. This is a collection of matrices.

Now, we create these types of tensors using the TensorFlow library and see the relevant NumPy equivalent too.

Creating a Scalar (0D Tensor)

Using TensorFlow:

import tensorflow as tf
a = tf.Variable(10)
print(a)
print("\nRank:", tf.rank(a))
print("Shape:", a.shape)
print("Data type:", a.dtype)
(Image by author)

The NumPy equivalent is:

import numpy as np
b = np.array(10)
print(b)
print("\nRank:", b.ndim)
print("Shape:", b.shape)
print("Data type:", b.dtype)
(Image by author)

Creating a Vector (1D Tensor)

Using TensorFlow:

import tensorflow as tf
a = tf.Variable([2, 2.1, 3.0])
print(a)
print("\nRank:", tf.rank(a))
print("Shape:", a.shape)
print("Data type:", a.dtype)
(Image by author)

The NumPy equivalent is:

import numpy as np
b = np.array([2, 2.1, 3.0])
print(b)
print("\nRank:", b.ndim)
print("Shape:", b.shape)
print("Data type:", b.dtype)
(Image by author)

Creating a Matrix (2D Tensor)

Using TensorFlow:

import tensorflow as tf
a = tf.Variable([[1, 2, 3],
                 [4, 5, 6]])
print(a)
print("\nRank:", tf.rank(a))
print("Shape:", a.shape)
print("Data type:", a.dtype)
(Image by author)

The NumPy equivalent is:

import numpy as np
b = np.array([[1, 2, 3],
              [4, 5, 6]])
print(b)
print("\nRank:", b.ndim)
print("Shape:", b.shape)
print("Data type:", b.dtype)
(Image by author)

Creating a 3D Matrix (3D Tensor)

Using TensorFlow:

import tensorflow as tf
a = tf.Variable([[[1, 1, 1],
                  [1, 1, 1]],
                 [[2, 2, 2],
                  [2, 2, 2]]])
print(a)
print("\nRank:", tf.rank(a))
print("Shape:", a.shape)
print("Data type:", a.dtype)
(Image by author)

The NumPy equivalent is:

import numpy as np
b = np.array([[[1, 1, 1],
               [1, 1, 1]],
              [[2, 2, 2],
               [2, 2, 2]]])
print(b)
print("\nRank:", b.ndim)
print("Shape:", b.shape)
print("Data type:", b.dtype)
(Image by author)

The importance of the data type

Tensors and NumPy arrays are quite similar in appearance. However, by default, TensorFlow uses 32-bit data values when creating tensors. This is because TensorFlow was designed to increase the performance of deep learning models. 32-bit data values run faster and use less computational resources. In contrast, NumPy uses 64-bit data values when creating arrays.

Switching between the two

If you have a tensor, you can easily convert it to an equivalent NumPy array and vice versa.

Creating a NumPy array from a tensor

For this, we can use the np.array() function. The input is a tensor.

import numpy as np
import tensorflow as tf
a = tf.Variable([[1, 2, 3],
                 [4, 5, 6]])
b = np.array(a)
print(type(a))
print(type(b))
(Image by author)

Creating a tensor from a NumPy array

For this, we can use the tf.Variable() function. The input is a NumPy array.

import numpy as np
import tensorflow as tf
a = np.array([[1, 2, 3],
             [4, 5, 6]])
b = tf.Variable(a, dtype=tf.float32)
print(type(a))
print(type(b))
(Image by author)

Because the default data value in NumPy is 64-bit, we have to set dtype=tf.float32 when creating a tensor from a NumPy array.

Summary

We’ve just made the first step towards a long journey — Deep Learning and Neural Networks. First, we’ve set up the programming environment and then discussed the main data structure in TensorFlow. Now, you’re familiar with tensors.

The promise to deliver 0% plagiarism content

Yes, the above content has 0% plagiarism! Before publishing, I’ve checked this using Grammarly.

Writing unique contents that add value for readers in both context and appearance is my own responsibility. To ensure the uniqueness, I made this content with 0% plagiarism.

And also see how I maintain the quality of my contents by visiting:

Until next time, happy learning to everyone! Meanwhile, you can read my other posts at:

https://rukshanpramoditha.medium.com

Special credit goes to Sharon McCutcheon on Unsplash, who provides me with the cover image for this post. The written content, code samples, other images included in this post are copyrighted by the author.

Rukshan Pramoditha, 2021–06–23

Technology
TensorFlow
Tensor
Deep Learning
Python
Recommended from ReadMedium