Leveraging ChatGPT - GenAI as a Microsoft Data Expert

Speaker

Prerita Agarwal

Data Specialist @

23 Jul, 2024 @ 01:30 PM

Introduction

While working on machine learning and deep learning, it becomes necessary to work with tensors. In this article, we will discuss the Sparse and Ragged Tensors.

But we need to understand what tensors are before diving into the concepts.

Tensors

A tensor is an algebraic object that describes a multilinear relationship between sets of algebraic objects related to a vector space. Tensors may map between objects, such as vectors, scalars, and even other tensors. We can implement various operations to tensors. After understanding the tensors, let's have a basic introduction to TensorFlow.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

TensorFlow

TensorFlow is a free library mainly used for artificial intelligence and machine learning. It is developed by Google. TensorFlow enables fast numerical computing. By using TensorFlow, we can implement model tracking, performance monitoring, data automation, and model retraining.

Alright! Now we are all set to learn about Sparse and Ragged tensors.

Sparse and Ragged Tensors

A sparse tensor is a dataset in which the majority of the elements have a value of zero. We can consider a diagonal matrix as an example of a sparse matrix. If there is a 2-D tensor of 7x8 (rows indexing from 0 to 6 and columns indexing from 0 to 7), then we need to store 56 values of all the elements. Thus, it can be seen that there is a lot of waste of memory if there are very few non-zero elements in it. To save memory, they can be represented in arrays or linked lists.

Ragged tensors are equivalent to nested variable-length lists. Ragged tensors allow the storage and processing of non-uniformly shaped data.

We can use tf.sparse for creating a sparse matrix. Similarly, we can use tf.ragged for creating a ragged matrix.

We can represent a sparse matrix in Tensorflow into three dense vectors, which are indices, values, and dense shape. In Python, the three vectors are collected into a SparseTensor class for easy use. The sparse tensor tf.sparse comprises of the following components:

indices: It is a 2-D tensor of int64 and has shape [N, ndims], and they specify the indices of the non-zero elements.

values: It is a 1-D tensor of shape [N], and it supplies the values for each element.

dense_shape: It is a 1-D tensor of int64 data type and has shape [ndims]. It specifies the dense_shape of any given sparse tensor. It takes a list which indicates the number of elements present in each dimension.

tf.ragged is a package which defines ops for manipulating ragged tensors, which are tensors with shapes being irregular. Every ragged tensor may have one or more ragged dimensions. Ragged dimensions are the dimensions whose slices may have different lengths, i.e., the column slices have different lengths.

Working with tf.sparse Tensorflow

Before working with sparse tensors, we need to import some libraries in Python.

Code

import tensorflow as tf
import numpy as np

Code

st = tf.SparseTensor(
indices=[[0, 3], [2, 4]], values=[10, 20], dense_shape=[3, 10])
st

Output

<tensorflow.python.framework.sparse_tensor.SparseTensor at 0x7fabfa5b56d0>

We need to do some setup to install and import some important libraries.

Setup

!pip install --pre -U tensorflow
import math
import tensorflow as tf

Ragged tensors support many TensorFlow operations, which include array operations, math operations, string manipulation ops, control flow operations, etc.

Let's create a ragged tensor consisting of some constants.

Alright! Now we hope you understand the Tensorflow Advanced: Sparse and Ragged Tensors.

Frequently Asked Questions

What is a tensor?

A tensor is an algebraic object that describes a multilinear relationship between sets of algebraic objects related to a vector space.

What is the difference between a tensor and a matrix?

A tensor is an n-dimensional array satisfying a particular transformation law. Unike a matrix, it shows an object placed in a specific coordinate system.

What are the common libraries used for machine learning?

The common libraries used for machine learning are NumPy, Pandas, Scikit learn, NLTK, PyTorch, TensorFlow, etc.

Why is TensorFlow faster than PyTorch?

TensorFlow offers better visualisation, which allows developers to debug and track the training process easily. However, PyTorch provides only limited visualisation.

What is PyTorch?

PyTorch is a machine learning framework used for computer vision and natural language processing. It is free and easy to use.

Conclusion

In this article, we discussed sparse tensors and ragged tensors. We learnt how to create a sparse and ragged tensor in TensorFlow.

We hope this blog on Tensorflow Advanced: Sparse and Ragged Tensors was helpful. You can also refer to other similar articles.