【问题标题】:keras embedding layer buildkeras嵌入层构建
【发布时间】:2019-07-29 10:50:09
【问题描述】:

我正在学习 Andrew Ng 教授的序列模型课程。这里我们正在构建嵌入层,如下所示

 # Define Keras embedding layer with the correct output/input sizes, make it non-trainable. Use Embedding(...). Make sure to set trainable=False.


embedding_layer = Embedding(vocab_len, emb_dim, trainable = False)


    # Build the embedding layer, it is required before setting the weights of the embedding layer. Do not modify the "None".
    embedding_layer.build((None,)) 

我很难理解构建方法。下面的功能正在添加什么。我找不到构建 API。此 API 的参考资料会有所帮助。

构建嵌入层,在设置嵌入层权重之前需要。不要修改“无”。

embedding_layer.build((None,))

谢谢

【问题讨论】:

    标签: keras time-series


    【解决方案1】:

    通常,在一个层上不需要自己调用build()

    您只有在按照here 描述的方式编写图层时才关心它。 在那里,build 是必需的方法之一。您可以使用它来定义图层的权重。当您在最终模型上调用 model.compile() 时,这将在内部调用每个层的 build() 方法并将前一层的输出形状传递给它。

    所以,it is required before setting the weights of the embedding layer 的注释实际上是正确的:为了设置权重,层需要知道正确的形状,它会通过调用build() 来告知。但是,这并不意味着您必须自己致电build()。您也可以在模型编译后设置权重。

    因此,总而言之,除非您的代码在做任何花哨的事情(我不知道,因为您没有进一步发布任何内容),否则对 build 的调用已过时。

    【讨论】: