Keras:2.1.6,python 3.6,tensorflow 1.8.0
我正在尝试训练一个具有两个LSTM层和3个密集层的序列模型。我事先做了一些数据准备,并以LSTM层要求的格式(即(n_samples, n_timesteps, n_features)
)设置数据。我的数据有14个特征,实际上是一个5000步的长序列,因此我将其分解为500个样本,每个样本有10个时间步。完成此操作后,我从下面的模型开始,但是很快遇到了最后一层的输入形状错误。我尝试使用顺序API和功能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)
答案 0 :(得分:0)
由于您要在未来10天预测故事的价值,因此需要将第二个LSTM层的return_sequences
参数设置为True
,以形成整个模型的输出形状{{1 }}:
(None, 10, 1)
此外,一种更通用的预测未来## Second LSTM Layer
X = LSTM(10, return_sequences=True)(X)
天的值的解决方案是在第一个LSTM层之后立即使用d
层。这次您需要将第一个LSTM层的RepeatVector
参数设置为return_sequences
:
False
就好像第一层LSTM层对输入数据进行编码,而第二层LSTM层则基于该编码表示来预测未来值。另外,不用说标签数组(即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)
)的形状也必须是y_train
。