且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Keras中实施注意力

更新时间:2023-12-02 22:52:52

您可以在串联之前添加一个Reshape图层以确保兼容性. 请参见keras文档此处. ***重塑model_2输出(None, 64)

You can add a Reshape layer before concatenating to ensure compatibility. see keras documentation here. Probably best to reshape the model_2 output (None, 64)

基本上,您需要在连接之前添加具有目标形状的Reshape图层:

Essentially you need to add a Reshape layer with the target shape before concatenating:

model_2 = Reshape(new_shape)(model_2)

这将返回(batch_size, (new_shape)) 当然,您可以重塑网络的任一分支,只需使用model_2输出,因为它是一个更简单的示例

This will return (batch_size, (new_shape)) You can of course Reshape either branch of your network, just using model_2 output as it is a simpler example

话虽如此,也许值得重新考虑您的网络结构.特别是,此问题源于第二个点层(仅给您16个标量).因此,很难重塑形状以使两个分支匹配.

Having said that, maybe it's worth rethinking your network structure. In particular, this problem stems from the second dot layer (which gives you 16 scalars only). As such it's hard to reshape so that the two branches match.

在不知道模型试图预测什么或训练数据如何的情况下,很难评论是否需要两个点,但是潜在的重组将解决这个问题.

Without knowing what the model is trying to predict or what the training data looks like, it's hard to comment on whether two dots are necessary or not, but potentially re-structuring will solve this issue.