且构网

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

形状不匹配时如何在Keras中使用双向RNN和Conv1D?

更新时间:2023-12-02 08:11:46

您完全不需要重组任何结构即可将Conv1D层的输出转换为LSTM层.

You don't need to restructure anything at all to get the output of a Conv1D layer into an LSTM layer.

因此,问题仅在于Flatten层的存在,该层破坏了形状.

So, the problem is simply the presence of the Flatten layer, which destroys the shape.

这些是Conv1D和LSTM使用的形状:

These are the shapes used by Conv1D and LSTM:

  • Conv1D:(batch, length, channels)
  • LSTM:(batch, timeSteps, features)
  • Conv1D: (batch, length, channels)
  • LSTM: (batch, timeSteps, features)

长度与timeSteps相同,通道与要素相同.

Length is the same as timeSteps, and channels is the same as features.

使用Bidirectional包装器也不会改变任何事情.它只会复制您的输出功能.

Using the Bidirectional wrapper won't change a thing either. It will only duplicate your output features.

如果要对整个序列进行整体分类,则最后一个LSTM必须使用return_sequences=False. (或者您也可以在之后使用扁平+密集)

If you're going to classify the entire sequence as a whole, your last LSTM must use return_sequences=False. (Or you may use some flatten + dense instead after)

如果要对序列的每个步骤进行分类,则所有LSTM都应具有return_sequences=True.您不应该在它们之后将数据弄平.

If you're going to classify each step of the sequence, all your LSTMs should have return_sequences=True. You should not flatten the data after them.