EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials PyTorch Tutorial PyTorch gather
Secondary Sidebar
PyTorch Tutorial
  • PyTorch
    • PyTorch Image Classification
    • PyTorch Random
    • PyTorch Variable
    • PyTorch Activation Function
    • Python Formatted String
    • PyTorch GPU
    • PyTorch CUDA
    • PyTorch DataLoader
    • PyTorch LSTM
    • PyTorch Pad
    • PyTorch OpenCL
    • PyTorch Lightning
    • PyTorch SoftMax
    • PyTorch Flatten
    • PyTorch gan
    • PyTorch max
    • PyTorch pip
    • PyTorch Parameter
    • PyTorch Load Model
    • PyTorch Distributed
    • PyTorch BERT
    • PyTorch interpolate
    • PyTorch JIT
    • PyTorch expand
    • PyTorch AMD
    • PyTorch GRU
    • PyTorch rnn
    • PyTorch permute
    • PyTorch argmax
    • PyTorch SGD
    • PyTorch nn
    • PyTorch One Hot Encoding
    • PyTorch Tensors
    • What is PyTorch?
    • PyTorch MSELoss()
    • PyTorch NLLLOSS
    • PyTorch MaxPool2d
    • PyTorch Pretrained Models
    • PyTorch Squeeze
    • PyTorch Reinforcement Learning
    • PyTorch zero_grad
    • PyTorch norm
    • PyTorch VAE
    • PyTorch Early Stopping
    • PyTorch requires_grad
    • PyTorch MNIST
    • PyTorch Conv2d
    • Dataset Pytorch
    • PyTorch tanh
    • PyTorch bmm
    • PyTorch profiler
    • PyTorch unsqueeze
    • PyTorch adam
    • PyTorch backward
    • PyTorch concatenate
    • PyTorch Embedding
    • PyTorch Tensor to NumPy
    • PyTorch Normalize
    • PyTorch ReLU
    • PyTorch Autograd
    • PyTorch Transpose
    • PyTorch Object Detection
    • PyTorch Autoencoder
    • PyTorch Loss
    • PyTorch repeat
    • PyTorch gather
    • PyTorch sequential
    • PyTorch U-NET
    • PyTorch Sigmoid
    • PyTorch Neural Network
    • PyTorch Quantization
    • PyTorch Ignite
    • PyTorch Versions
    • PyTorch TensorBoard
    • PyTorch Dropout
    • PyTorch Model
    • PyTorch optimizer
    • PyTorch ResNet
    • PyTorch CNN
    • PyTorch Detach
    • Single Layer Perceptron
    • PyTorch vs Keras
    • torch.nn Module

PyTorch gather

PyTorch gather

Introduction to PyTorch gather

In deep learning we need to extract the values from the specified columns of the matrix at that time we can use the Pytorch gather() function. In other words, we can say that by using PyTorch gather we can create a new tensor from specified input tensor values from each row with specified input dimension. The gather() function uses an index to take the value from each row. When we need to work with multi-class classification at that time we can use the gather() function. We use gather() when we have registered yield probabilities in a network and we want to extricate one worth from each line, where the removed worth compares to the objective yield class.

What is PyTorch gather?

Gather values along a pivot determined by a faint. Information and files should have a similar number of aspects. Basically, the gather() function uses the different parameters as follows.

  • Input: Input is nothing but a source of tensor.
  • Dim: Dimension means axis with a specified index of tensor.
  • Index: Index is used for the elements to gather.

We should begin with going through the semantics of the various contentions: The principal contention, input, is the source tensor that we need to choose components from. The second, faint, is the aspect (or hub in tensor flow/NumPy) that we need to gather along.

Lastly, list is the records to file input.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,560 ratings)

out[a][b][c] = input[index[a][b][c]][b][c] # if dimension == 0
out[a][b][c] = input[a][index[a][b][c]][c] # if dimension == 1
out[a][b][c] = input[a][b][index[a][b][c]] # if dimension == 2

