我正在尝试在深度学习中实现答案选择模型,如下所示基于this论文的keras,
我理解在上面的流程中实现嵌入,双向LSTM和池化的步骤。 但是如何实现合并函数来计算keras中的余弦相似度和损失函数?
损失函数定义为,
L= max{0,M-cosine(q,a+)+cosine(q,a-)}
其中,
M
=恒定保证金q
=问题a+
=正确答案a-
=错误答案更新1: 经过一些博客后,这就是我实施的方式。
#build model
input_question = Input(shape=(max_len, embedding_dim))
input_sentence = Input(shape=(max_len, embedding_dim))
question_lstm = Bidirectional(LSTM(64))
sentence_lstm = Bidirectional(LSTM(64))
encoded_question = question_lstm(input_question)
encoded_sentence = sentence_lstm(input_sentence)
cos_distance = merge([encoded_question, encoded_sentence], mode='cos', dot_axes=1)
cos_distance = Reshape((1,))(cos_distance)
cos_similarity = Lambda(lambda x: 1-x)(cos_distance)
predictions = Dense(1, activation='sigmoid')(cos_similarity)
model = Model([input_question, input_sentence], [predictions])
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
通过上述实施,我仍然无法弄清楚如何实现铰链损耗。请帮忙