Conv2d error in forward function

When implementing this CNN model for text classification, I’m running into this error:

RuntimeError: Expected tensor for argument #1 ‘input’ to have the same dimension as tensor for ‘result’; but 4 does not equal 2 (while checking arguments for cudnn_convolution)

Is this a bug in pytorch library?

In forward function, my x is 2D, so I do:
x = x.unsqueeze(0)
to make it 3D and anything else is the same as the example.

It has to be a 4D tensor (NCHW). You have to add an extra dimension for batch size

Since my original data is 2D, I did the following:

    # input: (W, D)
    # use 1 batch only

    x = x.unsqueeze(0)  # (N, W, D)
    x = x.unsqueeze(1)  # (N, Ci, W, D)

I believe after doing that, it is ok. However, this error is only happening when I am using GPU. When I am using CPU, there is no error.

I guess pytorch library has another bug in their code. Anyone has a solution?

Is your data also 2D when doing computations on CPU? Maybe you have an implicit unsqueeze in your code for CPU instructions?

Usually the error should occur in both cases or in none of them

No, GPU and CPU are the same shape, I dont think this is the issue.

This issue was reported but I am wondering if there is any solution right now so I can run on GPU.

From the link you’ve posted, it seems that one spatial dimension is smaller than the kernel size. Could you check that?

Also, it’s strange that the CPU code runs without an error, while your GPU code crashes as @justusschock mentioned. Could you print the shapes of your input and the conv layer setup?