Hi, I am new to keras and trying to implements a model a part of that includes encoding sentences using convolution network.
# import statements
from keras.layers.embeddings import Embedding
from keras.layers.wrappers import TimeDistributed
from keras.layers.convolutional import Convolution1D
from keras.layers.pooling import MaxPooling1D
from keras.engine.topology import Merge
from keras.layers.core import Dropout, RepeatVector
from keras.layers import LSTM, Input, Dense
from keras.models import Model, Sequential
from IPython.display import SVG
def conv_encoder(nb_filters, filter_len, in_shape):
"""model to encode a sentence with a particular filter size
nb_filters: number of filters, filter_len: size of filter,
in_shape: input shape (_, SENT_LEN, WORD_LEN)
"""
model = Sequential()
model.add(Convolution1D(nb_filters, filter_len, border_mode='same', input_shape=in_shape))
model.add(MaxPooling1D(pool_length=2, stride=None, border_mode='same'))
return model
def make_sentence_encoder(nb_filters, filter_lens, in_shape):
"""model to encode a sentence with different filter sizes
basically, learns different types of representations based
on filter sizes like unigram, bigram, trigram etc.
"""
models = []
for flt in filter_lens:
models.append(conv_encoder(nb_filters, flt, in_shape))
merged_model = Sequential()
merged_model.add(Merge(models, mode='sum', concat_axis=1))
return merged_model
def encode_all_sentences(vocab_size, dense_emb_dim, doc_maxlen, sent_maxlen, nb_filters, filter_lens, in_shape):
"""A model that encodes each sentences using conv nets.
Returns encoded set of sentences.
Input: Document, SHAPE: (_, DOC_LEN, SENT_LEN)
Output: SHAPE: (_, DOC_LEN, NEW_SENT_LEN)
"""
# setup a sentence encoder
sent_encoder = make_sentence_encoder(nb_filters, filter_lens, in_shape)
# embed the input sequence into a sequence of vectors
sentences_encoder = Sequential()
# initialize embedding layer with wordvec
sentences_encoder.add(TimeDistributed(Embedding(input_dim=vocab_size, output_dim=dense_emb_dim, mask_zero=True),
input_shape=(doc_maxlen, sent_maxlen), input_dtype='int32'))
sentences_encoder.add(TimeDistributed(Dropout(0.3)))
sentences_encoder.add(TimeDistributed(sent_encoder))
return sentences_encoder
The line sentences_encoder.add(TimeDistributed(sent_encoder)) in encode_all_sentences method is source of problem. I guess it's because sent_encoder takes input as list of size equal to len(filter_lens) i.e total number of different filters used. I don't know/am confused how to pass it here.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.
Hi, I am new to keras and trying to implements a model a part of that includes encoding sentences using convolution network.
Test code:
The line
sentences_encoder.add(TimeDistributed(sent_encoder))
inencode_all_sentences
method is source of problem. I guess it's becausesent_encoder
takes input as list of size equal to len(filter_lens) i.e total number of different filters used. I don't know/am confused how to pass it here.Help will be highly appreciated. Thanks.
Error thrown :
`--------------------------------------------------------------------------- AssertionError Traceback (most recent call last)