我正在实现一个简单的LSTM网络。我想在输入层包含多个功能。这些功能是预先训练的单词嵌入和在给定句子中标记特定单词的向量。
例如:
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]
假设:
embedding is of dim = 100
index_flag is of dim = 50
max sentence length = 50
我的网络代码是:
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))
我的问题是如何以及在何处包括新特征向量? 我看了看Concatenate,但不确定如何准备特征向量2。
答案 0 :(得分:1)
您可以像添加第一个输入一样添加第二个输入,然后将其连接:
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], ...)