我一直在非常努力地制作一个 LSTM 模型,在我训练和拟合模型之后,我想用它来预测新数据。我使用 12 个月的数据(12 行)来预测下个月的数据。
然而,当我输入12个月的数据时,有12个预测,应该只有一个。
谁能帮忙看看我的模型结构有没有问题?我对此很陌生,所以我有点困惑。
可以看到我输入的shape是(12,13)(这里13是因为我用了13个特征)
def build_model(hp):
grid_model = Sequential()
grid_model.add(LSTM(hp.Int('input_unit',min_value=32,max_value=512,step=32),return_sequences=True,\
input_shape=(data_train_x.shape[1],data_train_x.shape[2]))) # (12,13) time steps + features
for i in range(hp.Int('n_layers', 1, 3)):
grid_model.add(LSTM(hp.Int(f'lstm_{i}_units',min_value=32,max_value=512,step=32),return_sequences=True))
grid_model.add(LSTM(hp.Int('last_layer_unit',min_value=32,max_value=512,step=32),return_sequences=False))
grid_model.add(Dropout(hp.Float('Dropout_rate',min_value=0.1,max_value=0.5,step=0.1)))
grid_model.add(Dense(1, activation=hp.Choice('dense_activation',values=['relu', 'sigmoid'],default='relu')))
optimizer = hp.Choice('optimizer', values = ['adam', 'rmsprop', 'adadelta'])
grid_model.compile(loss='mean_squared_error', optimizer = optimizer ,metrics = ['mse'])
#set learning rate
learning_rate = hp.Choice('lr', [0.02, 0.01, 0.005])
K.set_value(grid_model.optimizer.learning_rate, learning_rate)
return grid_model
我在这里使用了 10 的批量大小
tuner.search(x=data_train_x, y=data_train_y, epochs = 50, batch_size =10,
validation_split = 0.15, shuffle = False,
callbacks = [stop_early])
这就是我进行预测的方式,tensor_pred 的形状为 (1,12,13)
new_pred = hypermodel.predict(tensor_pred); new_pred
逆缩放后得到如下结果
array([756459.4 , 753678.1 , 724612.6 , 727920.5 , 721956. , 710755.5 ,
717139.7 , 713023.06, 705151.2 , 705286.5 , 700536.7 , 691794.06],
dtype=float32)
12 是您的批量大小,因此您的预测形状是 (12,),这只是每个输入的一个预测。你还是对的。