# Output shape of conv1d in Pytorch and Keras are different?

**URL:** <https://discuss.pytorch.org/t/output-shape-of-conv1d-in-pytorch-and-keras-are-different/3398>\
**Category:** Uncategorized\
**Created:** [May 24, 2017, 10:05am UTC](https://discuss.pytorch.org/t/output-shape-of-conv1d-in-pytorch-and-keras-are-different/3398 "2017-05-24T10:05:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bajibabu](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/4af34b/32.png) [@bajibabu](https://discuss.pytorch.org/u/bajibabu)\
**Post date:** [May 24, 2017, 10:05am UTC](https://discuss.pytorch.org/t/output-shape-of-conv1d-in-pytorch-and-keras-are-different/3398/1 "2017-05-24T10:05:12Z")

</div>

**[in Pytorch]**

```auto
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**

```auto
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,

---

<div class="post-metadata">

**Author:** ![tom](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tom/32/3162_2.png) [@tom](https://discuss.pytorch.org/u/tom)\
**Post date:** [May 24, 2017, 11:01am UTC](https://discuss.pytorch.org/t/output-shape-of-conv1d-in-pytorch-and-keras-are-different/3398/2 "2017-05-24T11:01:26Z")

</div>

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
