Python 用于Word2Vec模型的带有Keras Functional API的产品合并层
我正在尝试使用Keras实现带有负采样的Word2Vec CBOW,代码如下: 然而,我在合并部分得到了一个错误,因为嵌入层是一个3D张量,而cbow只有2维。我假设我需要将嵌入(即[?,1100])重塑为[1100],但我找不到如何使用函数API重塑。 我正在使用Tensorflow后端 另外,如果有人能指出CBOW与Keras(Gensim免费)的另一个实现,我很想看看它 谢谢大家! 编辑:这里是错误Python 用于Word2Vec模型的带有Keras Functional API的产品合并层,python,nlp,keras,word2vec,word-embedding,Python,Nlp,Keras,Word2vec,Word Embedding,我正在尝试使用Keras实现带有负采样的Word2Vec CBOW,代码如下: 然而,我在合并部分得到了一个错误,因为嵌入层是一个3D张量,而cbow只有2维。我假设我需要将嵌入(即[?,1100])重塑为[1100],但我找不到如何使用函数API重塑。 我正在使用Tensorflow后端 另外,如果有人能指出CBOW与Keras(Gensim免费)的另一个实现,我很想看看它 谢谢大家! 编辑:这里是错误 Traceback (most recent call last): File "cb
Traceback (most recent call last):
File "cbow.py", line 48, in <module>
word_context_product = merge([word_embedding, cbow], mode='dot')
.
.
.
ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shapes: [?,1,100], [?,100].
回溯(最近一次呼叫最后一次):
文件“cbow.py”,第48行,在
word\u context\u product=merge([word\u嵌入,cbow],mode='dot'))
.
.
.
ValueError:形状必须为秩2,但对于输入形状为[?,1100],[?,100]的“MatMul”(op:“MatMul”)而言,它为秩3。
实际上,您需要重塑单词的嵌入
张量。有两种方法:
- 您可以使用从
导入的keras.layers.core
层,操作如下:reformate()
word_embedding = Reshape((100,))(word_embedding)
的参数是具有目标形状的元组reformate
- 或者您可以使用
图层,也可以从flatte()
导入,如下所示:keras.layers.core
不使用任何内容作为参数,它只会删除“空”维度word_embedding = Flatten()(word_embedding)
merge()
有点棘手。Keras中的点
合并只接受相同秩的张量,因此相同的len(shape)
。
因此,您要做的是使用restrape()
层添加回1个空维度,然后使用特征dot\u轴
而不是concat\u轴
,这与dot
合并无关。
这就是我为解决方案向您提出的建议:
word_embedding = shared_embedding_layer(word_index)
# Shape output = (None,1,emb_size)
context_embeddings = shared_embedding_layer(context)
# Shape output = (None, 2*window_size, emb_size)
negative_words_embedding = shared_embedding_layer(negative_samples)
# Shape output = (None, negative, emb_size)
# Now the context words are averaged to get the CBOW vector
cbow = Lambda(lambda x: K.mean(x, axis=1),
output_shape=(EMBEDDING_DIM,))(context_embeddings)
# Shape output = (None, emb_size)
cbow = Reshape((1,emb_size))(cbow)
# Shape output = (None, 1, emb_size)
# Context is multiplied (dot product) with current word and negative
# sampled words
word_context_product = merge([word_embedding, cbow], mode='dot')
# Shape output = (None, 1, 1)
word_context_product = Flatten()(word_context_product)
# Shape output = (None,1)
negative_context_product = merge([negative_words_embedding, cbow], mode='dot',dot_axes=[2,2])
# Shape output = (None, negative, 1)
negative_context_product = Flatten()(negative_context_product)
# Shape output = (None, negative)
工作正常吗?:)
这个问题来自TF关于矩阵乘法的刚性。与“点”模式合并调用后端
batch\u dot()
函数,与否相反,TensorFlow要求矩阵具有相同的秩: 你能给我看看错误吗当然,对不起。完全忘记了!我确实为第一次合并提供了帮助是的,非常感谢!但是,对于第二个,我得到了同样的错误,我不能用展平或重塑来修复,因为负样本的形状是(5,)而不是(1,):ValueError:shape必须是秩2,但对于输入形状为[?,5100],?,100]的“MatMul”(op:'MatMul'),它是秩3.
我不明白的是,这段代码与Theano配合得很好,但与Tensorflow配合得不好..我确实可以,非常感谢!谢谢你的澄清,我对西亚诺一无所知!太棒了,很高兴我能帮上忙:)
word_embedding = Flatten()(word_embedding)
word_embedding = shared_embedding_layer(word_index)
# Shape output = (None,1,emb_size)
context_embeddings = shared_embedding_layer(context)
# Shape output = (None, 2*window_size, emb_size)
negative_words_embedding = shared_embedding_layer(negative_samples)
# Shape output = (None, negative, emb_size)
# Now the context words are averaged to get the CBOW vector
cbow = Lambda(lambda x: K.mean(x, axis=1),
output_shape=(EMBEDDING_DIM,))(context_embeddings)
# Shape output = (None, emb_size)
cbow = Reshape((1,emb_size))(cbow)
# Shape output = (None, 1, emb_size)
# Context is multiplied (dot product) with current word and negative
# sampled words
word_context_product = merge([word_embedding, cbow], mode='dot')
# Shape output = (None, 1, 1)
word_context_product = Flatten()(word_context_product)
# Shape output = (None,1)
negative_context_product = merge([negative_words_embedding, cbow], mode='dot',dot_axes=[2,2])
# Shape output = (None, negative, 1)
negative_context_product = Flatten()(negative_context_product)
# Shape output = (None, negative)