简体   繁体   English

输入层的多种功能Keras Python

[英]Multiple Features at the Input Layer Keras Python

I am implementing a simple LSTM network. 我正在实现一个简单的LSTM网络。 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. 我看了看Concatenate,但不确定如何准备特征向量2。

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], ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM