I want to use Conv1D and MaxPool1D in pytorch for a 3-d tensor to its third dimension

3.8k views Asked by At

For example, there is a 3-d tensor, I want to run the conv1d calculation on its third dimension,

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)

I hope the shape of y is (4,5,2,-1), but I get an error

Given groups=1, weight of size [2, 1, 5], expected input[4, 5, 6] to have 1 channels, but got 5 channels instead

Then I modified the code,

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)
x = x.unsqueeze(2)
y = conv1d(x)

There is another error:

Expected 3-dimensional input for 3-dimensional weight [2, 1, 5], but got 4-dimensional input of size [4, 5, 1, 6] instead

And if I want to run the maxpoo1d calulation in a tensor whose shape is (4,5,2,-1) ,in its last two dimension, what should I do?

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. Thank you all for your help.

I made an attempt, but I felt it couldn’t meet the actual needs, I wanted to know if it's good practice to do that and what would be the best way to do that?

import torch
import torch.nn as nn
x = torch.rand(4,5,6)
conv1d =nn.Conv1d(in_channels=1,out_channels=2,kernel_size=2,stride=3,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())
2

There are 2 answers

4
Yuri Feldman On

To use Conv1d you need your input to have 3 dimensions:

[batch_size, in_channels, data_dimension]

So, this would work:

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

You can use MaxPool1d in the same way:

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

I sovle this question by torch.reshape(). I put the code here, hoping it could help somebody.

import torch
import torch.nn as nn
x = torch.rand(4,5,6)
conv1d =nn.Conv1d(in_channels=1,out_channels=2,kernel_size=2,stride=3,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],2,-1)
print(z.size())