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

Flatten and back keras

我正在尝试使用自编码器获取简单向量中的值 这是我的代码…

如何按索引访问PyTorch模型参数

如果我的网络有10层,包括偏置项,如何仅通过索引访问第…

Python中多元逻辑回归显示错误

我试图使用逻辑回归进行预测,并使用Python和skl…

在MACOS上安装NLTK

我在我的2015款Mac Pro上尝试安装NLTK,操…

如何在R中将通过RFE选择的变量插入到机器学习模型中?

我想使用递归特征消除方法来选择最重要的特征,然后将这些…

CountVectorizer 错误:ValueError: setting an array element with a sequence

我有一个包含144条学生反馈的数据集,其中有72条正面…

发表回复

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