EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Software Development Software Development Tutorials PyTorch Tutorial PyTorch MaxPool2d

PyTorch MaxPool2d

Updated April 7, 2023

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.

ADVERTISEMENT
Popular Course in this category
3DS MAX Architecture Course Bundle - 4 Courses in 1 | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

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)

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 –

Code:

# 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

We hope that this EDUCBA information on “PyTorch MaxPool2d” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PyTorch TensorBoard
  2. Dataset Pytorch
  3. PyTorch Conv2d
  4. What is PyTorch?
ADVERTISEMENT
ANSIBLE Course Bundle - 4 Courses in 1
11+ Hour of HD Videos
4 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
LIGHTWORKS Course Bundle - 2 Courses in 1
18+ Hours of HD Videos
2 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
AUDACITY Course Bundle - 1 Course | 1 Mock Test
8+ Hours of HD Videos
1 Courses
1 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
UNITY Course Bundle - 26 Courses in 1 | 5 Mock Tests
109+ Hours of HD Videos
26 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

© 2023 - 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

Let’s Get Started

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

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

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

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

Forgot Password?

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

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW