在 keras 中的一组文档上应用共享嵌入层
Apply a shared Embedding layer on a set of documents in keras
我正在尝试创建一个模型,我想在其中预测给定特定查询的特定文档集的顺序。我的想法基本上是为查询和文档使用共享嵌入层,然后使用每个文档和查询之间的余弦相似度(使用自定义 lambda)合并两个 "branches"。然后损失函数将计算预期位置和预测相似度之间的差异。
我的问题是:有没有办法为一组文本特征创建嵌入(前提是它们具有相同的长度)?
我可以通过应用 Embedding + Convolution1D + GlobalMaxPooling1D 在 "doc2vec-like embedding" 中正确地转换我的查询,但是我没有运气在文档集上使用相同的策略(并且 Reshaping + 2D 卷积并不是真的考虑到我正在处理文本数据,这对我来说很有意义。
请注意,我的一个限制是我需要为我的查询和文档集使用相同的嵌入层(我正在使用 Keras 的功能性 API 来这样做)。
[编辑,添加示例代码]
Q = Input(shape=(5, )) # each query is made of 5 words
T = Input(shape=(50, 50)) # each search result is made of 50 words and 50 docs
emb = Embedding(
max_val,
embedding_dims,
dropout=embedding_dropout
)
left = emb(Q)
left = Convolution1D(nb_filter=5,
filter_length=5,
border_mode='valid',
activation='relu',
subsample_length=1)(left)
left = GlobalMaxPooling1D()(left)
print(left)
right = emb(T) # <-- this is my problem, I don't really know what to do/apply here
def merger(vests):
x, y = vests
x = K.l2_normalize(x, axis=0) # Normalize rows
y = K.l2_normalize(y, axis=-1) # Normalize the vector
return tf.matmul(x, y) # obviously throws an error because of mismatching matrix ranks
def cos_dist_output_shape(shapes):
shape1, shape2 = shapes
return (50, 1)
merger_f = Lambda(merger)
predictions = merge([left, right], output_shape=cos_dist_output_shape, mode=merger_f)
model = Model(input=[Q, T], output=predictions)
def custom_objective(y_true, y_pred):
ordered_output = tf.cast(tf.nn.top_k(y_pred)[1], tf.float32) # returns the indices of the top values
return K.mean(K.square(ordered_output - y_true), axis=-1)
model.compile(optimizer='adam', loss=custom_objective)
[解决方案] 感谢 Nassim Ben,像这样使用 TimeDistributed
将图层循环应用到图层的所有维度,如下所示:
right = TimeDistributed(emb)(T)
right = TimeDistributed(Convolution1D(nb_filter=5,
filter_length=5,
border_mode='valid',
activation='relu',
subsample_length=1)(right)
right = TimeDistributed(GlobalMaxPooling1D())(right)
好的。如果我理解正确的话,你有 50 个长度为 50 的文本片段要嵌入。
做词嵌入后,你会发现自己有一个形状为 (50,50,emb_size) 的 Tensor T。
我会做的是在 TimeDistributed 包装器中使用 LSTM 层。在 emb(T)
之后添加这些行:
right = TimeDistributed(LSTM(5))(right)
这将对 50 个文档中的每一个应用相同的 LSTM,并在每个文档处理结束时输出长度为 5 的最终状态。这一步之后的形状是(50,5)。您已将每个文档嵌入到一个长度为 5 的向量中。
TimeDistributed 的优点是应用于每个文档的 LSTM 将共享相同的权重,因此您的文档将 'treated' 以相同的方式。您可以找到有关 LSTM here and about TimeDistributed here 的文档。
希望对您有所帮助。
我正在尝试创建一个模型,我想在其中预测给定特定查询的特定文档集的顺序。我的想法基本上是为查询和文档使用共享嵌入层,然后使用每个文档和查询之间的余弦相似度(使用自定义 lambda)合并两个 "branches"。然后损失函数将计算预期位置和预测相似度之间的差异。
我的问题是:有没有办法为一组文本特征创建嵌入(前提是它们具有相同的长度)?
我可以通过应用 Embedding + Convolution1D + GlobalMaxPooling1D 在 "doc2vec-like embedding" 中正确地转换我的查询,但是我没有运气在文档集上使用相同的策略(并且 Reshaping + 2D 卷积并不是真的考虑到我正在处理文本数据,这对我来说很有意义。
请注意,我的一个限制是我需要为我的查询和文档集使用相同的嵌入层(我正在使用 Keras 的功能性 API 来这样做)。
[编辑,添加示例代码]
Q = Input(shape=(5, )) # each query is made of 5 words
T = Input(shape=(50, 50)) # each search result is made of 50 words and 50 docs
emb = Embedding(
max_val,
embedding_dims,
dropout=embedding_dropout
)
left = emb(Q)
left = Convolution1D(nb_filter=5,
filter_length=5,
border_mode='valid',
activation='relu',
subsample_length=1)(left)
left = GlobalMaxPooling1D()(left)
print(left)
right = emb(T) # <-- this is my problem, I don't really know what to do/apply here
def merger(vests):
x, y = vests
x = K.l2_normalize(x, axis=0) # Normalize rows
y = K.l2_normalize(y, axis=-1) # Normalize the vector
return tf.matmul(x, y) # obviously throws an error because of mismatching matrix ranks
def cos_dist_output_shape(shapes):
shape1, shape2 = shapes
return (50, 1)
merger_f = Lambda(merger)
predictions = merge([left, right], output_shape=cos_dist_output_shape, mode=merger_f)
model = Model(input=[Q, T], output=predictions)
def custom_objective(y_true, y_pred):
ordered_output = tf.cast(tf.nn.top_k(y_pred)[1], tf.float32) # returns the indices of the top values
return K.mean(K.square(ordered_output - y_true), axis=-1)
model.compile(optimizer='adam', loss=custom_objective)
[解决方案] 感谢 Nassim Ben,像这样使用 TimeDistributed
将图层循环应用到图层的所有维度,如下所示:
right = TimeDistributed(emb)(T)
right = TimeDistributed(Convolution1D(nb_filter=5,
filter_length=5,
border_mode='valid',
activation='relu',
subsample_length=1)(right)
right = TimeDistributed(GlobalMaxPooling1D())(right)
好的。如果我理解正确的话,你有 50 个长度为 50 的文本片段要嵌入。
做词嵌入后,你会发现自己有一个形状为 (50,50,emb_size) 的 Tensor T。
我会做的是在 TimeDistributed 包装器中使用 LSTM 层。在 emb(T)
之后添加这些行:
right = TimeDistributed(LSTM(5))(right)
这将对 50 个文档中的每一个应用相同的 LSTM,并在每个文档处理结束时输出长度为 5 的最终状态。这一步之后的形状是(50,5)。您已将每个文档嵌入到一个长度为 5 的向量中。 TimeDistributed 的优点是应用于每个文档的 LSTM 将共享相同的权重,因此您的文档将 'treated' 以相同的方式。您可以找到有关 LSTM here and about TimeDistributed here 的文档。
希望对您有所帮助。