Keras:2.1.6,python3.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)
:
我的模型摘要是这样的:
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)
:此外,预测未来值
^{pr2}$d
天的更一般的解决方案是在第一个LSTM层之后使用RepeatVector
层。这次您需要将第一个LSTM层的return_sequences
参数设置为False
:这就好像第一个LSTM层对输入数据进行编码,而第二个LSTM层根据编码的表示预测未来的值。另外,不用说标签数组(即
y_train
)的形状也必须是(n_samples, d, n_feats)
。在相关问题 更多 >
编程相关推荐