【问题标题】:Keras - Translation from Sequential to Functional APIKeras - 从顺序到函数式 API 的转换
【发布时间】:2018-10-10 16:05:02
【问题描述】:

我一直在关注 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 的 Sequential API 中实现)转换为功能 API,以解决 Merge 层的弃用问题,将其替换为 keras.layers.Dot 层。但是,我仍然停留在将两个模型(单词和上下文)合并为最终模型的这一步,其架构必须是这样的:

这是作者使用的代码:

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 被调用时输入不是符号 张量。接收类型:.满的 输入: [, ]。所有输入 该层应该是张量。

我是 Keras 函数式 API 的初学者,如果你能在这种情况下给我一些指导,我将如何将上下文和单词模型输入到点层以实现架构图片。

【问题讨论】:

    标签: python tensorflow keras word2vec word-embedding


    【解决方案1】:

    您正在将 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)
    

    此外,您需要应用Dense 层,该层紧随dot_output,并将Input 层的实例作为Model 的输入传递。因此:

    model_output = layers.Dense(1, kernel_initializer='glorot_uniform',
                                activation='sigmoid')(dot_output)
    
    model = Model([word_input, context_input], model_output)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-05
      • 2020-09-02
      • 2017-10-06
      • 2020-05-10
      • 2020-07-22
      • 2018-08-07
      • 1970-01-01
      • 2019-06-18
      相关资源
      最近更新 更多