如何使用函数式API在keras中实现合并功能

时间:2019-02-28 11:26:23

标签: python keras deep-learning lstm

print("Building model...")
ques1_enc = Sequential()
ques1_enc.add(Embedding(output_dim=64, input_dim=vocab_size, weights=[embedding_weights], mask_zero=True))
ques1_enc.add(LSTM(100, input_shape=(64, seq_maxlen), return_sequences=False))
ques1_enc.add(Dropout(0.3))

ques2_enc = Sequential()
ques2_enc.add(Embedding(output_dim=64, input_dim=vocab_size, weights=[embedding_weights], mask_zero=True))
ques2_enc.add(LSTM(100, input_shape=(64, seq_maxlen), return_sequences=False))
ques2_enc.add(Dropout(0.3))

model = Sequential()
model.add(Merge([ques1_enc, ques2_enc], mode="sum"))
model.add(Dense(2, activation="softmax"))

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
print("Building model costs:", time.time() - start)

print("Training...")
checkpoint = ModelCheckpoint(filepath=os.path.join("C:/Users/", "quora_dul_best_lstm.hdf5"), verbose=1, save_best_only=True)
model.fit([x_ques1train, x_ques2train], ytrain, batch_size=32, epochs=1, validation_split=0.1, verbose=2, callbacks=[checkpoint])
print("Training neural network costs:", time.time() - start)

我想将以上代码转换为keras中的功能性API,因为不支持顺序API Merge()函数。我已经尝试了很长一段时间,但几乎没有错误。关于属性的详细信息: ques_pairs包含预处理的数据, word2index包含字数, seq_maxlen包含问题一或二的最大长度。 iam尝试在Quora问题对数据集https://www.kaggle.com/c/quora-question-pairs

上实现此模型

2 个答案:

答案 0 :(得分:0)

我将举一个小例子,您可以将其应用于自己的模型:

from keras.layers import Input, Dense, Add

input1 = Input(shape=(16,))
output1 = Dense(8, activation='relu')(input1)
output1 = Dense(4, activation='relu')(output1) # Add as many layers as you like like this

input2 = Input(shape=(16,))
output2 = Dense(8, activation='relu')(input2)
output2 = Dense(4, activation='relu')(output2) # Add as many layers as you like like this

output_full = Add()([output1, output2])
output_full = Dense(1, activation='sigmoid')(output_full) # Add as many layers as you like like this

model_full = Model(inputs=[input1, input2], outputs=output_full)

您需要首先为每个模型零件定义一个Input,然后向这两个模型添加图层(如代码所示)。然后,您可以使用Add层添加它们。最后,调用Model并列出输入层和输出层。

model_full然后可以像其他模型一样进行编译和训练。

答案 1 :(得分:0)

您是否正在尝试实现以下目标?

from tensorflow.python import keras
from keras.layers import *
from keras.models import Sequential, Model

vocab_size = 1000
seq_maxlen = 32

embedding_weights = np.zeros((vocab_size, 64))

print("Building model...")
ques1_enc = Sequential()
ques1_enc.add(Embedding(output_dim=64, input_dim=vocab_size, weights=[embedding_weights], mask_zero=True))
ques1_enc.add(LSTM(100, input_shape=(64, seq_maxlen), return_sequences=False))
ques1_enc.add(Dropout(0.3))

ques2_enc = Sequential()
ques2_enc.add(Embedding(output_dim=64, input_dim=vocab_size, weights=[embedding_weights], mask_zero=True))
ques2_enc.add(LSTM(100, input_shape=(64, seq_maxlen), return_sequences=False))
ques2_enc.add(Dropout(0.3))

merge = Concatenate(axis=1)([ques1_enc.output, ques2_enc.output])
output = Dense(2, activation="softmax")(merge)
model = Model([ques1_enc.input, ques2_enc.input], output)

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.summary()