EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PyTorch gather

Home » Data Science » Data Science Tutorials » Machine Learning Tutorial » 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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • 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.

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].

Popular Course in this category
Sale
Machine Learning Training (19 Courses, 29+ Projects)19 Online Courses | 29 Hands-on Projects | 178+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.7 (13,865 ratings)
Course Price

View Course

Related Courses
Deep Learning Training (16 Courses, 24+ Projects)Artificial Intelligence Training (5 Courses, 2 Project)

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?

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Machine Learning Tutorial
  • PyTorch
    • 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
  • Basic
    • Introduction To Machine Learning
    • What is Machine Learning?
    • Uses of Machine Learning
    • Applications of Machine Learning
    • Naive Bayes in Machine Learning
    • Dataset Labelling
    • DataSet Example
    • Dataset ZFS
    • Careers in Machine Learning
    • What is Machine Cycle?
    • Machine Learning Feature
    • Machine Learning Programming Languages
    • What is Kernel in Machine Learning
    • Machine Learning Tools
    • Machine Learning Models
    • Machine Learning Platform
    • Machine Learning Libraries
    • Machine Learning Life Cycle
    • Machine Learning System
    • Machine Learning Datasets
    • Top 7 Useful Benefits Of Machine Learning Certifications
    • Machine Learning Python vs R
    • Optimization for Machine Learning
    • Types of Machine Learning
    • Machine Learning Methods
    • Machine Learning Software
    • Machine Learning Techniques
    • Machine Learning Feature Selection
    • Ensemble Methods in Machine Learning
    • Support Vector Machine in Machine Learning
    • Decision Making Techniques
    • Restricted Boltzmann Machine
    • Regularization Machine Learning
    • What is Regression?
    • What is Linear Regression?
    • Dataset for Linear Regression
    • Decision tree limitations
    • What is Decision Tree?
    • What is Random Forest
  • Algorithms
    • Machine Learning Algorithms
    • Apriori Algorithm in Machine Learning
    • Types of Machine Learning Algorithms
    • Bayes Theorem
    • AdaBoost Algorithm
    • Classification Algorithms
    • Clustering Algorithm
    • Gradient Boosting Algorithm
    • Mean Shift Algorithm
    • Hierarchical Clustering Algorithm
    • Hierarchical Clustering Agglomerative
    • What is a Greedy Algorithm?
    • What is Genetic Algorithm?
    • Random Forest Algorithm
    • Nearest Neighbors Algorithm
    • Weak Law of Large Numbers
    • Ray Tracing Algorithm
    • SVM Algorithm
    • Naive Bayes Algorithm
    • Neural Network Algorithms
    • Boosting Algorithm
    • XGBoost Algorithm
    • Pattern Searching
    • Loss Functions in Machine Learning
    • Decision Tree in Machine Learning
    • Hyperparameter Machine Learning
    • Unsupervised Machine Learning
    • K- Means Clustering Algorithm
    • KNN Algorithm
    • Monty Hall Problem
  • Supervised
    • What is Supervised Learning
    • Supervised Machine Learning
    • Supervised Machine Learning Algorithms
    • Perceptron Learning Algorithm
    • Simple Linear Regression
    • Polynomial Regression
    • Multivariate Regression
    • Regression in Machine Learning
    • Hierarchical Clustering Analysis
    • Linear Regression Analysis
    • Support Vector Regression
    • Multiple Linear Regression
    • Linear Algebra in Machine Learning
    • Statistics for Machine Learning
    • What is Regression Analysis?
    • Clustering Methods
    • Backward Elimination
    • Ensemble Techniques
    • Bagging and Boosting
    • Linear Regression Modeling
    • What is Reinforcement Learning
  • Classification
    • Kernel Methods in Machine Learning
    • Clustering in Machine Learning
    • Machine Learning Architecture
    • Automation Anywhere Architecture
    • Machine Learning C++ Library
    • Machine Learning Frameworks
    • Data Preprocessing in Machine Learning
    • Data Science Machine Learning
    • Classification of Neural Network
    • Neural Network Machine Learning
    • What is Convolutional Neural Network?
    • Single Layer Neural Network
    • Kernel Methods
    • Forward and Backward Chaining
    • Forward Chaining
    • Backward Chaining
  • Deep Learning
    • What Is Deep learning
    • Overviews Deep Learning
    • Application of Deep Learning
    • Careers in Deep Learnings
    • Deep Learning Frameworks
    • Deep Learning Model
    • Deep Learning Algorithms
    • Deep Learning Technique
    • Deep Learning Networks
    • Deep Learning Libraries
    • Deep Learning Toolbox
    • Types of Neural Networks
    • Convolutional Neural Networks
    • Create Decision Tree
    • Deep Learning for NLP
    • Caffe Deep Learning
    • Deep Learning with TensorFlow
  • RPA
    • What is RPA
    • What is Robotics?
    • Benefits of RPA
    • RPA Applications
    • Types of Robots
    • RPA Tools
    • Line Follower Robot
    • What is Blue Prism?
    • RPA vs BPM
  • UiPath
    • What is UiPath
    • UiPath Action Center
    • UiPath?Orchestrator
    • UiPath web automation
    • UiPath Orchestrator API
    • UiPath Delay
    • UiPath Careers
    • UiPath Architecture
    • UiPath version
    • Uipath Reframework
    • UiPath Studio
  • Interview Questions
    • Deep Learning Interview Questions And Answer
    • Machine Learning Cheat Sheet

Related Courses

Machine Learning Training

Deep Learning Training

Artificial Intelligence Training

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
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

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.

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

EDUCBA Login

Forgot Password?

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.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & 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
Free Data Science Course

Hadoop, Data Science, Statistics & 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.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - Machine Learning Training Learn More