How do I get the correct Tensor dimensions after Conv2d Layer? (keras to PyTorch)

I am trying to recreate a keras model in PyTorch. My input tensor has the shape (2, 10, 25). In keras, I apply the following Conv2d layer to my input tensor:

import tensorflow as tf

batch_size = 1
x = tf.random.normal( (batch_size, 2, 10, 25) )
y = tf.keras.layers.Conv2D(filters=34, 
                           kernel_size=(1,10),
                           padding='valid',
                           input_shape=input_shape[2:])(x)
print(y.shape)

# (1, 2, 1, 34)

When trying to recreate this layer in PyTorch, I am not quite grasping the dimensionalities to use, since the Conv2d layer in PyTorch has no filters parameter. This is how far I’ve gotten on my own:

import torch
import torch.nn as nn

batch_size = 1
x = torch.randn(batch_size, 2, 10, 25)
conv = nn.Conv2d(in_channels=2, out_channels=2, kernel_size=(10,1), bias=True)

y = conv(input_data)
print(y.shape)

# torch.Size([1, 2, 1, 25])

Now, all dimensions except for the last one are identical to the keras implementation. I suspect my understanding of the out_channels parameter is not correct but I am not sure how to proceed from here.

Any help is hugely appreciated!

The documentation for Conv2d gives the formulas for computing the output shape; search for the string “Shape:” to find these in that page.

Thanks for the input, however, when keeping all common parameters the same when porting from keras to PyTorch (padding=‘valid’ → no padding, stride=(1,1), dilation=(1,1), groups=1), the dimensionalities don’t add up. That’s what’s confusing me.

I know I can achieve the right dimensions when I add padding=(0,5) and kernel_size=(10,2), but that does not seem to result in the same output. The number of parameters for the keras implementation are:

model.summary()

Model: "model_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 2, 10, 25)]  0                                            
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, 2, 1, 34)     8534        input_1[0][0]                 

whereas for the PyTorch implementation with padding=(0,5) and kernel_size=(10,2):

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = SoccerModel().to(device)
summary(model, [(2, 10, 25), (2, 1, 11)])

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1             [-1, 2, 1, 34]              82

You use the variable input_shape in your keras code, but you don’t show how you initialize this variable. Could you show the value of input_shape?

TF/Keras use the channels-last memory layout so your input has the shape [batch_size, height=2, width=10, channels=25], while PyTorch uses channels-first.
The nn.Conv2d layer is currently using in_channels=2 in PyTorch, which is wrong, so you should change it to in_channels=25 and permute the input to [batch_size, 25, 2, 10].