我是Keras的新手,我正在努力解决在Keras使用NN的句子类似任务.我使用word2vec作为单词嵌入,然后使用Siamese Network来预测两个句子的相似程度.Siamese网络的基础网络是LSTM,为了合并两个基础网络,我使用具有余弦相似度量的Lambda层.作为数据集,我正在使用SICK数据集,它为每对句子提供一个分数,从1(不同)到5(非常相似).
我创建了网络并运行,但我有很多疑问:首先,我不确定我用LSTM提供句子的方式是否正常.我为每个单词采用word2vec嵌入,每个句子只创建一个数组,用零填充到seq_len,以获得相同的长度数组.然后我以这种方式重塑它: data_A = embedding_A.reshape((len(embedding_A), seq_len, feature_dim))
此外我不确定我的暹罗网络是否正确,因为不同对的预测很多,并且损失没有太大变化(在10个时期内从0.3300到0.2105,并且在100个时期内变化不大)时期).
有人可以帮我找到并理解我的错误吗?非常感谢(抱歉我的英语不好)
对我的代码感兴趣
def cosine_distance(vecs):
#I'm not sure about this function too
y_true, y_pred = vecs
y_true = K.l2_normalize(y_true, axis=-1)
y_pred = K.l2_normalize(y_pred, axis=-1)
return K.mean(1 - K.sum((y_true * y_pred), axis=-1))
def cosine_dist_output_shape(shapes):
shape1, shape2 = shapes
print((shape1[0], 1))
return (shape1[0], 1)
def contrastive_loss(y_true, y_pred):
margin = 1
return K.mean(y_true * K.square(y_pred) + (1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))
def create_base_network(feature_dim,seq_len):
model = Sequential()
model.add(LSTM(100, batch_input_shape=(1,seq_len,feature_dim),return_sequences=True))
model.add(Dense(50, activation='relu'))
model.add(Dense(10, activation='relu')) …Run Code Online (Sandbox Code Playgroud)