【问题标题】:Merging layers on Keras (dot product)在 Keras 上合并图层(点积)
【发布时间】:2019-03-03 16:20:10
【问题描述】:

我一直在关注 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 来合并图层。但问题是,我是 Keras 的初学者,不知道如何将我的代码从 Sequential 转换为 Functional,这是作者使用的代码(我复制了):

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")

# view model summary
print(model.summary())

# visualize model structure
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot

SVG(model_to_dot(model, show_shapes=True, show_layer_names=False, 
rankdir='TB').create(prog='dot', format='svg'))

当我运行该块时,显示以下错误:

ImportError                               Traceback (most recent call last)
<ipython-input-79-80d604373468> in <module>()
----> 1 from keras.layers import Merge
      2 from keras.layers.core import Dense, Reshape
      3 from keras.layers.embeddings import Embedding
      4 from keras.models import Sequential
      5 

ImportError: cannot import name 'Merge'

我在这里要问的是有关如何将此 Sequential 转换为函数式 API 结构的一些指导。

【问题讨论】:

  • 你有没有得到这个更新?我正在尝试使用相同的东西,但一个答案似乎不起作用。

标签: python tensorflow keras word2vec word-embedding


【解决方案1】:

Merge 似乎已被弃用,因此在嵌入时直接使用 Dot 代替 Merge(而不是与模型一起使用)。使用下面的代码。

from keras.layers import Input
from keras.models import Model
from keras.layers.embeddings import Embedding
from keras.layers.core import Dense, Reshape
from keras.layers import dot

input_target = Input((1,))
input_context = Input((1,))

embedding = Embedding(vocab_size, embed_size, input_length=1, name='embedding')

word_embedding = embedding(input_target)
word_embedding = Reshape((embed_size, 1))(word_embedding)
context_embedding = embedding(input_context)
context_embedding = Reshape((embed_size, 1))(context_embedding)

# now perform the dot product operation  
dot_product = dot([word_embedding, context_embedding], axes=1)
dot_product = Reshape((1,))(dot_product)

# add the sigmoid output layer
output = Dense(1, activation='sigmoid')(dot_product)

model = Model(input=[input_target, input_context], output=output)
model.compile(loss='mean_squared_error', optimizer='rmsprop')

# view model summary
print(model.summary())

# visualize model structure
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot

SVG(model_to_dot(model, show_shapes=True, show_layer_names=False, 
                 rankdir='TB').create(prog='dot', format='svg'))

【讨论】:

    【解决方案2】:

    这确实改变了。对于点积,您现在可以使用dot 层:

    from keras.layers import dot
    ...
    dot_product = dot([target, context], axes=1, normalize=False)
    ...
    

    当然,你必须根据你的数据设置axis参数。如果您设置normalize=True,这将提供余弦接近度。如需更多信息,请参阅the documentation

    要了解 Keras 的功能 API,文档中有一个很好的 guide to the functional API。如果您已经了解顺序 API,切换起来并不难。

    【讨论】:

    • @Lucas 如果答案有帮助,请考虑支持/接受它。谢谢!
    • 对不起,这确实很有帮助!但现在我需要更多地研究顺序模型和函数模型,你的回答给了我一个启动,谢谢!
    • @IonicSolutions 嘿,我在使用点 Layer dot_3 was called with an input that isn't a symbolic tensor. Received type: &lt;class 'keras.engine.sequential.Sequential'&gt;. Full input: [&lt;keras.engine.sequential.Sequential object at 0x0000016B8DC8FD68&gt;, &lt;keras.engine.sequential.Sequential object at 0x0000016B8DC8F668&gt;]. All inputs to the layer should be tensors. 后出现此错误
    • 请打开一个新问题并显示您的代码。我很高兴来看看!
    • 代码与@LucasFigueiredo 提到的相同。我已更改为model.add(dot([word_model, context_model], axes=1, normalize=False))
    猜你喜欢
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2017-08-26
    相关资源
    最近更新 更多