class DeepWise_MaxPool(nn.MaxPool1d):
def __init__(self, channels):
super(DeepWise_MaxPool, self).__init__(channels)
def forward(self, input):
n, c, h, w = input.size()
input = input.view(n, c, h * w).permute(0, 2, 1)
pooled = torch.nn.functional.max_pool1d(input, self.kernel_size, self.stride,
self.padding, self.dilation, self.ceil_mode, self.return_indices)
_, _, c = pooled.size()
pooled = pooled.permute(0, 2, 1)
return pooled.view(n, c, h, w)
在模型中使用该类作为基本结构的一部分时,网络反向传播报错,使用contiguous()将permute()操作之后的tensor变为连续。
Returns a contiguous in memory tensor containing the same data as self tensor. If self tensor is already in the specified memory format, this function returns the self tensor
class DeepWise_MaxPool(nn.MaxPool1d):
def __init__(self, channels):
super(DeepWise_MaxPool, self).__init__(channels)
def forward(self, input):
n, c, h, w = input.size()
input = input.view(n, c, h * w).permute(0, 2, 1).contiguous()
pooled = torch.nn.functional.max_pool1d(input, self.kernel_size, self.stride,
self.padding,self.dilation, self.ceil_mode, self.return_indices)
_, _, c = pooled.size()
pooled = pooled.permute(0, 2, 1).contiguous()
return pooled.view(n, c, h, w)