Is there any way in Pytorch to reduce dimensions of tensor in model?
Asked
Active
Viewed 806 times
0
-
plz marks relevant answers as correct – Prajot Kuvalekar Jul 06 '21 at 21:01
2 Answers
0
Adaptive Average Pooling or in fact any typical pooling in Pytorch does not reduce the dimensions of a tensor. You can find all the types of poolings, Pytorch offers over here: https://pytorch.org/docs/master/nn.html#pooling-layers
I suggest to use this template code to try out different poolings and their affect on dimensions:
m = nn.AdaptiveAvgPool2d((5,7))
input = torch.randn(1, 64, 8, 9)
output = m(input)
print(output.size())
In order to reduce dimension in Pytorch models you can specify a block that does squeeze() to the tensor or even flattens the tensor with example_tensor.view(-1, x, y) for example.
Sarthak Jain

SarthakJain
- 1,226
- 6
- 11
0
This code should work to compress (1,64,224,224) --> (1,64)
import torch
import torch.nn as nn
m = nn.AdaptiveAvgPool2d((1,1))
input = torch.randn(1, 64, 224, 224)
output = m(input).view(1,-1)
print(output.size()) #torch.Size([1, 64])

Prajot Kuvalekar
- 5,128
- 3
- 21
- 32