keras多维输入到SimpleRNN:维度不匹配

输入元素有3行,每行有199列,输出有46行和1列

Input.shape, output.shape((204563, 3, 199), (204563, 46, 1))

当输入时,会抛出以下错误:

from keras.layers import Densefrom keras.models import Sequentialfrom keras.layers.recurrent import SimpleRNNmodel = Sequential()model.add(SimpleRNN(100, input_shape = (Input.shape[1], Input.shape[2])))model.add(Dense(output.shape[1], activation = 'softmax'))model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])model.fit(Input, output, epochs = 20, batch_size = 200)

抛出的错误:

Epoch 1/20---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-134-378dd431cf45> in <module>()      3 model.add(Dense(y_target.shape[1], activation = 'softmax'))      4 model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])----> 5 model.fit(X_input, y_target, epochs = 20, batch_size = 200)...ValueError: Error when checking model target: expected dense_6 to have 2 dimensions, but got array with shape (204563, 46, 1)

请解释问题的原因和可能的解决方案


回答:

问题在于SimpleRNN(100)返回的张量形状为(204563, 100),因此,Dense(46)(因为output.shape[1]=46)将返回形状为(204563, 46)的张量,但你的y_target的形状为(204563, 46, 1)。你需要移除最后一个维度,例如使用y_target = np.squeeze(y_target),以确保维度一致

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中创建了一个多类分类项目。该项目可以对…

发表回复

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