1

I would like to build a CNN from the following diagram

CNN

I presume that each time the sentence can have various lengths and it is still possible to build it.

The below is a something I found on the web but kind of got lost

ngram_filters = [2, 3, 4] conv_filters = [] for n_gram in ngram_filters: conv_filters.append(Sequential()) conv_filters[-1].add(Conv2D(1, 1, n_gram, 5)) conv_filters[-1].add(MaxPooling2D(poolsize=(nb_tokens - n_gram + 1, 1))) conv_filters[-1].add(Flatten()) model = Sequential() model.add(Merge(conv_filters, mode='concat')) 

The picture comes from the following blog - http://www.wildml.com/2015/11/understanding-convolutional-neural-networks-for-nlp/

1

1 Answer 1

1

You don't need to use a variable input size, you just need to pad your input with zeros (make sure to mask these zeros). You can define your input size as a really big one and just pad the difference between each sentence length and the input size.

There is a built function in Keras', you can find it in Sequence Preprocessing and the function is "pad_sequences". Make sure your model is aware of the padding value by masking these values using the Masking class.

You can see an example of the implementation here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.