Search code examples
pythonkerasconcatenationlstmembedding

Keras - LSTM with embeddings of 2 words at each time step


The code below constructs a LSTM model. I would like to change this exact model to have at the beginning an embedding layer, which at each time step receives 2 different words, embeds them (with the same embedding layer): It concatenates their embedding, and then follows the rest of my model.

k_model = Sequential()

k_model.add(LSTM(int(document_max_num_words*1.5), input_shape=(document_max_num_words, num_features)))
k_model.add(Dropout(0.3))
k_model.add(Dense(num_categories))
k_model.add(Activation('sigmoid'))

k_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

Solution

  • If I understand your question correctly, assuming the input data has a shape of (n_samples, n_timesteps, 2) (i.e. two words per step), you can achieve what you are looking for using TimeDistributed wrapper:

    from keras import layers
    from keras import models
    
    n_vocab = 1000
    n_timesteps = 500
    embed_dim = 128
    words_per_step = 2
    
    model = models.Sequential()
    model.add(layers.TimeDistributed(layers.Embedding(n_vocab, embed_dim), input_shape=(n_timesteps, words_per_step)))
    model.add(layers.TimeDistributed(layers.Flatten()))
    # the rest of the model
    
    model.summary()
    

    Model summary:

    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    time_distributed_12 (TimeDis (None, 500, 2, 128)       128000    
    _________________________________________________________________
    time_distributed_13 (TimeDis (None, 500, 256)          0         
    =================================================================
    Total params: 128,000
    Trainable params: 128,000
    Non-trainable params: 0
    _________________________________________________________________