# Convolutional NN for text input

**URL:** <https://discuss.pytorch.org/t/convolutional-nn-for-text-input/3442>\
**Category:** Uncategorized\
**Created:** [May 26, 2017, 12:51am UTC](https://discuss.pytorch.org/t/convolutional-nn-for-text-input/3442 "2017-05-26T00:51:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wasiahmad](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wasiahmad/32/248_2.png) [@wasiahmad](https://discuss.pytorch.org/u/wasiahmad)\
**Post date:** [May 26, 2017, 12:51am UTC](https://discuss.pytorch.org/t/convolutional-nn-for-text-input/3442/1 "2017-05-26T00:51:12Z")

</div>

I am trying to implement a [text classification model](http://d3kbpzbmcynnmx.cloudfront.net/wp-content/uploads/2015/11/Screen-Shot-2015-11-06-at-12.05.40-PM.png) using CNN. As far as I know, for text data, we should use 1d Convolution. I saw an example in pytorch using Conv2d but I want to know how can I apply Conv1d for text? Or, it is actually not possible?

Here is my model scenario:

Number of in-channels: 1, Number of out-channels: 128  
Kernel size : 3 (only want to consider trigrams)  
Batch size : 16

So, I will provide tensors of shape, \<16, 1, 28, 300\> where 28 is the length of a sentence. I want to use Conv1d which will give me 128 feature maps of length 26 (as I am considering trigrams).

I am not sure, how to define `nn.Conv1d()` for this setting. I can use Conv2d but want to know is it possible to achieve the same using Conv1d?

---

<div class="post-metadata">

**Author:** ![ehsanmok](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ehsanmok/32/15849_2.png) [@ehsanmok](https://discuss.pytorch.org/u/ehsanmok)\
**Post date:** [May 26, 2017, 1:16am UTC](https://discuss.pytorch.org/t/convolutional-nn-for-text-input/3442/2 "2017-05-26T01:16:27Z")

</div>

You can always `view(1, -1)` the tensor though!

---

<div class="post-metadata">

**Author:** ![wasiahmad](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wasiahmad/32/248_2.png) [@wasiahmad](https://discuss.pytorch.org/u/wasiahmad)\
**Post date:** [May 26, 2017, 1:17am UTC](https://discuss.pytorch.org/t/convolutional-nn-for-text-input/3442/3 "2017-05-26T01:17:14Z")

</div>

I didn’t understand your answer. Please explain briefly.

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [May 26, 2017, 3:07am UTC](https://discuss.pytorch.org/t/convolutional-nn-for-text-input/3442/4 "2017-05-26T03:07:44Z")

</div>

have you looked at the example of Conv1d in the documentation? It will help you figure it out.  
Conv1d takes 3d inputs (mini-batch x Channels x width).

---

<div class="post-metadata">

**Author:** ![spro](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/spro/32/146_2.png) [@spro](https://discuss.pytorch.org/u/spro)\
**Post date:** [May 26, 2017, 4:36am UTC](https://discuss.pytorch.org/t/convolutional-nn-for-text-input/3442/5 "2017-05-26T04:36:08Z")

</div>

Here’s an example of Conv1d and Pool1d layers into an RNN: [https://gist.github.com/spro/c87cc706625b8a54e604fb1024106556](https://gist.github.com/spro/c87cc706625b8a54e604fb1024106556)

---

<div class="post-metadata">

**Author:** ![wasiahmad](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wasiahmad/32/248_2.png) [@wasiahmad](https://discuss.pytorch.org/u/wasiahmad)\
**Post date:** [May 26, 2017, 8:00am UTC](https://discuss.pytorch.org/t/convolutional-nn-for-text-input/3442/6 "2017-05-26T08:00:49Z")

</div>

Thanks, the following worked for me. Your example helped me. I didn’t know that the embedding dimension can be used as the number of in-channels.

```
m = nn.Conv1d(200, 10, 2)
input = Variable(torch.randn(10, 200, 5))
feature_maps1 = m(input)
print(feature_maps1.size())

```

I have one question. Why the kernel size can be a tuple in Conv1d? Even the stride, padding, dilation everything can be tuple. How they can be tuple for Conv1d? Can you provide an example?

@smth, yes I saw that example previously. At that time, I was assuming that for text data, number of channels should be 1. I never thought the way @spro used Conv1d in his provided example!

---

<div class="post-metadata">

**Author:** ![Sam131112](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sam131112/32/15279_2.png) [@Sam131112](https://discuss.pytorch.org/u/Sam131112)\
**Post date:** [December 16, 2018, 5:35am UTC](https://discuss.pytorch.org/t/convolutional-nn-for-text-input/3442/7 "2018-12-16T05:35:34Z")

</div>

hi , did you find any good resource to understand 1d convolution for text ? I mean I am not able to understand by having the embedding dimention as input channel makes sense ? Also in your example the 5 word sequence (each 200 dimension ) i.e 1000 vector for each sentence is converted into 40 after the process right ?
