How to apply 4D maxpool in pytorch?

r00bi: <p>I want to convert a 4d maxpool from TensorFlow to PyTorch, but I could not find the equivalent function:</p>
<p>tf.nn.max_pool(first_layer<br>
ksize=[1, 1, 5, 1],<br>
strides=[1, 1, 5, 1],<br>
padding=‘SAME’)<br>
to<br>
torch.nn.MaxPool4d(kernel_size=(1, 1, 5, 1), stride=(1, 1, 5, 1))<br>
the first layer is a 4d tensor<br>
But, it seems that PyTorch doesn’t have maxpool4d.<br>
How can I do this conversion?</p>

ptrblck: <aside class="quote no-group" data-username="r00bi" data-post="1" data-topic="145851">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/r00bi/48/47334_2.png" class="avatar"> r00bi:</div>
<blockquote>
<p>the first layer is a 4d tensor</p>
</blockquote>
</aside>
<p>I’m not sure if this means your input tensor has 4 dimensions, but if so you could use <code>nn.MaxPool2d</code> assuming the input tensor dimensions are defined as <code>[batch_size, channels, height, width]</code> and specify the <code>kernel_size</code> as well as the <code>stride</code> for the spatial dimensions only (the first two are set to <code>1</code> so don’t have an effect).</p>

r00bi: <p>Thanks, it works:  torch.nn.MaxPool2d(kernel_size=( 5, 1), stride=( 5, 1))</p>

r00bi: <p>But, after applying a conv2d on the output of maxpool2d, a tensor with reduced dimension(32, 5, 3000, 1)<br>
nn.Conv2d(5, 5, kernel_size=(5, 4, 1, 4), stride=(1, 5, 1, 1),  bias=True)<br>
it returns the following error:<br>
RuntimeError: expected padding to be a single integer value or a list of 4 values to match the convolution dimensions, but got padding=[0, 0]</p>

ptrblck: <p><code>2d</code> layers are using the <code>kernel_size</code>and <code>stride</code> on the spatial dimensions.<br>
Since you are again defining a 4D <code>kernel_size</code>, could you explain your use case a bit more and give more information about the input dimensions to these layers?</p>

r00bi: <p>I think, I should use kernel_size = (5,4) instead of (5,4,14).</p>

r00bi: <p>It didn’t work. this is the information about input dimensions and layers:<br>
s = 150,000<br>
my input is [32,5,s, 1] and I need to apply the following maxpool and conv functions:</p>
<ol>
<li>maxpool, kernel size = (50,1), stride (50,1) → output =  (32, 5, 3000, 1) so: torch.nn.MaxPool2d(kernel_size=( 50,1), stride=( 50,1))</li>
<li>conv2d, kernel size = (5,4), stride(5,1) → output = (32, 1, 3000, 4): torch.nn.Conv2d(5, 1, kernel_size=(1,4),stride=(1, 1), bias=True)</li>
<li>conv2d, kernel size = (1,4), stide (1,1) -&gt;output = (32, 1, 3000, 4): torch.nn.Conv2d(1,1, kernel_size=(1,4), stride=(1,1))</li>
<li>maxpool2d, kernel size (1,6), stride (1,4) → output = (32, 1, 750, 4): torch.nn.MaxPool2d((1, 6), stride=(1, 4))</li>
<li>reshape to (32, 3000)</li>
<li>fully connected (32, 512)<br>
After applying the first conv2d at the second line it returns the following error:<br>
RuntimeError: Calculated padded input size per channel: (3000 x 1). Kernel size: (5 x 4). Kernel size can’t be greater than actual input size</li>
</ol>

ptrblck: <aside class="quote no-group quote-modified" data-username="r00bi" data-post="7" data-topic="145851">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/r00bi/48/47334_2.png" class="avatar"> r00bi:</div>
<blockquote>
<p>conv2d, kernel size = (5,4), stride(5,1) → output = (32, 1, 3000, 4): torch.nn.Conv2d(5, 5, kernel_size=(5, 4),stride=(5, 1), bias=True)</p>
</blockquote>
</aside>
<p>This layer won’t work as the <code>kernel_size</code> is specified as <code>(5, 4)</code> while the input seems to have a shape of <code>[batch_size, channels, 3000, 1]</code> which is too small in the <code>width</code>.</p>