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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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