AldehydeDeva
AldehydeDeva

Reputation: 188

How to properly implement 1D CNN for numerical data in PyTorch?

I have a 500x2000 matrix, where each row represents an individual and each column is a measurement of some particular quality about that individual. I'm using a batch size of 64, so the input for each cycle of the network is actually a 64x2000 matrix. I'm trying to build a CNN in PyTorch to classify individuals given a set of these measurements. However, I've stumbled on the parameters for the convolutional layer.

Below is my current definition for a simple convolutional neural network.

class CNNnet(nn.Module)
    def __init__(self):
        self.conv1 = nn.Conv1d(2000, 200, (1,2), stride=10)
        self.pool = nn.MaxPool1d(kernel_size = (1, 2), stride = 2)

        self.fc1 = nn.Linear(64, 30)
        self.fc2 = nn.Linear(30, 7)

    def forward(self, x):
        x = x.view(64, 2000, 1)
        x = F.relu(self.conv1(x))
        x = self.pool(x)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

Attempting to train this model produces the following error:

"RuntimeError: Expected 4-dimensional input for 4-dimensional weight 200 2000 1 2, but got 3-dimensional input of size [64, 2000, 1] instead".

I'm confused on why it's expecting a 4D 200x2000x1x2 matrix (shouldn't the number of output channels be irrelevant to the input? And why is there a 2 at the end?).

My question is what would be the proper syntax or approach for writing a CNN (specifically the convolutional layer) when dealing with 1D data. Any help is greatly appreciated.

Upvotes: 3

Views: 3001

Answers (1)

David
David

Reputation: 8318

So the kernel size in the 1 dimensional case is simply a vector. So if you’ll want a kernel of size ‘1X2’ you need to specify the ‘2’ In the 2 dimensional case 2 will mean a ‘2X2’ kernel size.

You gave a tuple of 2 values so you use 2 kernel types each will create its own channel

Upvotes: 3

Related Questions