Pytorch MaxPool2d

先说说nn.functional和nn:只调用函数的话,其实是一回事。
再说说我学nn.MaxPool2d时遇到的问题:

import torch
import torch.nn as nn
m=nn.MaxPool2d(3,stride=2)
input=torch.randn(6,6)
output=m(input)

然后就会报这个错:

RuntimeError: non-empty 3D or 4D (batch mode) tensor expected for input

我寻思这不是maxpool2d么,为啥非得要3d或4d的输入数据啊。后来才明白,这个3d多出来的一维是指多个2d输入存到一个input里,然后用这个maxpool2d批量处理。把输入改一下:

input=torch.randn(6,6,6)
input

tensor([[[ 1.3651, -0.5862,  0.0039,  0.7669, -1.1744, -0.6920],
         [-1.2498,  0.5902,  0.8022,  0.3453,  0.4135, -2.0394],
         [-1.1461, -0.8356,  0.2398, -1.0064, -0.8885,  0.5996],
         [-0.6647,  0.7676, -1.0845, -0.0285, -0.8607, -1.5220],
         [-0.6462,  0.4841, -0.8933, -0.0123,  0.1304, -1.6382],
         [-1.6518,  0.1764, -0.3518,  2.1585,  0.7927, -1.2119]],

        [[-0.4033, -0.3612, -1.0288,  0.1967,  0.5706, -0.2084],
         [-0.3500, -0.5760,  0.4434, -0.0729, -0.6940,  0.8788],
         [ 0.3062, -0.3983, -1.0179, -0.0206,  1.6909,  1.3363],
         [-0.5441, -0.3139,  0.3312,  0.0118,  0.6946,  1.0262],
         [-0.7408,  1.8493, -0.3663, -0.7789,  0.6197,  1.3990],
         [ 0.2292,  0.4398,  0.9694,  0.7889, -0.9330,  0.1868]],

        [[-0.6787, -0.9165, -1.2320, -1.3221,  1.0197,  0.8859],
         [ 0.3160, -0.6479, -1.8085,  0.0580,  1.6721,  0.8309],
         [ 0.0744, -0.0405, -1.5407,  1.7720,  1.3292,  1.6349],
         [-0.8554, -0.9928,  0.1849,  0.2057,  0.0687,  0.7527],
         [ 0.9199, -0.7936,  0.3789, -0.1087, -0.4615, -0.9368],
         [-0.1616, -0.0135, -0.0403,  0.0218, -1.7800, -0.3535]],

        [[ 1.1523, -0.6533, -0.6440, -1.4352, -0.4556, -0.5540],
         [ 1.8307, -0.3786, -0.0065, -0.9373, -1.3478, -0.9279],
         [ 0.1462, -0.8618,  1.1309, -1.6756, -0.2423,  0.4219],
         [-1.3056,  0.6921,  0.0219, -0.0710, -0.4203,  0.9639],
         [ 0.5725, -1.2465, -0.4121,  1.0231,  1.3580,  0.7545],
         [-0.8918, -0.1593, -1.7663, -0.7283,  0.9509,  1.6359]],

        [[-0.8375, -0.8949, -1.7319, -0.9304,  2.3549, -0.3261],
         [ 0.3865, -0.4016,  0.4349,  0.5395,  0.0925, -0.9209],
         [ 0.5214,  0.3324,  0.9826, -0.4607,  0.5845, -0.4193],
         [ 1.5872, -0.5236, -0.6116, -0.1904, -1.7557, -0.8296],
         [-0.1711,  0.0454, -0.3158,  2.6561,  1.6830,  0.5439],
         [-0.2289, -1.1473, -1.0294,  0.0683, -0.0164,  0.8971]],

        [[-0.3294,  0.7784,  0.8532, -1.1260, -0.8300, -1.7882],
         [-1.3112,  0.7504,  1.0123, -1.0672,  1.0213, -0.4506],
         [-1.2506,  0.4484, -0.7977,  0.2568, -0.7581,  0.6981],
         [-1.3001,  0.1575, -1.2660,  0.5367, -1.6008,  0.7765],
         [ 1.3728, -1.2267, -0.1835, -0.8143, -0.1198, -0.3916],
         [ 2.8967,  1.0502,  0.8877,  0.4246, -0.2282,  0.0319]]])

output=m(input)
output

tensor([[[1.3651, 0.8022],
         [0.7676, 0.2398]],

        [[0.4434, 1.6909],
         [1.8493, 1.6909]],

        [[0.3160, 1.7720],
         [0.9199, 1.7720]],

        [[1.8307, 1.1309],
         [1.1309, 1.3580]],

        [[0.9826, 2.3549],
         [1.5872, 2.6561]],

        [[1.0123, 1.0213],
         [1.3728, 0.5367]]])

这回结果就出来了。

你可能感兴趣的:(pytorch,神经网络)