EDUCBA

EDUCBA

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

PyTorch MaxPool2d

Home » Data Science » Data Science Tutorials » Machine Learning Tutorial » PyTorch MaxPool2d

PyTorch MaxPool2d

Introduction to PyTorch MaxPool2d

PyTorch MaxPool2d is the class of PyTorch that is used in neural networks for pooling over specified signal inputs which internally contain various planes of input. It accepts various parameters in the class definition which include dilation, ceil mode, size of kernel, stride, dilation, padding, and return indices.

In this article, we will try to get more information about what is PyTorch MaxPool2d, how do we use PyTorch MaxPool2d, PyTorch MaxPool2d Class, PyTorch MaxPool2d examples, and a conclusion about PyTorch MaxPool2d.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

What is PyTorch MaxPool2d?

PyTorch MaxPool2d is the class of torch library which has its complete definition as:

Class torch.neuralnetwork.MaxPool2d(size of kernel, stride = none, dilation = 1, ceil mode = false, padding = 0, return indices = false)

Simply put, we can understand it by using the case where the size of the input layer is (N, C, H, W) for the specified tensor and the output layer value is the (N, C, H out, W out) and the size of the kernel is (kH, kW) then MaxPool2d can be described by using the below equation –

Out ((Ni, Cj, H, W)) = max (m=0, …., k H-1) max (n=0, …. kW-1)
Input (Ni, Cj, stride [0] * h + m, stride [1] * w +n)

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

View Course

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

When the value of padding is non zero then the negative infinity value is padded on both the ides of the input implicitly for the number of points. In order to control the spacing that is present between the points of kernel, we need to specify the dilation. If you want to understand in detail the working of dilation and why we need to specify it then refer to this link.

When the value of the ceil mode parameter is set to true then if the sliding windows begin from the left input or padding then it is permitted to them to go off the bounds. The sliding windows are completely ignored if they begin from the right padded region.

How do we use PyTorch MaxPool2d?

While using PyTorch MaxPool2d, you need to specify certain parameters as specified in its class definition. Let’s discuss how you are supposed to pass the parameters and what each one of them does to understand how we can use MaxPool2d.

The value of the parameters – dilation, padding, size f kernel and stride can posses either of the below mentioned values –

A singular integer (int) value – This will specify that the same value will be considered for both the width and height dimensions.

A tuple of two integers (int) values – This case will involve consideration of the first integer for the value of height while the second one will be used for the width dimension of the value.

Let us try to describe each of the parameters use –

  • Dilation – In the specified window, to control the element’s strides we make the use of this parameter.
  • Stride – The parameter helps in specifying the stride value of the window and the default value when not specified is the size of the kernel.
  • Size of the kernel – This is the parameter that specifies the window size in order to take that value to the max over.
  • Padding – This is used for implicitly adding the padding on both of the sides of value.
  • Ceil mode – When we set the value of this parameter to true then ceil operation is used instead of the floor which is used for the computation of the shape of output.
  • Return indices – When we set the value to true this parameter will return the outputs along with the value of maximum indices. This parameter plays a crucial role in torch. nn.MaxUnpool2d is used in the later phase.

PyTorch MaxPool2d Class

The class of PyTorch MaxPool2d has its definition –

Class torch. neuralnetwork. MaxPool2d(size of kernel, stride = none, dilation = 1, ceil mode = false, padding = 0, return indices = false)

Where the parameters used are already described above. The working of MaxPool2d requires input and output whose shapes can be defined as –

Shape of input is – (C, H in, W in) or (N, C, H in, W in)

While the shape of output is – is (C, H out, W out) or is (N, C, H out, W out)

The calculation of H out and W out is carried out by using below formula –

W out = [((W in +2 * padding [1] – dilation [1] * (size of kernel [1] - 1) -1)/ stride [1]) +1] H out = [((H in +2 * padding [0] – dilation [0] * (size of kernel [0] - 1) -1)/ stride [1]) +1]

PyTorch MaxPool2d Examples

Let us consider certain examples that will help us understand the implementation of MaxPool2d in PyTorch –

# The size is 3 and stride is 2 for a fully squared window

sampleEducbaMatrix = nn. MaxPool2d(3, stride = 2)
# Window pool having non squared regions or values
sampleEducbaMatrix = nn. MaxPool2d((3, 2), stride = (2, 1))
sampleEducbaInput = torch. randn (20, 16, 50, 32)
sampleEducbaOutput = sampleEducbaMatrix (sampleEducbaInput)

The output of the above program gives the same input matrix but scaled-down version of it as the output as we made the use of MaxPool2d. We can observe the output of above program in the below image –

output

In order to understand the importance and usage of MaxPool2d, you can refer to this link. The size of the kernel determines the small window for the area of pool over while the step from which to move is determined by the stride. In short, scaling is determined by the stride value that we specify.

Conclusion

We can make the use of MaxPool2d class in PyTorch for implementing and scaling down the input layer to a small output value. We can use various props of MaxPool2d to define its behavior such as the size of kernel, stride, and many others. We can call max unpool 2d to scale in upward way that means the input will be small and output will be scaled up.

Recommended Articles

This is a guide to PyTorch MaxPool2d. Here we discuss the Introduction, What is PyTorch MaxPool2d?, How do we use PyTorch MaxPool2d? respectively. You may also have a look at the following articles to learn more –

  1. PyTorch TensorBoard
  2. Dataset Pytorch
  3. PyTorch Conv2d
  4. What is PyTorch?

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
    • 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 Insights
    • UiPath Split String
    • UiPath Installation
    • UiPath Filter Data Table
    • UiPath Test Suite
    • UiPath Competitors
    • 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
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.

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

*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

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