Keras - 从顺序到函数的转换 API
Keras - Translation from Sequential to Functional API
我一直在关注 Towards Data Science 关于 word2vec 和 skip-gram 模型的教程,但我偶然发现了一个我无法解决的问题,尽管我进行了大量搜索并尝试了多种不成功的解决方案。
它向您展示如何构建 skip-gram 模型架构的步骤似乎已被弃用,因为使用了来自 keras.layers 的 Merge 层。
我试图做的是将他的一段代码(在 Keras 的顺序 API 中实现)翻译成功能 API 以解决合并层的弃用问题,方法是替换它与 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 was called with an input that isn't a symbolic
tensor. Received type: . Full
input: [,
]. All inputs to
the layer should be tensors.
我完全是 Keras 的 Functional 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
的输入。因此:
model_output = layers.Dense(1, kernel_initializer='glorot_uniform',
activation='sigmoid')(dot_output)
model = Model([word_input, context_input], model_output)
我一直在关注 Towards Data Science 关于 word2vec 和 skip-gram 模型的教程,但我偶然发现了一个我无法解决的问题,尽管我进行了大量搜索并尝试了多种不成功的解决方案。
它向您展示如何构建 skip-gram 模型架构的步骤似乎已被弃用,因为使用了来自 keras.layers 的 Merge 层。
我试图做的是将他的一段代码(在 Keras 的顺序 API 中实现)翻译成功能 API 以解决合并层的弃用问题,方法是替换它与 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 was called with an input that isn't a symbolic tensor. Received type: . Full input: [, ]. All inputs to the layer should be tensors.
我完全是 Keras 的 Functional 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
的输入。因此:
model_output = layers.Dense(1, kernel_initializer='glorot_uniform',
activation='sigmoid')(dot_output)
model = Model([word_input, context_input], model_output)