Do you think IIT Guwahati certified course can help you in your career?
No
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.
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
You can also try this code with Online Python Compiler
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))
You can also try this code with Online Python Compiler
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)
You can also try this code with Online Python Compiler
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.