简体   繁体   中英

Concatenate an input of 27 fields to the output of the LSTM layer using Keras in Python

I have an existing LSTM model that looks as follows:

model_glove1 = Sequential()
model_glove1.add(Embedding(vocabulary_size, 25, input_length=50, weights=[embedding_matrix25],trainable=False))
model_glove1.add(LSTM(32))
model_glove1.add(Dense(128, activation='relu'))
model_glove1.add(Dense(64, activation='relu'))
model_glove1.add(Dense(1, activation='softmax'))
model_glove1.compile(loss='binary_crossentropy',optimizer='adam',metrics['accuracy',auc_roc])
model_glove1.fit(data, np.array(train_y), batch_size=32,
epochs=4,
verbose=1,
validation_split=0.1,
shuffle=True)

I want to add an additional auxiliary input layer which is present in a dataframe of 27 columns . I want that layer to be concatenated with the output of the LSTM layer. Is it possible ? If so how can I achieve it?

Before using the code, please check the secondary input has the same dimension like output of LSTM layer.

Moreover, in model1_glove.fit() function, you need to provide two inputs

def NNStructure():
    initial_input= Embedding(vocabulary_size, 25, input_length=50, weights= 
    [embedding_matrix25],trainable=False) 
    lstm = LSTM(32)(initial_input)   
    secondary_input = Input(shape=(Number_of_row,27))    
    merge = concatenate([lstm, secondary_input])
    first_dense = Dense(128, activation='relu')(merge)
    second_dense=Dense(64, activation='relu')(first_dense)
    output=Dense(1, activation='softmax')(second_dense)

    model_glove1 = Model(inputs=[initial_input, secondary_input], outputs=output)
    return model_glove1

model_glove1=NNStructure()
model_glove1.compile(loss='binary_crossentropy',optimizer='adam',metrics['accuracy',auc_roc])
model_glove1.fit(x=[data1,data2], y=np.array(train_y), batch_size=32,
epochs=4,
verbose=1,
validation_split=0.1,
shuffle=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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