So how about we go through the model.

The information tensor is [[1, 2], [3, 4]], and the faint contention is 1, for example, we need to gather from the subsequent aspect. The records for the subsequent aspect are given as [0, 0] and [1, 0].

As we “skip” the main aspect (the aspect we need to gather along is 1), the primary element of the outcome is certainly given as the principal aspect of the file. That implies that the lists hold the subsequent aspect, or the section records, however not the column files. Those are given by the lists of the list tensor itself. For the model, this implies that the yield will have in its first line a choice of the components of the information tensor’s first line too, as given by the main column of the list tensor’s first line. As the segment files are given by [0, 0], we consequently select the principal component of the main column of the information twice, coming about in [1, 1]. Likewise, the components of the second column of the outcome are a consequence of ordering the second line of the information tensor by the components of the second line of the list tensor, coming about in [4, 3].

Usage PyTorch gather

Now let’s see how we can use PyTorch gather as follows.

In the above point, we already discussed what the PyTorch gather() function is, basically the gather() function is used to extract the value from the input tensor along with the specified dimension that we want. In deep learning, we need accurate results rather than predicted outcomes; so many times we need to change the tensor dimension as per the requirement. We can say that we need to make the changes inside the trained model. So Pytorch provides the different types of functions to the user to reduce their work as well as they also try to reduce the complexity of code. By using the gather ( ) function we fetch the value from the tensor with a specified dimension so that we can use the PyTorch gather() function as per our requirement.

PyTorch gather Function

Now let’s see what PyTorch gather function with syntax is as follows.

Syntax

torch. gather(specified input, specified dim, index value)

Explanation

By using the above syntax we can implement the gather() function. In the above syntax we use the gather() function with different parameters such as specified input, specified dimension, and index values as well it also consists of some keyword arguments as follows.

Boolean value: This is an optional part of this syntax; it is used to specify the gradient with respect to the tensor.

Out tensor: This is an optional part of this gather() and it is used to specify the target tensor.

PyTorch gather Examples

Now let’s see the different examples of PyTorch gather() function for better understanding as follows.

import torch
ten = torch.tensor([[2, 1], [2, 5]])
a = torch.gather(ten, 1, torch.tensor([[1, 1], [0, 1]]))
print(a)

Explanation

In the above example, we try to implement the gather() function, here first we need to import the torch, after that we declare the tensor values as shown. Next line we use the gather function with dimension 1 and here we also specify the index values 0 and 1 as shown. The final output of the above program we illustrated by using the following screenshot as follows.

5

Now let’s use the gather() function as follows.

import torch
ten = torch.tensor([[5, 7], [1, 3]])
a = torch.gather(ten, 1, torch.tensor([[1, 1], [0, 1]]))
print(a)

Explanation

In this example we follow the same process, here we just change the value of tensor, and reaming all things is the same which means package and gather() function as shown. The final output of the above program we illustrated by using the following screenshot as follows.

8

So in this we can use PyTorch gather() function as well we can use gather() to solve the problem of 2D and 3D dimensional tenor means suppose we need to extract the specified index but without gather() function it is not possible to fetch the value. So at that time, we can gather() functions to avoid this problem.

Conclusion

We hope from this article you learn more about the Pytorch gather. From the above article, we have taken in the essential idea of the Pytorch gather and we also see the representation and example of the Pytorch gather. From this article, we learned how and when we use the Pytorch gather.

Recommended Articles

This is a guide to PyTorch gather. Here we discuss the introduction, usage, and functions with examples and applications. You may also have a look at the following articles to learn more –

  1. Data Science Techniques
  2. Python File Methods
  3. dataset preprocessing
  4. What are the Data Science Applications?
Popular Course in this category
Machine Learning Training (20 Courses, 29+ Projects)
  19 Online Courses |  29 Hands-on Projects |  178+ Hours |  Verifiable Certificate of Completion
4.7
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more