pytroch如何对线性层进行池化(pooling)?Expected 3-dimensional tensor, but got 2-dimensional tensor for argument

版权声明:转载注明出处 https://blog.csdn.net/york1996/article/details/84110765

要实现的功能如图所示

而池化操作是要有通道的,如果直接对(batchsize,num_neuron)的张量进行一维池化(nn.MaxPool1d)操作,会有以下的错误

import torch
t=torch.randn(10,64)
n=torch.nn.MaxPool1d(2,2)
n(t)

Expected 3-dimensional tensor, but got 2-dimensional tensor for argument #1 'self' (while checking arguments for max_pool1d)

正确的做法应该是先进行升维,升出来通道维度。

import torch
t=torch.randn(10,64)
t=t.unsqueeze(1)
n=torch.nn.MaxPool1d(2,2)
out=n(t)
print(out.size())

 以上代码的输出为:

torch.Size([10, 1, 32])

在对多层感知机进行池化的过程也是类似的。

池化之后应该就不需要通道维了,可以用squeeze降低维度。 

猜你喜欢

转载自blog.csdn.net/york1996/article/details/84110765
今日推荐