【问题标题】:ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (500, 10, 14)ValueError:检查目标时出错:预期dense_3有2维,但得到了形状为(500、10、14)的数组
【发布时间】:2019-04-17 23:54:00
【问题描述】:

Keras:2.1.6、python 3.6、张量流 1.8.0

我正在尝试训练具有两个 LSTM 层和 3 个密集层的序列模型。我事先做了一些数据准备,并以 LSTM 层需要的格式设置我的数据,即(n_samples, n_timesteps, n_features)。我的数据有 14 个特征,实际上是 5000 个步骤的长序列,因此我将其分解为 500 个样本到每个 10 个时间步长。完成后,我从下面的模型开始,但很快就遇到了最后一层的输入形状错误。我尝试使用 Sequential 和 Functional API 都产生相同的错误。

import keras 
from keras import callbacks
import tensorflow as tf
from keras.models import Model
from keras.layers import Input, Dense
from keras.layers import LSTM


X_input = Input(X_train.shape[1:]);

## First LSTM Layer
X = LSTM(10, return_sequences=True, input_shape = (10,14), name = 'LSTM_1')(X_input);

## Second LSTM Layer
X = LSTM(10)(X);

## First Dense Layer
X = Dense(10, activation='relu', name = 'dense_1')(X)

## Second Dense Layer
X = Dense(5, activation='relu', name = 'dense_2')(X)

## Final Dense Layer
X = Dense(1, activation='relu', name = 'dense_3')(X)

    ##The model object
model = Model(inputs = X_input, outputs = X, name='LSTMModel')

model.compile(optimizer = "Adam" , loss = "mean_squared_error", metrics = ['mean_squared_error','cosine', 'mae']);

Model.fit(x = X_train, y = Y_train, epochs = 300, callbacks=[tensorboard], validation_data=(X_eval,Y_eval));

我的数据是(500,10,14):

>>> X_train.shape
(500,10,14)

我的模型摘要如下所示:

model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 10, 14)            0
_________________________________________________________________
LSTM_1 (LSTM)                (None, 10, 10)            1000
_________________________________________________________________
LSTM_2 (LSTM)                (None, 10)                840
_________________________________________________________________
dense_1 (Dense)              (None, 10)                110
_________________________________________________________________
dense_2 (Dense)              (None, 5)                 55
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 6
=================================================================
Total params: 2,011
Trainable params: 2,011
Non-trainable params: 0
_________________________________________________________________

虽然,我仍然得到错误:

ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (500, 10, 14)

我的标签形状如下:

X_train = np.reshape(Train_data_scaled.values,(500,10,14));
Y_train = np.reshape(Train_labels_scaled.values,(500,10,1));
X_eval = np.reshape(Validation_data_scaled.values,(10,10,14));
Y_eval = np.reshape(Validation_labels_scaled.values,(10,10,1));

添加RepeatVector层后,我发现这里的另一个问题是相同的堆栈跟踪。

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 10, 14)            0
_________________________________________________________________
LSTM_1 (LSTM)                (None, 10)                1000
_________________________________________________________________
repeat_vector_1 (RepeatVecto (None, 10, 10)            0
_________________________________________________________________
LSTM_2 (LSTM)                (None, 10, 10)            840
_________________________________________________________________
dense_1 (Dense)              (None, 10, 10)            110
_________________________________________________________________
dense_2 (Dense)              (None, 10, 5)             55
_________________________________________________________________
dense_3 (Dense)              (None, 10, 1)             6
=================================================================
Total params: 2,011
Trainable params: 2,011
Non-trainable params: 0
_________________________________________________________________
Traceback (most recent call last):
  File ".\lstm.py", line 76, in <module>
    tf.app.run()
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\platform\app.py", line 126, in run
    _sys.exit(main(argv))
  File ".\lstm.py", line 67, in main
    Hist =  Model.fit(x = X_train, y = Y_train, epochs = 300,batch_size=10, callbacks=[tensorboard], validation_data=(X_eval,Y_eval));
  File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1630, in fit
    batch_size=batch_size)
  File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1480, in _standardize_user_data
    exception_prefix='target')
  File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 123, in _standardize_input_data
    str(data_shape))
ValueError: Error when checking target: expected dense_3 to have shape (10, 1) but got array with shape (10, 14)

【问题讨论】:

  • y_train 形状是什么?你想为每个序列预测什么?
  • Y_train 形状是 (500,10,1) 我正在尝试预测未来 10 天的故事的价值

标签: python machine-learning keras lstm rnn


【解决方案1】:

由于您想预测未来 10 天的故事的价值,您需要将第二个 LSTM 层的return_sequences 参数设置为True,以使整个模型的输出形状为(None, 10, 1)

## Second LSTM Layer
X = LSTM(10, return_sequences=True)(X)

此外,预测未来 d 天的值的更通用解决方案是在第一个 LSTM 层之后使用 RepeatVector 层。这次需要将第一个LSTM层的return_sequences参数设置为False

d = 5  # how many days in the future you want to predict?

## First LSTM Layer
X = LSTM(10, input_shape = (10,14), name = 'LSTM_1')(X_input);

X = RepeatVector(d)(X)

## Second LSTM Layer
X = LSTM(10, return_sequences=True)(X)

就好像第一个 LSTM 层对输入数据进行编码,第二个 LSTM 层根据该编码表示预测未来值。另外,标签数组的形状(即y_train)不用说也必须是(n_samples, d, n_feats)

【讨论】:

  • 感谢您的解决方案,但是当我尝试此操作时,我又遇到了另一个错误。 ValueError: Error when checking target: expected dense_3 to have shape (10, 1) but got array with shape (10, 14) 我还尝试在两个层中都打开 return_sequences 导致相同的错误。
  • @NeerajNatu 你确定y_train 的形状是(n_samples, 10, 1) 吗?您是否错误地将x_train 代替y_train 传递给目标值,因为错误表明您作为标签传递的数组的形状为(10,14)
  • 我希望这是错误,但不是。我将我的数据框重塑为 np 数组X_train = np.reshape(Train_data_scaled.values,(500,10,14)); Y_train = np.reshape(Train_labels_scaled.values,(500,10,1)); X_eval = np.reshape(Validation_data_scaled.values,(10,10,14)); Y_eval = np.reshape(Validation_labels_scaled.values,(10,10,1));,然后我用它来训练模型。 Hist = Model.fit(x = X_train, y = Y_train, epochs = 300, callbacks=[tensorboard], validation_data=(X_eval,Y_eval));
  • @NeerajNatu 您能否编辑您的问题并在修改后使用新的model.summary() 以及y_trainy_val 的形状进行更新? 不要删除之前的信息,在最后添加您的编辑。
  • @NeerajNatu 新的,在您根据我的回答修改了代码之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
  • 2019-05-10
  • 2018-12-29
相关资源
最近更新 更多