An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
April 18, 2021 04:15 pm GMT
Original Link: https://dev.to/ambarishg/adaptiveavgpool2d-in-pytorch-1dke
AdaptiveAvgPool2d in PyTorch
I had trouble understanding the AdaptiveAvgPool2d
function in PyTorch. The following examples helped me to teach myself better. Hopefully, somebody may benefit from this.
Example 1
import torchimport torch.nn as nnimport numpy as npm = nn.AdaptiveAvgPool2d((1))x = np.array([ [ 2. , 3.], [ 4. , 1.],])input = torch.tensor(x)print(input)output = m(input)print(output)print(torch.mean(input))
The output will be equal to torch.mean(input)
Example 2 with a 3 x 3 x 3 tensor
x = np.array([ [ [ 2. , 3. , 2.], [ 2. , 3. , 2.], [ 2. , 3. , 2.], ], [ [ 1. , 4. , 5.], [ 1. , 4. , 5.], [ 1. , 4. , 5. ], ], [ [ 7. , 3. , 2.], [ 7. , 3. , 2.], [ 7. , 3. , 2.], ]])
This is a 3 x 3 x 3 array
input = torch.tensor(x)m = nn.AdaptiveAvgPool2d((2))output = m(input)print(output)
Let's investigate why the 1st element is 2.5
We take a 2 x 2 tensor out of the 3 x 3 x 3 tensor and take the mean and see that it is 2.5
x2 = torch.tensor(np.array([2. , 3. , 2. , 3.]))torch.mean(x2)
Example 3
We see that the 6th element is 4.5. How is this calculated?
We take the mean of the following section
x3 = torch.tensor(np.array([ 4.0 , 5. , 4. , 5.]))torch.mean(x3)
Example 4 with a 4 x 3 x 3 tensor
x = np.array([ [ [ 2. , 3. , 2.], [ 2. , 3. , 2.], [ 2. , 3. , 2.], ], [ [ 1. , 4. , 5.], [ 1. , 4. , 5.], [ 1. , 4. , 5. ], ], [ [ 7. , 3. , 2.], [ 7. , 3. , 2.], [ 7. , 3. , 2.], ], [ [ 8. , 3. , 2.], [ 8. , 3. , 2.], [ 8. , 3. , 2.], ]])input = torch.tensor(x)print(input)print(input.shape)
This is a 4 x 3 x 3 tensor
m = nn.AdaptiveAvgPool2d([1,1])output= m(input)print(output)
Let us explore why the first element is 2.3333
x4 = torch.tensor( np.array( [ 2. , 3. , 2., 2. , 3. , 2., 2. , 3. , 2.]) )print(torch.mean(x4))
Original Link: https://dev.to/ambarishg/adaptiveavgpool2d-in-pytorch-1dke
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To