ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (500, 10, 14)
ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (500, 10, 14)
Keras:2.1.6,python3.6,tensorflow 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)
由于您想预测未来 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)
.
Keras:2.1.6,python3.6,tensorflow 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)
由于您想预测未来 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)
.