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 a concept related to tensors which is Tensor Slicing.

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.

What is Tensor Slicing?

Tensor slicing refers to selecting elements of the tensor by using the slicing operator, which is denoted by the symbol ':' (colon). We can also slice tensors by using tf.slice.

Performing Tensor Slicing using Tensorflow

Before using a tensor, we need to import some libraries.

Code

import tensorflow as tf # Importing TensorFlow library
import numpy as np # Importing NumPy library

We can also use tf.gather to get articular indices from a single axis of a tensor.

Code

print(tf.gather(t1, indices=[0, 3, 6]))

Output

tf.Tensor([1 7 6], shape=(3,), dtype=int32)

You can notice that it is the same output if we do t1[::3].

To extract slices from multiple axes of a tensor, we can use tf.gather_nd. We can use this when we want to gather the elements of the matrix as opposed to just its rows or columns.

We can also insert data into tensors with the help of tf.scatter_nd to insert values at the required indices, which start from zero.

Code

t6 = tf.constant([10]) # Creating a tensor with values zero in it
indices = tf.constant([[2], [3], [5], [7]]) # The indices where we want to update the values
data = tf.constant([1, 3, 5, 7]) # The value which is to be inserted
print(tf.scatter_nd(indices=indices,
updates=data,
shape=t6))

The method of tf.scatter_nd has a demerit that it requires a zero-initialized tensor.

Code

# Gathering values from a previously created tensor by specifying indices
new_indices = tf.constant([[1, 2], [3, 1], [2, 3]])
t7 = tf.gather_nd(t2, indices=new_indices)
print(t7)

Output

tf.Tensor([ 5 16 13], shape=(3,), dtype=int32)

We can also add values of a tensor into another tensor.

Code

# Adding the contents of tensor t7 to tensor t8
t8 = tf.scatter_nd(indices=new_indices, updates=t7, shape=tf.constant([4, 5]))
print(t8)

Alright! Now we hope you understand Tensorflow Advanced: Tensor Slicing.

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 Tensorflow Advanced: Tensor Slicing. We learnt how to create a tensor, about 1-D and 2-D tensors, and to slice tensors in TensorFlow.

We hope this blog on Tensorflow Advanced: Tensor Slicing was helpful. You can also refer to other similar articles.