问题:
当我尝试在model.predict()
和test_X
上运行train_X
和# define model
model = Sequential()
model.add(LSTM(50, activation= 'relu' , input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
callback = [EarlyStopping(monitor="loss", min_delta = 0.00001, patience = 20, mode = 'auto', restore_best_weights=True)]
# fit model
history = model.fit(train_X, train_y, epochs=100, validation_data=(test_X, test_y), verbose=1,
batch_size=16, shuffle=False, callbacks=callback)
时,出现形状不匹配的情况,这是因为我尝试学习如何使用n个TimeSteps进行回溯以预测未来的1步。
我正在使用什么:
Windows10,
Python3.8,
Keras 2.3.1,
在conda venv内部
我已经尝试过:
使用https://machinelearningmastery.com/convert-time-series-supervised-learning-problem-python/中的示例,根据培训和测试数据创建了监督培训集。
我从这里和MachineLearningMastery尝试了许多示例,并以最后一个示例结束。 https://www.pytorials.com/predict-cryptocurrency-price-using-lstm/。我想我过去两天可能已经读到正确的答案,但是现在信息被淹没了,不完全确定下一步是什么。
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
(25607, 60, 3) (25607,) (6360, 60, 3) (6360,)
图表明,损失和val_loss收敛良好,没有异常。
这是我预测之前的数据形状。
# make predictions
trainPredict = model.predict(train_X)
testPredict = model.predict(test_X)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform(train_y)
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform(test_y)
# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(train_y[:,0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(test_y[:,0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-128-11d08dc2a08f> in <module>
3 testPredict = model.predict(test_X)
4 # invert predictions
----> 5 trainPredict = scaler.inverse_transform(trainPredict)
6 trainY = scaler.inverse_transform(train_y)
7 testPredict = scaler.inverse_transform(testPredict)
c:\users\user\miniconda3\lib\site-packages\sklearn\preprocessing\_data.py in inverse_transform(self, X)
434 force_all_finite="allow-nan")
435
--> 436 X -= self.min_
437 X /= self.scale_
438 return X
ValueError: non-broadcastable output operand with shape (25607,1) doesn't match the broadcast shape (25607,4)
trainPredict = scaler.inverse_transform(trainPredict.reshape(-1,1))
trainY = scaler.inverse_transform(train_y)
testPredict = scaler.inverse_transform(testPredict.reshape(-1,1))
testY = scaler.inverse_transform(test_y)
PS:这4个来自哪里,因为我没有其他形状之一。
在此先感谢您的帮助或指导。
编辑 还尝试重塑预测变量,如下所示,但有相同的错误:
git add -f *