Reputation: 854
I looked at several answers, and was not able to see a clear solution to what I'm trying to do.
I have an LSTM for binary text classification that takes the top 40k words in a corpus, then operates on the first 50 tokens. Prepared like this:
max_words = 40000
max_review_length = 50
embedding_vector_length = 100
batch_size = 128
epochs = 10
all_texts = combo.title.tolist()
lstm_text_tokenizer = Tokenizer(nb_words=max_words)
lstm_text_tokenizer.fit_on_texts(all_texts)
x_train = lstm_text_tokenizer.texts_to_sequences(x_train.title.tolist())
x_test = lstm_text_tokenizer.texts_to_sequences(x_test.title.tolist())
x_test = sequence.pad_sequences(x_test, maxlen=50)
x_train = sequence.pad_sequences(x_train, maxlen=50)
My current model looks like this:
def lstm_cnn_model(max_words, embedding_vector_length, max_review_length):
model = Sequential()
model.add(Embedding(max_words, embedding_vector_length, input_length=max_review_length))
model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(LSTM(100))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
return model
I also have a 1-dimensial list of meta data for each example, with one value per example. I may have more complex meta data to add in the future.
My question is, what is the best way to combine these two inputs in the training of the model?
Upvotes: 4
Views: 1597
Reputation: 11225
It would be wise to now switch to the functional API and create a multi-input network which will take the text as well the meta data:
text_in = Input(shape=(max_review_length,))
meta_in = Input(shape=(1,)) # so 1 meta feature per review
# Embedding(...)(text_in)
# You process text_in however you like
text_features = LSTM(100)(embedded_text)
merged = concatenate([text_features, meta_in]) # (samples, 101)
text_class = Dense(num_classes, activation='softmax')(merged)
model = Model([text_in, meta_in], text_class)
return model
The idea is that the functional API gives you the option to create computation graphs that can use both inputs in a non-sequential way. You can extract features from text, features from meta data and merge them to see if it improves classification. You might want to look at how to encode data for using meta data.
Upvotes: 4