python - Keras - 从顺序 API 到函数式 API 的转换

标签 python tensorflow keras word2vec word-embedding

我一直在关注 Towards Data Science 关于 word2vec 和 skip-gram 模型的教程,但我偶然发现了一个我无法解决的问题,尽管我进行了大量搜索并尝试了多种不成功的解决方案。

https://towardsdatascience.com/understanding-feature-engineering-part-4-deep-learning-methods-for-text-data-96c44370bbfa

它向您展示如何构建 skip-gram 模型架构的步骤似乎已被弃用,因为使用了 keras.layers 中的 Merge 层。

我试图做的是将他的一段代码(在 Keras 的顺序 API 中实现)翻译成函数式 API 以解决 Merge 层的弃用问题,方法是将其替换为 keras.layers.Dot 层。但是,我仍然停留在将两个模型(单词和上下文)合并为最终模型的步骤中,其架构必须是这样的:

Skip-gram model summary and architecture

这是作者使用的代码:

from keras.layers import Merge
from keras.layers.core import Dense, Reshape
from keras.layers.embeddings import Embedding
from keras.models import Sequential

# build skip-gram architecture
word_model = Sequential()
word_model.add(Embedding(vocab_size, embed_size,
                         embeddings_initializer="glorot_uniform",
                         input_length=1))
word_model.add(Reshape((embed_size, )))

context_model = Sequential()
context_model.add(Embedding(vocab_size, embed_size,
                  embeddings_initializer="glorot_uniform",
                  input_length=1))
context_model.add(Reshape((embed_size,)))

model = Sequential()
model.add(Merge([word_model, context_model], mode="dot"))
model.add(Dense(1, kernel_initializer="glorot_uniform", activation="sigmoid"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")

这是我将顺序代码实现转换为功能代码实现的尝试:

from keras import models
from keras import layers
from keras import Input, Model

word_input = Input(shape=(1,))
word_x = layers.Embedding(vocab_size, embed_size, embeddings_initializer='glorot_uniform')(word_input)
word_reshape = layers.Reshape((embed_size,))(word_x)

word_model = Model(word_input, word_reshape)    

context_input = Input(shape=(1,))
context_x = layers.Embedding(vocab_size, embed_size, embeddings_initializer='glorot_uniform')(context_input)
context_reshape = layers.Reshape((embed_size,))(context_x)

context_model = Model(context_input, context_reshape)

model_input = layers.dot([word_model, context_model], axes=1, normalize=False)
model_output = layers.Dense(1, kernel_initializer='glorot_uniform', activation='sigmoid')

model = Model(model_input, model_output)

但是执行时,返回如下错误:

ValueError: Layer dot_5 was called with an input that isn't a symbolic tensor. Received type: . Full input: [, ]. All inputs to the layer should be tensors.

我是 Keras 函数式 API 的新手,如果您能在这种情况下给我一些指导,我将不胜感激,我将如何将上下文和单词模型输入点层以实现图像。

最佳答案

您正在将 Model 实例传递给层,但是由于错误表明您需要将 Keras 张量(即层或模型的输出)传递给 Keras 中的层。你在这里有两个选择。一种是像这样使用 Model 实例的 .output 属性:

dot_output = layers.dot([word_model.output, context_model.output], axes=1, normalize=False)

或者等效地,您可以直接使用输出张量:

dot_output = layers.dot([word_reshape, context_reshape], axes=1, normalize=False)

此外,您需要在 dot_output 上应用 Dense 层,并将 Input 层的实例作为 的输入>模型。因此:

model_output = layers.Dense(1, kernel_initializer='glorot_uniform',
                            activation='sigmoid')(dot_output)

model = Model([word_input, context_input], model_output)

关于python - Keras - 从顺序 API 到函数式 API 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52744467/

相关文章:

gradient - TensorFlow 远程应用梯度

python - 有没有办法在gpu计算的时候并行加载数据?

python - 如何使用 lstm 执行多类多输出分类

Python Exchangelib读取子文件夹中的邮件

python - 我的代码效率低下,是来自 Project Euler 的 3 和 5 的倍数,但有 10 秒的超时条件

python - Python Bottle 路由中的类作用域

python - 尝试在 Anaconda 中导入 Tensorflow 时出错

tensorflow - 如何从 Pandas DataFrame 到用于 NLP 的 Tensorflow BatchDataset?

python - 使用 Python 在 Pandas 中读取 CSV 文件时出现 UnicodeDecodeError

keras - 具有输入内核和偏差的自定义层