我想在pytorch中将Conv1D和MaxPool1D用于3D张量到其第三维

问题描述

例如,有一个3-d张量,我想在其第三维上运行conv1d计算,

import torch
import torch.nn as nn
x = torch.rand(4,5,6)
conv1d =nn.Conv1d(in_channels=1,out_channels=2,kernel_size=5,stride=3,padding=0)
y = conv1d(x)

我希望y的形状为(4,2,-1),但出现错误

给定组= 1,权重为[2,1,5],预期输入[4,5,6]具有1个通道,但改为5个通道

然后我修改代码

import torch
import torch.nn as nn
x = torch.rand(4,padding=0)
x = x.unsqueeze(2)
y = conv1d(x)

还有另一个错误

对于3维权重[2,1,5]预期的3维输入,但是得到的尺寸为[4,5,1,6]的4维输入

如果我想在形状为(4,2,-1)的张量中运行maxpoo1d计算,在最后一个二维中,我应该怎么做?

我正在网上搜索很长时间。但是没用。请帮助或尝试给出一些想法来实现这一目标。谢谢大家的帮助。

我尝试了一下,但我觉得这不能满足实际需求,我想知道这样做是否是一种好习惯,什么是最好的方法

import torch
import torch.nn as nn
x = torch.rand(4,kernel_size=2,padding=0)
x = x.unsqueeze(2)
for i in range(4): 
    y = conv1d(x[i,:,:])
    y = y.unsqueeze(0)
    if i==0:
        z = y
    else:
        z = torch.cat((z,y),0)
    print(y)
print(z.size())

解决方法

要使用Conv1d,您需要输入3个维度:

[batch_size,in_channels,data_dimension]

因此,这将起作用:

x = torch.rand(4,1,50)  # [batch_size=4,in_channels=1,data_dimension=50]
conv1d = nn.Conv1d(in_channels=1,out_channels=2,kernel_size=2,stride=3,padding=0)
x = conv1d(x)
print(x.shape) # Will output [4,2,16] 4=batch_size,2=channels,16=data_dimension

您可以通过以下方式使用MaxPool1d

maxpool1d = nn.MaxPool1d(5)
x = maxpool1d(x)
print(x.shape) # Will output [4,3]  4=batch_size,3=data_dimension
,

我通过torch.reshape()解决了这个问题。我将代码放在这里,希望它能对某人有所帮助。

import torch
import torch.nn as nn
x = torch.rand(4,5,6)
conv1d =nn.Conv1d(in_channels=1,padding=0)

y = x.reshape(x.shape[0]*x.shape[1],-1)
y = y.unsqueeze(1)
y = conv1d(y)
z = y.reshape(x.shape[0],x.shape[1],-1)
print(z.size())