Keras函数式API出现错误 “expected ndim=3, found ndim=4”

我在尝试使用keras函数式API构建一个多输入多输出的模型,并按照他们的代码进行操作,但遇到了以下错误:

ValueError: Input 0 is incompatible with layer lstm_54: expected ndim=3, found ndim=4

我在创建lstm_out层时遇到了这个错误,以下是代码:

def build_model(self):    main_input = Input(shape=(self.seq_len, 1), name='main_input')    #seq_len = 50, vocab_len = 1000    x = Embedding(output_dim=512, input_dim=self.vocab_len()+1, input_length=self.seq_len)(main_input)    # A LSTM will transform the vector sequence into a single vector,    # containing information about the entire sequence    lstm_out = LSTM(50)(x)    self.auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)    auxiliary_input = Input(shape=(self.seq_len,1), name='aux_input')    x = concatenate([lstm_out, auxiliary_input])    # We stack a deep densely-connected network on top    x = Dense(64, activation='relu')(x)    x = Dense(64, activation='relu')(x)    x = Dense(64, activation='relu')(x)    # And finally we add the main logistic regression layer    main_output = Dense(1, activation='sigmoid', name='main_output')(x)    self.model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])    print(self.model.summary())    self.model.compile(optimizer='rmsprop', loss='binary_crossentropy',              loss_weights=[1., 0.2])

我原本以为问题出在Embedding层的input_dim上,但我查看了keras Embedding文档,发现(input_dim应该等于词汇量大小 + 1)。

我不知道为什么会出现这个错误,我的input_dim具体哪里出了问题,以及如何解决它?


回答:

Embedding层的输入形状应该是二维张量,形状为:(batch_size, sequence_length)。在你的代码片段中,main_input是一个三维张量。要解决这个问题,请更改以下代码行:

main_input = Input(shape=(self.seq_len, 1), name='main_input')<...>auxiliary_input = Input(shape=(self.seq_len,1), name='aux_input')

为:

main_input = Input(shape=(self.seq_len, ), name='main_input')<...>auxiliary_input = Input(shape=(self.seq_len, ), name='aux_input')

这应该能解决不同维度的问题

Related Posts

如何使用Google Protobuf解析、编辑和生成object_detection/pipeline.config文件

我在一个常见的集成学习范式中训练多个模型,目前我在处理…

我的GridSearchCV不起作用,我不知道为什么

大家好,我在使用GridSearchCV时遇到了问题,…

Keras: 两个同时进行的层,其中一个对前一层的输出进行卷积

我想实现这样的模型连接: 输入图像1 -> 卷积层1 …

如何将行数据转换为列数据而不使用独热编码

我有一个如下所示的数据集。 MonthDate Day…

使用 ML Kit 与 NNAPI

我正在尝试在运行 Android 9 的设备上使用新的…

Vowpal Wabbit 可能的哈希冲突

我在VW中生成了一个模型,并且在相同的数据上生成了两个…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注