Omnia
Omnia

Reputation: 887

Multiple Features at the Input Layer Keras Python

I am implementing a simple LSTM network. I would like to include multiple features at the input layer. These features are a pre-trained word embeddings and a vector to flag a specific word in the given sentence.

For example:

Sentence = "I have a question"
feature_vector_1 = [4, 2, 281, 5201] #word2index which will be passed to the embedding layer
feature_vector_2 = [0, 1, 0, 0]

final features= [feature_vector_1 + feature_vector_2]

suppose that:

embedding is of dim = 100
index_flag is of dim = 50 
max sentence length = 50 

My network code is:

input= Input(shape=(None,))
embedded_layer_input=Embedding(input_dim=embedding_matrix.shape[0], output_dim=embedding_matrix.shape[1],
                     input_length=tweet_max_length, weights= [embedding_matrix], trainable=False)(input)
lstm_layer=Bidirectional(LSTM(64))(embedded_layer_input)
output_layer=Dense(1,activation='sigmoid')(lstm_layer)

model=Model(input, output_layer)

#complie and train
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])  

# Summarize the model
print(model.summary()) 

# Fit the model
model.fit(padded_train_x, y_train, epochs=epochs, batch_size=batch_size, shuffle=False, verbose=1, validation_data=(padded_dev_x,y_dev))  

My question is how and where to include the new feature vector? I looked at Concatenate but I am not sure how to prepare feature vector 2.

Upvotes: 0

Views: 689

Answers (1)

nuric
nuric

Reputation: 11225

You can add a second input just like the first one and concatenate afterwards:

input= Input(shape=(None,))
flag_in = Input(shape=(None,)) ##
embedded_layer_input=Embedding(input_dim=embedding_matrix.shape[0], output_dim=embedding_matrix.shape[1],
                     input_length=tweet_max_length, weights= [embedding_matrix], trainable=False)(input)
combined = Concatenate()([embedded_layer_input, flag_in])
lstm_layer=Bidirectional(LSTM(64))(combined)
output_layer=Dense(1,activation='sigmoid')(lstm_layer)
# From now on you pass a list as your input to your model
model=Model([input, flag_in], output_layer)
# ...
model.fit([padded_xtrain, x_flag_inputs], ...)

Upvotes: 2

Related Questions