TypeError: max_pool2d_with_indices(): argument 'input' (position 1) must be Tensor, not Tensor

username: zhao_jing
cooked: <p>When SPP is invoked, the system reports errors:<br>
code:</p>
<pre><code class="lang-python">import torch
import math
import torch.nn.functional as F
def spatial_pyramid_pool(previous_conv, num_sample, previous_conv_size, out_pool_size):
    for i in range(len(out_pool_size)):
        kernel_size = (math.ceil(previous_conv_size[0] / out_pool_size[i]), math.ceil(previous_conv_size[1] / out_pool_size[i]))
        stride = (math.ceil(previous_conv_size[0] / out_pool_size[i]), math.ceil(previous_conv_size[1] / out_pool_size[i]))
        pooling = (
            math.floor((kernel_size[0] * out_pool_size[i] - previous_conv_size[0] + 1) / 2), math.floor((kernel_size[1] * out_pool_size[i] - previous_conv_size[1] + 1) / 2))
       x = (F.max_pool2d(previous_conv, kernel_size=kernel_size, stride=stride, padding=pooling)).view(num_sample, -1)

        if(i == 0):
            spp = x.view(num_sample, -1)
            # nn.MaxPool2d.view()  已经试过，不对
            # print("spp size:",spp.size())
        else:
            # print("size:",spp.size())
            spp = torch.cat((spp, x.view(num_sample, -1)), 1)
    return spp



errors:
 File "C:/Users/15044/pycharmProjects/cell/CNN_SPP/recognition.py", line 161, in &lt;module&gt;
    logits = inference(x, False, regularizer)
  File "C:/Users/15044/pycharmProjects/cell/CNN_SPP/recognition.py", line 126, in inference
    spp = spatial_pyramid_pool(relu4, 63, [256, 256], [1, 4])
  File "C:\Users\15044\pycharmProjects\cell\CNN_SPP\spp_layer.py", line 38, in spatial_pyramid_pool
    x = (F.max_pool2d(previous_conv, kernel_size=kernel_size, stride=stride, padding=pooling)).view(num_sample, -1)
  File "D:\Users\15044\Anaconda3\lib\site-packages\torch\_jit_internal.py", line 132, in fn
    return if_false(*args, **kwargs)
  File "D:\Users\15044\Anaconda3\lib\site-packages\torch\nn\functional.py", line 425, in _max_pool2d
    input, kernel_size, stride, padding, dilation, ceil_mode)[0]
  File "D:\Users\15044\Anaconda3\lib\site-packages\torch\nn\functional.py", line 417, in max_pool2d_with_indices
    return torch._C._nn.max_pool2d_with_indices(input, kernel_size, _stride, padding, dilation, ceil_mode)
TypeError: max_pool2d_with_indices(): argument 'input' (position 1) must be Tensor, not Tensor
</code></pre>

username: ptrblck
cooked: <p>I’m not sure, which setup throws this error, since I’ve tried a few and all seem to work:</p>
<pre><code class="lang-python">previous_conv = torch.randn(1, 3, 4, 4)
num_sample = 1
previous_conv_size = [3, 3]
out_pool_size = [2, 2]

spatial_pyramid_pool(previous_conv, num_sample, previous_conv_size, out_pool_size)
</code></pre>
<p>Could you post a code snippet to reproduce this issue?</p>
<p>PS: I’ve formatted your code so that I could easily copy it. You can add code snippets by wrapping them in three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: zhao_jing
cooked: <p>I think the software version caused this error.<br>
I changed the code as follows,this error no longer appears, but a new error has occurred.<br>
code:<br>
change “import torch.nn.functional as F” to  “import torch.nn as nn”<br>
change"x = (F.max_pool2d(previous_conv, kernel_size=kernel_size, stride=stride, padding=pooling)).view(num_sample, -1)" to “x = nn.MaxPool2d(previous_conv, kernel_size, stride, pooling)”<br>
The new error is “AttributeError: ‘MaxPool2d’ object has no attribute ‘view’”<br>
I don’t kown why,could you heip me?</p>

username: ptrblck
cooked: <p><code>nn.MaxPool2d</code> is a module, so you would have to pass the tensor to it:</p>
<pre><code class="lang-python">pool = nn.MaxPool2d(kernel_size, stride)
x = pool(previous_conv)
</code></pre>
<p>It looks like you are trying to pass the tensor directly to the constructor of the class.<br>
The functional approach (using <code>F.max_pool2d</code>) should also work.</p>

username: ptrblck
cooked: <p>The code you’ve posted looks like Tensorflow code, so you might be luckier asking on stack overflow. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>