Output shape of conv1d in Pytorch and Keras are different?

[in Pytorch]

fc1 = Sequential (
  (0): Linear (147 -> 1000)
  (1): BatchNorm1d(1000, eps=1e-05, momentum=0.1, affine=True)
  (2): LeakyReLU (0.2, inplace)
)

conv1 = Sequential (
  (0): Conv1d(100, 250, kernel_size=(13,), stride=(1,), padding=(6,))
  (1): BatchNorm1d(250, eps=1e-05, momentum=0.1, affine=True)
  (2): LeakyReLU (0.2, inplace)
)

a = Variable(torch.randn(10, 147))
b = fc1(a)
b.size()
Out[71]: torch.Size([10, 1000])
torch.Size([10, 1000])
b = b.view(10,100,10)
c = conv1(b)
c.size() 
Out[75]: torch.Size([10, 250, 10])

In Keras

x = Input(shape=(147,),name="input")
In [78]: x
Out[78]: <tf.Tensor 'input:0' shape=(?, 147) dtype=float32>
x = Dense(10 * 100)(x) 
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x) # output shape is 10*100

x = Reshape((100, 10))(x) # shape is 100 x 10

x = Conv1D(filters=250, kernel_size=13, padding='same')(x)
 x
Out[80]: <tf.Tensor 'conv1d_4/add:0' shape=(?, 100, 250) dtype=float32>

The question is the output after applying the pytorch conv1d is (?, 250, 10) where as the keras output is (?, 100, 250)
Could you tell me why they are different and how to write conv1d in pytorch to match with keras conv1d.

Thanks,

Hello,

this is due to the different memory layout conventions between pytorch and tensorflow (as a Keras backend).
Pytorch (and e.g. Theano, Keras+Theano) is “channel first”, while Tensorflow and Keras + Tensorflow are “channel last”.
Keras adapts to the backend and has keras.backend.image_data_format() to differentiate between the two.

Best regards

Thomas